/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 <stdint.h> |
49 | | #include <cstddef> |
50 | | #include <memory> |
51 | | #include <string> |
52 | | #include <type_traits> |
53 | | |
54 | | #include "absl/base/config.h" |
55 | | #include "absl/hash/hash.h" |
56 | | #include "absl/strings/cord.h" |
57 | | #include "absl/strings/string_view.h" |
58 | | |
59 | | #ifdef ABSL_HAVE_STD_STRING_VIEW |
60 | | #include <string_view> |
61 | | #endif |
62 | | |
63 | | namespace absl { |
64 | | ABSL_NAMESPACE_BEGIN |
65 | | namespace container_internal { |
66 | | |
67 | | // The hash of an object of type T is computed by using absl::Hash. |
68 | | template <class T, class E = void> |
69 | | struct HashEq { |
70 | | using Hash = absl::Hash<T>; |
71 | | using Eq = std::equal_to<T>; |
72 | | }; |
73 | | |
74 | | struct StringHash { |
75 | | using is_transparent = void; |
76 | | |
77 | 62 | size_t operator()(absl::string_view v) const { |
78 | 62 | return absl::Hash<absl::string_view>{}(v); |
79 | 62 | } |
80 | 0 | size_t operator()(const absl::Cord& v) const { |
81 | 0 | return absl::Hash<absl::Cord>{}(v); |
82 | 0 | } |
83 | | }; |
84 | | |
85 | | struct StringEq { |
86 | | using is_transparent = void; |
87 | 20 | bool operator()(absl::string_view lhs, absl::string_view rhs) const { |
88 | 20 | return lhs == rhs; |
89 | 20 | } |
90 | 0 | bool operator()(const absl::Cord& lhs, const absl::Cord& rhs) const { |
91 | 0 | return lhs == rhs; |
92 | 0 | } |
93 | 0 | bool operator()(const absl::Cord& lhs, absl::string_view rhs) const { |
94 | 0 | return lhs == rhs; |
95 | 0 | } |
96 | 0 | bool operator()(absl::string_view lhs, const absl::Cord& rhs) const { |
97 | 0 | return lhs == rhs; |
98 | 0 | } |
99 | | }; |
100 | | |
101 | | // Supports heterogeneous lookup for string-like elements. |
102 | | struct StringHashEq { |
103 | | using Hash = StringHash; |
104 | | using Eq = StringEq; |
105 | | }; |
106 | | |
107 | | template <> |
108 | | struct HashEq<std::string> : StringHashEq {}; |
109 | | template <> |
110 | | struct HashEq<absl::string_view> : StringHashEq {}; |
111 | | template <> |
112 | | struct HashEq<absl::Cord> : StringHashEq {}; |
113 | | |
114 | | #ifdef ABSL_HAVE_STD_STRING_VIEW |
115 | | |
116 | | template <typename TChar> |
117 | | struct BasicStringHash { |
118 | | using is_transparent = void; |
119 | | |
120 | | size_t operator()(std::basic_string_view<TChar> v) const { |
121 | | return absl::Hash<std::basic_string_view<TChar>>{}(v); |
122 | | } |
123 | | }; |
124 | | |
125 | | template <typename TChar> |
126 | | struct BasicStringEq { |
127 | | using is_transparent = void; |
128 | | bool operator()(std::basic_string_view<TChar> lhs, |
129 | | std::basic_string_view<TChar> rhs) const { |
130 | | return lhs == rhs; |
131 | | } |
132 | | }; |
133 | | |
134 | | // Supports heterogeneous lookup for w/u16/u32 string + string_view + char*. |
135 | | template <typename TChar> |
136 | | struct BasicStringHashEq { |
137 | | using Hash = BasicStringHash<TChar>; |
138 | | using Eq = BasicStringEq<TChar>; |
139 | | }; |
140 | | |
141 | | template <> |
142 | | struct HashEq<std::wstring> : BasicStringHashEq<wchar_t> {}; |
143 | | template <> |
144 | | struct HashEq<std::wstring_view> : BasicStringHashEq<wchar_t> {}; |
145 | | template <> |
146 | | struct HashEq<std::u16string> : BasicStringHashEq<char16_t> {}; |
147 | | template <> |
148 | | struct HashEq<std::u16string_view> : BasicStringHashEq<char16_t> {}; |
149 | | template <> |
150 | | struct HashEq<std::u32string> : BasicStringHashEq<char32_t> {}; |
151 | | template <> |
152 | | struct HashEq<std::u32string_view> : BasicStringHashEq<char32_t> {}; |
153 | | |
154 | | #endif // ABSL_HAVE_STD_STRING_VIEW |
155 | | |
156 | | // Supports heterogeneous lookup for pointers and smart pointers. |
157 | | template <class T> |
158 | | struct HashEq<T*> { |
159 | | struct Hash { |
160 | | using is_transparent = void; |
161 | | template <class U> |
162 | | size_t operator()(const U& ptr) const { |
163 | | return absl::Hash<const T*>{}(HashEq::ToPtr(ptr)); |
164 | | } |
165 | | }; |
166 | | struct Eq { |
167 | | using is_transparent = void; |
168 | | template <class A, class B> |
169 | | bool operator()(const A& a, const B& b) const { |
170 | | return HashEq::ToPtr(a) == HashEq::ToPtr(b); |
171 | | } |
172 | | }; |
173 | | |
174 | | private: |
175 | | static const T* ToPtr(const T* ptr) { return ptr; } |
176 | | template <class U, class D> |
177 | | static const T* ToPtr(const std::unique_ptr<U, D>& ptr) { |
178 | | return ptr.get(); |
179 | | } |
180 | | template <class U> |
181 | | static const T* ToPtr(const std::shared_ptr<U>& ptr) { |
182 | | return ptr.get(); |
183 | | } |
184 | | }; |
185 | | |
186 | | template <class T, class D> |
187 | | struct HashEq<std::unique_ptr<T, D>> : HashEq<T*> {}; |
188 | | template <class T> |
189 | | struct HashEq<std::shared_ptr<T>> : HashEq<T*> {}; |
190 | | |
191 | | // This header's visibility is restricted. If you need to access the default |
192 | | // hasher please use the container's ::hasher alias instead. |
193 | | // |
194 | | // Example: typename Hash = typename absl::flat_hash_map<K, V>::hasher |
195 | | template <class T> |
196 | | using hash_default_hash = typename container_internal::HashEq<T>::Hash; |
197 | | |
198 | | // This header's visibility is restricted. If you need to access the default |
199 | | // key equal please use the container's ::key_equal alias instead. |
200 | | // |
201 | | // Example: typename Eq = typename absl::flat_hash_map<K, V, Hash>::key_equal |
202 | | template <class T> |
203 | | using hash_default_eq = typename container_internal::HashEq<T>::Eq; |
204 | | |
205 | | } // namespace container_internal |
206 | | ABSL_NAMESPACE_END |
207 | | } // namespace absl |
208 | | |
209 | | #endif // ABSL_CONTAINER_INTERNAL_HASH_FUNCTION_DEFAULTS_H_ |