hash.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. // Copyright 2005-2009 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. #if !defined(BOOST_FUNCTIONAL_HASH_HASH_HPP)
  8. #define BOOST_FUNCTIONAL_HASH_HASH_HPP
  9. #include <boost/functional/hash/hash_fwd.hpp>
  10. #include <functional>
  11. #include <boost/functional/hash/detail/hash_float.hpp>
  12. #include <string>
  13. #include <boost/limits.hpp>
  14. #if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
  15. #include <boost/type_traits/is_pointer.hpp>
  16. #endif
  17. #if BOOST_WORKAROUND(__GNUC__, < 3) && !defined(__SGI_STL_PORT) && !defined(_STLPORT_VERSION)
  18. #define BOOST_HASH_CHAR_TRAITS string_char_traits
  19. #else
  20. #define BOOST_HASH_CHAR_TRAITS char_traits
  21. #endif
  22. namespace boost
  23. {
  24. std::size_t hash_value(bool);
  25. std::size_t hash_value(char);
  26. std::size_t hash_value(unsigned char);
  27. std::size_t hash_value(signed char);
  28. std::size_t hash_value(short);
  29. std::size_t hash_value(unsigned short);
  30. std::size_t hash_value(int);
  31. std::size_t hash_value(unsigned int);
  32. std::size_t hash_value(long);
  33. std::size_t hash_value(unsigned long);
  34. #if !defined(BOOST_NO_INTRINSIC_WCHAR_T)
  35. std::size_t hash_value(wchar_t);
  36. #endif
  37. #if defined(BOOST_HAS_LONG_LONG)
  38. std::size_t hash_value(boost::long_long_type);
  39. std::size_t hash_value(boost::ulong_long_type);
  40. #endif
  41. #if !BOOST_WORKAROUND(__DMC__, <= 0x848)
  42. template <class T> std::size_t hash_value(T* const&);
  43. #else
  44. template <class T> std::size_t hash_value(T*);
  45. #endif
  46. #if !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
  47. template< class T, unsigned N >
  48. std::size_t hash_value(const T (&x)[N]);
  49. template< class T, unsigned N >
  50. std::size_t hash_value(T (&x)[N]);
  51. #endif
  52. std::size_t hash_value(float v);
  53. std::size_t hash_value(double v);
  54. std::size_t hash_value(long double v);
  55. template <class Ch, class A>
  56. std::size_t hash_value(std::basic_string<Ch, std::BOOST_HASH_CHAR_TRAITS<Ch>, A> const&);
  57. // Implementation
  58. namespace hash_detail
  59. {
  60. template <class T>
  61. inline std::size_t hash_value_signed(T val)
  62. {
  63. const int size_t_bits = std::numeric_limits<std::size_t>::digits;
  64. // ceiling(std::numeric_limits<T>::digits / size_t_bits) - 1
  65. const int length = (std::numeric_limits<T>::digits - 1)
  66. / size_t_bits;
  67. std::size_t seed = 0;
  68. T positive = val < 0 ? -1 - val : val;
  69. // Hopefully, this loop can be unrolled.
  70. for(unsigned int i = length * size_t_bits; i > 0; i -= size_t_bits)
  71. {
  72. seed ^= (std::size_t) (positive >> i) + (seed<<6) + (seed>>2);
  73. }
  74. seed ^= (std::size_t) val + (seed<<6) + (seed>>2);
  75. return seed;
  76. }
  77. template <class T>
  78. inline std::size_t hash_value_unsigned(T val)
  79. {
  80. const int size_t_bits = std::numeric_limits<std::size_t>::digits;
  81. // ceiling(std::numeric_limits<T>::digits / size_t_bits) - 1
  82. const int length = (std::numeric_limits<T>::digits - 1)
  83. / size_t_bits;
  84. std::size_t seed = 0;
  85. // Hopefully, this loop can be unrolled.
  86. for(unsigned int i = length * size_t_bits; i > 0; i -= size_t_bits)
  87. {
  88. seed ^= (std::size_t) (val >> i) + (seed<<6) + (seed>>2);
  89. }
  90. seed ^= (std::size_t) val + (seed<<6) + (seed>>2);
  91. return seed;
  92. }
  93. }
  94. inline std::size_t hash_value(bool v)
  95. {
  96. return static_cast<std::size_t>(v);
  97. }
  98. inline std::size_t hash_value(char v)
  99. {
  100. return static_cast<std::size_t>(v);
  101. }
  102. inline std::size_t hash_value(unsigned char v)
  103. {
  104. return static_cast<std::size_t>(v);
  105. }
  106. inline std::size_t hash_value(signed char v)
  107. {
  108. return static_cast<std::size_t>(v);
  109. }
  110. inline std::size_t hash_value(short v)
  111. {
  112. return static_cast<std::size_t>(v);
  113. }
  114. inline std::size_t hash_value(unsigned short v)
  115. {
  116. return static_cast<std::size_t>(v);
  117. }
  118. inline std::size_t hash_value(int v)
  119. {
  120. return static_cast<std::size_t>(v);
  121. }
  122. inline std::size_t hash_value(unsigned int v)
  123. {
  124. return static_cast<std::size_t>(v);
  125. }
  126. inline std::size_t hash_value(long v)
  127. {
  128. return static_cast<std::size_t>(v);
  129. }
  130. inline std::size_t hash_value(unsigned long v)
  131. {
  132. return static_cast<std::size_t>(v);
  133. }
  134. #if !defined(BOOST_NO_INTRINSIC_WCHAR_T)
  135. inline std::size_t hash_value(wchar_t v)
  136. {
  137. return static_cast<std::size_t>(v);
  138. }
  139. #endif
  140. #if defined(BOOST_HAS_LONG_LONG)
  141. inline std::size_t hash_value(boost::long_long_type v)
  142. {
  143. return hash_detail::hash_value_signed(v);
  144. }
  145. inline std::size_t hash_value(boost::ulong_long_type v)
  146. {
  147. return hash_detail::hash_value_unsigned(v);
  148. }
  149. #endif
  150. // Implementation by Alberto Barbati and Dave Harris.
  151. #if !BOOST_WORKAROUND(__DMC__, <= 0x848)
  152. template <class T> std::size_t hash_value(T* const& v)
  153. #else
  154. template <class T> std::size_t hash_value(T* v)
  155. #endif
  156. {
  157. std::size_t x = static_cast<std::size_t>(
  158. reinterpret_cast<std::ptrdiff_t>(v));
  159. return x + (x >> 3);
  160. }
  161. #if BOOST_WORKAROUND(BOOST_MSVC, < 1300)
  162. template <class T>
  163. inline void hash_combine(std::size_t& seed, T& v)
  164. #else
  165. template <class T>
  166. inline void hash_combine(std::size_t& seed, T const& v)
  167. #endif
  168. {
  169. boost::hash<T> hasher;
  170. seed ^= hasher(v) + 0x9e3779b9 + (seed<<6) + (seed>>2);
  171. }
  172. template <class It>
  173. inline std::size_t hash_range(It first, It last)
  174. {
  175. std::size_t seed = 0;
  176. for(; first != last; ++first)
  177. {
  178. hash_combine(seed, *first);
  179. }
  180. return seed;
  181. }
  182. template <class It>
  183. inline void hash_range(std::size_t& seed, It first, It last)
  184. {
  185. for(; first != last; ++first)
  186. {
  187. hash_combine(seed, *first);
  188. }
  189. }
  190. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551))
  191. template <class T>
  192. inline std::size_t hash_range(T* first, T* last)
  193. {
  194. std::size_t seed = 0;
  195. for(; first != last; ++first)
  196. {
  197. boost::hash<T> hasher;
  198. seed ^= hasher(*first) + 0x9e3779b9 + (seed<<6) + (seed>>2);
  199. }
  200. return seed;
  201. }
  202. template <class T>
  203. inline void hash_range(std::size_t& seed, T* first, T* last)
  204. {
  205. for(; first != last; ++first)
  206. {
  207. boost::hash<T> hasher;
  208. seed ^= hasher(*first) + 0x9e3779b9 + (seed<<6) + (seed>>2);
  209. }
  210. }
  211. #endif
  212. #if !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
  213. template< class T, unsigned N >
  214. inline std::size_t hash_value(const T (&x)[N])
  215. {
  216. return hash_range(x, x + N);
  217. }
  218. template< class T, unsigned N >
  219. inline std::size_t hash_value(T (&x)[N])
  220. {
  221. return hash_range(x, x + N);
  222. }
  223. #endif
  224. template <class Ch, class A>
  225. inline std::size_t hash_value(std::basic_string<Ch, std::BOOST_HASH_CHAR_TRAITS<Ch>, A> const& v)
  226. {
  227. return hash_range(v.begin(), v.end());
  228. }
  229. inline std::size_t hash_value(float v)
  230. {
  231. return boost::hash_detail::float_hash_value(v);
  232. }
  233. inline std::size_t hash_value(double v)
  234. {
  235. return boost::hash_detail::float_hash_value(v);
  236. }
  237. inline std::size_t hash_value(long double v)
  238. {
  239. return boost::hash_detail::float_hash_value(v);
  240. }
  241. //
  242. // boost::hash
  243. //
  244. // Define the specializations required by the standard. The general purpose
  245. // boost::hash is defined later in extensions.hpp if BOOST_HASH_NO_EXTENSIONS
  246. // is not defined.
  247. // BOOST_HASH_SPECIALIZE - define a specialization for a type which is
  248. // passed by copy.
  249. //
  250. // BOOST_HASH_SPECIALIZE_REF - define a specialization for a type which is
  251. // passed by copy.
  252. //
  253. // These are undefined later.
  254. #if !BOOST_WORKAROUND(BOOST_MSVC, < 1300)
  255. #define BOOST_HASH_SPECIALIZE(type) \
  256. template <> struct hash<type> \
  257. : public std::unary_function<type, std::size_t> \
  258. { \
  259. std::size_t operator()(type v) const \
  260. { \
  261. return boost::hash_value(v); \
  262. } \
  263. };
  264. #define BOOST_HASH_SPECIALIZE_REF(type) \
  265. template <> struct hash<type> \
  266. : public std::unary_function<type, std::size_t> \
  267. { \
  268. std::size_t operator()(type const& v) const \
  269. { \
  270. return boost::hash_value(v); \
  271. } \
  272. };
  273. #else
  274. #define BOOST_HASH_SPECIALIZE(type) \
  275. template <> struct hash<type> \
  276. : public std::unary_function<type, std::size_t> \
  277. { \
  278. std::size_t operator()(type v) const \
  279. { \
  280. return boost::hash_value(v); \
  281. } \
  282. }; \
  283. \
  284. template <> struct hash<const type> \
  285. : public std::unary_function<const type, std::size_t> \
  286. { \
  287. std::size_t operator()(const type v) const \
  288. { \
  289. return boost::hash_value(v); \
  290. } \
  291. };
  292. #define BOOST_HASH_SPECIALIZE_REF(type) \
  293. template <> struct hash<type> \
  294. : public std::unary_function<type, std::size_t> \
  295. { \
  296. std::size_t operator()(type const& v) const \
  297. { \
  298. return boost::hash_value(v); \
  299. } \
  300. }; \
  301. \
  302. template <> struct hash<const type> \
  303. : public std::unary_function<const type, std::size_t> \
  304. { \
  305. std::size_t operator()(type const& v) const \
  306. { \
  307. return boost::hash_value(v); \
  308. } \
  309. };
  310. #endif
  311. BOOST_HASH_SPECIALIZE(bool)
  312. BOOST_HASH_SPECIALIZE(char)
  313. BOOST_HASH_SPECIALIZE(signed char)
  314. BOOST_HASH_SPECIALIZE(unsigned char)
  315. #if !defined(BOOST_NO_INTRINSIC_WCHAR_T)
  316. BOOST_HASH_SPECIALIZE(wchar_t)
  317. #endif
  318. BOOST_HASH_SPECIALIZE(short)
  319. BOOST_HASH_SPECIALIZE(unsigned short)
  320. BOOST_HASH_SPECIALIZE(int)
  321. BOOST_HASH_SPECIALIZE(unsigned int)
  322. BOOST_HASH_SPECIALIZE(long)
  323. BOOST_HASH_SPECIALIZE(unsigned long)
  324. BOOST_HASH_SPECIALIZE(float)
  325. BOOST_HASH_SPECIALIZE(double)
  326. BOOST_HASH_SPECIALIZE(long double)
  327. BOOST_HASH_SPECIALIZE_REF(std::string)
  328. #if !defined(BOOST_NO_STD_WSTRING)
  329. BOOST_HASH_SPECIALIZE_REF(std::wstring)
  330. #endif
  331. #if defined(BOOST_HAS_LONG_LONG)
  332. BOOST_HASH_SPECIALIZE(boost::long_long_type)
  333. BOOST_HASH_SPECIALIZE(boost::ulong_long_type)
  334. #endif
  335. #undef BOOST_HASH_SPECIALIZE
  336. #undef BOOST_HASH_SPECIALIZE_REF
  337. // Specializing boost::hash for pointers.
  338. #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
  339. template <class T>
  340. struct hash<T*>
  341. : public std::unary_function<T*, std::size_t>
  342. {
  343. std::size_t operator()(T* v) const
  344. {
  345. #if !BOOST_WORKAROUND(__SUNPRO_CC, <= 0x590)
  346. return boost::hash_value(v);
  347. #else
  348. std::size_t x = static_cast<std::size_t>(
  349. reinterpret_cast<std::ptrdiff_t>(v));
  350. return x + (x >> 3);
  351. #endif
  352. }
  353. };
  354. #else
  355. // For compilers without partial specialization, we define a
  356. // boost::hash for all remaining types. But hash_impl is only defined
  357. // for pointers in 'extensions.hpp' - so when BOOST_HASH_NO_EXTENSIONS
  358. // is defined there will still be a compile error for types not supported
  359. // in the standard.
  360. namespace hash_detail
  361. {
  362. template <bool IsPointer>
  363. struct hash_impl;
  364. template <>
  365. struct hash_impl<true>
  366. {
  367. template <class T>
  368. struct inner
  369. : public std::unary_function<T, std::size_t>
  370. {
  371. std::size_t operator()(T val) const
  372. {
  373. #if !BOOST_WORKAROUND(__SUNPRO_CC, <= 590)
  374. return boost::hash_value(val);
  375. #else
  376. std::size_t x = static_cast<std::size_t>(
  377. reinterpret_cast<std::ptrdiff_t>(val));
  378. return x + (x >> 3);
  379. #endif
  380. }
  381. };
  382. };
  383. }
  384. template <class T> struct hash
  385. : public boost::hash_detail::hash_impl<boost::is_pointer<T>::value>
  386. ::BOOST_NESTED_TEMPLATE inner<T>
  387. {
  388. };
  389. #endif
  390. }
  391. #undef BOOST_HASH_CHAR_TRAITS
  392. #endif // BOOST_FUNCTIONAL_HASH_HASH_HPP
  393. // Include this outside of the include guards in case the file is included
  394. // twice - once with BOOST_HASH_NO_EXTENSIONS defined, and then with it
  395. // undefined.
  396. #if !defined(BOOST_HASH_NO_EXTENSIONS) \
  397. && !defined(BOOST_FUNCTIONAL_HASH_EXTENSIONS_HPP)
  398. #include <boost/functional/hash/extensions.hpp>
  399. #endif