extensions.hpp 8.9 KB

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