-
Notifications
You must be signed in to change notification settings - Fork 729
/
Copy pathhash.hh
83 lines (65 loc) · 1.96 KB
/
hash.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#ifndef hash_hh_INCLUDED
#define hash_hh_INCLUDED
#include <type_traits>
#include <utility>
#include <cstddef>
#include <cstdint>
namespace Kakoune
{
inline size_t fnv1a(const char* data, size_t len)
{
constexpr uint32_t FNV_prime_32 = 16777619;
constexpr uint32_t offset_basis_32 = 2166136261;
uint32_t hash_value = offset_basis_32;
for (size_t i = 0; i < len; ++i)
hash_value = (hash_value ^ data[i]) * FNV_prime_32;
return hash_value;
}
size_t murmur3(const char* input, size_t len);
template<typename Type> requires std::is_integral_v<Type>
constexpr size_t hash_value(const Type& val)
{
return (size_t)val;
}
template<typename Type> requires std::is_enum_v<Type>
constexpr size_t hash_value(const Type& val)
{
return hash_value((std::underlying_type_t<Type>)val);
}
template<typename Type>
constexpr size_t hash_values(Type&& t)
{
return hash_value(std::forward<Type>(t));
}
constexpr size_t combine_hash(size_t lhs, size_t rhs)
{
return lhs ^ (rhs + 0x9e3779b9 + (lhs << 6) + (lhs >> 2));
}
template<typename Type, typename... RemainingTypes>
constexpr size_t hash_values(Type&& t, RemainingTypes&&... rt)
{
size_t seed = hash_values(std::forward<RemainingTypes>(rt)...);
return combine_hash(seed, hash_value(std::forward<Type>(t)));
}
template<typename T1, typename T2>
constexpr size_t hash_value(const std::pair<T1, T2>& val)
{
return hash_values(val.first, val.second);
}
template<typename Type>
struct Hash
{
constexpr size_t operator()(const Type& val) const
{
return hash_value(val);
}
};
// Traits specifying if two types have compatible hashing, that is,
// if lhs == rhs => hash_value(lhs) == hash_value(rhs)
template<typename Lhs, typename Rhs>
struct HashCompatible : std::false_type {};
template<typename T> struct HashCompatible<T, T> : std::true_type {};
template<typename Lhs, typename Rhs>
constexpr bool IsHashCompatible = HashCompatible<Lhs, Rhs>::value;
}
#endif // hash_hh_INCLUDED