hash_integral.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #ifndef BOOST_INTRUSIVE_DETAIL_HASH_INTEGRAL_HPP
  15. #define BOOST_INTRUSIVE_DETAIL_HASH_INTEGRAL_HPP
  16. #include <boost/config.hpp>
  17. #include "hash_mix.hpp"
  18. #include <cstddef>
  19. #include <climits>
  20. #include <boost/intrusive/detail/mpl.hpp>
  21. namespace boost {
  22. namespace intrusive {
  23. namespace detail {
  24. template<class T,
  25. bool bigger_than_size_t = (sizeof(T) > sizeof(std::size_t)),
  26. bool is_unsigned = is_unsigned<T>::value,
  27. std::size_t size_t_bits = sizeof(std::size_t) * CHAR_BIT,
  28. std::size_t type_bits = sizeof(T) * CHAR_BIT>
  29. struct hash_integral_impl;
  30. template<class T, bool is_unsigned, std::size_t size_t_bits, std::size_t type_bits>
  31. struct hash_integral_impl<T, false, is_unsigned, size_t_bits, type_bits>
  32. {
  33. static std::size_t fn( T v )
  34. {
  35. return static_cast<std::size_t>( v );
  36. }
  37. };
  38. template<class T, std::size_t size_t_bits, std::size_t type_bits>
  39. struct hash_integral_impl<T, true, false, size_t_bits, type_bits>
  40. {
  41. static std::size_t fn( T v )
  42. {
  43. typedef typename make_unsigned<T>::type U;
  44. if( v >= 0 )
  45. {
  46. return hash_integral_impl<U>::fn( static_cast<U>( v ) );
  47. }
  48. else
  49. {
  50. return ~hash_integral_impl<U>::fn( static_cast<U>( ~static_cast<U>( v ) ) );
  51. }
  52. }
  53. };
  54. template<class T>
  55. struct hash_integral_impl<T, true, true, 32, 64>
  56. {
  57. static std::size_t fn( T v )
  58. {
  59. std::size_t seed = 0;
  60. seed = static_cast<std::size_t>( v >> 32 ) + (hash_mix)( seed );
  61. seed = static_cast<std::size_t>( v & 0xFFFFFFFF ) + (hash_mix)( seed );
  62. return seed;
  63. }
  64. };
  65. template<class T>
  66. struct hash_integral_impl<T, true, true, 32, 128>
  67. {
  68. static std::size_t fn( T v )
  69. {
  70. std::size_t seed = 0;
  71. seed = static_cast<std::size_t>( v >> 96 ) + (hash_mix)( seed );
  72. seed = static_cast<std::size_t>( v >> 64 ) + (hash_mix)( seed );
  73. seed = static_cast<std::size_t>( v >> 32 ) + (hash_mix)( seed );
  74. seed = static_cast<std::size_t>( v ) + (hash_mix)( seed );
  75. return seed;
  76. }
  77. };
  78. template<class T>
  79. struct hash_integral_impl<T, true, true, 64, 128>
  80. {
  81. static std::size_t fn( T v )
  82. {
  83. std::size_t seed = 0;
  84. seed = static_cast<std::size_t>( v >> 64 ) + (hash_mix)( seed );
  85. seed = static_cast<std::size_t>( v ) + (hash_mix)( seed );
  86. return seed;
  87. }
  88. };
  89. template <typename T>
  90. typename enable_if_c<is_integral<T>::value, std::size_t>::type
  91. hash_value( T v )
  92. {
  93. return hash_integral_impl<T>::fn( v );
  94. }
  95. } // namespace detail
  96. } // namespace intrusive
  97. } // namespace boost
  98. #endif // #ifndef BOOST_INTRUSIVE_DETAIL_HASH_INTEGRAL_HPP