hash_policy_traits.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. #ifndef ABSL_CONTAINER_INTERNAL_HASH_POLICY_TRAITS_H_
  15. #define ABSL_CONTAINER_INTERNAL_HASH_POLICY_TRAITS_H_
  16. #include <cstddef>
  17. #include <memory>
  18. #include <new>
  19. #include <type_traits>
  20. #include <utility>
  21. #include "absl/meta/type_traits.h"
  22. namespace absl {
  23. ABSL_NAMESPACE_BEGIN
  24. namespace container_internal {
  25. // Defines how slots are initialized/destroyed/moved.
  26. template <class Policy, class = void>
  27. struct hash_policy_traits {
  28. // The type of the keys stored in the hashtable.
  29. using key_type = typename Policy::key_type;
  30. private:
  31. struct ReturnKey {
  32. // When C++17 is available, we can use std::launder to provide mutable
  33. // access to the key for use in node handle.
  34. #if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606
  35. template <class Key,
  36. absl::enable_if_t<std::is_lvalue_reference<Key>::value, int> = 0>
  37. static key_type& Impl(Key&& k, int) {
  38. return *std::launder(
  39. const_cast<key_type*>(std::addressof(std::forward<Key>(k))));
  40. }
  41. #endif
  42. template <class Key>
  43. static Key Impl(Key&& k, char) {
  44. return std::forward<Key>(k);
  45. }
  46. // When Key=T&, we forward the lvalue reference.
  47. // When Key=T, we return by value to avoid a dangling reference.
  48. // eg, for string_hash_map.
  49. template <class Key, class... Args>
  50. auto operator()(Key&& k, const Args&...) const
  51. -> decltype(Impl(std::forward<Key>(k), 0)) {
  52. return Impl(std::forward<Key>(k), 0);
  53. }
  54. };
  55. template <class P = Policy, class = void>
  56. struct ConstantIteratorsImpl : std::false_type {};
  57. template <class P>
  58. struct ConstantIteratorsImpl<P, absl::void_t<typename P::constant_iterators>>
  59. : P::constant_iterators {};
  60. public:
  61. // The actual object stored in the hash table.
  62. using slot_type = typename Policy::slot_type;
  63. // The argument type for insertions into the hashtable. This is different
  64. // from value_type for increased performance. See initializer_list constructor
  65. // and insert() member functions for more details.
  66. using init_type = typename Policy::init_type;
  67. using reference = decltype(Policy::element(std::declval<slot_type*>()));
  68. using pointer = typename std::remove_reference<reference>::type*;
  69. using value_type = typename std::remove_reference<reference>::type;
  70. // Policies can set this variable to tell raw_hash_set that all iterators
  71. // should be constant, even `iterator`. This is useful for set-like
  72. // containers.
  73. // Defaults to false if not provided by the policy.
  74. using constant_iterators = ConstantIteratorsImpl<>;
  75. // PRECONDITION: `slot` is UNINITIALIZED
  76. // POSTCONDITION: `slot` is INITIALIZED
  77. template <class Alloc, class... Args>
  78. static void construct(Alloc* alloc, slot_type* slot, Args&&... args) {
  79. Policy::construct(alloc, slot, std::forward<Args>(args)...);
  80. }
  81. // PRECONDITION: `slot` is INITIALIZED
  82. // POSTCONDITION: `slot` is UNINITIALIZED
  83. template <class Alloc>
  84. static void destroy(Alloc* alloc, slot_type* slot) {
  85. Policy::destroy(alloc, slot);
  86. }
  87. // Transfers the `old_slot` to `new_slot`. Any memory allocated by the
  88. // allocator inside `old_slot` to `new_slot` can be transferred.
  89. //
  90. // OPTIONAL: defaults to:
  91. //
  92. // clone(new_slot, std::move(*old_slot));
  93. // destroy(old_slot);
  94. //
  95. // PRECONDITION: `new_slot` is UNINITIALIZED and `old_slot` is INITIALIZED
  96. // POSTCONDITION: `new_slot` is INITIALIZED and `old_slot` is
  97. // UNINITIALIZED
  98. template <class Alloc>
  99. static void transfer(Alloc* alloc, slot_type* new_slot, slot_type* old_slot) {
  100. transfer_impl(alloc, new_slot, old_slot, 0);
  101. }
  102. // PRECONDITION: `slot` is INITIALIZED
  103. // POSTCONDITION: `slot` is INITIALIZED
  104. template <class P = Policy>
  105. static auto element(slot_type* slot) -> decltype(P::element(slot)) {
  106. return P::element(slot);
  107. }
  108. // Returns the amount of memory owned by `slot`, exclusive of `sizeof(*slot)`.
  109. //
  110. // If `slot` is nullptr, returns the constant amount of memory owned by any
  111. // full slot or -1 if slots own variable amounts of memory.
  112. //
  113. // PRECONDITION: `slot` is INITIALIZED or nullptr
  114. template <class P = Policy>
  115. static size_t space_used(const slot_type* slot) {
  116. return P::space_used(slot);
  117. }
  118. // Provides generalized access to the key for elements, both for elements in
  119. // the table and for elements that have not yet been inserted (or even
  120. // constructed). We would like an API that allows us to say: `key(args...)`
  121. // but we cannot do that for all cases, so we use this more general API that
  122. // can be used for many things, including the following:
  123. //
  124. // - Given an element in a table, get its key.
  125. // - Given an element initializer, get its key.
  126. // - Given `emplace()` arguments, get the element key.
  127. //
  128. // Implementations of this must adhere to a very strict technical
  129. // specification around aliasing and consuming arguments:
  130. //
  131. // Let `value_type` be the result type of `element()` without ref- and
  132. // cv-qualifiers. The first argument is a functor, the rest are constructor
  133. // arguments for `value_type`. Returns `std::forward<F>(f)(k, xs...)`, where
  134. // `k` is the element key, and `xs...` are the new constructor arguments for
  135. // `value_type`. It's allowed for `k` to alias `xs...`, and for both to alias
  136. // `ts...`. The key won't be touched once `xs...` are used to construct an
  137. // element; `ts...` won't be touched at all, which allows `apply()` to consume
  138. // any rvalues among them.
  139. //
  140. // If `value_type` is constructible from `Ts&&...`, `Policy::apply()` must not
  141. // trigger a hard compile error unless it originates from `f`. In other words,
  142. // `Policy::apply()` must be SFINAE-friendly. If `value_type` is not
  143. // constructible from `Ts&&...`, either SFINAE or a hard compile error is OK.
  144. //
  145. // If `Ts...` is `[cv] value_type[&]` or `[cv] init_type[&]`,
  146. // `Policy::apply()` must work. A compile error is not allowed, SFINAE or not.
  147. template <class F, class... Ts, class P = Policy>
  148. static auto apply(F&& f, Ts&&... ts)
  149. -> decltype(P::apply(std::forward<F>(f), std::forward<Ts>(ts)...)) {
  150. return P::apply(std::forward<F>(f), std::forward<Ts>(ts)...);
  151. }
  152. // Returns the "key" portion of the slot.
  153. // Used for node handle manipulation.
  154. template <class P = Policy>
  155. static auto mutable_key(slot_type* slot)
  156. -> decltype(P::apply(ReturnKey(), element(slot))) {
  157. return P::apply(ReturnKey(), element(slot));
  158. }
  159. // Returns the "value" (as opposed to the "key") portion of the element. Used
  160. // by maps to implement `operator[]`, `at()` and `insert_or_assign()`.
  161. template <class T, class P = Policy>
  162. static auto value(T* elem) -> decltype(P::value(elem)) {
  163. return P::value(elem);
  164. }
  165. private:
  166. // Use auto -> decltype as an enabler.
  167. template <class Alloc, class P = Policy>
  168. static auto transfer_impl(Alloc* alloc, slot_type* new_slot,
  169. slot_type* old_slot, int)
  170. -> decltype((void)P::transfer(alloc, new_slot, old_slot)) {
  171. P::transfer(alloc, new_slot, old_slot);
  172. }
  173. template <class Alloc>
  174. static void transfer_impl(Alloc* alloc, slot_type* new_slot,
  175. slot_type* old_slot, char) {
  176. construct(alloc, new_slot, std::move(element(old_slot)));
  177. destroy(alloc, old_slot);
  178. }
  179. };
  180. } // namespace container_internal
  181. ABSL_NAMESPACE_END
  182. } // namespace absl
  183. #endif // ABSL_CONTAINER_INTERNAL_HASH_POLICY_TRAITS_H_