farm_hash.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #pragma once
  2. #include <contrib/libs/farmhash/farmhash.h>
  3. #include <util/system/types.h>
  4. #include <util/generic/strbuf.h>
  5. namespace NYT {
  6. ////////////////////////////////////////////////////////////////////////////////
  7. using TFingerprint = ui64;
  8. static inline TFingerprint FarmHash(ui64 value)
  9. {
  10. return ::util::Fingerprint(value);
  11. }
  12. static inline TFingerprint FarmHash(const void* buf, size_t len)
  13. {
  14. return ::util::Hash64(static_cast<const char*>(buf), len);
  15. }
  16. static inline TFingerprint FarmHash(const void* buf, size_t len, ui64 seed)
  17. {
  18. return ::util::Hash64WithSeed(static_cast<const char*>(buf), len, seed);
  19. }
  20. static inline TFingerprint FarmFingerprint(ui64 value)
  21. {
  22. return ::util::Fingerprint(value);
  23. }
  24. static inline TFingerprint FarmFingerprint(const void* buf, size_t len)
  25. {
  26. return ::util::Fingerprint64(static_cast<const char*>(buf), len);
  27. }
  28. static inline TFingerprint FarmFingerprint(TStringBuf buf)
  29. {
  30. return FarmFingerprint(buf.Data(), buf.Size());
  31. }
  32. static inline TFingerprint FarmFingerprint(ui64 first, ui64 second)
  33. {
  34. return ::util::Fingerprint(::util::Uint128(first, second));
  35. }
  36. // Forever-fixed Google FarmHash fingerprint.
  37. template <class T>
  38. TFingerprint FarmFingerprint(const T* begin, const T* end)
  39. {
  40. ui64 result = 0xdeadc0de;
  41. for (const auto* value = begin; value < end; ++value) {
  42. result = FarmFingerprint(result, FarmFingerprint(*value));
  43. }
  44. return result ^ (end - begin);
  45. }
  46. ////////////////////////////////////////////////////////////////////////////////
  47. } // namespace NYT