-
Notifications
You must be signed in to change notification settings - Fork 6
/
hash.hpp
177 lines (140 loc) · 3.52 KB
/
hash.hpp
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
* @file hash.hpp
* @brief
* @date 2019-5-14
* @author Peter
* @copyright
* Peter of [ThinkSpirit Laboratory](http://thinkspirit.org/)
* of [Nanjing University of Information Science & Technology](http://www.nuist.edu.cn/)
* all rights reserved
*/
#ifndef KERBAL_HASH_HASH_HPP
#define KERBAL_HASH_HASH_HPP
#include <kerbal/compatibility/constexpr.hpp>
#include <kerbal/compatibility/noexcept.hpp>
#include <kerbal/compatibility/static_assert.hpp>
#include <kerbal/hash/murmur_hash2.hpp>
#include <kerbal/type_traits/is_integral.hpp>
#include <cstddef>
namespace kerbal
{
namespace hash
{
template <typename T>
struct integral_hash
{
private:
KERBAL_STATIC_ASSERT(kerbal::type_traits::is_integral<T>::value, "");
public:
typedef std::size_t result_type;
typedef T argument_type;
KERBAL_CONSTEXPR
result_type operator()(const T & val) const KERBAL_NOEXCEPT
{
return static_cast<std::size_t>(val);
}
};
/// Primary class template hash.
template <typename >
struct hash;
template <>
struct hash<bool> : kerbal::hash::integral_hash<bool>
{
};
template <>
struct hash<char> : kerbal::hash::integral_hash<char>
{
};
# if __cplusplus >= 201103L
template <>
struct hash<char16_t> : kerbal::hash::integral_hash<char16_t>
{
};
template <>
struct hash<char32_t> : kerbal::hash::integral_hash<char32_t>
{
};
# endif
template <>
struct hash<signed char> : kerbal::hash::integral_hash<signed char>
{
};
template <>
struct hash<unsigned char> : kerbal::hash::integral_hash<unsigned char>
{
};
template <>
struct hash<short> : kerbal::hash::integral_hash<short>
{
};
template <>
struct hash<unsigned short> : kerbal::hash::integral_hash<unsigned short>
{
};
template <>
struct hash<int> : kerbal::hash::integral_hash<int>
{
};
template <>
struct hash<unsigned int> : kerbal::hash::integral_hash<unsigned int>
{
};
template <>
struct hash<long> : kerbal::hash::integral_hash<long>
{
};
template <>
struct hash<unsigned long> : kerbal::hash::integral_hash<unsigned long>
{
};
template <>
struct hash<long long> : kerbal::hash::integral_hash<long long>
{
};
template <>
struct hash<unsigned long long> : kerbal::hash::integral_hash<unsigned long long>
{
};
template <>
struct hash<float>
{
typedef kerbal::hash::murmur_hash2<float>::result_type result_type;
typedef float argument_type;
std::size_t operator()(float val) const KERBAL_NOEXCEPT
{
return val == 0.0 ? 0 : kerbal::hash::murmur_hash2<float>()(val);
}
};
template <>
struct hash<double>
{
typedef kerbal::hash::murmur_hash2<double>::result_type result_type;
typedef double argument_type;
std::size_t operator()(double val) const KERBAL_NOEXCEPT
{
return val == 0.0 ? 0 : kerbal::hash::murmur_hash2<double>()(val);
}
};
template <>
struct hash<long double>
{
typedef kerbal::hash::murmur_hash2<long double>::result_type result_type;
typedef long double argument_type;
std::size_t operator()(long double val) const KERBAL_NOEXCEPT
{
return val == 0.0 ? 0 : kerbal::hash::murmur_hash2<long double>()(val);
}
};
template <typename T>
struct hash<T *>
{
typedef std::size_t result_type;
typedef T * argument_type;
std::size_t operator()(T * p) const KERBAL_NOEXCEPT
{
return reinterpret_cast<std::size_t>(p);
}
};
} // namespace hash
} // namespace kerbal
#endif // KERBAL_HASH_HASH_HPP