add support for std::variant to hashlib

This commit is contained in:
Emily Schmidt 2024-06-20 16:25:21 +01:00
parent dbf2bc3b1d
commit 248d5f72d4
2 changed files with 20 additions and 0 deletions

View File

@ -186,6 +186,25 @@ inline unsigned int mkhash(const T &v) {
return hash_ops<T>().hash(v); return hash_ops<T>().hash(v);
} }
template<> struct hash_ops<std::monostate> {
static inline bool cmp(std::monostate a, std::monostate b) {
return a == b;
}
static inline unsigned int hash(std::monostate) {
return mkhash_init;
}
};
template<typename... T> struct hash_ops<std::variant<T...>> {
static inline bool cmp(std::variant<T...> a, std::variant<T...> b) {
return a == b;
}
static inline unsigned int hash(std::variant<T...> a) {
unsigned int h = std::visit([](const auto &v) { return mkhash(v); }, a);
return mkhash(a.index(), h);
}
};
inline int hashtable_size(int min_size) inline int hashtable_size(int min_size)
{ {
// Primes as generated by https://oeis.org/A175953 // Primes as generated by https://oeis.org/A175953

View File

@ -30,6 +30,7 @@
#include <unordered_map> #include <unordered_map>
#include <unordered_set> #include <unordered_set>
#include <initializer_list> #include <initializer_list>
#include <variant>
#include <stdexcept> #include <stdexcept>
#include <memory> #include <memory>
#include <cmath> #include <cmath>