1 | |
---|
2 | // Copyright 2005-2009 Daniel James. |
---|
3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
---|
4 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
---|
5 | |
---|
6 | // Based on Peter Dimov's proposal |
---|
7 | // http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2005/n1756.pdf |
---|
8 | // issue 6.18. |
---|
9 | |
---|
10 | // This implements the extensions to the standard. |
---|
11 | // It's undocumented, so you shouldn't use it.... |
---|
12 | |
---|
13 | #if !defined(BOOST_FUNCTIONAL_HASH_EXTENSIONS_HPP) |
---|
14 | #define BOOST_FUNCTIONAL_HASH_EXTENSIONS_HPP |
---|
15 | |
---|
16 | #include <boost/functional/hash/hash.hpp> |
---|
17 | #include <boost/detail/container_fwd.hpp> |
---|
18 | |
---|
19 | #if defined(_MSC_VER) && (_MSC_VER >= 1020) |
---|
20 | # pragma once |
---|
21 | #endif |
---|
22 | |
---|
23 | #if defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING) |
---|
24 | #include <boost/type_traits/is_array.hpp> |
---|
25 | #endif |
---|
26 | |
---|
27 | #if BOOST_WORKAROUND(BOOST_MSVC, < 1300) |
---|
28 | #include <boost/type_traits/is_const.hpp> |
---|
29 | #endif |
---|
30 | |
---|
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 | |
---|
50 | template <class T> |
---|
51 | std::size_t hash_value(std::complex<T> const&); |
---|
52 | |
---|
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 | hash_combine(seed, v.first); |
---|
58 | hash_combine(seed, v.second); |
---|
59 | return seed; |
---|
60 | } |
---|
61 | |
---|
62 | template <class T, class A> |
---|
63 | std::size_t hash_value(std::vector<T, A> const& v) |
---|
64 | { |
---|
65 | return hash_range(v.begin(), v.end()); |
---|
66 | } |
---|
67 | |
---|
68 | template <class T, class A> |
---|
69 | std::size_t hash_value(std::list<T, A> const& v) |
---|
70 | { |
---|
71 | return hash_range(v.begin(), v.end()); |
---|
72 | } |
---|
73 | |
---|
74 | template <class T, class A> |
---|
75 | std::size_t hash_value(std::deque<T, A> const& v) |
---|
76 | { |
---|
77 | return hash_range(v.begin(), v.end()); |
---|
78 | } |
---|
79 | |
---|
80 | template <class K, class C, class A> |
---|
81 | std::size_t hash_value(std::set<K, C, A> const& v) |
---|
82 | { |
---|
83 | return hash_range(v.begin(), v.end()); |
---|
84 | } |
---|
85 | |
---|
86 | template <class K, class C, class A> |
---|
87 | std::size_t hash_value(std::multiset<K, C, A> const& v) |
---|
88 | { |
---|
89 | return hash_range(v.begin(), v.end()); |
---|
90 | } |
---|
91 | |
---|
92 | template <class K, class T, class C, class A> |
---|
93 | std::size_t hash_value(std::map<K, T, C, A> const& v) |
---|
94 | { |
---|
95 | return hash_range(v.begin(), v.end()); |
---|
96 | } |
---|
97 | |
---|
98 | template <class K, class T, class C, class A> |
---|
99 | std::size_t hash_value(std::multimap<K, T, C, A> const& v) |
---|
100 | { |
---|
101 | return hash_range(v.begin(), v.end()); |
---|
102 | } |
---|
103 | |
---|
104 | template <class T> |
---|
105 | std::size_t hash_value(std::complex<T> const& v) |
---|
106 | { |
---|
107 | boost::hash<T> hasher; |
---|
108 | std::size_t seed = hasher(v.imag()); |
---|
109 | seed ^= hasher(v.real()) + (seed<<6) + (seed>>2); |
---|
110 | return seed; |
---|
111 | } |
---|
112 | |
---|
113 | // |
---|
114 | // call_hash_impl |
---|
115 | // |
---|
116 | |
---|
117 | // On compilers without function template ordering, this deals with arrays. |
---|
118 | |
---|
119 | #if defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING) |
---|
120 | namespace hash_detail |
---|
121 | { |
---|
122 | template <bool IsArray> |
---|
123 | struct call_hash_impl |
---|
124 | { |
---|
125 | template <class T> |
---|
126 | struct inner |
---|
127 | { |
---|
128 | static std::size_t call(T const& v) |
---|
129 | { |
---|
130 | using namespace boost; |
---|
131 | return hash_value(v); |
---|
132 | } |
---|
133 | }; |
---|
134 | }; |
---|
135 | |
---|
136 | template <> |
---|
137 | struct call_hash_impl<true> |
---|
138 | { |
---|
139 | template <class Array> |
---|
140 | struct inner |
---|
141 | { |
---|
142 | #if !BOOST_WORKAROUND(BOOST_MSVC, < 1300) |
---|
143 | static std::size_t call(Array const& v) |
---|
144 | #else |
---|
145 | static std::size_t call(Array& v) |
---|
146 | #endif |
---|
147 | { |
---|
148 | const int size = sizeof(v) / sizeof(*v); |
---|
149 | return boost::hash_range(v, v + size); |
---|
150 | } |
---|
151 | }; |
---|
152 | }; |
---|
153 | |
---|
154 | template <class T> |
---|
155 | struct call_hash |
---|
156 | : public call_hash_impl<boost::is_array<T>::value> |
---|
157 | ::BOOST_NESTED_TEMPLATE inner<T> |
---|
158 | { |
---|
159 | }; |
---|
160 | } |
---|
161 | #endif // BOOST_NO_FUNCTION_TEMPLATE_ORDERING |
---|
162 | |
---|
163 | // |
---|
164 | // boost::hash |
---|
165 | // |
---|
166 | |
---|
167 | |
---|
168 | #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) |
---|
169 | |
---|
170 | template <class T> struct hash |
---|
171 | : std::unary_function<T, std::size_t> |
---|
172 | { |
---|
173 | #if !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING) |
---|
174 | std::size_t operator()(T const& val) const |
---|
175 | { |
---|
176 | return hash_value(val); |
---|
177 | } |
---|
178 | #else |
---|
179 | std::size_t operator()(T const& val) const |
---|
180 | { |
---|
181 | return hash_detail::call_hash<T>::call(val); |
---|
182 | } |
---|
183 | #endif |
---|
184 | }; |
---|
185 | |
---|
186 | #if BOOST_WORKAROUND(__DMC__, <= 0x848) |
---|
187 | template <class T, unsigned int n> struct hash<T[n]> |
---|
188 | : std::unary_function<T[n], std::size_t> |
---|
189 | { |
---|
190 | std::size_t operator()(const T* val) const |
---|
191 | { |
---|
192 | return boost::hash_range(val, val+n); |
---|
193 | } |
---|
194 | }; |
---|
195 | #endif |
---|
196 | |
---|
197 | #else // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION |
---|
198 | |
---|
199 | // On compilers without partial specialization, boost::hash<T> |
---|
200 | // has already been declared to deal with pointers, so just |
---|
201 | // need to supply the non-pointer version of hash_impl. |
---|
202 | |
---|
203 | namespace hash_detail |
---|
204 | { |
---|
205 | template <bool IsPointer> |
---|
206 | struct hash_impl; |
---|
207 | |
---|
208 | #if !BOOST_WORKAROUND(BOOST_MSVC, < 1300) |
---|
209 | |
---|
210 | template <> |
---|
211 | struct hash_impl<false> |
---|
212 | { |
---|
213 | template <class T> |
---|
214 | struct inner |
---|
215 | : std::unary_function<T, std::size_t> |
---|
216 | { |
---|
217 | #if !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING) |
---|
218 | std::size_t operator()(T const& val) const |
---|
219 | { |
---|
220 | return hash_value(val); |
---|
221 | } |
---|
222 | #else |
---|
223 | std::size_t operator()(T const& val) const |
---|
224 | { |
---|
225 | return hash_detail::call_hash<T>::call(val); |
---|
226 | } |
---|
227 | #endif |
---|
228 | }; |
---|
229 | }; |
---|
230 | |
---|
231 | #else // Visual C++ 6.5 |
---|
232 | |
---|
233 | // Visual C++ 6.5 has problems with nested member functions and |
---|
234 | // applying const to const types in templates. So we get this: |
---|
235 | |
---|
236 | template <bool IsConst> |
---|
237 | struct hash_impl_msvc |
---|
238 | { |
---|
239 | template <class T> |
---|
240 | struct inner |
---|
241 | : public std::unary_function<T, std::size_t> |
---|
242 | { |
---|
243 | std::size_t operator()(T const& val) const |
---|
244 | { |
---|
245 | return hash_detail::call_hash<T const>::call(val); |
---|
246 | } |
---|
247 | |
---|
248 | std::size_t operator()(T& val) const |
---|
249 | { |
---|
250 | return hash_detail::call_hash<T>::call(val); |
---|
251 | } |
---|
252 | }; |
---|
253 | }; |
---|
254 | |
---|
255 | template <> |
---|
256 | struct hash_impl_msvc<true> |
---|
257 | { |
---|
258 | template <class T> |
---|
259 | struct inner |
---|
260 | : public std::unary_function<T, std::size_t> |
---|
261 | { |
---|
262 | std::size_t operator()(T& val) const |
---|
263 | { |
---|
264 | return hash_detail::call_hash<T>::call(val); |
---|
265 | } |
---|
266 | }; |
---|
267 | }; |
---|
268 | |
---|
269 | template <class T> |
---|
270 | struct hash_impl_msvc2 |
---|
271 | : public hash_impl_msvc<boost::is_const<T>::value> |
---|
272 | ::BOOST_NESTED_TEMPLATE inner<T> {}; |
---|
273 | |
---|
274 | template <> |
---|
275 | struct hash_impl<false> |
---|
276 | { |
---|
277 | template <class T> |
---|
278 | struct inner : public hash_impl_msvc2<T> {}; |
---|
279 | }; |
---|
280 | |
---|
281 | #endif // Visual C++ 6.5 |
---|
282 | } |
---|
283 | #endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION |
---|
284 | } |
---|
285 | |
---|
286 | #endif |
---|