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