hash_function_defaults.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // Copyright 2018 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // Define the default Hash and Eq functions for SwissTable containers.
  16. //
  17. // std::hash<T> and std::equal_to<T> are not appropriate hash and equal
  18. // functions for SwissTable containers. There are two reasons for this.
  19. //
  20. // SwissTable containers are power of 2 sized containers:
  21. //
  22. // This means they use the lower bits of the hash value to find the slot for
  23. // each entry. The typical hash function for integral types is the identity.
  24. // This is a very weak hash function for SwissTable and any power of 2 sized
  25. // hashtable implementation which will lead to excessive collisions. For
  26. // SwissTable we use murmur3 style mixing to reduce collisions to a minimum.
  27. //
  28. // SwissTable containers support heterogeneous lookup:
  29. //
  30. // In order to make heterogeneous lookup work, hash and equal functions must be
  31. // polymorphic. At the same time they have to satisfy the same requirements the
  32. // C++ standard imposes on hash functions and equality operators. That is:
  33. //
  34. // if hash_default_eq<T>(a, b) returns true for any a and b of type T, then
  35. // hash_default_hash<T>(a) must equal hash_default_hash<T>(b)
  36. //
  37. // For SwissTable containers this requirement is relaxed to allow a and b of
  38. // any and possibly different types. Note that like the standard the hash and
  39. // equal functions are still bound to T. This is important because some type U
  40. // can be hashed by/tested for equality differently depending on T. A notable
  41. // example is `const char*`. `const char*` is treated as a c-style string when
  42. // the hash function is hash<TString> but as a pointer when the hash
  43. // function is hash<void*>.
  44. //
  45. #ifndef Y_ABSL_CONTAINER_INTERNAL_HASH_FUNCTION_DEFAULTS_H_
  46. #define Y_ABSL_CONTAINER_INTERNAL_HASH_FUNCTION_DEFAULTS_H_
  47. #include <stdint.h>
  48. #include <cstddef>
  49. #include <memory>
  50. #include <util/generic/string.h>
  51. #include <type_traits>
  52. #include "y_absl/base/config.h"
  53. #include "y_absl/hash/hash.h"
  54. #include "y_absl/strings/cord.h"
  55. #include "y_absl/strings/string_view.h"
  56. #ifdef Y_ABSL_HAVE_STD_STRING_VIEW
  57. #include <string_view>
  58. #endif
  59. namespace y_absl {
  60. Y_ABSL_NAMESPACE_BEGIN
  61. namespace container_internal {
  62. // The hash of an object of type T is computed by using y_absl::Hash.
  63. template <class T, class E = void>
  64. struct HashEq {
  65. using Hash = y_absl::Hash<T>;
  66. using Eq = std::equal_to<T>;
  67. };
  68. struct StringHash {
  69. using is_transparent = void;
  70. size_t operator()(y_absl::string_view v) const {
  71. return y_absl::Hash<y_absl::string_view>{}(v);
  72. }
  73. size_t operator()(const y_absl::Cord& v) const {
  74. return y_absl::Hash<y_absl::Cord>{}(v);
  75. }
  76. };
  77. struct StringEq {
  78. using is_transparent = void;
  79. bool operator()(y_absl::string_view lhs, y_absl::string_view rhs) const {
  80. return lhs == rhs;
  81. }
  82. bool operator()(const y_absl::Cord& lhs, const y_absl::Cord& rhs) const {
  83. return lhs == rhs;
  84. }
  85. bool operator()(const y_absl::Cord& lhs, y_absl::string_view rhs) const {
  86. return lhs == rhs;
  87. }
  88. bool operator()(y_absl::string_view lhs, const y_absl::Cord& rhs) const {
  89. return lhs == rhs;
  90. }
  91. };
  92. // Supports heterogeneous lookup for string-like elements.
  93. struct StringHashEq {
  94. using Hash = StringHash;
  95. using Eq = StringEq;
  96. };
  97. template <>
  98. struct HashEq<TString> : StringHashEq {};
  99. template <>
  100. struct HashEq<y_absl::string_view> : StringHashEq {};
  101. template <>
  102. struct HashEq<y_absl::Cord> : StringHashEq {};
  103. #ifdef Y_ABSL_HAVE_STD_STRING_VIEW
  104. template <typename TChar>
  105. struct BasicStringHash {
  106. using is_transparent = void;
  107. size_t operator()(std::basic_string_view<TChar> v) const {
  108. return y_absl::Hash<std::basic_string_view<TChar>>{}(v);
  109. }
  110. };
  111. template <typename TChar>
  112. struct BasicStringEq {
  113. using is_transparent = void;
  114. bool operator()(std::basic_string_view<TChar> lhs,
  115. std::basic_string_view<TChar> rhs) const {
  116. return lhs == rhs;
  117. }
  118. };
  119. // Supports heterogeneous lookup for w/u16/u32 string + string_view + char*.
  120. template <typename TChar>
  121. struct BasicStringHashEq {
  122. using Hash = BasicStringHash<TChar>;
  123. using Eq = BasicStringEq<TChar>;
  124. };
  125. template <>
  126. struct HashEq<std::wstring> : BasicStringHashEq<wchar_t> {};
  127. template <>
  128. struct HashEq<std::wstring_view> : BasicStringHashEq<wchar_t> {};
  129. template <>
  130. struct HashEq<std::u16string> : BasicStringHashEq<char16_t> {};
  131. template <>
  132. struct HashEq<std::u16string_view> : BasicStringHashEq<char16_t> {};
  133. template <>
  134. struct HashEq<std::u32string> : BasicStringHashEq<char32_t> {};
  135. template <>
  136. struct HashEq<std::u32string_view> : BasicStringHashEq<char32_t> {};
  137. #endif // Y_ABSL_HAVE_STD_STRING_VIEW
  138. // Supports heterogeneous lookup for pointers and smart pointers.
  139. template <class T>
  140. struct HashEq<T*> {
  141. struct Hash {
  142. using is_transparent = void;
  143. template <class U>
  144. size_t operator()(const U& ptr) const {
  145. return y_absl::Hash<const T*>{}(HashEq::ToPtr(ptr));
  146. }
  147. };
  148. struct Eq {
  149. using is_transparent = void;
  150. template <class A, class B>
  151. bool operator()(const A& a, const B& b) const {
  152. return HashEq::ToPtr(a) == HashEq::ToPtr(b);
  153. }
  154. };
  155. private:
  156. static const T* ToPtr(const T* ptr) { return ptr; }
  157. template <class U, class D>
  158. static const T* ToPtr(const std::unique_ptr<U, D>& ptr) {
  159. return ptr.get();
  160. }
  161. template <class U>
  162. static const T* ToPtr(const std::shared_ptr<U>& ptr) {
  163. return ptr.get();
  164. }
  165. };
  166. template <class T, class D>
  167. struct HashEq<std::unique_ptr<T, D>> : HashEq<T*> {};
  168. template <class T>
  169. struct HashEq<std::shared_ptr<T>> : HashEq<T*> {};
  170. // This header's visibility is restricted. If you need to access the default
  171. // hasher please use the container's ::hasher alias instead.
  172. //
  173. // Example: typename Hash = typename y_absl::flat_hash_map<K, V>::hasher
  174. template <class T>
  175. using hash_default_hash = typename container_internal::HashEq<T>::Hash;
  176. // This header's visibility is restricted. If you need to access the default
  177. // key equal please use the container's ::key_equal alias instead.
  178. //
  179. // Example: typename Eq = typename y_absl::flat_hash_map<K, V, Hash>::key_equal
  180. template <class T>
  181. using hash_default_eq = typename container_internal::HashEq<T>::Eq;
  182. } // namespace container_internal
  183. Y_ABSL_NAMESPACE_END
  184. } // namespace y_absl
  185. #endif // Y_ABSL_CONTAINER_INTERNAL_HASH_FUNCTION_DEFAULTS_H_