extensions.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. // This implements the extensions to the standard.
  8. // It's undocumented, so you shouldn't use it....
  9. #if !defined(BOOST_FUNCTIONAL_HASH_EXTENSIONS_HPP)
  10. #define BOOST_FUNCTIONAL_HASH_EXTENSIONS_HPP
  11. #include <boost/config.hpp>
  12. #if defined(BOOST_HAS_PRAGMA_ONCE)
  13. #pragma once
  14. #endif
  15. #include <boost/container_hash/hash.hpp>
  16. #include <boost/detail/container_fwd.hpp>
  17. #include <boost/core/enable_if.hpp>
  18. #include <boost/static_assert.hpp>
  19. #include <vector>
  20. #if !defined(BOOST_NO_CXX11_HDR_ARRAY)
  21. # include <array>
  22. #endif
  23. #if !defined(BOOST_NO_CXX11_HDR_TUPLE)
  24. # include <tuple>
  25. #endif
  26. #if !defined(BOOST_NO_CXX11_HDR_MEMORY)
  27. # include <memory>
  28. #endif
  29. #if defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
  30. #include <boost/type_traits/is_array.hpp>
  31. #endif
  32. namespace boost
  33. {
  34. template <class A, class B>
  35. std::size_t hash_value(std::pair<A, B> const&);
  36. template <class T, class A>
  37. std::size_t hash_value(std::vector<T, A> const&);
  38. template <class T, class A>
  39. std::size_t hash_value(std::list<T, A> const& v);
  40. template <class T, class A>
  41. std::size_t hash_value(std::deque<T, A> const& v);
  42. template <class K, class C, class A>
  43. std::size_t hash_value(std::set<K, C, A> const& v);
  44. template <class K, class C, class A>
  45. std::size_t hash_value(std::multiset<K, C, A> const& v);
  46. template <class K, class T, class C, class A>
  47. std::size_t hash_value(std::map<K, T, C, A> const& v);
  48. template <class K, class T, class C, class A>
  49. std::size_t hash_value(std::multimap<K, T, C, A> const& v);
  50. template <class T>
  51. std::size_t hash_value(std::complex<T> const&);
  52. template <class A, class B>
  53. std::size_t hash_value(std::pair<A, B> const& v)
  54. {
  55. std::size_t seed = 0;
  56. boost::hash_combine(seed, v.first);
  57. boost::hash_combine(seed, v.second);
  58. return seed;
  59. }
  60. inline std::size_t hash_range(
  61. std::vector<bool>::iterator first,
  62. std::vector<bool>::iterator last)
  63. {
  64. std::size_t seed = 0;
  65. for(; first != last; ++first)
  66. {
  67. hash_combine<bool>(seed, *first);
  68. }
  69. return seed;
  70. }
  71. inline std::size_t hash_range(
  72. std::vector<bool>::const_iterator first,
  73. std::vector<bool>::const_iterator last)
  74. {
  75. std::size_t seed = 0;
  76. for(; first != last; ++first)
  77. {
  78. hash_combine<bool>(seed, *first);
  79. }
  80. return seed;
  81. }
  82. inline void hash_range(
  83. std::size_t& seed,
  84. std::vector<bool>::iterator first,
  85. std::vector<bool>::iterator last)
  86. {
  87. for(; first != last; ++first)
  88. {
  89. hash_combine<bool>(seed, *first);
  90. }
  91. }
  92. inline void hash_range(
  93. std::size_t& seed,
  94. std::vector<bool>::const_iterator first,
  95. std::vector<bool>::const_iterator last)
  96. {
  97. for(; first != last; ++first)
  98. {
  99. hash_combine<bool>(seed, *first);
  100. }
  101. }
  102. template <class T, class A>
  103. std::size_t hash_value(std::vector<T, A> const& v)
  104. {
  105. return boost::hash_range(v.begin(), v.end());
  106. }
  107. template <class T, class A>
  108. std::size_t hash_value(std::list<T, A> const& v)
  109. {
  110. return boost::hash_range(v.begin(), v.end());
  111. }
  112. template <class T, class A>
  113. std::size_t hash_value(std::deque<T, A> const& v)
  114. {
  115. return boost::hash_range(v.begin(), v.end());
  116. }
  117. template <class K, class C, class A>
  118. std::size_t hash_value(std::set<K, C, A> const& v)
  119. {
  120. return boost::hash_range(v.begin(), v.end());
  121. }
  122. template <class K, class C, class A>
  123. std::size_t hash_value(std::multiset<K, C, A> const& v)
  124. {
  125. return boost::hash_range(v.begin(), v.end());
  126. }
  127. template <class K, class T, class C, class A>
  128. std::size_t hash_value(std::map<K, T, C, A> const& v)
  129. {
  130. return boost::hash_range(v.begin(), v.end());
  131. }
  132. template <class K, class T, class C, class A>
  133. std::size_t hash_value(std::multimap<K, T, C, A> const& v)
  134. {
  135. return boost::hash_range(v.begin(), v.end());
  136. }
  137. template <class T>
  138. std::size_t hash_value(std::complex<T> const& v)
  139. {
  140. boost::hash<T> hasher;
  141. std::size_t seed = hasher(v.imag());
  142. seed ^= hasher(v.real()) + (seed<<6) + (seed>>2);
  143. return seed;
  144. }
  145. #if !defined(BOOST_NO_CXX11_HDR_ARRAY)
  146. template <class T, std::size_t N>
  147. std::size_t hash_value(std::array<T, N> const& v)
  148. {
  149. return boost::hash_range(v.begin(), v.end());
  150. }
  151. #endif
  152. #if !defined(BOOST_NO_CXX11_HDR_TUPLE)
  153. namespace hash_detail {
  154. template <std::size_t I, typename T>
  155. inline typename boost::enable_if_c<(I == std::tuple_size<T>::value),
  156. void>::type
  157. hash_combine_tuple(std::size_t&, T const&)
  158. {
  159. }
  160. template <std::size_t I, typename T>
  161. inline typename boost::enable_if_c<(I < std::tuple_size<T>::value),
  162. void>::type
  163. hash_combine_tuple(std::size_t& seed, T const& v)
  164. {
  165. boost::hash_combine(seed, std::get<I>(v));
  166. boost::hash_detail::hash_combine_tuple<I + 1>(seed, v);
  167. }
  168. template <typename T>
  169. inline std::size_t hash_tuple(T const& v)
  170. {
  171. std::size_t seed = 0;
  172. boost::hash_detail::hash_combine_tuple<0>(seed, v);
  173. return seed;
  174. }
  175. }
  176. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  177. template <typename... T>
  178. inline std::size_t hash_value(std::tuple<T...> const& v)
  179. {
  180. return boost::hash_detail::hash_tuple(v);
  181. }
  182. #else
  183. inline std::size_t hash_value(std::tuple<> const& v)
  184. {
  185. return boost::hash_detail::hash_tuple(v);
  186. }
  187. template<typename A0>
  188. inline std::size_t hash_value(std::tuple<A0> const& v)
  189. {
  190. return boost::hash_detail::hash_tuple(v);
  191. }
  192. template<typename A0, typename A1>
  193. inline std::size_t hash_value(std::tuple<A0, A1> const& v)
  194. {
  195. return boost::hash_detail::hash_tuple(v);
  196. }
  197. template<typename A0, typename A1, typename A2>
  198. inline std::size_t hash_value(std::tuple<A0, A1, A2> const& v)
  199. {
  200. return boost::hash_detail::hash_tuple(v);
  201. }
  202. template<typename A0, typename A1, typename A2, typename A3>
  203. inline std::size_t hash_value(std::tuple<A0, A1, A2, A3> const& v)
  204. {
  205. return boost::hash_detail::hash_tuple(v);
  206. }
  207. template<typename A0, typename A1, typename A2, typename A3, typename A4>
  208. inline std::size_t hash_value(std::tuple<A0, A1, A2, A3, A4> const& v)
  209. {
  210. return boost::hash_detail::hash_tuple(v);
  211. }
  212. template<typename A0, typename A1, typename A2, typename A3, typename A4, typename A5>
  213. inline std::size_t hash_value(std::tuple<A0, A1, A2, A3, A4, A5> const& v)
  214. {
  215. return boost::hash_detail::hash_tuple(v);
  216. }
  217. template<typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6>
  218. inline std::size_t hash_value(std::tuple<A0, A1, A2, A3, A4, A5, A6> const& v)
  219. {
  220. return boost::hash_detail::hash_tuple(v);
  221. }
  222. template<typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7>
  223. inline std::size_t hash_value(std::tuple<A0, A1, A2, A3, A4, A5, A6, A7> const& v)
  224. {
  225. return boost::hash_detail::hash_tuple(v);
  226. }
  227. template<typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8>
  228. inline std::size_t hash_value(std::tuple<A0, A1, A2, A3, A4, A5, A6, A7, A8> const& v)
  229. {
  230. return boost::hash_detail::hash_tuple(v);
  231. }
  232. template<typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8, typename A9>
  233. inline std::size_t hash_value(std::tuple<A0, A1, A2, A3, A4, A5, A6, A7, A8, A9> const& v)
  234. {
  235. return boost::hash_detail::hash_tuple(v);
  236. }
  237. #endif
  238. #endif
  239. #if !defined(BOOST_NO_CXX11_SMART_PTR)
  240. template <typename T>
  241. inline std::size_t hash_value(std::shared_ptr<T> const& x) {
  242. return boost::hash_value(x.get());
  243. }
  244. template <typename T, typename Deleter>
  245. inline std::size_t hash_value(std::unique_ptr<T, Deleter> const& x) {
  246. return boost::hash_value(x.get());
  247. }
  248. #endif
  249. //
  250. // call_hash_impl
  251. //
  252. // On compilers without function template ordering, this deals with arrays.
  253. #if defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
  254. namespace hash_detail
  255. {
  256. template <bool IsArray>
  257. struct call_hash_impl
  258. {
  259. template <class T>
  260. struct inner
  261. {
  262. static std::size_t call(T const& v)
  263. {
  264. using namespace boost;
  265. return hash_value(v);
  266. }
  267. };
  268. };
  269. template <>
  270. struct call_hash_impl<true>
  271. {
  272. template <class Array>
  273. struct inner
  274. {
  275. static std::size_t call(Array const& v)
  276. {
  277. const int size = sizeof(v) / sizeof(*v);
  278. return boost::hash_range(v, v + size);
  279. }
  280. };
  281. };
  282. template <class T>
  283. struct call_hash
  284. : public call_hash_impl<boost::is_array<T>::value>
  285. ::BOOST_NESTED_TEMPLATE inner<T>
  286. {
  287. };
  288. }
  289. #endif // BOOST_NO_FUNCTION_TEMPLATE_ORDERING
  290. //
  291. // boost::hash
  292. //
  293. #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
  294. template <class T> struct hash
  295. : boost::hash_detail::hash_base<T>
  296. {
  297. #if !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
  298. std::size_t operator()(T const& val) const
  299. {
  300. return hash_value(val);
  301. }
  302. #else
  303. std::size_t operator()(T const& val) const
  304. {
  305. return hash_detail::call_hash<T>::call(val);
  306. }
  307. #endif
  308. };
  309. #if BOOST_WORKAROUND(__DMC__, <= 0x848)
  310. template <class T, unsigned int n> struct hash<T[n]>
  311. : boost::hash_detail::hash_base<T[n]>
  312. {
  313. std::size_t operator()(const T* val) const
  314. {
  315. return boost::hash_range(val, val+n);
  316. }
  317. };
  318. #endif
  319. #else // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  320. // On compilers without partial specialization, boost::hash<T>
  321. // has already been declared to deal with pointers, so just
  322. // need to supply the non-pointer version of hash_impl.
  323. namespace hash_detail
  324. {
  325. template <bool IsPointer>
  326. struct hash_impl;
  327. template <>
  328. struct hash_impl<false>
  329. {
  330. template <class T>
  331. struct inner
  332. : boost::hash_detail::hash_base<T>
  333. {
  334. #if !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
  335. std::size_t operator()(T const& val) const
  336. {
  337. return hash_value(val);
  338. }
  339. #else
  340. std::size_t operator()(T const& val) const
  341. {
  342. return hash_detail::call_hash<T>::call(val);
  343. }
  344. #endif
  345. };
  346. };
  347. }
  348. #endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  349. }
  350. #endif