hash.h 20 KB

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