xref: /freebsd/contrib/llvm-project/libcxx/include/__functional/hash.h (revision a03411e84728e9b267056fd31c7d1d9d1dc1b01e)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef _LIBCPP___FUNCTIONAL_HASH_H
10 #define _LIBCPP___FUNCTIONAL_HASH_H
11 
12 #include <__config>
13 #include <__functional/invoke.h>
14 #include <__functional/unary_function.h>
15 #include <__fwd/hash.h>
16 #include <__tuple/sfinae_helpers.h>
17 #include <__type_traits/is_copy_constructible.h>
18 #include <__type_traits/is_default_constructible.h>
19 #include <__type_traits/is_enum.h>
20 #include <__type_traits/is_move_constructible.h>
21 #include <__type_traits/underlying_type.h>
22 #include <__utility/forward.h>
23 #include <__utility/move.h>
24 #include <__utility/pair.h>
25 #include <__utility/swap.h>
26 #include <cstddef>
27 #include <cstdint>
28 #include <cstring>
29 #include <limits>
30 
31 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
32 #  pragma GCC system_header
33 #endif
34 
35 _LIBCPP_BEGIN_NAMESPACE_STD
36 
37 template <class _Size>
38 inline _LIBCPP_HIDE_FROM_ABI
39 _Size
40 __loadword(const void* __p)
41 {
42     _Size __r;
43     _VSTD::memcpy(&__r, __p, sizeof(__r));
44     return __r;
45 }
46 
47 // We use murmur2 when size_t is 32 bits, and cityhash64 when size_t
48 // is 64 bits.  This is because cityhash64 uses 64bit x 64bit
49 // multiplication, which can be very slow on 32-bit systems.
50 template <class _Size, size_t = sizeof(_Size)*__CHAR_BIT__>
51 struct __murmur2_or_cityhash;
52 
53 template <class _Size>
54 struct __murmur2_or_cityhash<_Size, 32>
55 {
56     _LIBCPP_HIDE_FROM_ABI _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
57     _Size operator()(const void* __key, _Size __len) const {
58       // murmur2
59       const _Size __m = 0x5bd1e995;
60       const _Size __r = 24;
61       _Size __h = __len;
62       const unsigned char* __data = static_cast<const unsigned char*>(__key);
63       for (; __len >= 4; __data += 4, __len -= 4)
64       {
65           _Size __k = std::__loadword<_Size>(__data);
66           __k *= __m;
67           __k ^= __k >> __r;
68           __k *= __m;
69           __h *= __m;
70           __h ^= __k;
71       }
72       switch (__len)
73       {
74       case 3:
75           __h ^= static_cast<_Size>(__data[2] << 16);
76           _LIBCPP_FALLTHROUGH();
77       case 2:
78           __h ^= static_cast<_Size>(__data[1] << 8);
79           _LIBCPP_FALLTHROUGH();
80       case 1:
81           __h ^= __data[0];
82           __h *= __m;
83       }
84       __h ^= __h >> 13;
85       __h *= __m;
86       __h ^= __h >> 15;
87       return __h;
88     }
89 };
90 
91 template <class _Size>
92 struct __murmur2_or_cityhash<_Size, 64>
93 {
94   // cityhash64
95   _LIBCPP_HIDE_FROM_ABI _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
96   _Size operator()(const void* __key, _Size __len) const {
97     const char* __s = static_cast<const char*>(__key);
98     if (__len <= 32) {
99         if (__len <= 16) {
100         return __hash_len_0_to_16(__s, __len);
101         } else {
102         return __hash_len_17_to_32(__s, __len);
103         }
104     } else if (__len <= 64) {
105         return __hash_len_33_to_64(__s, __len);
106     }
107 
108     // For strings over 64 bytes we hash the end first, and then as we
109     // loop we keep 56 bytes of state: v, w, x, y, and z.
110     _Size __x = std::__loadword<_Size>(__s + __len - 40);
111     _Size __y = std::__loadword<_Size>(__s + __len - 16) +
112                 std::__loadword<_Size>(__s + __len - 56);
113     _Size __z = __hash_len_16(std::__loadword<_Size>(__s + __len - 48) + __len,
114                             std::__loadword<_Size>(__s + __len - 24));
115     pair<_Size, _Size> __v = __weak_hash_len_32_with_seeds(__s + __len - 64, __len, __z);
116     pair<_Size, _Size> __w = __weak_hash_len_32_with_seeds(__s + __len - 32, __y + __k1, __x);
117     __x = __x * __k1 + std::__loadword<_Size>(__s);
118 
119     // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
120     __len = (__len - 1) & ~static_cast<_Size>(63);
121     do {
122         __x = __rotate(__x + __y + __v.first + std::__loadword<_Size>(__s + 8), 37) * __k1;
123         __y = __rotate(__y + __v.second + std::__loadword<_Size>(__s + 48), 42) * __k1;
124         __x ^= __w.second;
125         __y += __v.first + std::__loadword<_Size>(__s + 40);
126         __z = __rotate(__z + __w.first, 33) * __k1;
127         __v = __weak_hash_len_32_with_seeds(__s, __v.second * __k1, __x + __w.first);
128         __w = __weak_hash_len_32_with_seeds(__s + 32, __z + __w.second,
129                                             __y + std::__loadword<_Size>(__s + 16));
130         _VSTD::swap(__z, __x);
131         __s += 64;
132         __len -= 64;
133     } while (__len != 0);
134     return __hash_len_16(
135         __hash_len_16(__v.first, __w.first) + __shift_mix(__y) * __k1 + __z,
136         __hash_len_16(__v.second, __w.second) + __x);
137   }
138 
139   private:
140     // Some primes between 2^63 and 2^64.
141     static const _Size __k0 = 0xc3a5c85c97cb3127ULL;
142     static const _Size __k1 = 0xb492b66fbe98f273ULL;
143     static const _Size __k2 = 0x9ae16a3b2f90404fULL;
144     static const _Size __k3 = 0xc949d7c7509e6557ULL;
145 
146   _LIBCPP_HIDE_FROM_ABI
147   static _Size __rotate(_Size __val, int __shift) {
148     return __shift == 0 ? __val : ((__val >> __shift) | (__val << (64 - __shift)));
149   }
150 
151   _LIBCPP_HIDE_FROM_ABI
152   static _Size __rotate_by_at_least_1(_Size __val, int __shift) {
153     return (__val >> __shift) | (__val << (64 - __shift));
154   }
155 
156   _LIBCPP_HIDE_FROM_ABI
157   static _Size __shift_mix(_Size __val) {
158     return __val ^ (__val >> 47);
159   }
160 
161   _LIBCPP_HIDE_FROM_ABI _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
162   static _Size __hash_len_16(_Size __u, _Size __v) {
163     const _Size __mul = 0x9ddfea08eb382d69ULL;
164     _Size __a = (__u ^ __v) * __mul;
165     __a ^= (__a >> 47);
166     _Size __b = (__v ^ __a) * __mul;
167     __b ^= (__b >> 47);
168     __b *= __mul;
169     return __b;
170   }
171 
172   _LIBCPP_HIDE_FROM_ABI _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
173   static _Size __hash_len_0_to_16(const char* __s, _Size __len) {
174     if (__len > 8) {
175       const _Size __a = std::__loadword<_Size>(__s);
176       const _Size __b = std::__loadword<_Size>(__s + __len - 8);
177       return __hash_len_16(__a, __rotate_by_at_least_1(__b + __len, __len)) ^ __b;
178     }
179     if (__len >= 4) {
180       const uint32_t __a = std::__loadword<uint32_t>(__s);
181       const uint32_t __b = std::__loadword<uint32_t>(__s + __len - 4);
182 #ifdef _LIBCPP_ABI_FIX_CITYHASH_IMPLEMENTATION
183       return __hash_len_16(__len + (static_cast<_Size>(__a) << 3), __b);
184 #else
185       return __hash_len_16(__len + (__a << 3), __b);
186 #endif
187     }
188     if (__len > 0) {
189       const unsigned char __a = static_cast<unsigned char>(__s[0]);
190       const unsigned char __b = static_cast<unsigned char>(__s[__len >> 1]);
191       const unsigned char __c = static_cast<unsigned char>(__s[__len - 1]);
192       const uint32_t __y = static_cast<uint32_t>(__a) +
193                            (static_cast<uint32_t>(__b) << 8);
194       const uint32_t __z = __len + (static_cast<uint32_t>(__c) << 2);
195       return __shift_mix(__y * __k2 ^ __z * __k3) * __k2;
196     }
197     return __k2;
198   }
199 
200   _LIBCPP_HIDE_FROM_ABI _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
201   static _Size __hash_len_17_to_32(const char *__s, _Size __len) {
202     const _Size __a = std::__loadword<_Size>(__s) * __k1;
203     const _Size __b = std::__loadword<_Size>(__s + 8);
204     const _Size __c = std::__loadword<_Size>(__s + __len - 8) * __k2;
205     const _Size __d = std::__loadword<_Size>(__s + __len - 16) * __k0;
206     return __hash_len_16(__rotate(__a - __b, 43) + __rotate(__c, 30) + __d,
207                          __a + __rotate(__b ^ __k3, 20) - __c + __len);
208   }
209 
210   // Return a 16-byte hash for 48 bytes.  Quick and dirty.
211   // Callers do best to use "random-looking" values for a and b.
212   _LIBCPP_HIDE_FROM_ABI _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
213   static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
214       _Size __w, _Size __x, _Size __y, _Size __z, _Size __a, _Size __b)
215   {
216     __a += __w;
217     __b = __rotate(__b + __a + __z, 21);
218     const _Size __c = __a;
219     __a += __x;
220     __a += __y;
221     __b += __rotate(__a, 44);
222     return pair<_Size, _Size>(__a + __z, __b + __c);
223   }
224 
225   // Return a 16-byte hash for s[0] ... s[31], a, and b.  Quick and dirty.
226   _LIBCPP_HIDE_FROM_ABI _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
227   static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
228       const char* __s, _Size __a, _Size __b)
229   {
230     return __weak_hash_len_32_with_seeds(std::__loadword<_Size>(__s),
231                                          std::__loadword<_Size>(__s + 8),
232                                          std::__loadword<_Size>(__s + 16),
233                                          std::__loadword<_Size>(__s + 24),
234                                          __a,
235                                          __b);
236   }
237 
238   // Return an 8-byte hash for 33 to 64 bytes.
239   _LIBCPP_HIDE_FROM_ABI _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
240   static _Size __hash_len_33_to_64(const char *__s, size_t __len) {
241     _Size __z = std::__loadword<_Size>(__s + 24);
242     _Size __a = std::__loadword<_Size>(__s) +
243                 (__len + std::__loadword<_Size>(__s + __len - 16)) * __k0;
244     _Size __b = __rotate(__a + __z, 52);
245     _Size __c = __rotate(__a, 37);
246     __a += std::__loadword<_Size>(__s + 8);
247     __c += __rotate(__a, 7);
248     __a += std::__loadword<_Size>(__s + 16);
249     _Size __vf = __a + __z;
250     _Size __vs = __b + __rotate(__a, 31) + __c;
251     __a = std::__loadword<_Size>(__s + 16) + std::__loadword<_Size>(__s + __len - 32);
252     __z += std::__loadword<_Size>(__s + __len - 8);
253     __b = __rotate(__a + __z, 52);
254     __c = __rotate(__a, 37);
255     __a += std::__loadword<_Size>(__s + __len - 24);
256     __c += __rotate(__a, 7);
257     __a += std::__loadword<_Size>(__s + __len - 16);
258     _Size __wf = __a + __z;
259     _Size __ws = __b + __rotate(__a, 31) + __c;
260     _Size __r = __shift_mix((__vf + __ws) * __k2 + (__wf + __vs) * __k0);
261     return __shift_mix(__r * __k0 + __vs) * __k2;
262   }
263 };
264 
265 template <class _Tp, size_t = sizeof(_Tp) / sizeof(size_t)>
266 struct __scalar_hash;
267 
268 template <class _Tp>
269 struct __scalar_hash<_Tp, 0>
270     : public __unary_function<_Tp, size_t>
271 {
272     _LIBCPP_HIDE_FROM_ABI
273     size_t operator()(_Tp __v) const _NOEXCEPT
274     {
275         union
276         {
277             _Tp    __t;
278             size_t __a;
279         } __u;
280         __u.__a = 0;
281         __u.__t = __v;
282         return __u.__a;
283     }
284 };
285 
286 template <class _Tp>
287 struct __scalar_hash<_Tp, 1>
288     : public __unary_function<_Tp, size_t>
289 {
290     _LIBCPP_HIDE_FROM_ABI
291     size_t operator()(_Tp __v) const _NOEXCEPT
292     {
293         union
294         {
295             _Tp    __t;
296             size_t __a;
297         } __u;
298         __u.__t = __v;
299         return __u.__a;
300     }
301 };
302 
303 template <class _Tp>
304 struct __scalar_hash<_Tp, 2>
305     : public __unary_function<_Tp, size_t>
306 {
307     _LIBCPP_HIDE_FROM_ABI
308     size_t operator()(_Tp __v) const _NOEXCEPT
309     {
310         union
311         {
312             _Tp __t;
313             struct
314             {
315                 size_t __a;
316                 size_t __b;
317             } __s;
318         } __u;
319         __u.__t = __v;
320         return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
321     }
322 };
323 
324 template <class _Tp>
325 struct __scalar_hash<_Tp, 3>
326     : public __unary_function<_Tp, size_t>
327 {
328     _LIBCPP_HIDE_FROM_ABI
329     size_t operator()(_Tp __v) const _NOEXCEPT
330     {
331         union
332         {
333             _Tp __t;
334             struct
335             {
336                 size_t __a;
337                 size_t __b;
338                 size_t __c;
339             } __s;
340         } __u;
341         __u.__t = __v;
342         return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
343     }
344 };
345 
346 template <class _Tp>
347 struct __scalar_hash<_Tp, 4>
348     : public __unary_function<_Tp, size_t>
349 {
350     _LIBCPP_HIDE_FROM_ABI
351     size_t operator()(_Tp __v) const _NOEXCEPT
352     {
353         union
354         {
355             _Tp __t;
356             struct
357             {
358                 size_t __a;
359                 size_t __b;
360                 size_t __c;
361                 size_t __d;
362             } __s;
363         } __u;
364         __u.__t = __v;
365         return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
366     }
367 };
368 
369 struct _PairT {
370   size_t first;
371   size_t second;
372 };
373 
374 _LIBCPP_HIDE_FROM_ABI
375 inline size_t __hash_combine(size_t __lhs, size_t __rhs) _NOEXCEPT {
376     typedef __scalar_hash<_PairT> _HashT;
377     const _PairT __p = {__lhs, __rhs};
378     return _HashT()(__p);
379 }
380 
381 template<class _Tp>
382 struct _LIBCPP_TEMPLATE_VIS hash<_Tp*>
383     : public __unary_function<_Tp*, size_t>
384 {
385     _LIBCPP_HIDE_FROM_ABI
386     size_t operator()(_Tp* __v) const _NOEXCEPT
387     {
388         union
389         {
390             _Tp* __t;
391             size_t __a;
392         } __u;
393         __u.__t = __v;
394         return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
395     }
396 };
397 
398 template <>
399 struct _LIBCPP_TEMPLATE_VIS hash<bool>
400     : public __unary_function<bool, size_t>
401 {
402     _LIBCPP_HIDE_FROM_ABI
403     size_t operator()(bool __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
404 };
405 
406 template <>
407 struct _LIBCPP_TEMPLATE_VIS hash<char>
408     : public __unary_function<char, size_t>
409 {
410     _LIBCPP_HIDE_FROM_ABI
411     size_t operator()(char __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
412 };
413 
414 template <>
415 struct _LIBCPP_TEMPLATE_VIS hash<signed char>
416     : public __unary_function<signed char, size_t>
417 {
418     _LIBCPP_HIDE_FROM_ABI
419     size_t operator()(signed char __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
420 };
421 
422 template <>
423 struct _LIBCPP_TEMPLATE_VIS hash<unsigned char>
424     : public __unary_function<unsigned char, size_t>
425 {
426     _LIBCPP_HIDE_FROM_ABI
427     size_t operator()(unsigned char __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
428 };
429 
430 #ifndef _LIBCPP_HAS_NO_CHAR8_T
431 template <>
432 struct _LIBCPP_TEMPLATE_VIS hash<char8_t>
433     : public __unary_function<char8_t, size_t>
434 {
435     _LIBCPP_HIDE_FROM_ABI
436     size_t operator()(char8_t __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
437 };
438 #endif // !_LIBCPP_HAS_NO_CHAR8_T
439 
440 template <>
441 struct _LIBCPP_TEMPLATE_VIS hash<char16_t>
442     : public __unary_function<char16_t, size_t>
443 {
444     _LIBCPP_HIDE_FROM_ABI
445     size_t operator()(char16_t __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
446 };
447 
448 template <>
449 struct _LIBCPP_TEMPLATE_VIS hash<char32_t>
450     : public __unary_function<char32_t, size_t>
451 {
452     _LIBCPP_HIDE_FROM_ABI
453     size_t operator()(char32_t __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
454 };
455 
456 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
457 template <>
458 struct _LIBCPP_TEMPLATE_VIS hash<wchar_t>
459     : public __unary_function<wchar_t, size_t>
460 {
461     _LIBCPP_HIDE_FROM_ABI
462     size_t operator()(wchar_t __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
463 };
464 #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
465 
466 template <>
467 struct _LIBCPP_TEMPLATE_VIS hash<short>
468     : public __unary_function<short, size_t>
469 {
470     _LIBCPP_HIDE_FROM_ABI
471     size_t operator()(short __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
472 };
473 
474 template <>
475 struct _LIBCPP_TEMPLATE_VIS hash<unsigned short>
476     : public __unary_function<unsigned short, size_t>
477 {
478     _LIBCPP_HIDE_FROM_ABI
479     size_t operator()(unsigned short __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
480 };
481 
482 template <>
483 struct _LIBCPP_TEMPLATE_VIS hash<int>
484     : public __unary_function<int, size_t>
485 {
486     _LIBCPP_HIDE_FROM_ABI
487     size_t operator()(int __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
488 };
489 
490 template <>
491 struct _LIBCPP_TEMPLATE_VIS hash<unsigned int>
492     : public __unary_function<unsigned int, size_t>
493 {
494     _LIBCPP_HIDE_FROM_ABI
495     size_t operator()(unsigned int __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
496 };
497 
498 template <>
499 struct _LIBCPP_TEMPLATE_VIS hash<long>
500     : public __unary_function<long, size_t>
501 {
502     _LIBCPP_HIDE_FROM_ABI
503     size_t operator()(long __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
504 };
505 
506 template <>
507 struct _LIBCPP_TEMPLATE_VIS hash<unsigned long>
508     : public __unary_function<unsigned long, size_t>
509 {
510     _LIBCPP_HIDE_FROM_ABI
511     size_t operator()(unsigned long __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
512 };
513 
514 template <>
515 struct _LIBCPP_TEMPLATE_VIS hash<long long>
516     : public __scalar_hash<long long>
517 {
518 };
519 
520 template <>
521 struct _LIBCPP_TEMPLATE_VIS hash<unsigned long long>
522     : public __scalar_hash<unsigned long long>
523 {
524 };
525 
526 #ifndef _LIBCPP_HAS_NO_INT128
527 
528 template <>
529 struct _LIBCPP_TEMPLATE_VIS hash<__int128_t>
530     : public __scalar_hash<__int128_t>
531 {
532 };
533 
534 template <>
535 struct _LIBCPP_TEMPLATE_VIS hash<__uint128_t>
536     : public __scalar_hash<__uint128_t>
537 {
538 };
539 
540 #endif
541 
542 template <>
543 struct _LIBCPP_TEMPLATE_VIS hash<float>
544     : public __scalar_hash<float>
545 {
546     _LIBCPP_HIDE_FROM_ABI
547     size_t operator()(float __v) const _NOEXCEPT
548     {
549         // -0.0 and 0.0 should return same hash
550        if (__v == 0.0f)
551            return 0;
552         return __scalar_hash<float>::operator()(__v);
553     }
554 };
555 
556 template <>
557 struct _LIBCPP_TEMPLATE_VIS hash<double>
558     : public __scalar_hash<double>
559 {
560     _LIBCPP_HIDE_FROM_ABI
561     size_t operator()(double __v) const _NOEXCEPT
562     {
563         // -0.0 and 0.0 should return same hash
564        if (__v == 0.0)
565            return 0;
566         return __scalar_hash<double>::operator()(__v);
567     }
568 };
569 
570 template <>
571 struct _LIBCPP_TEMPLATE_VIS hash<long double>
572     : public __scalar_hash<long double>
573 {
574     _LIBCPP_HIDE_FROM_ABI
575     size_t operator()(long double __v) const _NOEXCEPT
576     {
577         // -0.0 and 0.0 should return same hash
578         if (__v == 0.0L)
579             return 0;
580 #if defined(__i386__) || (defined(__x86_64__) && defined(__ILP32__))
581         // Zero out padding bits
582         union
583         {
584             long double __t;
585             struct
586             {
587                 size_t __a;
588                 size_t __b;
589                 size_t __c;
590                 size_t __d;
591             } __s;
592         } __u;
593         __u.__s.__a = 0;
594         __u.__s.__b = 0;
595         __u.__s.__c = 0;
596         __u.__s.__d = 0;
597         __u.__t = __v;
598         return __u.__s.__a ^ __u.__s.__b ^ __u.__s.__c ^ __u.__s.__d;
599 #elif defined(__x86_64__)
600         // Zero out padding bits
601         union
602         {
603             long double __t;
604             struct
605             {
606                 size_t __a;
607                 size_t __b;
608             } __s;
609         } __u;
610         __u.__s.__a = 0;
611         __u.__s.__b = 0;
612         __u.__t = __v;
613         return __u.__s.__a ^ __u.__s.__b;
614 #else
615         return __scalar_hash<long double>::operator()(__v);
616 #endif
617     }
618 };
619 
620 template <class _Tp, bool = is_enum<_Tp>::value>
621 struct _LIBCPP_TEMPLATE_VIS __enum_hash
622     : public __unary_function<_Tp, size_t>
623 {
624     _LIBCPP_HIDE_FROM_ABI
625     size_t operator()(_Tp __v) const _NOEXCEPT
626     {
627         typedef typename underlying_type<_Tp>::type type;
628         return hash<type>()(static_cast<type>(__v));
629     }
630 };
631 template <class _Tp>
632 struct _LIBCPP_TEMPLATE_VIS __enum_hash<_Tp, false> {
633     __enum_hash() = delete;
634     __enum_hash(__enum_hash const&) = delete;
635     __enum_hash& operator=(__enum_hash const&) = delete;
636 };
637 
638 template <class _Tp>
639 struct _LIBCPP_TEMPLATE_VIS hash : public __enum_hash<_Tp>
640 {
641 };
642 
643 #if _LIBCPP_STD_VER >= 17
644 
645 template <>
646 struct _LIBCPP_TEMPLATE_VIS hash<nullptr_t>
647   : public __unary_function<nullptr_t, size_t>
648 {
649     _LIBCPP_HIDE_FROM_ABI
650     size_t operator()(nullptr_t) const _NOEXCEPT {
651         return 662607004ull;
652     }
653 };
654 #endif
655 
656 #ifndef _LIBCPP_CXX03_LANG
657 template <class _Key, class _Hash>
658 using __check_hash_requirements _LIBCPP_NODEBUG = integral_constant<bool,
659     is_copy_constructible<_Hash>::value &&
660     is_move_constructible<_Hash>::value &&
661     __invokable_r<size_t, _Hash, _Key const&>::value
662 >;
663 
664 template <class _Key, class _Hash = hash<_Key> >
665 using __has_enabled_hash _LIBCPP_NODEBUG = integral_constant<bool,
666     __check_hash_requirements<_Key, _Hash>::value &&
667     is_default_constructible<_Hash>::value
668 >;
669 
670 #if _LIBCPP_STD_VER >= 17
671 template <class _Type, class>
672 using __enable_hash_helper_imp _LIBCPP_NODEBUG = _Type;
673 
674 template <class _Type, class ..._Keys>
675 using __enable_hash_helper _LIBCPP_NODEBUG = __enable_hash_helper_imp<_Type,
676   typename enable_if<__all<__has_enabled_hash<_Keys>::value...>::value>::type
677 >;
678 #else
679 template <class _Type, class ...>
680 using __enable_hash_helper _LIBCPP_NODEBUG = _Type;
681 #endif
682 
683 #endif // !_LIBCPP_CXX03_LANG
684 
685 _LIBCPP_END_NAMESPACE_STD
686 
687 #endif // _LIBCPP___FUNCTIONAL_HASH_H
688