extensions.hpp 10 KB

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