hash.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. #ifndef _LIBCPP___FUNCTIONAL_HASH_H
  9. #define _LIBCPP___FUNCTIONAL_HASH_H
  10. #include <__config>
  11. #include <__functional/unary_function.h>
  12. #include <__fwd/functional.h>
  13. #include <__type_traits/conjunction.h>
  14. #include <__type_traits/invoke.h>
  15. #include <__type_traits/is_constructible.h>
  16. #include <__type_traits/is_enum.h>
  17. #include <__type_traits/underlying_type.h>
  18. #include <__utility/pair.h>
  19. #include <__utility/swap.h>
  20. #include <cstddef>
  21. #include <cstdint>
  22. #include <cstring>
  23. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  24. # pragma GCC system_header
  25. #endif
  26. _LIBCPP_BEGIN_NAMESPACE_STD
  27. template <class _Size>
  28. inline _LIBCPP_HIDE_FROM_ABI _Size __loadword(const void* __p) {
  29. _Size __r;
  30. std::memcpy(&__r, __p, sizeof(__r));
  31. return __r;
  32. }
  33. // We use murmur2 when size_t is 32 bits, and cityhash64 when size_t
  34. // is 64 bits. This is because cityhash64 uses 64bit x 64bit
  35. // multiplication, which can be very slow on 32-bit systems.
  36. template <class _Size, size_t = sizeof(_Size) * __CHAR_BIT__>
  37. struct __murmur2_or_cityhash;
  38. template <class _Size>
  39. struct __murmur2_or_cityhash<_Size, 32> {
  40. _LIBCPP_HIDE_FROM_ABI _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK _Size
  41. operator()(const void* __key, _Size __len) const {
  42. // murmur2
  43. const _Size __m = 0x5bd1e995;
  44. const _Size __r = 24;
  45. _Size __h = __len;
  46. const unsigned char* __data = static_cast<const unsigned char*>(__key);
  47. for (; __len >= 4; __data += 4, __len -= 4) {
  48. _Size __k = std::__loadword<_Size>(__data);
  49. __k *= __m;
  50. __k ^= __k >> __r;
  51. __k *= __m;
  52. __h *= __m;
  53. __h ^= __k;
  54. }
  55. switch (__len) {
  56. case 3:
  57. __h ^= static_cast<_Size>(__data[2] << 16);
  58. _LIBCPP_FALLTHROUGH();
  59. case 2:
  60. __h ^= static_cast<_Size>(__data[1] << 8);
  61. _LIBCPP_FALLTHROUGH();
  62. case 1:
  63. __h ^= __data[0];
  64. __h *= __m;
  65. }
  66. __h ^= __h >> 13;
  67. __h *= __m;
  68. __h ^= __h >> 15;
  69. return __h;
  70. }
  71. };
  72. template <class _Size>
  73. struct __murmur2_or_cityhash<_Size, 64> {
  74. // cityhash64
  75. _LIBCPP_HIDE_FROM_ABI _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK _Size
  76. operator()(const void* __key, _Size __len) const {
  77. const char* __s = static_cast<const char*>(__key);
  78. if (__len <= 32) {
  79. if (__len <= 16) {
  80. return __hash_len_0_to_16(__s, __len);
  81. } else {
  82. return __hash_len_17_to_32(__s, __len);
  83. }
  84. } else if (__len <= 64) {
  85. return __hash_len_33_to_64(__s, __len);
  86. }
  87. // For strings over 64 bytes we hash the end first, and then as we
  88. // loop we keep 56 bytes of state: v, w, x, y, and z.
  89. _Size __x = std::__loadword<_Size>(__s + __len - 40);
  90. _Size __y = std::__loadword<_Size>(__s + __len - 16) + std::__loadword<_Size>(__s + __len - 56);
  91. _Size __z =
  92. __hash_len_16(std::__loadword<_Size>(__s + __len - 48) + __len, std::__loadword<_Size>(__s + __len - 24));
  93. pair<_Size, _Size> __v = __weak_hash_len_32_with_seeds(__s + __len - 64, __len, __z);
  94. pair<_Size, _Size> __w = __weak_hash_len_32_with_seeds(__s + __len - 32, __y + __k1, __x);
  95. __x = __x * __k1 + std::__loadword<_Size>(__s);
  96. // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
  97. __len = (__len - 1) & ~static_cast<_Size>(63);
  98. do {
  99. __x = __rotate(__x + __y + __v.first + std::__loadword<_Size>(__s + 8), 37) * __k1;
  100. __y = __rotate(__y + __v.second + std::__loadword<_Size>(__s + 48), 42) * __k1;
  101. __x ^= __w.second;
  102. __y += __v.first + std::__loadword<_Size>(__s + 40);
  103. __z = __rotate(__z + __w.first, 33) * __k1;
  104. __v = __weak_hash_len_32_with_seeds(__s, __v.second * __k1, __x + __w.first);
  105. __w = __weak_hash_len_32_with_seeds(__s + 32, __z + __w.second, __y + std::__loadword<_Size>(__s + 16));
  106. std::swap(__z, __x);
  107. __s += 64;
  108. __len -= 64;
  109. } while (__len != 0);
  110. return __hash_len_16(__hash_len_16(__v.first, __w.first) + __shift_mix(__y) * __k1 + __z,
  111. __hash_len_16(__v.second, __w.second) + __x);
  112. }
  113. private:
  114. // Some primes between 2^63 and 2^64.
  115. static const _Size __k0 = 0xc3a5c85c97cb3127ULL;
  116. static const _Size __k1 = 0xb492b66fbe98f273ULL;
  117. static const _Size __k2 = 0x9ae16a3b2f90404fULL;
  118. static const _Size __k3 = 0xc949d7c7509e6557ULL;
  119. _LIBCPP_HIDE_FROM_ABI static _Size __rotate(_Size __val, int __shift) {
  120. return __shift == 0 ? __val : ((__val >> __shift) | (__val << (64 - __shift)));
  121. }
  122. _LIBCPP_HIDE_FROM_ABI static _Size __rotate_by_at_least_1(_Size __val, int __shift) {
  123. return (__val >> __shift) | (__val << (64 - __shift));
  124. }
  125. _LIBCPP_HIDE_FROM_ABI static _Size __shift_mix(_Size __val) { return __val ^ (__val >> 47); }
  126. _LIBCPP_HIDE_FROM_ABI _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK static _Size __hash_len_16(_Size __u, _Size __v) {
  127. const _Size __mul = 0x9ddfea08eb382d69ULL;
  128. _Size __a = (__u ^ __v) * __mul;
  129. __a ^= (__a >> 47);
  130. _Size __b = (__v ^ __a) * __mul;
  131. __b ^= (__b >> 47);
  132. __b *= __mul;
  133. return __b;
  134. }
  135. _LIBCPP_HIDE_FROM_ABI _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK static _Size
  136. __hash_len_0_to_16(const char* __s, _Size __len) {
  137. if (__len > 8) {
  138. const _Size __a = std::__loadword<_Size>(__s);
  139. const _Size __b = std::__loadword<_Size>(__s + __len - 8);
  140. return __hash_len_16(__a, __rotate_by_at_least_1(__b + __len, __len)) ^ __b;
  141. }
  142. if (__len >= 4) {
  143. const uint32_t __a = std::__loadword<uint32_t>(__s);
  144. const uint32_t __b = std::__loadword<uint32_t>(__s + __len - 4);
  145. #ifdef _LIBCPP_ABI_FIX_CITYHASH_IMPLEMENTATION
  146. return __hash_len_16(__len + (static_cast<_Size>(__a) << 3), __b);
  147. #else
  148. return __hash_len_16(__len + (__a << 3), __b);
  149. #endif
  150. }
  151. if (__len > 0) {
  152. const unsigned char __a = static_cast<unsigned char>(__s[0]);
  153. const unsigned char __b = static_cast<unsigned char>(__s[__len >> 1]);
  154. const unsigned char __c = static_cast<unsigned char>(__s[__len - 1]);
  155. const uint32_t __y = static_cast<uint32_t>(__a) + (static_cast<uint32_t>(__b) << 8);
  156. const uint32_t __z = __len + (static_cast<uint32_t>(__c) << 2);
  157. return __shift_mix(__y * __k2 ^ __z * __k3) * __k2;
  158. }
  159. return __k2;
  160. }
  161. _LIBCPP_HIDE_FROM_ABI _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK static _Size
  162. __hash_len_17_to_32(const char* __s, _Size __len) {
  163. const _Size __a = std::__loadword<_Size>(__s) * __k1;
  164. const _Size __b = std::__loadword<_Size>(__s + 8);
  165. const _Size __c = std::__loadword<_Size>(__s + __len - 8) * __k2;
  166. const _Size __d = std::__loadword<_Size>(__s + __len - 16) * __k0;
  167. return __hash_len_16(
  168. __rotate(__a - __b, 43) + __rotate(__c, 30) + __d, __a + __rotate(__b ^ __k3, 20) - __c + __len);
  169. }
  170. // Return a 16-byte hash for 48 bytes. Quick and dirty.
  171. // Callers do best to use "random-looking" values for a and b.
  172. _LIBCPP_HIDE_FROM_ABI _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK static pair<_Size, _Size>
  173. __weak_hash_len_32_with_seeds(_Size __w, _Size __x, _Size __y, _Size __z, _Size __a, _Size __b) {
  174. __a += __w;
  175. __b = __rotate(__b + __a + __z, 21);
  176. const _Size __c = __a;
  177. __a += __x;
  178. __a += __y;
  179. __b += __rotate(__a, 44);
  180. return pair<_Size, _Size>(__a + __z, __b + __c);
  181. }
  182. // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
  183. _LIBCPP_HIDE_FROM_ABI _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK static pair<_Size, _Size>
  184. __weak_hash_len_32_with_seeds(const char* __s, _Size __a, _Size __b) {
  185. return __weak_hash_len_32_with_seeds(
  186. std::__loadword<_Size>(__s),
  187. std::__loadword<_Size>(__s + 8),
  188. std::__loadword<_Size>(__s + 16),
  189. std::__loadword<_Size>(__s + 24),
  190. __a,
  191. __b);
  192. }
  193. // Return an 8-byte hash for 33 to 64 bytes.
  194. _LIBCPP_HIDE_FROM_ABI _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK static _Size
  195. __hash_len_33_to_64(const char* __s, size_t __len) {
  196. _Size __z = std::__loadword<_Size>(__s + 24);
  197. _Size __a = std::__loadword<_Size>(__s) + (__len + std::__loadword<_Size>(__s + __len - 16)) * __k0;
  198. _Size __b = __rotate(__a + __z, 52);
  199. _Size __c = __rotate(__a, 37);
  200. __a += std::__loadword<_Size>(__s + 8);
  201. __c += __rotate(__a, 7);
  202. __a += std::__loadword<_Size>(__s + 16);
  203. _Size __vf = __a + __z;
  204. _Size __vs = __b + __rotate(__a, 31) + __c;
  205. __a = std::__loadword<_Size>(__s + 16) + std::__loadword<_Size>(__s + __len - 32);
  206. __z += std::__loadword<_Size>(__s + __len - 8);
  207. __b = __rotate(__a + __z, 52);
  208. __c = __rotate(__a, 37);
  209. __a += std::__loadword<_Size>(__s + __len - 24);
  210. __c += __rotate(__a, 7);
  211. __a += std::__loadword<_Size>(__s + __len - 16);
  212. _Size __wf = __a + __z;
  213. _Size __ws = __b + __rotate(__a, 31) + __c;
  214. _Size __r = __shift_mix((__vf + __ws) * __k2 + (__wf + __vs) * __k0);
  215. return __shift_mix(__r * __k0 + __vs) * __k2;
  216. }
  217. };
  218. template <class _Tp, size_t = sizeof(_Tp) / sizeof(size_t)>
  219. struct __scalar_hash;
  220. template <class _Tp>
  221. struct __scalar_hash<_Tp, 0> : public __unary_function<_Tp, size_t> {
  222. _LIBCPP_HIDE_FROM_ABI size_t operator()(_Tp __v) const _NOEXCEPT {
  223. union {
  224. _Tp __t;
  225. size_t __a;
  226. } __u;
  227. __u.__a = 0;
  228. __u.__t = __v;
  229. return __u.__a;
  230. }
  231. };
  232. template <class _Tp>
  233. struct __scalar_hash<_Tp, 1> : public __unary_function<_Tp, size_t> {
  234. _LIBCPP_HIDE_FROM_ABI size_t operator()(_Tp __v) const _NOEXCEPT {
  235. union {
  236. _Tp __t;
  237. size_t __a;
  238. } __u;
  239. __u.__t = __v;
  240. return __u.__a;
  241. }
  242. };
  243. template <class _Tp>
  244. struct __scalar_hash<_Tp, 2> : public __unary_function<_Tp, size_t> {
  245. _LIBCPP_HIDE_FROM_ABI size_t operator()(_Tp __v) const _NOEXCEPT {
  246. union {
  247. _Tp __t;
  248. struct {
  249. size_t __a;
  250. size_t __b;
  251. } __s;
  252. } __u;
  253. __u.__t = __v;
  254. return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
  255. }
  256. };
  257. template <class _Tp>
  258. struct __scalar_hash<_Tp, 3> : public __unary_function<_Tp, size_t> {
  259. _LIBCPP_HIDE_FROM_ABI size_t operator()(_Tp __v) const _NOEXCEPT {
  260. union {
  261. _Tp __t;
  262. struct {
  263. size_t __a;
  264. size_t __b;
  265. size_t __c;
  266. } __s;
  267. } __u;
  268. __u.__t = __v;
  269. return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
  270. }
  271. };
  272. template <class _Tp>
  273. struct __scalar_hash<_Tp, 4> : public __unary_function<_Tp, size_t> {
  274. _LIBCPP_HIDE_FROM_ABI size_t operator()(_Tp __v) const _NOEXCEPT {
  275. union {
  276. _Tp __t;
  277. struct {
  278. size_t __a;
  279. size_t __b;
  280. size_t __c;
  281. size_t __d;
  282. } __s;
  283. } __u;
  284. __u.__t = __v;
  285. return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
  286. }
  287. };
  288. struct _PairT {
  289. size_t first;
  290. size_t second;
  291. };
  292. _LIBCPP_HIDE_FROM_ABI inline size_t __hash_combine(size_t __lhs, size_t __rhs) _NOEXCEPT {
  293. typedef __scalar_hash<_PairT> _HashT;
  294. const _PairT __p = {__lhs, __rhs};
  295. return _HashT()(__p);
  296. }
  297. template <class _Tp>
  298. struct _LIBCPP_TEMPLATE_VIS hash<_Tp*> : public __unary_function<_Tp*, size_t> {
  299. _LIBCPP_HIDE_FROM_ABI size_t operator()(_Tp* __v) const _NOEXCEPT {
  300. union {
  301. _Tp* __t;
  302. size_t __a;
  303. } __u;
  304. __u.__t = __v;
  305. return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
  306. }
  307. };
  308. template <>
  309. struct _LIBCPP_TEMPLATE_VIS hash<bool> : public __unary_function<bool, size_t> {
  310. _LIBCPP_HIDE_FROM_ABI size_t operator()(bool __v) const _NOEXCEPT { return static_cast<size_t>(__v); }
  311. };
  312. template <>
  313. struct _LIBCPP_TEMPLATE_VIS hash<char> : public __unary_function<char, size_t> {
  314. _LIBCPP_HIDE_FROM_ABI size_t operator()(char __v) const _NOEXCEPT { return static_cast<size_t>(__v); }
  315. };
  316. template <>
  317. struct _LIBCPP_TEMPLATE_VIS hash<signed char> : public __unary_function<signed char, size_t> {
  318. _LIBCPP_HIDE_FROM_ABI size_t operator()(signed char __v) const _NOEXCEPT { return static_cast<size_t>(__v); }
  319. };
  320. template <>
  321. struct _LIBCPP_TEMPLATE_VIS hash<unsigned char> : public __unary_function<unsigned char, size_t> {
  322. _LIBCPP_HIDE_FROM_ABI size_t operator()(unsigned char __v) const _NOEXCEPT { return static_cast<size_t>(__v); }
  323. };
  324. #ifndef _LIBCPP_HAS_NO_CHAR8_T
  325. template <>
  326. struct _LIBCPP_TEMPLATE_VIS hash<char8_t> : public __unary_function<char8_t, size_t> {
  327. _LIBCPP_HIDE_FROM_ABI size_t operator()(char8_t __v) const _NOEXCEPT { return static_cast<size_t>(__v); }
  328. };
  329. #endif // !_LIBCPP_HAS_NO_CHAR8_T
  330. template <>
  331. struct _LIBCPP_TEMPLATE_VIS hash<char16_t> : public __unary_function<char16_t, size_t> {
  332. _LIBCPP_HIDE_FROM_ABI size_t operator()(char16_t __v) const _NOEXCEPT { return static_cast<size_t>(__v); }
  333. };
  334. template <>
  335. struct _LIBCPP_TEMPLATE_VIS hash<char32_t> : public __unary_function<char32_t, size_t> {
  336. _LIBCPP_HIDE_FROM_ABI size_t operator()(char32_t __v) const _NOEXCEPT { return static_cast<size_t>(__v); }
  337. };
  338. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  339. template <>
  340. struct _LIBCPP_TEMPLATE_VIS hash<wchar_t> : public __unary_function<wchar_t, size_t> {
  341. _LIBCPP_HIDE_FROM_ABI size_t operator()(wchar_t __v) const _NOEXCEPT { return static_cast<size_t>(__v); }
  342. };
  343. #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
  344. template <>
  345. struct _LIBCPP_TEMPLATE_VIS hash<short> : public __unary_function<short, size_t> {
  346. _LIBCPP_HIDE_FROM_ABI size_t operator()(short __v) const _NOEXCEPT { return static_cast<size_t>(__v); }
  347. };
  348. template <>
  349. struct _LIBCPP_TEMPLATE_VIS hash<unsigned short> : public __unary_function<unsigned short, size_t> {
  350. _LIBCPP_HIDE_FROM_ABI size_t operator()(unsigned short __v) const _NOEXCEPT { return static_cast<size_t>(__v); }
  351. };
  352. template <>
  353. struct _LIBCPP_TEMPLATE_VIS hash<int> : public __unary_function<int, size_t> {
  354. _LIBCPP_HIDE_FROM_ABI size_t operator()(int __v) const _NOEXCEPT { return static_cast<size_t>(__v); }
  355. };
  356. template <>
  357. struct _LIBCPP_TEMPLATE_VIS hash<unsigned int> : public __unary_function<unsigned int, size_t> {
  358. _LIBCPP_HIDE_FROM_ABI size_t operator()(unsigned int __v) const _NOEXCEPT { return static_cast<size_t>(__v); }
  359. };
  360. template <>
  361. struct _LIBCPP_TEMPLATE_VIS hash<long> : public __unary_function<long, size_t> {
  362. _LIBCPP_HIDE_FROM_ABI size_t operator()(long __v) const _NOEXCEPT { return static_cast<size_t>(__v); }
  363. };
  364. template <>
  365. struct _LIBCPP_TEMPLATE_VIS hash<unsigned long> : public __unary_function<unsigned long, size_t> {
  366. _LIBCPP_HIDE_FROM_ABI size_t operator()(unsigned long __v) const _NOEXCEPT { return static_cast<size_t>(__v); }
  367. };
  368. template <>
  369. struct _LIBCPP_TEMPLATE_VIS hash<long long> : public __scalar_hash<long long> {};
  370. template <>
  371. struct _LIBCPP_TEMPLATE_VIS hash<unsigned long long> : public __scalar_hash<unsigned long long> {};
  372. #ifndef _LIBCPP_HAS_NO_INT128
  373. template <>
  374. struct _LIBCPP_TEMPLATE_VIS hash<__int128_t> : public __scalar_hash<__int128_t> {};
  375. template <>
  376. struct _LIBCPP_TEMPLATE_VIS hash<__uint128_t> : public __scalar_hash<__uint128_t> {};
  377. #endif
  378. template <>
  379. struct _LIBCPP_TEMPLATE_VIS hash<float> : public __scalar_hash<float> {
  380. _LIBCPP_HIDE_FROM_ABI size_t operator()(float __v) const _NOEXCEPT {
  381. // -0.0 and 0.0 should return same hash
  382. if (__v == 0.0f)
  383. return 0;
  384. return __scalar_hash<float>::operator()(__v);
  385. }
  386. };
  387. template <>
  388. struct _LIBCPP_TEMPLATE_VIS hash<double> : public __scalar_hash<double> {
  389. _LIBCPP_HIDE_FROM_ABI size_t operator()(double __v) const _NOEXCEPT {
  390. // -0.0 and 0.0 should return same hash
  391. if (__v == 0.0)
  392. return 0;
  393. return __scalar_hash<double>::operator()(__v);
  394. }
  395. };
  396. template <>
  397. struct _LIBCPP_TEMPLATE_VIS hash<long double> : public __scalar_hash<long double> {
  398. _LIBCPP_HIDE_FROM_ABI size_t operator()(long double __v) const _NOEXCEPT {
  399. // -0.0 and 0.0 should return same hash
  400. if (__v == 0.0L)
  401. return 0;
  402. #if defined(__i386__) || (defined(__x86_64__) && defined(__ILP32__))
  403. // Zero out padding bits
  404. union {
  405. long double __t;
  406. struct {
  407. size_t __a;
  408. size_t __b;
  409. size_t __c;
  410. size_t __d;
  411. } __s;
  412. } __u;
  413. __u.__s.__a = 0;
  414. __u.__s.__b = 0;
  415. __u.__s.__c = 0;
  416. __u.__s.__d = 0;
  417. __u.__t = __v;
  418. return __u.__s.__a ^ __u.__s.__b ^ __u.__s.__c ^ __u.__s.__d;
  419. #elif defined(__x86_64__)
  420. // Zero out padding bits
  421. union {
  422. long double __t;
  423. struct {
  424. size_t __a;
  425. size_t __b;
  426. } __s;
  427. } __u;
  428. __u.__s.__a = 0;
  429. __u.__s.__b = 0;
  430. __u.__t = __v;
  431. return __u.__s.__a ^ __u.__s.__b;
  432. #else
  433. return __scalar_hash<long double>::operator()(__v);
  434. #endif
  435. }
  436. };
  437. template <class _Tp, bool = is_enum<_Tp>::value>
  438. struct _LIBCPP_TEMPLATE_VIS __enum_hash : public __unary_function<_Tp, size_t> {
  439. _LIBCPP_HIDE_FROM_ABI size_t operator()(_Tp __v) const _NOEXCEPT {
  440. typedef typename underlying_type<_Tp>::type type;
  441. return hash<type>()(static_cast<type>(__v));
  442. }
  443. };
  444. template <class _Tp>
  445. struct _LIBCPP_TEMPLATE_VIS __enum_hash<_Tp, false> {
  446. __enum_hash() = delete;
  447. __enum_hash(__enum_hash const&) = delete;
  448. __enum_hash& operator=(__enum_hash const&) = delete;
  449. };
  450. template <class _Tp>
  451. struct _LIBCPP_TEMPLATE_VIS hash : public __enum_hash<_Tp> {};
  452. #if _LIBCPP_STD_VER >= 17
  453. template <>
  454. struct _LIBCPP_TEMPLATE_VIS hash<nullptr_t> : public __unary_function<nullptr_t, size_t> {
  455. _LIBCPP_HIDE_FROM_ABI size_t operator()(nullptr_t) const _NOEXCEPT { return 662607004ull; }
  456. };
  457. #endif
  458. #ifndef _LIBCPP_CXX03_LANG
  459. template <class _Key, class _Hash>
  460. using __check_hash_requirements _LIBCPP_NODEBUG =
  461. integral_constant<bool,
  462. is_copy_constructible<_Hash>::value && is_move_constructible<_Hash>::value &&
  463. __invokable_r<size_t, _Hash, _Key const&>::value >;
  464. template <class _Key, class _Hash = hash<_Key> >
  465. using __has_enabled_hash _LIBCPP_NODEBUG =
  466. integral_constant<bool, __check_hash_requirements<_Key, _Hash>::value && is_default_constructible<_Hash>::value >;
  467. # if _LIBCPP_STD_VER >= 17
  468. template <class _Type, class>
  469. using __enable_hash_helper_imp _LIBCPP_NODEBUG = _Type;
  470. template <class _Type, class... _Keys>
  471. using __enable_hash_helper _LIBCPP_NODEBUG =
  472. __enable_hash_helper_imp<_Type, __enable_if_t<__all<__has_enabled_hash<_Keys>::value...>::value> >;
  473. # else
  474. template <class _Type, class...>
  475. using __enable_hash_helper _LIBCPP_NODEBUG = _Type;
  476. # endif
  477. #endif // !_LIBCPP_CXX03_LANG
  478. _LIBCPP_END_NAMESPACE_STD
  479. #endif // _LIBCPP___FUNCTIONAL_HASH_H