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