murmur.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #pragma once
  2. #include <util/system/defaults.h>
  3. #include <util/generic/array_ref.h>
  4. /*
  5. * murmur2 from http://murmurhash.googlepages.com/
  6. *
  7. */
  8. namespace NMurmurPrivate {
  9. Y_PURE_FUNCTION ui32 MurmurHash32(const void* key, size_t len, ui32 seed) noexcept;
  10. Y_PURE_FUNCTION ui64 MurmurHash64(const void* key, size_t len, ui64 seed) noexcept;
  11. template <unsigned N>
  12. struct TMurHelper;
  13. #define DEF_MUR(t) \
  14. template <> \
  15. struct TMurHelper<t> { \
  16. static inline ui##t MurmurHash(const void* buf, size_t len, ui##t init) noexcept { \
  17. return MurmurHash##t(buf, len, init); \
  18. } \
  19. };
  20. DEF_MUR(32)
  21. DEF_MUR(64)
  22. #undef DEF_MUR
  23. } // namespace NMurmurPrivate
  24. template <class T>
  25. inline T MurmurHash(const void* buf, size_t len, T init) noexcept {
  26. return (T)NMurmurPrivate::TMurHelper<8 * sizeof(T)>::MurmurHash(buf, len, init);
  27. }
  28. template <class T>
  29. inline T MurmurHash(const void* buf, size_t len) noexcept {
  30. return MurmurHash<T>(buf, len, (T)0);
  31. }
  32. template <typename TOut = size_t>
  33. struct TMurmurHash {
  34. TOut operator()(const void* buf, size_t len) const noexcept {
  35. return MurmurHash<TOut>(buf, len);
  36. }
  37. template <typename ElementType>
  38. TOut operator()(const TArrayRef<ElementType>& data) const noexcept {
  39. return operator()(data.data(), data.size() * sizeof(ElementType));
  40. }
  41. };