hash.h 20 KB

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