Added support for custom hash functions in unordered containers; #414 (#415)

This commit is contained in:
Dr. Patrick Urbanke (劉自成)
2025-04-26 21:13:39 +08:00
committed by GitHub
parent c36baf8d0b
commit 25cd3f8525
5 changed files with 114 additions and 64 deletions
+17 -16
View File
@@ -18,29 +18,30 @@ namespace rfl {
namespace parsing {
template <class R, class W, class T, class ProcessorsType>
requires AreReaderAndWriter<R, W, std::map<std::string, T>>
requires AreReaderAndWriter<R, W, std::map<std::string, T>>
struct Parser<R, W, std::map<std::string, T>, ProcessorsType>
: public MapParser<R, W, std::map<std::string, T>, ProcessorsType> {
};
: public MapParser<R, W, std::map<std::string, T>, ProcessorsType> {};
template <class R, class W, class T, class Hash, class KeyEqual,
class Allocator, class ProcessorsType>
requires AreReaderAndWriter<
R, W, std::unordered_map<std::string, T, Hash, KeyEqual, Allocator>>
struct Parser<R, W,
std::unordered_map<std::string, T, Hash, KeyEqual, Allocator>,
ProcessorsType>
: public MapParser<
R, W, std::unordered_map<std::string, T, Hash, KeyEqual, Allocator>,
ProcessorsType> {};
template <class R, class W, class T, class ProcessorsType>
requires AreReaderAndWriter<R, W, std::unordered_map<std::string, T>>
struct Parser<R, W, std::unordered_map<std::string, T>, ProcessorsType>
: public MapParser<R, W, std::unordered_map<std::string, T>,
ProcessorsType> {
};
template <class R, class W, class T, class ProcessorsType>
requires AreReaderAndWriter<R, W, Object<T>>
requires AreReaderAndWriter<R, W, Object<T>>
struct Parser<R, W, Object<T>, ProcessorsType>
: public MapParser<R, W, Object<T>, ProcessorsType> {
};
: public MapParser<R, W, Object<T>, ProcessorsType> {};
template <class R, class W, class T, class ProcessorsType>
requires AreReaderAndWriter<R, W, ExtraFields<T>>
requires AreReaderAndWriter<R, W, ExtraFields<T>>
struct Parser<R, W, ExtraFields<T>, ProcessorsType>
: public MapParser<R, W, ExtraFields<T>, ProcessorsType> {
};
: public MapParser<R, W, ExtraFields<T>, ProcessorsType> {};
} // namespace parsing
} // namespace rfl
+49 -42
View File
@@ -22,76 +22,83 @@ namespace rfl {
namespace parsing {
template <class R, class W, class T, class ProcessorsType>
requires AreReaderAndWriter<R, W, std::deque<T>>
requires AreReaderAndWriter<R, W, std::deque<T>>
struct Parser<R, W, std::deque<T>, ProcessorsType>
: public VectorParser<R, W, std::deque<T>, ProcessorsType> {
};
: public VectorParser<R, W, std::deque<T>, ProcessorsType> {};
template <class R, class W, class T, class ProcessorsType>
requires AreReaderAndWriter<R, W, std::forward_list<T>>
requires AreReaderAndWriter<R, W, std::forward_list<T>>
struct Parser<R, W, std::forward_list<T>, ProcessorsType>
: public VectorParser<R, W, std::forward_list<T>, ProcessorsType> {
};
: public VectorParser<R, W, std::forward_list<T>, ProcessorsType> {};
template <class R, class W, class T, class ProcessorsType>
requires AreReaderAndWriter<R, W, std::list<T>>
requires AreReaderAndWriter<R, W, std::list<T>>
struct Parser<R, W, std::list<T>, ProcessorsType>
: public VectorParser<R, W, std::list<T>, ProcessorsType> {
};
: public VectorParser<R, W, std::list<T>, ProcessorsType> {};
template <class R, class W, class K, class V, class ProcessorsType>
requires AreReaderAndWriter<R, W, std::map<K, V>>
requires AreReaderAndWriter<R, W, std::map<K, V>>
struct Parser<R, W, std::map<K, V>, ProcessorsType>
: public VectorParser<R, W, std::map<K, V>, ProcessorsType> {
};
: public VectorParser<R, W, std::map<K, V>, ProcessorsType> {};
template <class R, class W, class K, class V, class ProcessorsType>
requires AreReaderAndWriter<R, W, std::multimap<K, V>>
requires AreReaderAndWriter<R, W, std::multimap<K, V>>
struct Parser<R, W, std::multimap<K, V>, ProcessorsType>
: public VectorParser<R, W, std::multimap<K, V>, ProcessorsType> {
};
: public VectorParser<R, W, std::multimap<K, V>, ProcessorsType> {};
template <class R, class W, class T, class ProcessorsType>
requires AreReaderAndWriter<R, W, std::multiset<T>>
requires AreReaderAndWriter<R, W, std::multiset<T>>
struct Parser<R, W, std::multiset<T>, ProcessorsType>
: public VectorParser<R, W, std::multiset<T>, ProcessorsType> {
};
: public VectorParser<R, W, std::multiset<T>, ProcessorsType> {};
template <class R, class W, class T, class ProcessorsType>
requires AreReaderAndWriter<R, W, std::set<T>>
requires AreReaderAndWriter<R, W, std::set<T>>
struct Parser<R, W, std::set<T>, ProcessorsType>
: public VectorParser<R, W, std::set<T>, ProcessorsType> {
};
: public VectorParser<R, W, std::set<T>, ProcessorsType> {};
template <class R, class W, class K, class V, class ProcessorsType>
requires AreReaderAndWriter<R, W, std::unordered_map<K, V>>
struct Parser<R, W, std::unordered_map<K, V>, ProcessorsType>
: public VectorParser<R, W, std::unordered_map<K, V>, ProcessorsType> {
};
template <class R, class W, class K, class V, class Hash, class KeyEqual,
class Allocator, class ProcessorsType>
requires AreReaderAndWriter<
R, W, std::unordered_map<K, V, Hash, KeyEqual, Allocator>>
struct Parser<R, W, std::unordered_map<K, V, Hash, KeyEqual, Allocator>,
ProcessorsType>
: public VectorParser<R, W,
std::unordered_map<K, V, Hash, KeyEqual, Allocator>,
ProcessorsType> {};
template <class R, class W, class T, class ProcessorsType>
requires AreReaderAndWriter<R, W, std::unordered_multiset<T>>
struct Parser<R, W, std::unordered_multiset<T>, ProcessorsType>
: public VectorParser<R, W, std::unordered_multiset<T>, ProcessorsType> {
};
template <class R, class W, class T, class Hash, class KeyEqual,
class Allocator, class ProcessorsType>
requires AreReaderAndWriter<
R, W, std::unordered_multiset<T, Hash, KeyEqual, Allocator>>
struct Parser<R, W, std::unordered_multiset<T, Hash, KeyEqual, Allocator>,
ProcessorsType>
: public VectorParser<R, W,
std::unordered_multiset<T, Hash, KeyEqual, Allocator>,
ProcessorsType> {};
template <class R, class W, class K, class V, class ProcessorsType>
requires AreReaderAndWriter<R, W, std::unordered_multimap<K, V>>
struct Parser<R, W, std::unordered_multimap<K, V>, ProcessorsType>
template <class R, class W, class K, class V, class Hash, class KeyEqual,
class Allocator, class ProcessorsType>
requires AreReaderAndWriter<
R, W, std::unordered_multimap<K, V, Hash, KeyEqual, Allocator>>
struct Parser<R, W, std::unordered_multimap<K, V, Hash, KeyEqual, Allocator>,
ProcessorsType>
: public VectorParser<R, W, std::unordered_multimap<K, V>, ProcessorsType> {
};
template <class R, class W, class T, class ProcessorsType>
requires AreReaderAndWriter<R, W, std::unordered_set<T>>
struct Parser<R, W, std::unordered_set<T>, ProcessorsType>
: public VectorParser<R, W, std::unordered_set<T>, ProcessorsType> {
};
template <class R, class W, class T, class Hash, class KeyEqual,
class Allocator, class ProcessorsType>
requires AreReaderAndWriter<R, W,
std::unordered_set<T, Hash, KeyEqual, Allocator>>
struct Parser<R, W, std::unordered_set<T, Hash, KeyEqual, Allocator>,
ProcessorsType>
: public VectorParser<R, W,
std::unordered_set<T, Hash, KeyEqual, Allocator>,
ProcessorsType> {};
template <class R, class W, class T, class ProcessorsType>
requires AreReaderAndWriter<R, W, std::vector<T>>
requires AreReaderAndWriter<R, W, std::vector<T>>
struct Parser<R, W, std::vector<T>, ProcessorsType>
: public VectorParser<R, W, std::vector<T>, ProcessorsType> {
};
: public VectorParser<R, W, std::vector<T>, ProcessorsType> {};
} // namespace parsing
} // namespace rfl
+6 -4
View File
@@ -20,11 +20,13 @@ class is_map_like<std::map<K, V>> : public std::true_type {};
template <class K, class V>
class is_map_like<std::multimap<K, V>> : public std::true_type {};
template <class K, class V>
class is_map_like<std::unordered_map<K, V>> : public std::true_type {};
template <class K, class V, class Hash, class KeyEqual, class Allocator>
class is_map_like<std::unordered_map<K, V, Hash, KeyEqual, Allocator>>
: public std::true_type {};
template <class K, class V>
class is_map_like<std::unordered_multimap<K, V>> : public std::true_type {};
template <class K, class V, class Hash, class KeyEqual, class Allocator>
class is_map_like<std::unordered_multimap<K, V, Hash, KeyEqual, Allocator>>
: public std::true_type {};
template <class T>
constexpr bool is_map_like_v =
@@ -17,8 +17,9 @@ class is_map_like_not_multimap : public std::false_type {};
template <class K, class V>
class is_map_like_not_multimap<std::map<K, V>> : public std::true_type {};
template <class K, class V>
class is_map_like_not_multimap<std::unordered_map<K, V>>
template <class K, class V, class Hash, class KeyEqual, class Allocator>
class is_map_like_not_multimap<
std::unordered_map<K, V, Hash, KeyEqual, Allocator>>
: public std::true_type {};
} // namespace parsing
@@ -0,0 +1,39 @@
#include <iostream>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <string>
#include <unordered_map>
#include "write_and_read.hpp"
namespace test_unordered_map_with_custom_hash {
template <typename T>
struct DummyHash {
std::size_t operator()(const T& v) const { return std::hash<T>()(v); }
};
struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name = "Simpson";
std::unique_ptr<std::unordered_map<int, Person, DummyHash<int>>> children;
};
TEST(json, test_unordered_map_with_custom_hash) {
auto children =
std::make_unique<std::unordered_map<int, Person, DummyHash<int>>>();
children->insert(std::make_pair(1, Person{.first_name = "Bart"}));
children->insert(std::make_pair(2, Person{.first_name = "Lisa"}));
children->insert(std::make_pair(3, Person{.first_name = "Maggie"}));
const auto homer =
Person{.first_name = "Homer", .children = std::move(children)};
// Unordered maps are unpredictable. We therefore only make sure that this
// compiles.
const auto json_string = rfl::json::write(homer);
const auto homer2 = rfl::json::read<Person>(json_string);
EXPECT_TRUE(true);
}
} // namespace test_unordered_map_with_custom_hash