extensions.hpp 10 KB

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