hash_function_defaults.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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<std::string> but as a pointer when the hash
  43. // function is hash<void*>.
  44. //
  45. #ifndef ABSL_CONTAINER_INTERNAL_HASH_FUNCTION_DEFAULTS_H_
  46. #define ABSL_CONTAINER_INTERNAL_HASH_FUNCTION_DEFAULTS_H_
  47. #include <cstddef>
  48. #include <functional>
  49. #include <memory>
  50. #include <string>
  51. #include <type_traits>
  52. #include "absl/base/config.h"
  53. #include "absl/container/internal/common.h"
  54. #include "absl/hash/hash.h"
  55. #include "absl/meta/type_traits.h"
  56. #include "absl/strings/cord.h"
  57. #include "absl/strings/string_view.h"
  58. #ifdef ABSL_HAVE_STD_STRING_VIEW
  59. #include <string_view>
  60. #endif
  61. namespace absl {
  62. ABSL_NAMESPACE_BEGIN
  63. namespace container_internal {
  64. // The hash of an object of type T is computed by using absl::Hash.
  65. template <class T, class E = void>
  66. struct HashEq {
  67. using Hash = absl::Hash<T>;
  68. using Eq = std::equal_to<T>;
  69. };
  70. struct StringHash {
  71. using is_transparent = void;
  72. size_t operator()(absl::string_view v) const {
  73. return absl::Hash<absl::string_view>{}(v);
  74. }
  75. size_t operator()(const absl::Cord& v) const {
  76. return absl::Hash<absl::Cord>{}(v);
  77. }
  78. };
  79. struct StringEq {
  80. using is_transparent = void;
  81. bool operator()(absl::string_view lhs, absl::string_view rhs) const {
  82. return lhs == rhs;
  83. }
  84. bool operator()(const absl::Cord& lhs, const absl::Cord& rhs) const {
  85. return lhs == rhs;
  86. }
  87. bool operator()(const absl::Cord& lhs, absl::string_view rhs) const {
  88. return lhs == rhs;
  89. }
  90. bool operator()(absl::string_view lhs, const absl::Cord& rhs) const {
  91. return lhs == rhs;
  92. }
  93. };
  94. // Supports heterogeneous lookup for string-like elements.
  95. struct StringHashEq {
  96. using Hash = StringHash;
  97. using Eq = StringEq;
  98. };
  99. template <>
  100. struct HashEq<std::string> : StringHashEq {};
  101. template <>
  102. struct HashEq<absl::string_view> : StringHashEq {};
  103. template <>
  104. struct HashEq<absl::Cord> : StringHashEq {};
  105. #ifdef ABSL_HAVE_STD_STRING_VIEW
  106. template <typename TChar>
  107. struct BasicStringHash {
  108. using is_transparent = void;
  109. size_t operator()(std::basic_string_view<TChar> v) const {
  110. return absl::Hash<std::basic_string_view<TChar>>{}(v);
  111. }
  112. };
  113. template <typename TChar>
  114. struct BasicStringEq {
  115. using is_transparent = void;
  116. bool operator()(std::basic_string_view<TChar> lhs,
  117. std::basic_string_view<TChar> rhs) const {
  118. return lhs == rhs;
  119. }
  120. };
  121. // Supports heterogeneous lookup for w/u16/u32 string + string_view + char*.
  122. template <typename TChar>
  123. struct BasicStringHashEq {
  124. using Hash = BasicStringHash<TChar>;
  125. using Eq = BasicStringEq<TChar>;
  126. };
  127. template <>
  128. struct HashEq<std::wstring> : BasicStringHashEq<wchar_t> {};
  129. template <>
  130. struct HashEq<std::wstring_view> : BasicStringHashEq<wchar_t> {};
  131. template <>
  132. struct HashEq<std::u16string> : BasicStringHashEq<char16_t> {};
  133. template <>
  134. struct HashEq<std::u16string_view> : BasicStringHashEq<char16_t> {};
  135. template <>
  136. struct HashEq<std::u32string> : BasicStringHashEq<char32_t> {};
  137. template <>
  138. struct HashEq<std::u32string_view> : BasicStringHashEq<char32_t> {};
  139. #endif // ABSL_HAVE_STD_STRING_VIEW
  140. // Supports heterogeneous lookup for pointers and smart pointers.
  141. template <class T>
  142. struct HashEq<T*> {
  143. struct Hash {
  144. using is_transparent = void;
  145. template <class U>
  146. size_t operator()(const U& ptr) const {
  147. return absl::Hash<const T*>{}(HashEq::ToPtr(ptr));
  148. }
  149. };
  150. struct Eq {
  151. using is_transparent = void;
  152. template <class A, class B>
  153. bool operator()(const A& a, const B& b) const {
  154. return HashEq::ToPtr(a) == HashEq::ToPtr(b);
  155. }
  156. };
  157. private:
  158. static const T* ToPtr(const T* ptr) { return ptr; }
  159. template <class U, class D>
  160. static const T* ToPtr(const std::unique_ptr<U, D>& ptr) {
  161. return ptr.get();
  162. }
  163. template <class U>
  164. static const T* ToPtr(const std::shared_ptr<U>& ptr) {
  165. return ptr.get();
  166. }
  167. };
  168. template <class T, class D>
  169. struct HashEq<std::unique_ptr<T, D>> : HashEq<T*> {};
  170. template <class T>
  171. struct HashEq<std::shared_ptr<T>> : HashEq<T*> {};
  172. template <typename T, typename E = void>
  173. struct HasAbslContainerHash : std::false_type {};
  174. template <typename T>
  175. struct HasAbslContainerHash<T, absl::void_t<typename T::absl_container_hash>>
  176. : std::true_type {};
  177. template <typename T, typename E = void>
  178. struct HasAbslContainerEq : std::false_type {};
  179. template <typename T>
  180. struct HasAbslContainerEq<T, absl::void_t<typename T::absl_container_eq>>
  181. : std::true_type {};
  182. template <typename T, typename E = void>
  183. struct AbslContainerEq {
  184. using type = std::equal_to<>;
  185. };
  186. template <typename T>
  187. struct AbslContainerEq<
  188. T, typename std::enable_if_t<HasAbslContainerEq<T>::value>> {
  189. using type = typename T::absl_container_eq;
  190. };
  191. template <typename T, typename E = void>
  192. struct AbslContainerHash {
  193. using type = void;
  194. };
  195. template <typename T>
  196. struct AbslContainerHash<
  197. T, typename std::enable_if_t<HasAbslContainerHash<T>::value>> {
  198. using type = typename T::absl_container_hash;
  199. };
  200. // HashEq specialization for user types that provide `absl_container_hash` and
  201. // (optionally) `absl_container_eq`. This specialization allows user types to
  202. // provide heterogeneous lookup without requiring to explicitly specify Hash/Eq
  203. // type arguments in unordered Abseil containers.
  204. //
  205. // Both `absl_container_hash` and `absl_container_eq` should be transparent
  206. // (have inner is_transparent type). While there is no technical reason to
  207. // restrict to transparent-only types, there is also no feasible use case when
  208. // it shouldn't be transparent - it is easier to relax the requirement later if
  209. // such a case arises rather than restricting it.
  210. //
  211. // If type provides only `absl_container_hash` then `eq` part will be
  212. // `std::equal_to<void>`.
  213. //
  214. // User types are not allowed to provide only a `Eq` part as there is no
  215. // feasible use case for this behavior - if Hash should be a default one then Eq
  216. // should be an equivalent to the `std::equal_to<T>`.
  217. template <typename T>
  218. struct HashEq<T, typename std::enable_if_t<HasAbslContainerHash<T>::value>> {
  219. using Hash = typename AbslContainerHash<T>::type;
  220. using Eq = typename AbslContainerEq<T>::type;
  221. static_assert(IsTransparent<Hash>::value,
  222. "absl_container_hash must be transparent. To achieve it add a "
  223. "`using is_transparent = void;` clause to this type.");
  224. static_assert(IsTransparent<Eq>::value,
  225. "absl_container_eq must be transparent. To achieve it add a "
  226. "`using is_transparent = void;` clause to this type.");
  227. };
  228. // This header's visibility is restricted. If you need to access the default
  229. // hasher please use the container's ::hasher alias instead.
  230. //
  231. // Example: typename Hash = typename absl::flat_hash_map<K, V>::hasher
  232. template <class T>
  233. using hash_default_hash = typename container_internal::HashEq<T>::Hash;
  234. // This header's visibility is restricted. If you need to access the default
  235. // key equal please use the container's ::key_equal alias instead.
  236. //
  237. // Example: typename Eq = typename absl::flat_hash_map<K, V, Hash>::key_equal
  238. template <class T>
  239. using hash_default_eq = typename container_internal::HashEq<T>::Eq;
  240. } // namespace container_internal
  241. ABSL_NAMESPACE_END
  242. } // namespace absl
  243. #endif // ABSL_CONTAINER_INTERNAL_HASH_FUNCTION_DEFAULTS_H_