hash.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #pragma once
  2. #include <unordered_set>
  3. #include <unordered_map>
  4. #include <util/generic/hash.h>
  5. #include <util/generic/hash_multi_map.h>
  6. #include <util/generic/hash_set.h>
  7. namespace NYql {
  8. #ifndef NDEBUG
  9. size_t VaryingHash(size_t src);
  10. #else
  11. inline size_t VaryingHash(size_t src) {
  12. return src;
  13. }
  14. #endif
  15. template <typename T, typename THasher = ::THash<T>>
  16. struct TVaryingHash {
  17. THasher Underlying;
  18. TVaryingHash() = default;
  19. TVaryingHash(const TVaryingHash&) = default;
  20. TVaryingHash(const THasher& underlying)
  21. : Underlying(underlying)
  22. {}
  23. TVaryingHash& operator=(const TVaryingHash& other) = default;
  24. size_t operator()(const T& elem) const {
  25. return VaryingHash(Underlying(elem));
  26. }
  27. };
  28. template <class TKey,
  29. class TValue,
  30. class THasher = std::hash<TKey>,
  31. class TEqual = std::equal_to<TKey>,
  32. class TAlloc = std::allocator<std::pair<const TKey, TValue>>>
  33. using TVaryingUnorderedMap = std::unordered_map<TKey, TValue, TVaryingHash<TKey, THasher>, TEqual, TAlloc>;
  34. template <class TKey,
  35. class TValue,
  36. class THasher = std::hash<TKey>,
  37. class TEqual = std::equal_to<TKey>,
  38. class TAlloc = std::allocator<std::pair<const TKey, TValue>>>
  39. using TVaryingUnorderedMultiMap = std::unordered_multimap<TKey, TValue, TVaryingHash<TKey, THasher>, TEqual, TAlloc>;
  40. template <class TKey,
  41. class THasher = std::hash<TKey>,
  42. class TEqual = std::equal_to<TKey>,
  43. class TAlloc = std::allocator<TKey>>
  44. using TVaryingUnorderedSet = std::unordered_set<TKey, TVaryingHash<TKey, THasher>, TEqual, TAlloc>;
  45. template <class TKey,
  46. class THasher = std::hash<TKey>,
  47. class TEqual = std::equal_to<TKey>,
  48. class TAlloc = std::allocator<TKey>>
  49. using TVaryingUnorderedMultiSet = std::unordered_multiset<TKey, TVaryingHash<TKey, THasher>, TEqual, TAlloc>;
  50. template <class TKey,
  51. class TValue,
  52. class THasher = THash<TKey>,
  53. class TEqual = TEqualTo<TKey>,
  54. class TAlloc = std::allocator<std::pair<const TKey, TValue>>>
  55. using TVaryingHashMap = THashMap<TKey, TValue, TVaryingHash<TKey, THasher>, TEqual, TAlloc>;
  56. template <class TKey,
  57. class TValue,
  58. class THasher = THash<TKey>,
  59. class TEqual = TEqualTo<TKey>,
  60. class TAlloc = std::allocator<std::pair<const TKey, TValue>>>
  61. using TVaryingHashMultiMap = THashMultiMap<TKey, TValue, TVaryingHash<TKey, THasher>, TEqual, TAlloc>;
  62. template <class TKey,
  63. class THasher = THash<TKey>,
  64. class TEqual = TEqualTo<TKey>,
  65. class TAlloc = std::allocator<TKey>>
  66. using TVaryingHashSet = THashSet<TKey, TVaryingHash<TKey, THasher>, TEqual, TAlloc>;
  67. template <class TKey,
  68. class THasher = THash<TKey>,
  69. class TEqual = TEqualTo<TKey>,
  70. class TAlloc = std::allocator<TKey>>
  71. using TVaryingHashMultiSet = THashMultiSet<TKey, TVaryingHash<TKey, THasher>, TEqual, TAlloc>;
  72. } // namespace NYql