Skip to content

Commit 3985ae7

Browse files
committed
add support for std::variant to hashlib
1 parent e36eaa5 commit 3985ae7

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

kernel/hashlib.h

+19
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,25 @@ inline unsigned int mkhash(const T &v) {
186186
return hash_ops<T>().hash(v);
187187
}
188188

189+
template<> struct hash_ops<std::monostate> {
190+
static inline bool cmp(std::monostate a, std::monostate b) {
191+
return a == b;
192+
}
193+
static inline unsigned int hash(std::monostate) {
194+
return mkhash_init;
195+
}
196+
};
197+
198+
template<typename... T> struct hash_ops<std::variant<T...>> {
199+
static inline bool cmp(std::variant<T...> a, std::variant<T...> b) {
200+
return a == b;
201+
}
202+
static inline unsigned int hash(std::variant<T...> a) {
203+
unsigned int h = std::visit([](const auto &v) { return mkhash(v); }, a);
204+
return mkhash(a.index(), h);
205+
}
206+
};
207+
189208
inline int hashtable_size(int min_size)
190209
{
191210
static std::vector<int> zero_and_some_primes = {

kernel/yosys_common.h

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <unordered_map>
3131
#include <unordered_set>
3232
#include <initializer_list>
33+
#include <variant>
3334
#include <stdexcept>
3435
#include <memory>
3536
#include <cmath>

0 commit comments

Comments
 (0)