hash.h 1.0 KB

123456789101112131415161718192021222324252627282930
  1. #pragma once
  2. #include <cstddef>
  3. #include <cstdint>
  4. #include <utility> // IWYU pragma: keep std::hash
  5. // NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers,readability-identifier-length)
  6. namespace jvalidate::compat {
  7. // Taken from boostorg/container_hash
  8. // https://github.com/boostorg/container_hash/blob/2698b43803c012601e6bb1a6116e83767b97986c/include/boost/container_hash/detail/hash_mix.hpp#L67-L81
  9. inline std::size_t hash_mix(std::size_t x) {
  10. static_assert(sizeof(std::size_t) == 8);
  11. constexpr static std::uint64_t m = 0xe9846af9b1a615d;
  12. x ^= x >> 32;
  13. x *= m;
  14. x ^= x >> 32;
  15. x *= m;
  16. x ^= x >> 28;
  17. return x;
  18. }
  19. // Taken from boostorg/container_hash
  20. // https://github.com/boostorg/container_hash/blob/2698b43803c012601e6bb1a6116e83767b97986c/include/boost/container_hash/hash.hpp#L474-L477
  21. template <class T> inline void hash_combine(std::size_t & seed, T const & v) {
  22. seed = hash_mix(seed + 0x9e3779b9 + std::hash<T>()(v));
  23. }
  24. }
  25. // NOLINTEND(cppcoreguidelines-avoid-magic-numbers,readability-identifier-length)