hash.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. // Copyright 2005-2014 Daniel James.
  2. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  3. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. // Based on Peter Dimov's proposal
  5. // http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2005/n1756.pdf
  6. // issue 6.18.
  7. //
  8. // This also contains public domain code from MurmurHash. From the
  9. // MurmurHash header:
  10. // MurmurHash3 was written by Austin Appleby, and is placed in the public
  11. // domain. The author hereby disclaims copyright to this source code.
  12. #if !defined(BOOST_FUNCTIONAL_HASH_HASH_HPP)
  13. #define BOOST_FUNCTIONAL_HASH_HASH_HPP
  14. #include <boost/functional/hash/hash_fwd.hpp>
  15. #include <functional>
  16. #include <boost/functional/hash/detail/hash_float.hpp>
  17. #include <string>
  18. #include <boost/limits.hpp>
  19. #include <boost/type_traits/is_enum.hpp>
  20. #include <boost/type_traits/is_integral.hpp>
  21. #include <boost/utility/enable_if.hpp>
  22. #include <boost/cstdint.hpp>
  23. #if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
  24. #include <boost/type_traits/is_pointer.hpp>
  25. #endif
  26. #if !defined(BOOST_NO_CXX11_HDR_TYPEINDEX)
  27. #include <typeindex>
  28. #endif
  29. #if defined(BOOST_MSVC)
  30. #pragma warning(push)
  31. #if BOOST_MSVC >= 1400
  32. #pragma warning(disable:6295) // Ill-defined for-loop : 'unsigned int' values
  33. // are always of range '0' to '4294967295'.
  34. // Loop executes infinitely.
  35. #endif
  36. #endif
  37. #if BOOST_WORKAROUND(__GNUC__, < 3) \
  38. && !defined(__SGI_STL_PORT) && !defined(_STLPORT_VERSION)
  39. #define BOOST_HASH_CHAR_TRAITS string_char_traits
  40. #else
  41. #define BOOST_HASH_CHAR_TRAITS char_traits
  42. #endif
  43. #if defined(_MSC_VER)
  44. # define BOOST_FUNCTIONAL_HASH_ROTL32(x, r) _rotl(x,r)
  45. #else
  46. # define BOOST_FUNCTIONAL_HASH_ROTL32(x, r) (x << r) | (x >> (32 - r))
  47. #endif
  48. namespace boost
  49. {
  50. namespace hash_detail
  51. {
  52. struct enable_hash_value { typedef std::size_t type; };
  53. template <typename T> struct basic_numbers {};
  54. template <typename T> struct long_numbers;
  55. template <typename T> struct ulong_numbers;
  56. template <typename T> struct float_numbers {};
  57. template <> struct basic_numbers<bool> :
  58. boost::hash_detail::enable_hash_value {};
  59. template <> struct basic_numbers<char> :
  60. boost::hash_detail::enable_hash_value {};
  61. template <> struct basic_numbers<unsigned char> :
  62. boost::hash_detail::enable_hash_value {};
  63. template <> struct basic_numbers<signed char> :
  64. boost::hash_detail::enable_hash_value {};
  65. template <> struct basic_numbers<short> :
  66. boost::hash_detail::enable_hash_value {};
  67. template <> struct basic_numbers<unsigned short> :
  68. boost::hash_detail::enable_hash_value {};
  69. template <> struct basic_numbers<int> :
  70. boost::hash_detail::enable_hash_value {};
  71. template <> struct basic_numbers<unsigned int> :
  72. boost::hash_detail::enable_hash_value {};
  73. template <> struct basic_numbers<long> :
  74. boost::hash_detail::enable_hash_value {};
  75. template <> struct basic_numbers<unsigned long> :
  76. boost::hash_detail::enable_hash_value {};
  77. #if !defined(BOOST_NO_INTRINSIC_WCHAR_T)
  78. template <> struct basic_numbers<wchar_t> :
  79. boost::hash_detail::enable_hash_value {};
  80. #endif
  81. // long_numbers is defined like this to allow for separate
  82. // specialization for long_long and int128_type, in case
  83. // they conflict.
  84. template <typename T> struct long_numbers2 {};
  85. template <typename T> struct ulong_numbers2 {};
  86. template <typename T> struct long_numbers : long_numbers2<T> {};
  87. template <typename T> struct ulong_numbers : ulong_numbers2<T> {};
  88. #if !defined(BOOST_NO_LONG_LONG)
  89. template <> struct long_numbers<boost::long_long_type> :
  90. boost::hash_detail::enable_hash_value {};
  91. template <> struct ulong_numbers<boost::ulong_long_type> :
  92. boost::hash_detail::enable_hash_value {};
  93. #endif
  94. #if defined(BOOST_HAS_INT128)
  95. template <> struct long_numbers2<boost::int128_type> :
  96. boost::hash_detail::enable_hash_value {};
  97. template <> struct ulong_numbers2<boost::uint128_type> :
  98. boost::hash_detail::enable_hash_value {};
  99. #endif
  100. template <> struct float_numbers<float> :
  101. boost::hash_detail::enable_hash_value {};
  102. template <> struct float_numbers<double> :
  103. boost::hash_detail::enable_hash_value {};
  104. template <> struct float_numbers<long double> :
  105. boost::hash_detail::enable_hash_value {};
  106. }
  107. template <typename T>
  108. typename boost::hash_detail::basic_numbers<T>::type hash_value(T);
  109. template <typename T>
  110. typename boost::hash_detail::long_numbers<T>::type hash_value(T);
  111. template <typename T>
  112. typename boost::hash_detail::ulong_numbers<T>::type hash_value(T);
  113. template <typename T>
  114. typename boost::enable_if<boost::is_enum<T>, std::size_t>::type
  115. hash_value(T);
  116. #if !BOOST_WORKAROUND(__DMC__, <= 0x848)
  117. template <class T> std::size_t hash_value(T* const&);
  118. #else
  119. template <class T> std::size_t hash_value(T*);
  120. #endif
  121. #if !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
  122. template< class T, unsigned N >
  123. std::size_t hash_value(const T (&x)[N]);
  124. template< class T, unsigned N >
  125. std::size_t hash_value(T (&x)[N]);
  126. #endif
  127. template <class Ch, class A>
  128. std::size_t hash_value(
  129. std::basic_string<Ch, std::BOOST_HASH_CHAR_TRAITS<Ch>, A> const&);
  130. template <typename T>
  131. typename boost::hash_detail::float_numbers<T>::type hash_value(T);
  132. #if !defined(BOOST_NO_CXX11_HDR_TYPEINDEX)
  133. std::size_t hash_value(std::type_index);
  134. #endif
  135. // Implementation
  136. namespace hash_detail
  137. {
  138. template <class T>
  139. inline std::size_t hash_value_signed(T val)
  140. {
  141. const int size_t_bits = std::numeric_limits<std::size_t>::digits;
  142. // ceiling(std::numeric_limits<T>::digits / size_t_bits) - 1
  143. const int length = (std::numeric_limits<T>::digits - 1)
  144. / size_t_bits;
  145. std::size_t seed = 0;
  146. T positive = val < 0 ? -1 - val : val;
  147. // Hopefully, this loop can be unrolled.
  148. for(unsigned int i = length * size_t_bits; i > 0; i -= size_t_bits)
  149. {
  150. seed ^= (std::size_t) (positive >> i) + (seed<<6) + (seed>>2);
  151. }
  152. seed ^= (std::size_t) val + (seed<<6) + (seed>>2);
  153. return seed;
  154. }
  155. template <class T>
  156. inline std::size_t hash_value_unsigned(T val)
  157. {
  158. const int size_t_bits = std::numeric_limits<std::size_t>::digits;
  159. // ceiling(std::numeric_limits<T>::digits / size_t_bits) - 1
  160. const int length = (std::numeric_limits<T>::digits - 1)
  161. / size_t_bits;
  162. std::size_t seed = 0;
  163. // Hopefully, this loop can be unrolled.
  164. for(unsigned int i = length * size_t_bits; i > 0; i -= size_t_bits)
  165. {
  166. seed ^= (std::size_t) (val >> i) + (seed<<6) + (seed>>2);
  167. }
  168. seed ^= (std::size_t) val + (seed<<6) + (seed>>2);
  169. return seed;
  170. }
  171. template <typename SizeT>
  172. inline void hash_combine_impl(SizeT& seed, SizeT value)
  173. {
  174. seed ^= value + 0x9e3779b9 + (seed<<6) + (seed>>2);
  175. }
  176. template <typename SizeT>
  177. inline void hash_combine_impl(boost::uint32_t& h1,
  178. boost::uint32_t k1)
  179. {
  180. const uint32_t c1 = 0xcc9e2d51;
  181. const uint32_t c2 = 0x1b873593;
  182. k1 *= c1;
  183. k1 = BOOST_FUNCTIONAL_HASH_ROTL32(k1,15);
  184. k1 *= c2;
  185. h1 ^= k1;
  186. h1 = BOOST_FUNCTIONAL_HASH_ROTL32(h1,13);
  187. h1 = h1*5+0xe6546b64;
  188. }
  189. // Don't define 64-bit hash combine on platforms with 64 bit integers,
  190. // and also not for 32-bit gcc as it warns about the 64-bit constant.
  191. #if !defined(BOOST_NO_INT64_T) && \
  192. !(defined(__GNUC__) && ULONG_MAX == 0xffffffff)
  193. template <typename SizeT>
  194. inline void hash_combine_impl(boost::uint64_t& h,
  195. boost::uint64_t k)
  196. {
  197. const uint64_t m = UINT64_C(0xc6a4a7935bd1e995);
  198. const int r = 47;
  199. k *= m;
  200. k ^= k >> r;
  201. k *= m;
  202. h ^= k;
  203. h *= m;
  204. }
  205. #endif // BOOST_NO_INT64_T
  206. }
  207. template <typename T>
  208. typename boost::hash_detail::basic_numbers<T>::type hash_value(T v)
  209. {
  210. return static_cast<std::size_t>(v);
  211. }
  212. template <typename T>
  213. typename boost::hash_detail::long_numbers<T>::type hash_value(T v)
  214. {
  215. return hash_detail::hash_value_signed(v);
  216. }
  217. template <typename T>
  218. typename boost::hash_detail::ulong_numbers<T>::type hash_value(T v)
  219. {
  220. return hash_detail::hash_value_unsigned(v);
  221. }
  222. template <typename T>
  223. typename boost::enable_if<boost::is_enum<T>, std::size_t>::type
  224. hash_value(T v)
  225. {
  226. return static_cast<std::size_t>(v);
  227. }
  228. // Implementation by Alberto Barbati and Dave Harris.
  229. #if !BOOST_WORKAROUND(__DMC__, <= 0x848)
  230. template <class T> std::size_t hash_value(T* const& v)
  231. #else
  232. template <class T> std::size_t hash_value(T* v)
  233. #endif
  234. {
  235. #if defined(__VMS) && __INITIAL_POINTER_SIZE == 64
  236. // for some reason ptrdiff_t on OpenVMS compiler with
  237. // 64 bit is not 64 bit !!!
  238. std::size_t x = static_cast<std::size_t>(
  239. reinterpret_cast<long long int>(v));
  240. #else
  241. std::size_t x = static_cast<std::size_t>(
  242. reinterpret_cast<std::ptrdiff_t>(v));
  243. #endif
  244. return x + (x >> 3);
  245. }
  246. #if defined(BOOST_MSVC)
  247. #pragma warning(push)
  248. #if BOOST_MSVC <= 1400
  249. #pragma warning(disable:4267) // 'argument' : conversion from 'size_t' to
  250. // 'unsigned int', possible loss of data
  251. // A misguided attempt to detect 64-bit
  252. // incompatability.
  253. #endif
  254. #endif
  255. template <class T>
  256. inline void hash_combine(std::size_t& seed, T const& v)
  257. {
  258. boost::hash<T> hasher;
  259. return boost::hash_detail::hash_combine_impl(seed, hasher(v));
  260. }
  261. #if defined(BOOST_MSVC)
  262. #pragma warning(pop)
  263. #endif
  264. template <class It>
  265. inline std::size_t hash_range(It first, It last)
  266. {
  267. std::size_t seed = 0;
  268. for(; first != last; ++first)
  269. {
  270. hash_combine(seed, *first);
  271. }
  272. return seed;
  273. }
  274. template <class It>
  275. inline void hash_range(std::size_t& seed, It first, It last)
  276. {
  277. for(; first != last; ++first)
  278. {
  279. hash_combine(seed, *first);
  280. }
  281. }
  282. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551))
  283. template <class T>
  284. inline std::size_t hash_range(T* first, T* last)
  285. {
  286. std::size_t seed = 0;
  287. for(; first != last; ++first)
  288. {
  289. boost::hash<T> hasher;
  290. seed ^= hasher(*first) + 0x9e3779b9 + (seed<<6) + (seed>>2);
  291. }
  292. return seed;
  293. }
  294. template <class T>
  295. inline void hash_range(std::size_t& seed, T* first, T* last)
  296. {
  297. for(; first != last; ++first)
  298. {
  299. boost::hash<T> hasher;
  300. seed ^= hasher(*first) + 0x9e3779b9 + (seed<<6) + (seed>>2);
  301. }
  302. }
  303. #endif
  304. #if !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
  305. template< class T, unsigned N >
  306. inline std::size_t hash_value(const T (&x)[N])
  307. {
  308. return hash_range(x, x + N);
  309. }
  310. template< class T, unsigned N >
  311. inline std::size_t hash_value(T (&x)[N])
  312. {
  313. return hash_range(x, x + N);
  314. }
  315. #endif
  316. template <class Ch, class A>
  317. inline std::size_t hash_value(
  318. std::basic_string<Ch, std::BOOST_HASH_CHAR_TRAITS<Ch>, A> const& v)
  319. {
  320. return hash_range(v.begin(), v.end());
  321. }
  322. template <typename T>
  323. typename boost::hash_detail::float_numbers<T>::type hash_value(T v)
  324. {
  325. return boost::hash_detail::float_hash_value(v);
  326. }
  327. #if !defined(BOOST_NO_CXX11_HDR_TYPEINDEX)
  328. inline std::size_t hash_value(std::type_index v)
  329. {
  330. return v.hash_code();
  331. }
  332. #endif
  333. //
  334. // boost::hash
  335. //
  336. // Define the specializations required by the standard. The general purpose
  337. // boost::hash is defined later in extensions.hpp if
  338. // BOOST_HASH_NO_EXTENSIONS is not defined.
  339. // BOOST_HASH_SPECIALIZE - define a specialization for a type which is
  340. // passed by copy.
  341. //
  342. // BOOST_HASH_SPECIALIZE_REF - define a specialization for a type which is
  343. // passed by const reference.
  344. //
  345. // These are undefined later.
  346. #define BOOST_HASH_SPECIALIZE(type) \
  347. template <> struct hash<type> \
  348. : public std::unary_function<type, std::size_t> \
  349. { \
  350. std::size_t operator()(type v) const \
  351. { \
  352. return boost::hash_value(v); \
  353. } \
  354. };
  355. #define BOOST_HASH_SPECIALIZE_REF(type) \
  356. template <> struct hash<type> \
  357. : public std::unary_function<type, std::size_t> \
  358. { \
  359. std::size_t operator()(type const& v) const \
  360. { \
  361. return boost::hash_value(v); \
  362. } \
  363. };
  364. BOOST_HASH_SPECIALIZE(bool)
  365. BOOST_HASH_SPECIALIZE(char)
  366. BOOST_HASH_SPECIALIZE(signed char)
  367. BOOST_HASH_SPECIALIZE(unsigned char)
  368. #if !defined(BOOST_NO_INTRINSIC_WCHAR_T)
  369. BOOST_HASH_SPECIALIZE(wchar_t)
  370. #endif
  371. BOOST_HASH_SPECIALIZE(short)
  372. BOOST_HASH_SPECIALIZE(unsigned short)
  373. BOOST_HASH_SPECIALIZE(int)
  374. BOOST_HASH_SPECIALIZE(unsigned int)
  375. BOOST_HASH_SPECIALIZE(long)
  376. BOOST_HASH_SPECIALIZE(unsigned long)
  377. BOOST_HASH_SPECIALIZE(float)
  378. BOOST_HASH_SPECIALIZE(double)
  379. BOOST_HASH_SPECIALIZE(long double)
  380. BOOST_HASH_SPECIALIZE_REF(std::string)
  381. #if !defined(BOOST_NO_STD_WSTRING)
  382. BOOST_HASH_SPECIALIZE_REF(std::wstring)
  383. #endif
  384. #if !defined(BOOST_NO_LONG_LONG)
  385. BOOST_HASH_SPECIALIZE(boost::long_long_type)
  386. BOOST_HASH_SPECIALIZE(boost::ulong_long_type)
  387. #endif
  388. #if defined(BOOST_HAS_INT128)
  389. BOOST_HASH_SPECIALIZE(boost::int128_type)
  390. BOOST_HASH_SPECIALIZE(boost::uint128_type)
  391. #endif
  392. #if !defined(BOOST_NO_CXX11_HDR_TYPEINDEX)
  393. BOOST_HASH_SPECIALIZE(std::type_index)
  394. #endif
  395. #undef BOOST_HASH_SPECIALIZE
  396. #undef BOOST_HASH_SPECIALIZE_REF
  397. // Specializing boost::hash for pointers.
  398. #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
  399. template <class T>
  400. struct hash<T*>
  401. : public std::unary_function<T*, std::size_t>
  402. {
  403. std::size_t operator()(T* v) const
  404. {
  405. #if !BOOST_WORKAROUND(__SUNPRO_CC, <= 0x590)
  406. return boost::hash_value(v);
  407. #else
  408. std::size_t x = static_cast<std::size_t>(
  409. reinterpret_cast<std::ptrdiff_t>(v));
  410. return x + (x >> 3);
  411. #endif
  412. }
  413. };
  414. #else
  415. // For compilers without partial specialization, we define a
  416. // boost::hash for all remaining types. But hash_impl is only defined
  417. // for pointers in 'extensions.hpp' - so when BOOST_HASH_NO_EXTENSIONS
  418. // is defined there will still be a compile error for types not supported
  419. // in the standard.
  420. namespace hash_detail
  421. {
  422. template <bool IsPointer>
  423. struct hash_impl;
  424. template <>
  425. struct hash_impl<true>
  426. {
  427. template <class T>
  428. struct inner
  429. : public std::unary_function<T, std::size_t>
  430. {
  431. std::size_t operator()(T val) const
  432. {
  433. #if !BOOST_WORKAROUND(__SUNPRO_CC, <= 590)
  434. return boost::hash_value(val);
  435. #else
  436. std::size_t x = static_cast<std::size_t>(
  437. reinterpret_cast<std::ptrdiff_t>(val));
  438. return x + (x >> 3);
  439. #endif
  440. }
  441. };
  442. };
  443. }
  444. template <class T> struct hash
  445. : public boost::hash_detail::hash_impl<boost::is_pointer<T>::value>
  446. ::BOOST_NESTED_TEMPLATE inner<T>
  447. {
  448. };
  449. #endif
  450. }
  451. #undef BOOST_HASH_CHAR_TRAITS
  452. #undef BOOST_FUNCTIONAL_HASH_ROTL32
  453. #if defined(BOOST_MSVC)
  454. #pragma warning(pop)
  455. #endif
  456. #endif // BOOST_FUNCTIONAL_HASH_HASH_HPP
  457. // Include this outside of the include guards in case the file is included
  458. // twice - once with BOOST_HASH_NO_EXTENSIONS defined, and then with it
  459. // undefined.
  460. #if !defined(BOOST_HASH_NO_EXTENSIONS) \
  461. && !defined(BOOST_FUNCTIONAL_HASH_EXTENSIONS_HPP)
  462. #include <boost/functional/hash/extensions.hpp>
  463. #endif