hash_function_defaults.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 <stdint.h>
  48. #include <cstddef>
  49. #include <memory>
  50. #include <string>
  51. #include <type_traits>
  52. #include "absl/base/config.h"
  53. #include "absl/hash/hash.h"
  54. #include "absl/strings/cord.h"
  55. #include "absl/strings/string_view.h"
  56. namespace absl {
  57. ABSL_NAMESPACE_BEGIN
  58. namespace container_internal {
  59. // The hash of an object of type T is computed by using absl::Hash.
  60. template <class T, class E = void>
  61. struct HashEq {
  62. using Hash = absl::Hash<T>;
  63. using Eq = std::equal_to<T>;
  64. };
  65. struct StringHash {
  66. using is_transparent = void;
  67. size_t operator()(absl::string_view v) const {
  68. return absl::Hash<absl::string_view>{}(v);
  69. }
  70. size_t operator()(const absl::Cord& v) const {
  71. return absl::Hash<absl::Cord>{}(v);
  72. }
  73. };
  74. struct StringEq {
  75. using is_transparent = void;
  76. bool operator()(absl::string_view lhs, absl::string_view rhs) const {
  77. return lhs == rhs;
  78. }
  79. bool operator()(const absl::Cord& lhs, const absl::Cord& rhs) const {
  80. return lhs == rhs;
  81. }
  82. bool operator()(const absl::Cord& lhs, absl::string_view rhs) const {
  83. return lhs == rhs;
  84. }
  85. bool operator()(absl::string_view lhs, const absl::Cord& rhs) const {
  86. return lhs == rhs;
  87. }
  88. };
  89. // Supports heterogeneous lookup for string-like elements.
  90. struct StringHashEq {
  91. using Hash = StringHash;
  92. using Eq = StringEq;
  93. };
  94. template <>
  95. struct HashEq<std::string> : StringHashEq {};
  96. template <>
  97. struct HashEq<absl::string_view> : StringHashEq {};
  98. template <>
  99. struct HashEq<absl::Cord> : StringHashEq {};
  100. // Supports heterogeneous lookup for pointers and smart pointers.
  101. template <class T>
  102. struct HashEq<T*> {
  103. struct Hash {
  104. using is_transparent = void;
  105. template <class U>
  106. size_t operator()(const U& ptr) const {
  107. return absl::Hash<const T*>{}(HashEq::ToPtr(ptr));
  108. }
  109. };
  110. struct Eq {
  111. using is_transparent = void;
  112. template <class A, class B>
  113. bool operator()(const A& a, const B& b) const {
  114. return HashEq::ToPtr(a) == HashEq::ToPtr(b);
  115. }
  116. };
  117. private:
  118. static const T* ToPtr(const T* ptr) { return ptr; }
  119. template <class U, class D>
  120. static const T* ToPtr(const std::unique_ptr<U, D>& ptr) {
  121. return ptr.get();
  122. }
  123. template <class U>
  124. static const T* ToPtr(const std::shared_ptr<U>& ptr) {
  125. return ptr.get();
  126. }
  127. };
  128. template <class T, class D>
  129. struct HashEq<std::unique_ptr<T, D>> : HashEq<T*> {};
  130. template <class T>
  131. struct HashEq<std::shared_ptr<T>> : HashEq<T*> {};
  132. // This header's visibility is restricted. If you need to access the default
  133. // hasher please use the container's ::hasher alias instead.
  134. //
  135. // Example: typename Hash = typename absl::flat_hash_map<K, V>::hasher
  136. template <class T>
  137. using hash_default_hash = typename container_internal::HashEq<T>::Hash;
  138. // This header's visibility is restricted. If you need to access the default
  139. // key equal please use the container's ::key_equal alias instead.
  140. //
  141. // Example: typename Eq = typename absl::flat_hash_map<K, V, Hash>::key_equal
  142. template <class T>
  143. using hash_default_eq = typename container_internal::HashEq<T>::Eq;
  144. } // namespace container_internal
  145. ABSL_NAMESPACE_END
  146. } // namespace absl
  147. #endif // ABSL_CONTAINER_INTERNAL_HASH_FUNCTION_DEFAULTS_H_