hash.h 26 KB

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