kernel: Re-implement `dict` hash code as a `dict` member function instead of a specialized template for `hash_ops`.

This commit is contained in:
Alberto Gonzalez 2020-04-22 22:04:22 +00:00
parent e173291649
commit 35b94d1f66
No known key found for this signature in database
GPG Key ID: 8395A8BA109708B2
1 changed files with 14 additions and 20 deletions

View File

@ -19,12 +19,6 @@
namespace hashlib { namespace hashlib {
template<typename T> struct hash_ops;
template<typename K, typename T, typename OPS = hash_ops<K>> class dict;
template<typename K, int offset = 0, typename OPS = hash_ops<K>> class idict;
template<typename K, typename OPS = hash_ops<K>> class pool;
template<typename K, typename OPS = hash_ops<K>> class mfp;
const int hashtable_size_trigger = 2; const int hashtable_size_trigger = 2;
const int hashtable_size_factor = 3; const int hashtable_size_factor = 3;
@ -106,20 +100,6 @@ template<typename P, typename Q> struct hash_ops<std::pair<P, Q>> {
} }
}; };
template<typename P, typename Q> struct hash_ops<dict<P, Q>> {
static inline bool cmp(dict<P, Q> a, dict<P, Q> b) {
return a == b;
}
static inline unsigned int hash(dict<P, Q> a) {
unsigned int h = mkhash_init;
for (auto &it : a) {
h = mkhash(h, hash_ops<P>::hash(it.first));
h = mkhash(h, hash_ops<Q>::hash(it.second));
}
return h;
}
};
template<typename... T> struct hash_ops<std::tuple<T...>> { template<typename... T> struct hash_ops<std::tuple<T...>> {
static inline bool cmp(std::tuple<T...> a, std::tuple<T...> b) { static inline bool cmp(std::tuple<T...> a, std::tuple<T...> b) {
return a == b; return a == b;
@ -211,6 +191,11 @@ inline int hashtable_size(int min_size)
throw std::length_error("hash table exceeded maximum size."); throw std::length_error("hash table exceeded maximum size.");
} }
template<typename K, typename T, typename OPS = hash_ops<K>> class dict;
template<typename K, int offset = 0, typename OPS = hash_ops<K>> class idict;
template<typename K, typename OPS = hash_ops<K>> class pool;
template<typename K, typename OPS = hash_ops<K>> class mfp;
template<typename K, typename T, typename OPS> template<typename K, typename T, typename OPS>
class dict class dict
{ {
@ -630,6 +615,15 @@ public:
return !operator==(other); return !operator==(other);
} }
unsigned int hash() const {
unsigned int h = mkhash_init;
for (auto &it : entries) {
h = mkhash(h, hash_ops<K>::hash(it.udata.first));
h = mkhash(h, hash_ops<T>::hash(it.udata.second));
}
return h;
}
void reserve(size_t n) { entries.reserve(n); } void reserve(size_t n) { entries.reserve(n); }
size_t size() const { return entries.size(); } size_t size() const { return entries.size(); }
bool empty() const { return entries.empty(); } bool empty() const { return entries.empty(); }