1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___FUNCTIONAL_HASH_H
10 #define _LIBCPP___FUNCTIONAL_HASH_H
13 #include <__functional/unary_function.h>
15 #include <__utility/forward.h>
16 #include <__utility/move.h>
17 #include <__utility/pair.h>
18 #include <__utility/swap.h>
23 #include <type_traits>
25 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
26 #pragma GCC system_header
29 _LIBCPP_BEGIN_NAMESPACE_STD
31 template <class _Size
>
32 inline _LIBCPP_INLINE_VISIBILITY
34 __loadword(const void* __p
)
37 _VSTD::memcpy(&__r
, __p
, sizeof(__r
));
41 // We use murmur2 when size_t is 32 bits, and cityhash64 when size_t
42 // is 64 bits. This is because cityhash64 uses 64bit x 64bit
43 // multiplication, which can be very slow on 32-bit systems.
44 template <class _Size
, size_t = sizeof(_Size
)*__CHAR_BIT__
>
45 struct __murmur2_or_cityhash
;
47 template <class _Size
>
48 struct __murmur2_or_cityhash
<_Size
, 32>
50 inline _Size
operator()(const void* __key
, _Size __len
)
51 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
;
55 template <class _Size
>
57 __murmur2_or_cityhash
<_Size
, 32>::operator()(const void* __key
, _Size __len
)
59 const _Size __m
= 0x5bd1e995;
62 const unsigned char* __data
= static_cast<const unsigned char*>(__key
);
63 for (; __len
>= 4; __data
+= 4, __len
-= 4)
65 _Size __k
= __loadword
<_Size
>(__data
);
75 __h
^= static_cast<_Size
>(__data
[2] << 16);
76 _LIBCPP_FALLTHROUGH();
78 __h
^= static_cast<_Size
>(__data
[1] << 8);
79 _LIBCPP_FALLTHROUGH();
90 template <class _Size
>
91 struct __murmur2_or_cityhash
<_Size
, 64>
93 inline _Size
operator()(const void* __key
, _Size __len
) _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
;
96 // Some primes between 2^63 and 2^64.
97 static const _Size __k0
= 0xc3a5c85c97cb3127ULL
;
98 static const _Size __k1
= 0xb492b66fbe98f273ULL
;
99 static const _Size __k2
= 0x9ae16a3b2f90404fULL
;
100 static const _Size __k3
= 0xc949d7c7509e6557ULL
;
102 static _Size
__rotate(_Size __val
, int __shift
) {
103 return __shift
== 0 ? __val
: ((__val
>> __shift
) | (__val
<< (64 - __shift
)));
106 static _Size
__rotate_by_at_least_1(_Size __val
, int __shift
) {
107 return (__val
>> __shift
) | (__val
<< (64 - __shift
));
110 static _Size
__shift_mix(_Size __val
) {
111 return __val
^ (__val
>> 47);
114 static _Size
__hash_len_16(_Size __u
, _Size __v
)
115 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
117 const _Size __mul
= 0x9ddfea08eb382d69ULL
;
118 _Size __a
= (__u
^ __v
) * __mul
;
120 _Size __b
= (__v
^ __a
) * __mul
;
126 static _Size
__hash_len_0_to_16(const char* __s
, _Size __len
)
127 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
130 const _Size __a
= __loadword
<_Size
>(__s
);
131 const _Size __b
= __loadword
<_Size
>(__s
+ __len
- 8);
132 return __hash_len_16(__a
, __rotate_by_at_least_1(__b
+ __len
, __len
)) ^ __b
;
135 const uint32_t __a
= __loadword
<uint32_t>(__s
);
136 const uint32_t __b
= __loadword
<uint32_t>(__s
+ __len
- 4);
137 return __hash_len_16(__len
+ (__a
<< 3), __b
);
140 const unsigned char __a
= static_cast<unsigned char>(__s
[0]);
141 const unsigned char __b
= static_cast<unsigned char>(__s
[__len
>> 1]);
142 const unsigned char __c
= static_cast<unsigned char>(__s
[__len
- 1]);
143 const uint32_t __y
= static_cast<uint32_t>(__a
) +
144 (static_cast<uint32_t>(__b
) << 8);
145 const uint32_t __z
= __len
+ (static_cast<uint32_t>(__c
) << 2);
146 return __shift_mix(__y
* __k2
^ __z
* __k3
) * __k2
;
151 static _Size
__hash_len_17_to_32(const char *__s
, _Size __len
)
152 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
154 const _Size __a
= __loadword
<_Size
>(__s
) * __k1
;
155 const _Size __b
= __loadword
<_Size
>(__s
+ 8);
156 const _Size __c
= __loadword
<_Size
>(__s
+ __len
- 8) * __k2
;
157 const _Size __d
= __loadword
<_Size
>(__s
+ __len
- 16) * __k0
;
158 return __hash_len_16(__rotate(__a
- __b
, 43) + __rotate(__c
, 30) + __d
,
159 __a
+ __rotate(__b
^ __k3
, 20) - __c
+ __len
);
162 // Return a 16-byte hash for 48 bytes. Quick and dirty.
163 // Callers do best to use "random-looking" values for a and b.
164 static pair
<_Size
, _Size
> __weak_hash_len_32_with_seeds(
165 _Size __w
, _Size __x
, _Size __y
, _Size __z
, _Size __a
, _Size __b
)
166 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
169 __b
= __rotate(__b
+ __a
+ __z
, 21);
170 const _Size __c
= __a
;
173 __b
+= __rotate(__a
, 44);
174 return pair
<_Size
, _Size
>(__a
+ __z
, __b
+ __c
);
177 // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
178 static pair
<_Size
, _Size
> __weak_hash_len_32_with_seeds(
179 const char* __s
, _Size __a
, _Size __b
)
180 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
182 return __weak_hash_len_32_with_seeds(__loadword
<_Size
>(__s
),
183 __loadword
<_Size
>(__s
+ 8),
184 __loadword
<_Size
>(__s
+ 16),
185 __loadword
<_Size
>(__s
+ 24),
190 // Return an 8-byte hash for 33 to 64 bytes.
191 static _Size
__hash_len_33_to_64(const char *__s
, size_t __len
)
192 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
194 _Size __z
= __loadword
<_Size
>(__s
+ 24);
195 _Size __a
= __loadword
<_Size
>(__s
) +
196 (__len
+ __loadword
<_Size
>(__s
+ __len
- 16)) * __k0
;
197 _Size __b
= __rotate(__a
+ __z
, 52);
198 _Size __c
= __rotate(__a
, 37);
199 __a
+= __loadword
<_Size
>(__s
+ 8);
200 __c
+= __rotate(__a
, 7);
201 __a
+= __loadword
<_Size
>(__s
+ 16);
202 _Size __vf
= __a
+ __z
;
203 _Size __vs
= __b
+ __rotate(__a
, 31) + __c
;
204 __a
= __loadword
<_Size
>(__s
+ 16) + __loadword
<_Size
>(__s
+ __len
- 32);
205 __z
+= __loadword
<_Size
>(__s
+ __len
- 8);
206 __b
= __rotate(__a
+ __z
, 52);
207 __c
= __rotate(__a
, 37);
208 __a
+= __loadword
<_Size
>(__s
+ __len
- 24);
209 __c
+= __rotate(__a
, 7);
210 __a
+= __loadword
<_Size
>(__s
+ __len
- 16);
211 _Size __wf
= __a
+ __z
;
212 _Size __ws
= __b
+ __rotate(__a
, 31) + __c
;
213 _Size __r
= __shift_mix((__vf
+ __ws
) * __k2
+ (__wf
+ __vs
) * __k0
);
214 return __shift_mix(__r
* __k0
+ __vs
) * __k2
;
219 template <class _Size
>
221 __murmur2_or_cityhash
<_Size
, 64>::operator()(const void* __key
, _Size __len
)
223 const char* __s
= static_cast<const char*>(__key
);
226 return __hash_len_0_to_16(__s
, __len
);
228 return __hash_len_17_to_32(__s
, __len
);
230 } else if (__len
<= 64) {
231 return __hash_len_33_to_64(__s
, __len
);
234 // For strings over 64 bytes we hash the end first, and then as we
235 // loop we keep 56 bytes of state: v, w, x, y, and z.
236 _Size __x
= __loadword
<_Size
>(__s
+ __len
- 40);
237 _Size __y
= __loadword
<_Size
>(__s
+ __len
- 16) +
238 __loadword
<_Size
>(__s
+ __len
- 56);
239 _Size __z
= __hash_len_16(__loadword
<_Size
>(__s
+ __len
- 48) + __len
,
240 __loadword
<_Size
>(__s
+ __len
- 24));
241 pair
<_Size
, _Size
> __v
= __weak_hash_len_32_with_seeds(__s
+ __len
- 64, __len
, __z
);
242 pair
<_Size
, _Size
> __w
= __weak_hash_len_32_with_seeds(__s
+ __len
- 32, __y
+ __k1
, __x
);
243 __x
= __x
* __k1
+ __loadword
<_Size
>(__s
);
245 // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
246 __len
= (__len
- 1) & ~static_cast<_Size
>(63);
248 __x
= __rotate(__x
+ __y
+ __v
.first
+ __loadword
<_Size
>(__s
+ 8), 37) * __k1
;
249 __y
= __rotate(__y
+ __v
.second
+ __loadword
<_Size
>(__s
+ 48), 42) * __k1
;
251 __y
+= __v
.first
+ __loadword
<_Size
>(__s
+ 40);
252 __z
= __rotate(__z
+ __w
.first
, 33) * __k1
;
253 __v
= __weak_hash_len_32_with_seeds(__s
, __v
.second
* __k1
, __x
+ __w
.first
);
254 __w
= __weak_hash_len_32_with_seeds(__s
+ 32, __z
+ __w
.second
,
255 __y
+ __loadword
<_Size
>(__s
+ 16));
256 _VSTD::swap(__z
, __x
);
259 } while (__len
!= 0);
260 return __hash_len_16(
261 __hash_len_16(__v
.first
, __w
.first
) + __shift_mix(__y
) * __k1
+ __z
,
262 __hash_len_16(__v
.second
, __w
.second
) + __x
);
265 template <class _Tp
, size_t = sizeof(_Tp
) / sizeof(size_t)>
266 struct __scalar_hash
;
268 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
270 struct __scalar_hash
<_Tp
, 0>
271 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
272 : public unary_function
<_Tp
, size_t>
275 _LIBCPP_SUPPRESS_DEPRECATED_POP
276 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
277 _LIBCPP_DEPRECATED_IN_CXX17
typedef size_t result_type
;
278 _LIBCPP_DEPRECATED_IN_CXX17
typedef _Tp argument_type
;
280 _LIBCPP_INLINE_VISIBILITY
281 size_t operator()(_Tp __v
) const _NOEXCEPT
294 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
296 struct __scalar_hash
<_Tp
, 1>
297 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
298 : public unary_function
<_Tp
, size_t>
301 _LIBCPP_SUPPRESS_DEPRECATED_POP
302 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
303 _LIBCPP_DEPRECATED_IN_CXX17
typedef size_t result_type
;
304 _LIBCPP_DEPRECATED_IN_CXX17
typedef _Tp argument_type
;
306 _LIBCPP_INLINE_VISIBILITY
307 size_t operator()(_Tp __v
) const _NOEXCEPT
319 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
321 struct __scalar_hash
<_Tp
, 2>
322 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
323 : public unary_function
<_Tp
, size_t>
326 _LIBCPP_SUPPRESS_DEPRECATED_POP
327 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
328 _LIBCPP_DEPRECATED_IN_CXX17
typedef size_t result_type
;
329 _LIBCPP_DEPRECATED_IN_CXX17
typedef _Tp argument_type
;
331 _LIBCPP_INLINE_VISIBILITY
332 size_t operator()(_Tp __v
) const _NOEXCEPT
344 return __murmur2_or_cityhash
<size_t>()(&__u
, sizeof(__u
));
348 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
350 struct __scalar_hash
<_Tp
, 3>
351 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
352 : public unary_function
<_Tp
, size_t>
355 _LIBCPP_SUPPRESS_DEPRECATED_POP
356 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
357 _LIBCPP_DEPRECATED_IN_CXX17
typedef size_t result_type
;
358 _LIBCPP_DEPRECATED_IN_CXX17
typedef _Tp argument_type
;
360 _LIBCPP_INLINE_VISIBILITY
361 size_t operator()(_Tp __v
) const _NOEXCEPT
374 return __murmur2_or_cityhash
<size_t>()(&__u
, sizeof(__u
));
378 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
380 struct __scalar_hash
<_Tp
, 4>
381 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
382 : public unary_function
<_Tp
, size_t>
385 _LIBCPP_SUPPRESS_DEPRECATED_POP
386 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
387 _LIBCPP_DEPRECATED_IN_CXX17
typedef size_t result_type
;
388 _LIBCPP_DEPRECATED_IN_CXX17
typedef _Tp argument_type
;
390 _LIBCPP_INLINE_VISIBILITY
391 size_t operator()(_Tp __v
) const _NOEXCEPT
405 return __murmur2_or_cityhash
<size_t>()(&__u
, sizeof(__u
));
414 _LIBCPP_INLINE_VISIBILITY
415 inline size_t __hash_combine(size_t __lhs
, size_t __rhs
) _NOEXCEPT
{
416 typedef __scalar_hash
<_PairT
> _HashT
;
417 const _PairT __p
= {__lhs
, __rhs
};
418 return _HashT()(__p
);
421 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
423 struct _LIBCPP_TEMPLATE_VIS hash
<_Tp
*>
424 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
425 : public unary_function
<_Tp
*, size_t>
428 _LIBCPP_SUPPRESS_DEPRECATED_POP
429 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
430 _LIBCPP_DEPRECATED_IN_CXX17
typedef size_t result_type
;
431 _LIBCPP_DEPRECATED_IN_CXX17
typedef _Tp
* argument_type
;
433 _LIBCPP_INLINE_VISIBILITY
434 size_t operator()(_Tp
* __v
) const _NOEXCEPT
442 return __murmur2_or_cityhash
<size_t>()(&__u
, sizeof(__u
));
446 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
448 struct _LIBCPP_TEMPLATE_VIS hash
<bool>
449 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
450 : public unary_function
<bool, size_t>
453 _LIBCPP_SUPPRESS_DEPRECATED_POP
454 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
455 _LIBCPP_DEPRECATED_IN_CXX17
typedef size_t result_type
;
456 _LIBCPP_DEPRECATED_IN_CXX17
typedef bool argument_type
;
458 _LIBCPP_INLINE_VISIBILITY
459 size_t operator()(bool __v
) const _NOEXCEPT
{return static_cast<size_t>(__v
);}
462 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
464 struct _LIBCPP_TEMPLATE_VIS hash
<char>
465 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
466 : public unary_function
<char, size_t>
469 _LIBCPP_SUPPRESS_DEPRECATED_POP
470 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
471 _LIBCPP_DEPRECATED_IN_CXX17
typedef size_t result_type
;
472 _LIBCPP_DEPRECATED_IN_CXX17
typedef char argument_type
;
474 _LIBCPP_INLINE_VISIBILITY
475 size_t operator()(char __v
) const _NOEXCEPT
{return static_cast<size_t>(__v
);}
478 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
480 struct _LIBCPP_TEMPLATE_VIS hash
<signed char>
481 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
482 : public unary_function
<signed char, size_t>
485 _LIBCPP_SUPPRESS_DEPRECATED_POP
486 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
487 _LIBCPP_DEPRECATED_IN_CXX17
typedef size_t result_type
;
488 _LIBCPP_DEPRECATED_IN_CXX17
typedef signed char argument_type
;
490 _LIBCPP_INLINE_VISIBILITY
491 size_t operator()(signed char __v
) const _NOEXCEPT
{return static_cast<size_t>(__v
);}
494 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
496 struct _LIBCPP_TEMPLATE_VIS hash
<unsigned char>
497 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
498 : public unary_function
<unsigned char, size_t>
501 _LIBCPP_SUPPRESS_DEPRECATED_POP
502 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
503 _LIBCPP_DEPRECATED_IN_CXX17
typedef size_t result_type
;
504 _LIBCPP_DEPRECATED_IN_CXX17
typedef unsigned char argument_type
;
506 _LIBCPP_INLINE_VISIBILITY
507 size_t operator()(unsigned char __v
) const _NOEXCEPT
{return static_cast<size_t>(__v
);}
510 #ifndef _LIBCPP_HAS_NO_CHAR8_T
511 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
513 struct _LIBCPP_TEMPLATE_VIS hash
<char8_t
>
514 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
515 : public unary_function
<char8_t
, size_t>
518 _LIBCPP_SUPPRESS_DEPRECATED_POP
519 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
520 _LIBCPP_DEPRECATED_IN_CXX17
typedef size_t result_type
;
521 _LIBCPP_DEPRECATED_IN_CXX17
typedef char8_t argument_type
;
523 _LIBCPP_INLINE_VISIBILITY
524 size_t operator()(char8_t __v
) const _NOEXCEPT
{return static_cast<size_t>(__v
);}
526 #endif // !_LIBCPP_HAS_NO_CHAR8_T
528 #ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
530 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
532 struct _LIBCPP_TEMPLATE_VIS hash
<char16_t
>
533 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
534 : public unary_function
<char16_t
, size_t>
537 _LIBCPP_SUPPRESS_DEPRECATED_POP
538 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
539 _LIBCPP_DEPRECATED_IN_CXX17
typedef size_t result_type
;
540 _LIBCPP_DEPRECATED_IN_CXX17
typedef char16_t argument_type
;
542 _LIBCPP_INLINE_VISIBILITY
543 size_t operator()(char16_t __v
) const _NOEXCEPT
{return static_cast<size_t>(__v
);}
546 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
548 struct _LIBCPP_TEMPLATE_VIS hash
<char32_t
>
549 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
550 : public unary_function
<char32_t
, size_t>
553 _LIBCPP_SUPPRESS_DEPRECATED_POP
554 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
555 _LIBCPP_DEPRECATED_IN_CXX17
typedef size_t result_type
;
556 _LIBCPP_DEPRECATED_IN_CXX17
typedef char32_t argument_type
;
558 _LIBCPP_INLINE_VISIBILITY
559 size_t operator()(char32_t __v
) const _NOEXCEPT
{return static_cast<size_t>(__v
);}
562 #endif // _LIBCPP_HAS_NO_UNICODE_CHARS
564 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
565 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
567 struct _LIBCPP_TEMPLATE_VIS hash
<wchar_t>
568 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
569 : public unary_function
<wchar_t, size_t>
572 _LIBCPP_SUPPRESS_DEPRECATED_POP
573 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
574 _LIBCPP_DEPRECATED_IN_CXX17
typedef size_t result_type
;
575 _LIBCPP_DEPRECATED_IN_CXX17
typedef wchar_t argument_type
;
577 _LIBCPP_INLINE_VISIBILITY
578 size_t operator()(wchar_t __v
) const _NOEXCEPT
{return static_cast<size_t>(__v
);}
580 #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
582 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
584 struct _LIBCPP_TEMPLATE_VIS hash
<short>
585 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
586 : public unary_function
<short, size_t>
589 _LIBCPP_SUPPRESS_DEPRECATED_POP
590 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
591 _LIBCPP_DEPRECATED_IN_CXX17
typedef size_t result_type
;
592 _LIBCPP_DEPRECATED_IN_CXX17
typedef short argument_type
;
594 _LIBCPP_INLINE_VISIBILITY
595 size_t operator()(short __v
) const _NOEXCEPT
{return static_cast<size_t>(__v
);}
598 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
600 struct _LIBCPP_TEMPLATE_VIS hash
<unsigned short>
601 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
602 : public unary_function
<unsigned short, size_t>
605 _LIBCPP_SUPPRESS_DEPRECATED_POP
606 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
607 _LIBCPP_DEPRECATED_IN_CXX17
typedef size_t result_type
;
608 _LIBCPP_DEPRECATED_IN_CXX17
typedef unsigned short argument_type
;
610 _LIBCPP_INLINE_VISIBILITY
611 size_t operator()(unsigned short __v
) const _NOEXCEPT
{return static_cast<size_t>(__v
);}
614 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
616 struct _LIBCPP_TEMPLATE_VIS hash
<int>
617 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
618 : public unary_function
<int, size_t>
621 _LIBCPP_SUPPRESS_DEPRECATED_POP
622 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
623 _LIBCPP_DEPRECATED_IN_CXX17
typedef size_t result_type
;
624 _LIBCPP_DEPRECATED_IN_CXX17
typedef int argument_type
;
626 _LIBCPP_INLINE_VISIBILITY
627 size_t operator()(int __v
) const _NOEXCEPT
{return static_cast<size_t>(__v
);}
630 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
632 struct _LIBCPP_TEMPLATE_VIS hash
<unsigned int>
633 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
634 : public unary_function
<unsigned int, size_t>
637 _LIBCPP_SUPPRESS_DEPRECATED_POP
638 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
639 _LIBCPP_DEPRECATED_IN_CXX17
typedef size_t result_type
;
640 _LIBCPP_DEPRECATED_IN_CXX17
typedef unsigned int argument_type
;
642 _LIBCPP_INLINE_VISIBILITY
643 size_t operator()(unsigned int __v
) const _NOEXCEPT
{return static_cast<size_t>(__v
);}
646 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
648 struct _LIBCPP_TEMPLATE_VIS hash
<long>
649 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
650 : public unary_function
<long, size_t>
653 _LIBCPP_SUPPRESS_DEPRECATED_POP
654 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
655 _LIBCPP_DEPRECATED_IN_CXX17
typedef size_t result_type
;
656 _LIBCPP_DEPRECATED_IN_CXX17
typedef long argument_type
;
658 _LIBCPP_INLINE_VISIBILITY
659 size_t operator()(long __v
) const _NOEXCEPT
{return static_cast<size_t>(__v
);}
662 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
664 struct _LIBCPP_TEMPLATE_VIS hash
<unsigned long>
665 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
666 : public unary_function
<unsigned long, size_t>
669 _LIBCPP_SUPPRESS_DEPRECATED_POP
670 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
671 _LIBCPP_DEPRECATED_IN_CXX17
typedef size_t result_type
;
672 _LIBCPP_DEPRECATED_IN_CXX17
typedef unsigned long argument_type
;
674 _LIBCPP_INLINE_VISIBILITY
675 size_t operator()(unsigned long __v
) const _NOEXCEPT
{return static_cast<size_t>(__v
);}
679 struct _LIBCPP_TEMPLATE_VIS hash
<long long>
680 : public __scalar_hash
<long long>
685 struct _LIBCPP_TEMPLATE_VIS hash
<unsigned long long>
686 : public __scalar_hash
<unsigned long long>
690 #ifndef _LIBCPP_HAS_NO_INT128
693 struct _LIBCPP_TEMPLATE_VIS hash
<__int128_t
>
694 : public __scalar_hash
<__int128_t
>
699 struct _LIBCPP_TEMPLATE_VIS hash
<__uint128_t
>
700 : public __scalar_hash
<__uint128_t
>
707 struct _LIBCPP_TEMPLATE_VIS hash
<float>
708 : public __scalar_hash
<float>
710 _LIBCPP_INLINE_VISIBILITY
711 size_t operator()(float __v
) const _NOEXCEPT
713 // -0.0 and 0.0 should return same hash
716 return __scalar_hash
<float>::operator()(__v
);
721 struct _LIBCPP_TEMPLATE_VIS hash
<double>
722 : public __scalar_hash
<double>
724 _LIBCPP_INLINE_VISIBILITY
725 size_t operator()(double __v
) const _NOEXCEPT
727 // -0.0 and 0.0 should return same hash
730 return __scalar_hash
<double>::operator()(__v
);
735 struct _LIBCPP_TEMPLATE_VIS hash
<long double>
736 : public __scalar_hash
<long double>
738 _LIBCPP_INLINE_VISIBILITY
739 size_t operator()(long double __v
) const _NOEXCEPT
741 // -0.0 and 0.0 should return same hash
744 #if defined(__i386__) || (defined(__x86_64__) && defined(__ILP32__))
745 // Zero out padding bits
762 return __u
.__s
.__a
^ __u
.__s
.__b
^ __u
.__s
.__c
^ __u
.__s
.__d
;
763 #elif defined(__x86_64__)
764 // Zero out padding bits
777 return __u
.__s
.__a
^ __u
.__s
.__b
;
779 return __scalar_hash
<long double>::operator()(__v
);
784 #if _LIBCPP_STD_VER > 11
786 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
787 template <class _Tp
, bool = is_enum
<_Tp
>::value
>
788 struct _LIBCPP_TEMPLATE_VIS __enum_hash
789 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
790 : public unary_function
<_Tp
, size_t>
793 _LIBCPP_SUPPRESS_DEPRECATED_POP
794 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
795 _LIBCPP_DEPRECATED_IN_CXX17
typedef size_t result_type
;
796 _LIBCPP_DEPRECATED_IN_CXX17
typedef _Tp argument_type
;
798 _LIBCPP_INLINE_VISIBILITY
799 size_t operator()(_Tp __v
) const _NOEXCEPT
801 typedef typename underlying_type
<_Tp
>::type type
;
802 return hash
<type
>{}(static_cast<type
>(__v
));
806 struct _LIBCPP_TEMPLATE_VIS __enum_hash
<_Tp
, false> {
807 __enum_hash() = delete;
808 __enum_hash(__enum_hash
const&) = delete;
809 __enum_hash
& operator=(__enum_hash
const&) = delete;
813 struct _LIBCPP_TEMPLATE_VIS hash
: public __enum_hash
<_Tp
>
818 #if _LIBCPP_STD_VER > 14
820 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
822 struct _LIBCPP_TEMPLATE_VIS hash
<nullptr_t
>
823 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
824 : public unary_function
<nullptr_t
, size_t>
827 _LIBCPP_SUPPRESS_DEPRECATED_POP
828 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
829 _LIBCPP_DEPRECATED_IN_CXX17
typedef size_t result_type
;
830 _LIBCPP_DEPRECATED_IN_CXX17
typedef nullptr_t argument_type
;
832 _LIBCPP_INLINE_VISIBILITY
833 size_t operator()(nullptr_t
) const _NOEXCEPT
{
839 #ifndef _LIBCPP_CXX03_LANG
840 template <class _Key
, class _Hash
>
841 using __check_hash_requirements _LIBCPP_NODEBUG
= integral_constant
<bool,
842 is_copy_constructible
<_Hash
>::value
&&
843 is_move_constructible
<_Hash
>::value
&&
844 __invokable_r
<size_t, _Hash
, _Key
const&>::value
847 template <class _Key
, class _Hash
= hash
<_Key
> >
848 using __has_enabled_hash _LIBCPP_NODEBUG
= integral_constant
<bool,
849 __check_hash_requirements
<_Key
, _Hash
>::value
&&
850 is_default_constructible
<_Hash
>::value
853 #if _LIBCPP_STD_VER > 14
854 template <class _Type
, class>
855 using __enable_hash_helper_imp _LIBCPP_NODEBUG
= _Type
;
857 template <class _Type
, class ..._Keys
>
858 using __enable_hash_helper _LIBCPP_NODEBUG
= __enable_hash_helper_imp
<_Type
,
859 typename enable_if
<__all
<__has_enabled_hash
<_Keys
>::value
...>::value
>::type
862 template <class _Type
, class ...>
863 using __enable_hash_helper _LIBCPP_NODEBUG
= _Type
;
866 #endif // !_LIBCPP_CXX03_LANG
868 _LIBCPP_END_NAMESPACE_STD
870 #endif // _LIBCPP___FUNCTIONAL_HASH_H