Coverage Report

Created: 2026-10-28 06:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/abseil-cpp/absl/container/internal/hash_function_defaults.h
Line
Count
Source
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
 private:
134
  friend struct absl::hash_internal::HashWithSeed;
135
136
  size_t hash_with_seed(std::basic_string_view<TChar> v, size_t seed) const {
137
    return absl::hash_internal::HashWithSeed().hash(
138
        absl::Hash<std::basic_string_view<TChar>>{}, v, seed);
139
  }
140
};
141
142
template <typename TChar>
143
struct BasicStringEq {
144
  using is_transparent = void;
145
  bool operator()(std::basic_string_view<TChar> lhs,
146
                  std::basic_string_view<TChar> rhs) const {
147
    return lhs == rhs;
148
  }
149
};
150
151
// Supports heterogeneous lookup for w/u8/u16/u32 string + string_view + char*.
152
template <typename TChar>
153
struct BasicStringHashEq {
154
  using Hash = BasicStringHash<TChar>;
155
  using Eq = BasicStringEq<TChar>;
156
};
157
158
template <>
159
struct HashEq<std::wstring> : BasicStringHashEq<wchar_t> {};
160
template <>
161
struct HashEq<std::wstring_view> : BasicStringHashEq<wchar_t> {};
162
#ifdef __cpp_char8_t
163
template <>
164
struct HashEq<std::u8string> : BasicStringHashEq<char8_t> {};
165
template <>
166
struct HashEq<std::u8string_view> : BasicStringHashEq<char8_t> {};
167
#endif
168
template <>
169
struct HashEq<std::u16string> : BasicStringHashEq<char16_t> {};
170
template <>
171
struct HashEq<std::u16string_view> : BasicStringHashEq<char16_t> {};
172
template <>
173
struct HashEq<std::u32string> : BasicStringHashEq<char32_t> {};
174
template <>
175
struct HashEq<std::u32string_view> : BasicStringHashEq<char32_t> {};
176
177
// Supports heterogeneous lookup for pointers and smart pointers.
178
template <class T>
179
struct HashEq<T*> {
180
  struct Hash {
181
    using is_transparent = void;
182
    template <class U>
183
    size_t operator()(const U& ptr) const {
184
      return absl::Hash<const T*>{}(HashEq::ToPtr(ptr));
185
    }
186
187
   private:
188
    friend struct absl::hash_internal::HashWithSeed;
189
190
    template <class U>
191
    size_t hash_with_seed(const U& ptr, size_t seed) const {
192
      return absl::hash_internal::HashWithSeed().hash(absl::Hash<const T*>{},
193
                                                      HashEq::ToPtr(ptr), seed);
194
    }
195
  };
196
  struct Eq {
197
    using is_transparent = void;
198
    template <class A, class B>
199
    bool operator()(const A& a, const B& b) const {
200
      return HashEq::ToPtr(a) == HashEq::ToPtr(b);
201
    }
202
  };
203
204
 private:
205
  static const T* ToPtr(const T* ptr) { return ptr; }
206
  template <class U, class D>
207
  static const T* ToPtr(const std::unique_ptr<U, D>& ptr) {
208
    return ptr.get();
209
  }
210
  template <class U>
211
  static const T* ToPtr(const std::shared_ptr<U>& ptr) {
212
    return ptr.get();
213
  }
214
};
215
216
template <class T, class D>
217
struct HashEq<std::unique_ptr<T, D>> : HashEq<T*> {};
218
template <class T>
219
struct HashEq<std::shared_ptr<T>> : HashEq<T*> {};
220
221
template <typename T, typename E = void>
222
struct HasAbslContainerHash : std::false_type {};
223
224
template <typename T>
225
struct HasAbslContainerHash<T, std::void_t<typename T::absl_container_hash>>
226
    : std::true_type {};
227
228
template <typename T, typename E = void>
229
struct HasAbslContainerEq : std::false_type {};
230
231
template <typename T>
232
struct HasAbslContainerEq<T, std::void_t<typename T::absl_container_eq>>
233
    : std::true_type {};
234
235
template <typename T, typename E = void>
236
struct AbslContainerEq {
237
  using type = std::equal_to<>;
238
};
239
240
template <typename T>
241
struct AbslContainerEq<
242
    T, typename std::enable_if_t<HasAbslContainerEq<T>::value>> {
243
  using type = typename T::absl_container_eq;
244
};
245
246
template <typename T, typename E = void>
247
struct AbslContainerHash {
248
  using type = void;
249
};
250
251
template <typename T>
252
struct AbslContainerHash<
253
    T, typename std::enable_if_t<HasAbslContainerHash<T>::value>> {
254
  using type = typename T::absl_container_hash;
255
};
256
257
// HashEq specialization for user types that provide `absl_container_hash` and
258
// (optionally) `absl_container_eq`. This specialization allows user types to
259
// provide heterogeneous lookup without requiring to explicitly specify Hash/Eq
260
// type arguments in unordered Abseil containers.
261
//
262
// Both `absl_container_hash` and `absl_container_eq` should be transparent
263
// (have inner is_transparent type). While there is no technical reason to
264
// restrict to transparent-only types, there is also no feasible use case when
265
// it shouldn't be transparent - it is easier to relax the requirement later if
266
// such a case arises rather than restricting it.
267
//
268
// If type provides only `absl_container_hash` then `eq` part will be
269
// `std::equal_to<void>`.
270
//
271
// User types are not allowed to provide only a `Eq` part as there is no
272
// feasible use case for this behavior - if Hash should be a default one then Eq
273
// should be an equivalent to the `std::equal_to<T>`.
274
template <typename T>
275
struct HashEq<T, typename std::enable_if_t<HasAbslContainerHash<T>::value>> {
276
  using Hash = typename AbslContainerHash<T>::type;
277
  using Eq = typename AbslContainerEq<T>::type;
278
  static_assert(IsTransparent<Hash>::value,
279
                "absl_container_hash must be transparent. To achieve it add a "
280
                "`using is_transparent = void;` clause to this type.");
281
  static_assert(IsTransparent<Eq>::value,
282
                "absl_container_eq must be transparent. To achieve it add a "
283
                "`using is_transparent = void;` clause to this type.");
284
};
285
286
// This header's visibility is restricted.  If you need to access the default
287
// hasher please use the container's ::hasher alias instead.
288
//
289
// Example: typename Hash = typename absl::flat_hash_map<K, V>::hasher
290
template <class T>
291
using hash_default_hash = typename container_internal::HashEq<T>::Hash;
292
293
// This header's visibility is restricted.  If you need to access the default
294
// key equal please use the container's ::key_equal alias instead.
295
//
296
// Example: typename Eq = typename absl::flat_hash_map<K, V, Hash>::key_equal
297
template <class T>
298
using hash_default_eq = typename container_internal::HashEq<T>::Eq;
299
300
}  // namespace container_internal
301
ABSL_NAMESPACE_END
302
}  // namespace absl
303
304
#endif  // ABSL_CONTAINER_INTERNAL_HASH_FUNCTION_DEFAULTS_H_