hash.hpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright 2021 Matt Borland. Distributed under the Boost
  3. // Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_MP_DETAIL_HASH_HPP
  6. #define BOOST_MP_DETAIL_HASH_HPP
  7. #include <cstddef>
  8. #include <functional>
  9. #include <boost/multiprecision/detail/standalone_config.hpp>
  10. namespace boost { namespace multiprecision { namespace detail {
  11. template <typename T>
  12. inline std::size_t hash_value(const T& v)
  13. {
  14. std::hash<T> hasher;
  15. return hasher(v);
  16. }
  17. #if defined(BOOST_HAS_INT128)
  18. std::size_t hash_value(const uint128_type& val);
  19. inline std::size_t hash_value(const int128_type& val)
  20. {
  21. return hash_value(static_cast<uint128_type>(val));
  22. }
  23. #endif
  24. inline void hash_combine(std::size_t&) {}
  25. template <typename T, typename... Args>
  26. inline void hash_combine(std::size_t& seed, const T& v, Args... args)
  27. {
  28. constexpr std::size_t adder = 0x9e3779b9;
  29. seed = seed ^ (hash_value(v) + adder + (seed<<6) + (seed>>2));
  30. hash_combine(seed, args...);
  31. }
  32. #if defined(BOOST_HAS_INT128)
  33. inline std::size_t hash_value(const uint128_type& val)
  34. {
  35. std::size_t result = static_cast<std::size_t>(val);
  36. hash_combine(result, static_cast<std::size_t>(val >> 64));
  37. return result;
  38. }
  39. #endif
  40. }}} // Namespaces
  41. #endif // BOOST_MP_DETAIL_HASH_HPP