hash-inl.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #ifndef HASH_INL_H_
  2. #error "Direct inclusion of this file is not allowed, include hash.h"
  3. // For the sake of sane code completion.
  4. #include "hash.h"
  5. #endif
  6. #include <cmath>
  7. namespace NYT {
  8. ////////////////////////////////////////////////////////////////////////////////
  9. inline void HashCombine(size_t& h, size_t k)
  10. {
  11. static_assert(sizeof(size_t) == 8, "size_t must be 64 bit.");
  12. const size_t m = 0xc6a4a7935bd1e995ULL;
  13. const int r = 47;
  14. k *= m;
  15. k ^= k >> r;
  16. k *= m;
  17. h ^= k;
  18. h *= m;
  19. }
  20. template <class T>
  21. void HashCombine(size_t& h, const T& k)
  22. {
  23. HashCombine(h, THash<T>()(k));
  24. }
  25. template <class T>
  26. Y_FORCE_INLINE size_t NaNSafeHash(const T& value)
  27. {
  28. return ::THash<T>()(value);
  29. }
  30. template <class T>
  31. requires std::is_floating_point_v<T>
  32. Y_FORCE_INLINE size_t NaNSafeHash(const T& value)
  33. {
  34. return std::isnan(value) ? 0 : ::THash<T>()(value);
  35. }
  36. ////////////////////////////////////////////////////////////////////////////////
  37. template <class TElement, class TUnderlying>
  38. TRandomizedHash<TElement, TUnderlying>::TRandomizedHash()
  39. : Seed_(RandomNumber<size_t>())
  40. { }
  41. template <class TElement, class TUnderlying>
  42. size_t TRandomizedHash<TElement, TUnderlying>::operator ()(const TElement& element) const
  43. {
  44. return Underlying_(element) + Seed_;
  45. }
  46. ////////////////////////////////////////////////////////////////////////////////
  47. } // namespace NYT