1/////////////////////////////////////////////////////////////////////////////
2//
3// Copyright 2021-2023 Peter Dimov
4// Copyright 2024 Ion Gaztanaga
5// Distributed under the Boost Software License, Version 1.0.
6// https://www.boost.org/LICENSE_1_0.txt
7//
8// The original C++11 implementation was done by Peter Dimov
9// The C++03 porting was done by Ion Gaztanaga
10//
11// See http://www.boost.org/libs/intrusive for documentation.
12//
13/////////////////////////////////////////////////////////////////////////////
14
15
16#ifndef BOOST_INTRUSIVE_DETAIL_HASH_INTEGRAL_HPP
17#define BOOST_INTRUSIVE_DETAIL_HASH_INTEGRAL_HPP
18
19#include <boost/config.hpp>
20#include "hash_mix.hpp"
21#include <cstddef>
22#include <climits>
23#include <boost/intrusive/detail/mpl.hpp>
24
25namespace boost {
26namespace intrusive {
27namespace detail {
28
29
30template<class T,
31 bool bigger_than_size_t = (sizeof(T) > sizeof(std::size_t)),
32 bool is_unsigned = is_unsigned<T>::value,
33 std::size_t size_t_bits = sizeof(std::size_t) * CHAR_BIT,
34 std::size_t type_bits = sizeof(T) * CHAR_BIT>
35struct hash_integral_impl;
36
37template<class T, bool is_unsigned, std::size_t size_t_bits, std::size_t type_bits>
38struct hash_integral_impl<T, false, is_unsigned, size_t_bits, type_bits>
39{
40 static std::size_t fn( T v )
41 {
42 return static_cast<std::size_t>( v );
43 }
44};
45
46template<class T, std::size_t size_t_bits, std::size_t type_bits>
47struct hash_integral_impl<T, true, false, size_t_bits, type_bits>
48{
49 static std::size_t fn( T v )
50 {
51 typedef typename make_unsigned<T>::type U;
52
53 if( v >= 0 )
54 {
55 return hash_integral_impl<U>::fn( static_cast<U>( v ) );
56 }
57 else
58 {
59 return ~hash_integral_impl<U>::fn( static_cast<U>( ~static_cast<U>( v ) ) );
60 }
61 }
62};
63
64template<class T>
65struct hash_integral_impl<T, true, true, 32, 64>
66{
67 static std::size_t fn( T v )
68 {
69 std::size_t seed = 0;
70
71 seed = static_cast<std::size_t>( v >> 32 ) + (hash_mix)( v: seed );
72 seed = static_cast<std::size_t>( v & 0xFFFFFFFF ) + (hash_mix)( v: seed );
73
74 return seed;
75 }
76};
77
78template<class T>
79struct hash_integral_impl<T, true, true, 32, 128>
80{
81 static std::size_t fn( T v )
82 {
83 std::size_t seed = 0;
84
85 seed = static_cast<std::size_t>( v >> 96 ) + (hash_mix)( v: seed );
86 seed = static_cast<std::size_t>( v >> 64 ) + (hash_mix)( v: seed );
87 seed = static_cast<std::size_t>( v >> 32 ) + (hash_mix)( v: seed );
88 seed = static_cast<std::size_t>( v ) + (hash_mix)( v: seed );
89
90 return seed;
91 }
92};
93
94template<class T>
95struct hash_integral_impl<T, true, true, 64, 128>
96{
97 static std::size_t fn( T v )
98 {
99 std::size_t seed = 0;
100
101 seed = static_cast<std::size_t>( v >> 64 ) + (hash_mix)( v: seed );
102 seed = static_cast<std::size_t>( v ) + (hash_mix)( v: seed );
103
104 return seed;
105 }
106};
107
108template <typename T>
109typename enable_if_c<is_integral<T>::value, std::size_t>::type
110 hash_value( T v )
111{
112 return hash_integral_impl<T>::fn( v );
113}
114
115} // namespace detail
116} // namespace intrusive
117} // namespace boost
118
119#endif // #ifndef BOOST_INTRUSIVE_DETAIL_HASH_INTEGRAL_HPP
120

source code of boost/libs/intrusive/include/boost/intrusive/detail/hash_integral.hpp