abseil-cpp/absl/container/internal/hash_function_defaults.h
Go to the documentation of this file.
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 namespace absl {
61 namespace container_internal {
62 
63 // The hash of an object of type T is computed by using absl::Hash.
64 template <class T, class E = void>
65 struct HashEq {
67  using Eq = std::equal_to<T>;
68 };
69 
70 struct StringHash {
71  using is_transparent = void;
72 
73  size_t operator()(absl::string_view v) const {
75  }
76  size_t operator()(const absl::Cord& v) const {
77  return absl::Hash<absl::Cord>{}(v);
78  }
79 };
80 
81 struct StringEq {
82  using is_transparent = void;
84  return lhs == rhs;
85  }
86  bool operator()(const absl::Cord& lhs, const absl::Cord& rhs) const {
87  return lhs == rhs;
88  }
89  bool operator()(const absl::Cord& lhs, absl::string_view rhs) const {
90  return lhs == rhs;
91  }
92  bool operator()(absl::string_view lhs, const absl::Cord& rhs) const {
93  return lhs == rhs;
94  }
95 };
96 
97 // Supports heterogeneous lookup for string-like elements.
98 struct StringHashEq {
99  using Hash = StringHash;
100  using Eq = StringEq;
101 };
102 
103 template <>
105 template <>
107 template <>
108 struct HashEq<absl::Cord> : StringHashEq {};
109 
110 // Supports heterogeneous lookup for pointers and smart pointers.
111 template <class T>
112 struct HashEq<T*> {
113  struct Hash {
114  using is_transparent = void;
115  template <class U>
116  size_t operator()(const U& ptr) const {
117  return absl::Hash<const T*>{}(HashEq::ToPtr(ptr));
118  }
119  };
120  struct Eq {
121  using is_transparent = void;
122  template <class A, class B>
123  bool operator()(const A& a, const B& b) const {
124  return HashEq::ToPtr(a) == HashEq::ToPtr(b);
125  }
126  };
127 
128  private:
129  static const T* ToPtr(const T* ptr) { return ptr; }
130  template <class U, class D>
131  static const T* ToPtr(const std::unique_ptr<U, D>& ptr) {
132  return ptr.get();
133  }
134  template <class U>
135  static const T* ToPtr(const std::shared_ptr<U>& ptr) {
136  return ptr.get();
137  }
138 };
139 
140 template <class T, class D>
141 struct HashEq<std::unique_ptr<T, D>> : HashEq<T*> {};
142 template <class T>
143 struct HashEq<std::shared_ptr<T>> : HashEq<T*> {};
144 
145 // This header's visibility is restricted. If you need to access the default
146 // hasher please use the container's ::hasher alias instead.
147 //
148 // Example: typename Hash = typename absl::flat_hash_map<K, V>::hasher
149 template <class T>
151 
152 // This header's visibility is restricted. If you need to access the default
153 // key equal please use the container's ::key_equal alias instead.
154 //
155 // Example: typename Eq = typename absl::flat_hash_map<K, V, Hash>::key_equal
156 template <class T>
158 
159 } // namespace container_internal
161 } // namespace absl
162 
163 #endif // ABSL_CONTAINER_INTERNAL_HASH_FUNCTION_DEFAULTS_H_
ptr
char * ptr
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:45
absl::Cord
Definition: abseil-cpp/absl/strings/cord.h:150
absl::hash_internal::Hash
Definition: abseil-cpp/absl/hash/internal/hash.h:1221
absl::container_internal::HashEq< T * >::Hash::operator()
size_t operator()(const U &ptr) const
Definition: abseil-cpp/absl/container/internal/hash_function_defaults.h:116
absl::container_internal::StringHashEq
Definition: abseil-cpp/absl/container/internal/hash_function_defaults.h:98
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
absl::container_internal::StringHashEq::Eq
Definition: bloaty/third_party/abseil-cpp/absl/container/internal/hash_function_defaults.h:84
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition: third_party/abseil-cpp/absl/base/config.h:171
T
#define T(upbtypeconst, upbtype, ctype, default_value)
absl::container_internal::HashEq< T * >::ToPtr
static const T * ToPtr(const std::unique_ptr< U, D > &ptr)
Definition: abseil-cpp/absl/container/internal/hash_function_defaults.h:131
absl::container_internal::StringHash
Definition: abseil-cpp/absl/container/internal/hash_function_defaults.h:70
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
absl::container_internal::StringHash::operator()
size_t operator()(absl::string_view v) const
Definition: abseil-cpp/absl/container/internal/hash_function_defaults.h:73
setup.v
v
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:42
absl::container_internal::StringEq
Definition: abseil-cpp/absl/container/internal/hash_function_defaults.h:81
testing::Eq
internal::EqMatcher< T > Eq(T x)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8561
absl::container_internal::HashEq< T * >::Eq::operator()
bool operator()(const A &a, const B &b) const
Definition: abseil-cpp/absl/container/internal/hash_function_defaults.h:123
absl::container_internal::StringEq::operator()
bool operator()(absl::string_view lhs, const absl::Cord &rhs) const
Definition: abseil-cpp/absl/container/internal/hash_function_defaults.h:92
absl::container_internal::HashEq::Eq
std::equal_to< T > Eq
Definition: abseil-cpp/absl/container/internal/hash_function_defaults.h:67
absl::container_internal::HashEq< T * >::ToPtr
static const T * ToPtr(const std::shared_ptr< U > &ptr)
Definition: abseil-cpp/absl/container/internal/hash_function_defaults.h:135
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
stdint.h
absl::container_internal::HashEq
Definition: abseil-cpp/absl/container/internal/hash_function_defaults.h:65
absl::container_internal::StringHash::operator()
size_t operator()(const absl::Cord &v) const
Definition: abseil-cpp/absl/container/internal/hash_function_defaults.h:76
absl::container_internal::HashEq< T * >::ToPtr
static const T * ToPtr(const T *ptr)
Definition: abseil-cpp/absl/container/internal/hash_function_defaults.h:129
absl::container_internal::StringEq::operator()
bool operator()(const absl::Cord &lhs, const absl::Cord &rhs) const
Definition: abseil-cpp/absl/container/internal/hash_function_defaults.h:86
absl::container_internal::hash_default_hash
typename container_internal::HashEq< T >::Hash hash_default_hash
Definition: abseil-cpp/absl/container/internal/hash_function_defaults.h:150
absl::container_internal::HashEq< T * >::Hash::is_transparent
void is_transparent
Definition: abseil-cpp/absl/container/internal/hash_function_defaults.h:114
absl::container_internal::StringHash::is_transparent
void is_transparent
Definition: abseil-cpp/absl/container/internal/hash_function_defaults.h:71
absl::container_internal::hash_default_eq
typename container_internal::HashEq< T >::Eq hash_default_eq
Definition: abseil-cpp/absl/container/internal/hash_function_defaults.h:157
bssl::acvp::StringEq
static bool StringEq(Span< const uint8_t > a, const char *b)
Definition: modulewrapper.cc:795
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
A
Definition: miscompile_with_no_unique_address_test.cc:23
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
absl::container_internal::HashEq< T * >::Eq::is_transparent
void is_transparent
Definition: abseil-cpp/absl/container/internal/hash_function_defaults.h:121
absl::container_internal::StringEq::operator()
bool operator()(const absl::Cord &lhs, absl::string_view rhs) const
Definition: abseil-cpp/absl/container/internal/hash_function_defaults.h:89
absl::container_internal::StringEq::operator()
bool operator()(absl::string_view lhs, absl::string_view rhs) const
Definition: abseil-cpp/absl/container/internal/hash_function_defaults.h:83
absl::Hash
absl::hash_internal::Hash< T > Hash
Definition: abseil-cpp/absl/hash/hash.h:247
absl::container_internal::StringEq::is_transparent
void is_transparent
Definition: abseil-cpp/absl/container/internal/hash_function_defaults.h:82


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:00