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 | #ifndef _FUNCTIONAL_HASH_H |
31 | #define _FUNCTIONAL_HASH_H 1 |
32 | |
33 | #pragma GCC system_header |
34 | |
35 | #include <bits/hash_bytes.h> |
36 | |
37 | namespace std _GLIBCXX_VISIBILITY(default) |
38 | { |
39 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
40 | |
41 | |
42 | |
43 | |
44 | |
45 | |
46 | |
47 | |
48 | |
49 | template<typename _Result, typename _Arg> |
50 | struct __hash_base |
51 | { |
52 | typedef _Result result_type; |
53 | typedef _Arg argument_type; |
54 | }; |
55 | |
56 | |
57 | template<typename _Tp> |
58 | struct hash; |
59 | |
60 | template<typename _Tp, typename = void> |
61 | struct __poison_hash |
62 | { |
63 | static constexpr bool __enable_hash_call = false; |
64 | private: |
65 | |
66 | __poison_hash(__poison_hash&&); |
67 | ~__poison_hash(); |
68 | }; |
69 | |
70 | template<typename _Tp> |
71 | struct __poison_hash<_Tp, __void_t<decltype(hash<_Tp>()(declval<_Tp>()))>> |
72 | { |
73 | static constexpr bool __enable_hash_call = true; |
74 | }; |
75 | |
76 | |
77 | template<typename _Tp, bool = is_enum<_Tp>::value> |
78 | struct __hash_enum |
79 | { |
80 | private: |
81 | |
82 | __hash_enum(__hash_enum&&); |
83 | ~__hash_enum(); |
84 | }; |
85 | |
86 | |
87 | template<typename _Tp> |
88 | struct __hash_enum<_Tp, true> : public __hash_base<size_t, _Tp> |
89 | { |
90 | size_t |
91 | operator()(_Tp __val) const noexcept |
92 | { |
93 | using __type = typename underlying_type<_Tp>::type; |
94 | return hash<__type>{}(static_cast<__type>(__val)); |
95 | } |
96 | }; |
97 | |
98 | |
99 | |
100 | template<typename _Tp> |
101 | struct hash : __hash_enum<_Tp> |
102 | { }; |
103 | |
104 | |
105 | template<typename _Tp> |
106 | struct hash<_Tp*> : public __hash_base<size_t, _Tp*> |
107 | { |
108 | size_t |
109 | operator()(_Tp* __p) const noexcept |
110 | { return reinterpret_cast<size_t>(__p); } |
111 | }; |
112 | |
113 | |
114 | #define _Cxx_hashtable_define_trivial_hash(_Tp) \ |
115 | template<> \ |
116 | struct hash<_Tp> : public __hash_base<size_t, _Tp> \ |
117 | { \ |
118 | size_t \ |
119 | operator()(_Tp __val) const noexcept \ |
120 | { return static_cast<size_t>(__val); } \ |
121 | }; |
122 | |
123 | |
124 | _Cxx_hashtable_define_trivial_hash(bool) |
125 | |
126 | |
127 | _Cxx_hashtable_define_trivial_hash(char) |
128 | |
129 | |
130 | _Cxx_hashtable_define_trivial_hash(signed char) |
131 | |
132 | |
133 | _Cxx_hashtable_define_trivial_hash(unsigned char) |
134 | |
135 | |
136 | _Cxx_hashtable_define_trivial_hash(wchar_t) |
137 | |
138 | |
139 | _Cxx_hashtable_define_trivial_hash(char16_t) |
140 | |
141 | |
142 | _Cxx_hashtable_define_trivial_hash(char32_t) |
143 | |
144 | |
145 | _Cxx_hashtable_define_trivial_hash(short) |
146 | |
147 | |
148 | _Cxx_hashtable_define_trivial_hash(int) |
149 | |
150 | |
151 | _Cxx_hashtable_define_trivial_hash(long) |
152 | |
153 | |
154 | _Cxx_hashtable_define_trivial_hash(long long) |
155 | |
156 | |
157 | _Cxx_hashtable_define_trivial_hash(unsigned short) |
158 | |
159 | |
160 | _Cxx_hashtable_define_trivial_hash(unsigned int) |
161 | |
162 | |
163 | _Cxx_hashtable_define_trivial_hash(unsigned long) |
164 | |
165 | |
166 | _Cxx_hashtable_define_trivial_hash(unsigned long long) |
167 | |
168 | #ifdef __GLIBCXX_TYPE_INT_N_0 |
169 | _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_0) |
170 | _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_0 unsigned) |
171 | #endif |
172 | #ifdef __GLIBCXX_TYPE_INT_N_1 |
173 | _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_1) |
174 | _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_1 unsigned) |
175 | #endif |
176 | #ifdef __GLIBCXX_TYPE_INT_N_2 |
177 | _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_2) |
178 | _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_2 unsigned) |
179 | #endif |
180 | #ifdef __GLIBCXX_TYPE_INT_N_3 |
181 | _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_3) |
182 | _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_3 unsigned) |
183 | #endif |
184 | |
185 | #undef _Cxx_hashtable_define_trivial_hash |
186 | |
187 | struct _Hash_impl |
188 | { |
189 | static size_t |
190 | hash(const void* __ptr, size_t __clength, |
191 | size_t __seed = static_cast<size_t>(0xc70f6907UL)) |
192 | { return _Hash_bytes(__ptr, __clength, __seed); } |
193 | |
194 | template<typename _Tp> |
195 | static size_t |
196 | hash(const _Tp& __val) |
197 | { return hash(&__val, sizeof(__val)); } |
198 | |
199 | template<typename _Tp> |
200 | static size_t |
201 | __hash_combine(const _Tp& __val, size_t __hash) |
202 | { return hash(&__val, sizeof(__val), __hash); } |
203 | }; |
204 | |
205 | |
206 | struct _Fnv_hash_impl |
207 | { |
208 | static size_t |
209 | hash(const void* __ptr, size_t __clength, |
210 | size_t __seed = static_cast<size_t>(2166136261UL)) |
211 | { return _Fnv_hash_bytes(__ptr, __clength, __seed); } |
212 | |
213 | template<typename _Tp> |
214 | static size_t |
215 | hash(const _Tp& __val) |
216 | { return hash(&__val, sizeof(__val)); } |
217 | |
218 | template<typename _Tp> |
219 | static size_t |
220 | __hash_combine(const _Tp& __val, size_t __hash) |
221 | { return hash(&__val, sizeof(__val), __hash); } |
222 | }; |
223 | |
224 | |
225 | template<> |
226 | struct hash<float> : public __hash_base<size_t, float> |
227 | { |
228 | size_t |
229 | operator()(float __val) const noexcept |
230 | { |
231 | |
232 | return __val != 0.0f ? std::_Hash_impl::hash(__val) : 0; |
233 | } |
234 | }; |
235 | |
236 | |
237 | template<> |
238 | struct hash<double> : public __hash_base<size_t, double> |
239 | { |
240 | size_t |
241 | operator()(double __val) const noexcept |
242 | { |
243 | |
244 | return __val != 0.0 ? std::_Hash_impl::hash(__val) : 0; |
245 | } |
246 | }; |
247 | |
248 | |
249 | template<> |
250 | struct hash<long double> |
251 | : public __hash_base<size_t, long double> |
252 | { |
253 | _GLIBCXX_PURE size_t |
254 | operator()(long double __val) const noexcept; |
255 | }; |
256 | |
257 | |
258 | |
259 | |
260 | |
261 | |
262 | |
263 | template<typename _Hash> |
264 | struct __is_fast_hash : public std::true_type |
265 | { }; |
266 | |
267 | template<> |
268 | struct __is_fast_hash<hash<long double>> : public std::false_type |
269 | { }; |
270 | |
271 | _GLIBCXX_END_NAMESPACE_VERSION |
272 | } |
273 | |
274 | #endif |
275 | |