1
2// Copyright 2005-2014 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 also contains public domain code from MurmurHash. From the
11// MurmurHash header:
12
13// MurmurHash3 was written by Austin Appleby, and is placed in the public
14// domain. The author hereby disclaims copyright to this source code.
15
16#if !defined(BOOST_FUNCTIONAL_HASH_HASH_HPP)
17#define BOOST_FUNCTIONAL_HASH_HASH_HPP
18
19#include <boost/container_hash/hash_fwd.hpp>
20#include <functional>
21#include <iterator>
22#include <boost/container_hash/detail/hash_float.hpp>
23#include <string>
24#include <boost/limits.hpp>
25#include <boost/type_traits/is_enum.hpp>
26#include <boost/type_traits/is_integral.hpp>
27#include <boost/core/enable_if.hpp>
28#include <boost/cstdint.hpp>
29
30#if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
31#include <boost/type_traits/is_pointer.hpp>
32#endif
33
34#if !defined(BOOST_NO_CXX11_HDR_TYPEINDEX)
35#include <typeindex>
36#endif
37
38#if !defined(BOOST_NO_CXX11_HDR_SYSTEM_ERROR)
39#include <system_error>
40#endif
41
42#if defined(BOOST_MSVC)
43#pragma warning(push)
44
45#if BOOST_MSVC >= 1400
46#pragma warning(disable:6295) // Ill-defined for-loop : 'unsigned int' values
47 // are always of range '0' to '4294967295'.
48 // Loop executes infinitely.
49#endif
50
51#endif
52
53#if BOOST_WORKAROUND(__GNUC__, < 3) \
54 && !defined(__SGI_STL_PORT) && !defined(_STLPORT_VERSION)
55#define BOOST_HASH_CHAR_TRAITS string_char_traits
56#else
57#define BOOST_HASH_CHAR_TRAITS char_traits
58#endif
59
60#if defined(_MSC_VER)
61# define BOOST_FUNCTIONAL_HASH_ROTL32(x, r) _rotl(x,r)
62#else
63# define BOOST_FUNCTIONAL_HASH_ROTL32(x, r) (x << r) | (x >> (32 - r))
64#endif
65
66// Detect whether standard library has C++17 headers
67
68#if !defined(BOOST_HASH_CXX17)
69# if defined(BOOST_MSVC)
70# if defined(_HAS_CXX17) && _HAS_CXX17
71# define BOOST_HASH_CXX17 1
72# endif
73# elif defined(__cplusplus) && __cplusplus >= 201703
74# define BOOST_HASH_CXX17 1
75# endif
76#endif
77
78#if !defined(BOOST_HASH_CXX17)
79# define BOOST_HASH_CXX17 0
80#endif
81
82#if BOOST_HASH_CXX17 && defined(__has_include)
83# if !defined(BOOST_HASH_HAS_STRING_VIEW) && __has_include(<string_view>)
84# define BOOST_HASH_HAS_STRING_VIEW 1
85# endif
86# if !defined(BOOST_HASH_HAS_OPTIONAL) && __has_include(<optional>)
87# define BOOST_HASH_HAS_OPTIONAL 1
88# endif
89# if !defined(BOOST_HASH_HAS_VARIANT) && __has_include(<variant>)
90# define BOOST_HASH_HAS_VARIANT 1
91# endif
92#endif
93
94#if !defined(BOOST_HASH_HAS_STRING_VIEW)
95# define BOOST_HASH_HAS_STRING_VIEW 0
96#endif
97
98#if !defined(BOOST_HASH_HAS_OPTIONAL)
99# define BOOST_HASH_HAS_OPTIONAL 0
100#endif
101
102#if !defined(BOOST_HASH_HAS_VARIANT)
103# define BOOST_HASH_HAS_VARIANT 0
104#endif
105
106#if BOOST_HASH_HAS_STRING_VIEW
107# include <string_view>
108#endif
109
110#if BOOST_HASH_HAS_OPTIONAL
111# include <optional>
112#endif
113
114#if BOOST_HASH_HAS_VARIANT
115# include <variant>
116#endif
117
118namespace boost
119{
120 namespace hash_detail
121 {
122#if defined(BOOST_NO_CXX98_FUNCTION_BASE)
123 template <typename T>
124 struct hash_base
125 {
126 typedef T argument_type;
127 typedef std::size_t result_type;
128 };
129#else
130 template <typename T>
131 struct hash_base : std::unary_function<T, std::size_t> {};
132#endif
133
134 struct enable_hash_value { typedef std::size_t type; };
135
136 template <typename T> struct basic_numbers {};
137 template <typename T> struct long_numbers;
138 template <typename T> struct ulong_numbers;
139 template <typename T> struct float_numbers {};
140
141 template <> struct basic_numbers<bool> :
142 boost::hash_detail::enable_hash_value {};
143 template <> struct basic_numbers<char> :
144 boost::hash_detail::enable_hash_value {};
145 template <> struct basic_numbers<unsigned char> :
146 boost::hash_detail::enable_hash_value {};
147 template <> struct basic_numbers<signed char> :
148 boost::hash_detail::enable_hash_value {};
149 template <> struct basic_numbers<short> :
150 boost::hash_detail::enable_hash_value {};
151 template <> struct basic_numbers<unsigned short> :
152 boost::hash_detail::enable_hash_value {};
153 template <> struct basic_numbers<int> :
154 boost::hash_detail::enable_hash_value {};
155 template <> struct basic_numbers<unsigned int> :
156 boost::hash_detail::enable_hash_value {};
157 template <> struct basic_numbers<long> :
158 boost::hash_detail::enable_hash_value {};
159 template <> struct basic_numbers<unsigned long> :
160 boost::hash_detail::enable_hash_value {};
161
162#if !defined(BOOST_NO_INTRINSIC_WCHAR_T)
163 template <> struct basic_numbers<wchar_t> :
164 boost::hash_detail::enable_hash_value {};
165#endif
166
167#if !defined(BOOST_NO_CXX11_CHAR16_T)
168 template <> struct basic_numbers<char16_t> :
169 boost::hash_detail::enable_hash_value {};
170#endif
171
172#if !defined(BOOST_NO_CXX11_CHAR32_T)
173 template <> struct basic_numbers<char32_t> :
174 boost::hash_detail::enable_hash_value {};
175#endif
176
177 // long_numbers is defined like this to allow for separate
178 // specialization for long_long and int128_type, in case
179 // they conflict.
180 template <typename T> struct long_numbers2 {};
181 template <typename T> struct ulong_numbers2 {};
182 template <typename T> struct long_numbers : long_numbers2<T> {};
183 template <typename T> struct ulong_numbers : ulong_numbers2<T> {};
184
185#if !defined(BOOST_NO_LONG_LONG)
186 template <> struct long_numbers<boost::long_long_type> :
187 boost::hash_detail::enable_hash_value {};
188 template <> struct ulong_numbers<boost::ulong_long_type> :
189 boost::hash_detail::enable_hash_value {};
190#endif
191
192#if defined(BOOST_HAS_INT128)
193 template <> struct long_numbers2<boost::int128_type> :
194 boost::hash_detail::enable_hash_value {};
195 template <> struct ulong_numbers2<boost::uint128_type> :
196 boost::hash_detail::enable_hash_value {};
197#endif
198
199 template <> struct float_numbers<float> :
200 boost::hash_detail::enable_hash_value {};
201 template <> struct float_numbers<double> :
202 boost::hash_detail::enable_hash_value {};
203 template <> struct float_numbers<long double> :
204 boost::hash_detail::enable_hash_value {};
205 }
206
207 template <typename T>
208 typename boost::hash_detail::basic_numbers<T>::type hash_value(T);
209 template <typename T>
210 typename boost::hash_detail::long_numbers<T>::type hash_value(T);
211 template <typename T>
212 typename boost::hash_detail::ulong_numbers<T>::type hash_value(T);
213
214 template <typename T>
215 typename boost::enable_if<boost::is_enum<T>, std::size_t>::type
216 hash_value(T);
217
218#if !BOOST_WORKAROUND(__DMC__, <= 0x848)
219 template <class T> std::size_t hash_value(T* const&);
220#else
221 template <class T> std::size_t hash_value(T*);
222#endif
223
224#if !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
225 template< class T, unsigned N >
226 std::size_t hash_value(const T (&x)[N]);
227
228 template< class T, unsigned N >
229 std::size_t hash_value(T (&x)[N]);
230#endif
231
232 template <class Ch, class A>
233 std::size_t hash_value(
234 std::basic_string<Ch, std::BOOST_HASH_CHAR_TRAITS<Ch>, A> const&);
235
236#if BOOST_HASH_HAS_STRING_VIEW
237 template <class Ch>
238 std::size_t hash_value(
239 std::basic_string_view<Ch, std::BOOST_HASH_CHAR_TRAITS<Ch> > const&);
240#endif
241
242 template <typename T>
243 typename boost::hash_detail::float_numbers<T>::type hash_value(T);
244
245#if BOOST_HASH_HAS_OPTIONAL
246 template <typename T>
247 std::size_t hash_value(std::optional<T> const&);
248#endif
249
250#if BOOST_HASH_HAS_VARIANT
251 std::size_t hash_value(std::monostate);
252 template <typename... Types>
253 std::size_t hash_value(std::variant<Types...> const&);
254#endif
255
256#if !defined(BOOST_NO_CXX11_HDR_TYPEINDEX)
257 std::size_t hash_value(std::type_index);
258#endif
259
260#if !defined(BOOST_NO_CXX11_HDR_SYSTEM_ERROR)
261 std::size_t hash_value(std::error_code const&);
262 std::size_t hash_value(std::error_condition const&);
263#endif
264
265 // Implementation
266
267 namespace hash_detail
268 {
269 template <class T>
270 inline std::size_t hash_value_signed(T val)
271 {
272 const unsigned int size_t_bits = std::numeric_limits<std::size_t>::digits;
273 // ceiling(std::numeric_limits<T>::digits / size_t_bits) - 1
274 const int length = (std::numeric_limits<T>::digits - 1)
275 / static_cast<int>(size_t_bits);
276
277 std::size_t seed = 0;
278 T positive = val < 0 ? -1 - val : val;
279
280 // Hopefully, this loop can be unrolled.
281 for(unsigned int i = length * size_t_bits; i > 0; i -= size_t_bits)
282 {
283 seed ^= (std::size_t) (positive >> i) + (seed<<6) + (seed>>2);
284 }
285 seed ^= (std::size_t) val + (seed<<6) + (seed>>2);
286
287 return seed;
288 }
289
290 template <class T>
291 inline std::size_t hash_value_unsigned(T val)
292 {
293 const unsigned int size_t_bits = std::numeric_limits<std::size_t>::digits;
294 // ceiling(std::numeric_limits<T>::digits / size_t_bits) - 1
295 const int length = (std::numeric_limits<T>::digits - 1)
296 / static_cast<int>(size_t_bits);
297
298 std::size_t seed = 0;
299
300 // Hopefully, this loop can be unrolled.
301 for(unsigned int i = length * size_t_bits; i > 0; i -= size_t_bits)
302 {
303 seed ^= (std::size_t) (val >> i) + (seed<<6) + (seed>>2);
304 }
305 seed ^= (std::size_t) val + (seed<<6) + (seed>>2);
306
307 return seed;
308 }
309
310 template <typename SizeT>
311 inline void hash_combine_impl(SizeT& seed, SizeT value)
312 {
313 seed ^= value + 0x9e3779b9 + (seed<<6) + (seed>>2);
314 }
315
316 inline void hash_combine_impl(boost::uint32_t& h1,
317 boost::uint32_t k1)
318 {
319 const uint32_t c1 = 0xcc9e2d51;
320 const uint32_t c2 = 0x1b873593;
321
322 k1 *= c1;
323 k1 = BOOST_FUNCTIONAL_HASH_ROTL32(k1,15);
324 k1 *= c2;
325
326 h1 ^= k1;
327 h1 = BOOST_FUNCTIONAL_HASH_ROTL32(h1,13);
328 h1 = h1*5+0xe6546b64;
329 }
330
331
332// Don't define 64-bit hash combine on platforms without 64 bit integers,
333// and also not for 32-bit gcc as it warns about the 64-bit constant.
334#if !defined(BOOST_NO_INT64_T) && \
335 !(defined(__GNUC__) && ULONG_MAX == 0xffffffff)
336
337 inline void hash_combine_impl(boost::uint64_t& h,
338 boost::uint64_t k)
339 {
340 const boost::uint64_t m = UINT64_C(0xc6a4a7935bd1e995);
341 const int r = 47;
342
343 k *= m;
344 k ^= k >> r;
345 k *= m;
346
347 h ^= k;
348 h *= m;
349
350 // Completely arbitrary number, to prevent 0's
351 // from hashing to 0.
352 h += 0xe6546b64;
353 }
354
355#endif // BOOST_NO_INT64_T
356 }
357
358 template <typename T>
359 typename boost::hash_detail::basic_numbers<T>::type hash_value(T v)
360 {
361 return static_cast<std::size_t>(v);
362 }
363
364 template <typename T>
365 typename boost::hash_detail::long_numbers<T>::type hash_value(T v)
366 {
367 return hash_detail::hash_value_signed(v);
368 }
369
370 template <typename T>
371 typename boost::hash_detail::ulong_numbers<T>::type hash_value(T v)
372 {
373 return hash_detail::hash_value_unsigned(v);
374 }
375
376 template <typename T>
377 typename boost::enable_if<boost::is_enum<T>, std::size_t>::type
378 hash_value(T v)
379 {
380 return static_cast<std::size_t>(v);
381 }
382
383 // Implementation by Alberto Barbati and Dave Harris.
384#if !BOOST_WORKAROUND(__DMC__, <= 0x848)
385 template <class T> std::size_t hash_value(T* const& v)
386#else
387 template <class T> std::size_t hash_value(T* v)
388#endif
389 {
390#if defined(__VMS) && __INITIAL_POINTER_SIZE == 64
391 // for some reason ptrdiff_t on OpenVMS compiler with
392 // 64 bit is not 64 bit !!!
393 std::size_t x = static_cast<std::size_t>(
394 reinterpret_cast<long long int>(v));
395#else
396 std::size_t x = static_cast<std::size_t>(
397 reinterpret_cast<std::ptrdiff_t>(v));
398#endif
399 return x + (x >> 3);
400 }
401
402#if defined(BOOST_MSVC)
403#pragma warning(push)
404#if BOOST_MSVC <= 1400
405#pragma warning(disable:4267) // 'argument' : conversion from 'size_t' to
406 // 'unsigned int', possible loss of data
407 // A misguided attempt to detect 64-bit
408 // incompatability.
409#endif
410#endif
411
412 template <class T>
413 inline void hash_combine(std::size_t& seed, T const& v)
414 {
415 boost::hash<T> hasher;
416 return boost::hash_detail::hash_combine_impl(seed, hasher(v));
417 }
418
419#if defined(BOOST_MSVC)
420#pragma warning(pop)
421#endif
422
423 template <class It>
424 inline std::size_t hash_range(It first, It last)
425 {
426 std::size_t seed = 0;
427
428 for(; first != last; ++first)
429 {
430 hash_combine<typename std::iterator_traits<It>::value_type>(seed, *first);
431 }
432
433 return seed;
434 }
435
436 template <class It>
437 inline void hash_range(std::size_t& seed, It first, It last)
438 {
439 for(; first != last; ++first)
440 {
441 hash_combine<typename std::iterator_traits<It>::value_type>(seed, *first);
442 }
443 }
444
445#if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551))
446 template <class T>
447 inline std::size_t hash_range(T* first, T* last)
448 {
449 std::size_t seed = 0;
450
451 for(; first != last; ++first)
452 {
453 boost::hash<T> hasher;
454 seed ^= hasher(*first) + 0x9e3779b9 + (seed<<6) + (seed>>2);
455 }
456
457 return seed;
458 }
459
460 template <class T>
461 inline void hash_range(std::size_t& seed, T* first, T* last)
462 {
463 for(; first != last; ++first)
464 {
465 boost::hash<T> hasher;
466 seed ^= hasher(*first) + 0x9e3779b9 + (seed<<6) + (seed>>2);
467 }
468 }
469#endif
470
471#if !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
472 template< class T, unsigned N >
473 inline std::size_t hash_value(const T (&x)[N])
474 {
475 return hash_range(x, x + N);
476 }
477
478 template< class T, unsigned N >
479 inline std::size_t hash_value(T (&x)[N])
480 {
481 return hash_range(x, x + N);
482 }
483#endif
484
485 template <class Ch, class A>
486 inline std::size_t hash_value(
487 std::basic_string<Ch, std::BOOST_HASH_CHAR_TRAITS<Ch>, A> const& v)
488 {
489 return hash_range(v.begin(), v.end());
490 }
491
492#if BOOST_HASH_HAS_STRING_VIEW
493 template <class Ch>
494 inline std::size_t hash_value(
495 std::basic_string_view<Ch, std::BOOST_HASH_CHAR_TRAITS<Ch> > const& v)
496 {
497 return hash_range(v.begin(), v.end());
498 }
499#endif
500
501 template <typename T>
502 typename boost::hash_detail::float_numbers<T>::type hash_value(T v)
503 {
504 return boost::hash_detail::float_hash_value(v);
505 }
506
507#if BOOST_HASH_HAS_OPTIONAL
508 template <typename T>
509 inline std::size_t hash_value(std::optional<T> const& v) {
510 if (!v) {
511 // Arbitray value for empty optional.
512 return 0x12345678;
513 } else {
514 boost::hash<T> hf;
515 return hf(*v);
516 }
517 }
518#endif
519
520#if BOOST_HASH_HAS_VARIANT
521 inline std::size_t hash_value(std::monostate) {
522 return 0x87654321;
523 }
524
525 template <typename... Types>
526 inline std::size_t hash_value(std::variant<Types...> const& v) {
527 std::size_t seed = 0;
528 hash_combine(seed, v.index());
529 std::visit([&seed](auto&& x) { hash_combine(seed, x); }, v);
530 return seed;
531 }
532#endif
533
534
535#if !defined(BOOST_NO_CXX11_HDR_TYPEINDEX)
536 inline std::size_t hash_value(std::type_index v)
537 {
538 return v.hash_code();
539 }
540#endif
541
542#if !defined(BOOST_NO_CXX11_HDR_SYSTEM_ERROR)
543 inline std::size_t hash_value(std::error_code const& v) {
544 std::size_t seed = 0;
545 hash_combine(seed, v: v.value());
546 hash_combine(seed, v: &v.category());
547 return seed;
548 }
549
550 inline std::size_t hash_value(std::error_condition const& v) {
551 std::size_t seed = 0;
552 hash_combine(seed, v: v.value());
553 hash_combine(seed, v: &v.category());
554 return seed;
555 }
556#endif
557
558 //
559 // boost::hash
560 //
561
562 // Define the specializations required by the standard. The general purpose
563 // boost::hash is defined later in extensions.hpp if
564 // BOOST_HASH_NO_EXTENSIONS is not defined.
565
566 // BOOST_HASH_SPECIALIZE - define a specialization for a type which is
567 // passed by copy.
568 //
569 // BOOST_HASH_SPECIALIZE_REF - define a specialization for a type which is
570 // passed by const reference.
571 //
572 // These are undefined later.
573
574#define BOOST_HASH_SPECIALIZE(type) \
575 template <> struct hash<type> \
576 : public boost::hash_detail::hash_base<type> \
577 { \
578 std::size_t operator()(type v) const \
579 { \
580 return boost::hash_value(v); \
581 } \
582 };
583
584#define BOOST_HASH_SPECIALIZE_REF(type) \
585 template <> struct hash<type> \
586 : public boost::hash_detail::hash_base<type> \
587 { \
588 std::size_t operator()(type const& v) const \
589 { \
590 return boost::hash_value(v); \
591 } \
592 };
593
594#define BOOST_HASH_SPECIALIZE_TEMPLATE_REF(type) \
595 struct hash<type> \
596 : public boost::hash_detail::hash_base<type> \
597 { \
598 std::size_t operator()(type const& v) const \
599 { \
600 return boost::hash_value(v); \
601 } \
602 };
603
604 BOOST_HASH_SPECIALIZE(bool)
605 BOOST_HASH_SPECIALIZE(char)
606 BOOST_HASH_SPECIALIZE(signed char)
607 BOOST_HASH_SPECIALIZE(unsigned char)
608#if !defined(BOOST_NO_INTRINSIC_WCHAR_T)
609 BOOST_HASH_SPECIALIZE(wchar_t)
610#endif
611#if !defined(BOOST_NO_CXX11_CHAR16_T)
612 BOOST_HASH_SPECIALIZE(char16_t)
613#endif
614#if !defined(BOOST_NO_CXX11_CHAR32_T)
615 BOOST_HASH_SPECIALIZE(char32_t)
616#endif
617 BOOST_HASH_SPECIALIZE(short)
618 BOOST_HASH_SPECIALIZE(unsigned short)
619 BOOST_HASH_SPECIALIZE(int)
620 BOOST_HASH_SPECIALIZE(unsigned int)
621 BOOST_HASH_SPECIALIZE(long)
622 BOOST_HASH_SPECIALIZE(unsigned long)
623
624 BOOST_HASH_SPECIALIZE(float)
625 BOOST_HASH_SPECIALIZE(double)
626 BOOST_HASH_SPECIALIZE(long double)
627
628 BOOST_HASH_SPECIALIZE_REF(std::string)
629#if !defined(BOOST_NO_STD_WSTRING) && !defined(BOOST_NO_INTRINSIC_WCHAR_T)
630 BOOST_HASH_SPECIALIZE_REF(std::wstring)
631#endif
632#if !defined(BOOST_NO_CXX11_CHAR16_T)
633 BOOST_HASH_SPECIALIZE_REF(std::basic_string<char16_t>)
634#endif
635#if !defined(BOOST_NO_CXX11_CHAR32_T)
636 BOOST_HASH_SPECIALIZE_REF(std::basic_string<char32_t>)
637#endif
638
639#if BOOST_HASH_HAS_STRING_VIEW
640 BOOST_HASH_SPECIALIZE_REF(std::string_view)
641# if !defined(BOOST_NO_STD_WSTRING) && !defined(BOOST_NO_INTRINSIC_WCHAR_T)
642 BOOST_HASH_SPECIALIZE_REF(std::wstring_view)
643# endif
644# if !defined(BOOST_NO_CXX11_CHAR16_T)
645 BOOST_HASH_SPECIALIZE_REF(std::basic_string_view<char16_t>)
646# endif
647# if !defined(BOOST_NO_CXX11_CHAR32_T)
648 BOOST_HASH_SPECIALIZE_REF(std::basic_string_view<char32_t>)
649# endif
650#endif
651
652#if !defined(BOOST_NO_LONG_LONG)
653 BOOST_HASH_SPECIALIZE(boost::long_long_type)
654 BOOST_HASH_SPECIALIZE(boost::ulong_long_type)
655#endif
656
657#if defined(BOOST_HAS_INT128)
658 BOOST_HASH_SPECIALIZE(boost::int128_type)
659 BOOST_HASH_SPECIALIZE(boost::uint128_type)
660#endif
661
662#if BOOST_HASH_HAS_OPTIONAL
663 template <typename T>
664 BOOST_HASH_SPECIALIZE_TEMPLATE_REF(std::optional<T>)
665#endif
666
667#if !defined(BOOST_HASH_HAS_VARIANT)
668 template <typename... T>
669 BOOST_HASH_SPECIALIZE_TEMPLATE_REF(std::variant<T...>)
670 BOOST_HASH_SPECIALIZE(std::monostate)
671#endif
672
673#if !defined(BOOST_NO_CXX11_HDR_TYPEINDEX)
674 BOOST_HASH_SPECIALIZE(std::type_index)
675#endif
676
677#undef BOOST_HASH_SPECIALIZE
678#undef BOOST_HASH_SPECIALIZE_REF
679#undef BOOST_HASH_SPECIALIZE_TEMPLATE_REF
680
681// Specializing boost::hash for pointers.
682
683#if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
684
685 template <class T>
686 struct hash<T*>
687 : public boost::hash_detail::hash_base<T*>
688 {
689 std::size_t operator()(T* v) const
690 {
691#if !BOOST_WORKAROUND(__SUNPRO_CC, <= 0x590)
692 return boost::hash_value(v);
693#else
694 std::size_t x = static_cast<std::size_t>(
695 reinterpret_cast<std::ptrdiff_t>(v));
696
697 return x + (x >> 3);
698#endif
699 }
700 };
701
702#else
703
704 // For compilers without partial specialization, we define a
705 // boost::hash for all remaining types. But hash_impl is only defined
706 // for pointers in 'extensions.hpp' - so when BOOST_HASH_NO_EXTENSIONS
707 // is defined there will still be a compile error for types not supported
708 // in the standard.
709
710 namespace hash_detail
711 {
712 template <bool IsPointer>
713 struct hash_impl;
714
715 template <>
716 struct hash_impl<true>
717 {
718 template <class T>
719 struct inner
720 : public boost::hash_detail::hash_base<T>
721 {
722 std::size_t operator()(T val) const
723 {
724#if !BOOST_WORKAROUND(__SUNPRO_CC, <= 590)
725 return boost::hash_value(val);
726#else
727 std::size_t x = static_cast<std::size_t>(
728 reinterpret_cast<std::ptrdiff_t>(val));
729
730 return x + (x >> 3);
731#endif
732 }
733 };
734 };
735 }
736
737 template <class T> struct hash
738 : public boost::hash_detail::hash_impl<boost::is_pointer<T>::value>
739 ::BOOST_NESTED_TEMPLATE inner<T>
740 {
741 };
742
743#endif
744}
745
746#undef BOOST_HASH_CHAR_TRAITS
747#undef BOOST_FUNCTIONAL_HASH_ROTL32
748
749#if defined(BOOST_MSVC)
750#pragma warning(pop)
751#endif
752
753#endif // BOOST_FUNCTIONAL_HASH_HASH_HPP
754
755// Include this outside of the include guards in case the file is included
756// twice - once with BOOST_HASH_NO_EXTENSIONS defined, and then with it
757// undefined.
758
759#if !defined(BOOST_HASH_NO_EXTENSIONS) \
760 && !defined(BOOST_FUNCTIONAL_HASH_EXTENSIONS_HPP)
761#include <boost/container_hash/extensions.hpp>
762#endif
763

source code of include/boost/container_hash/hash.hpp