raw_hash_map.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 Y_ABSL_CONTAINER_INTERNAL_RAW_HASH_MAP_H_
  15. #define Y_ABSL_CONTAINER_INTERNAL_RAW_HASH_MAP_H_
  16. #include <tuple>
  17. #include <type_traits>
  18. #include <utility>
  19. #include "y_absl/base/internal/throw_delegate.h"
  20. #include "y_absl/container/internal/container_memory.h"
  21. #include "y_absl/container/internal/raw_hash_set.h" // IWYU pragma: export
  22. namespace y_absl {
  23. Y_ABSL_NAMESPACE_BEGIN
  24. namespace container_internal {
  25. template <class Policy, class Hash, class Eq, class Alloc>
  26. class raw_hash_map : public raw_hash_set<Policy, Hash, Eq, Alloc> {
  27. // P is Policy. It's passed as a template argument to support maps that have
  28. // incomplete types as values, as in unordered_map<K, IncompleteType>.
  29. // MappedReference<> may be a non-reference type.
  30. template <class P>
  31. using MappedReference = decltype(P::value(
  32. std::addressof(std::declval<typename raw_hash_map::reference>())));
  33. // MappedConstReference<> may be a non-reference type.
  34. template <class P>
  35. using MappedConstReference = decltype(P::value(
  36. std::addressof(std::declval<typename raw_hash_map::const_reference>())));
  37. using KeyArgImpl =
  38. KeyArg<IsTransparent<Eq>::value && IsTransparent<Hash>::value>;
  39. public:
  40. using key_type = typename Policy::key_type;
  41. using mapped_type = typename Policy::mapped_type;
  42. template <class K>
  43. using key_arg = typename KeyArgImpl::template type<K, key_type>;
  44. static_assert(!std::is_reference<key_type>::value, "");
  45. // TODO(b/187807849): Evaluate whether to support reference mapped_type and
  46. // remove this assertion if/when it is supported.
  47. static_assert(!std::is_reference<mapped_type>::value, "");
  48. using iterator = typename raw_hash_map::raw_hash_set::iterator;
  49. using const_iterator = typename raw_hash_map::raw_hash_set::const_iterator;
  50. raw_hash_map() {}
  51. using raw_hash_map::raw_hash_set::raw_hash_set;
  52. // The last two template parameters ensure that both arguments are rvalues
  53. // (lvalue arguments are handled by the overloads below). This is necessary
  54. // for supporting bitfield arguments.
  55. //
  56. // union { int n : 1; };
  57. // flat_hash_map<int, int> m;
  58. // m.insert_or_assign(n, n);
  59. template <class K = key_type, class V = mapped_type, K* = nullptr,
  60. V* = nullptr>
  61. std::pair<iterator, bool> insert_or_assign(key_arg<K>&& k, V&& v)
  62. Y_ABSL_ATTRIBUTE_LIFETIME_BOUND {
  63. return insert_or_assign_impl(std::forward<K>(k), std::forward<V>(v));
  64. }
  65. template <class K = key_type, class V = mapped_type, K* = nullptr>
  66. std::pair<iterator, bool> insert_or_assign(key_arg<K>&& k, const V& v)
  67. Y_ABSL_ATTRIBUTE_LIFETIME_BOUND {
  68. return insert_or_assign_impl(std::forward<K>(k), v);
  69. }
  70. template <class K = key_type, class V = mapped_type, V* = nullptr>
  71. std::pair<iterator, bool> insert_or_assign(const key_arg<K>& k, V&& v)
  72. Y_ABSL_ATTRIBUTE_LIFETIME_BOUND {
  73. return insert_or_assign_impl(k, std::forward<V>(v));
  74. }
  75. template <class K = key_type, class V = mapped_type>
  76. std::pair<iterator, bool> insert_or_assign(const key_arg<K>& k, const V& v)
  77. Y_ABSL_ATTRIBUTE_LIFETIME_BOUND {
  78. return insert_or_assign_impl(k, v);
  79. }
  80. template <class K = key_type, class V = mapped_type, K* = nullptr,
  81. V* = nullptr>
  82. iterator insert_or_assign(const_iterator, key_arg<K>&& k,
  83. V&& v) Y_ABSL_ATTRIBUTE_LIFETIME_BOUND {
  84. return insert_or_assign(std::forward<K>(k), std::forward<V>(v)).first;
  85. }
  86. template <class K = key_type, class V = mapped_type, K* = nullptr>
  87. iterator insert_or_assign(const_iterator, key_arg<K>&& k,
  88. const V& v) Y_ABSL_ATTRIBUTE_LIFETIME_BOUND {
  89. return insert_or_assign(std::forward<K>(k), v).first;
  90. }
  91. template <class K = key_type, class V = mapped_type, V* = nullptr>
  92. iterator insert_or_assign(const_iterator, const key_arg<K>& k,
  93. V&& v) Y_ABSL_ATTRIBUTE_LIFETIME_BOUND {
  94. return insert_or_assign(k, std::forward<V>(v)).first;
  95. }
  96. template <class K = key_type, class V = mapped_type>
  97. iterator insert_or_assign(const_iterator, const key_arg<K>& k,
  98. const V& v) Y_ABSL_ATTRIBUTE_LIFETIME_BOUND {
  99. return insert_or_assign(k, v).first;
  100. }
  101. // All `try_emplace()` overloads make the same guarantees regarding rvalue
  102. // arguments as `std::unordered_map::try_emplace()`, namely that these
  103. // functions will not move from rvalue arguments if insertions do not happen.
  104. template <class K = key_type, class... Args,
  105. typename std::enable_if<
  106. !std::is_convertible<K, const_iterator>::value, int>::type = 0,
  107. K* = nullptr>
  108. std::pair<iterator, bool> try_emplace(key_arg<K>&& k, Args&&... args)
  109. Y_ABSL_ATTRIBUTE_LIFETIME_BOUND {
  110. return try_emplace_impl(std::forward<K>(k), std::forward<Args>(args)...);
  111. }
  112. template <class K = key_type, class... Args,
  113. typename std::enable_if<
  114. !std::is_convertible<K, const_iterator>::value, int>::type = 0>
  115. std::pair<iterator, bool> try_emplace(const key_arg<K>& k, Args&&... args)
  116. Y_ABSL_ATTRIBUTE_LIFETIME_BOUND {
  117. return try_emplace_impl(k, std::forward<Args>(args)...);
  118. }
  119. template <class K = key_type, class... Args, K* = nullptr>
  120. iterator try_emplace(const_iterator, key_arg<K>&& k,
  121. Args&&... args) Y_ABSL_ATTRIBUTE_LIFETIME_BOUND {
  122. return try_emplace(std::forward<K>(k), std::forward<Args>(args)...).first;
  123. }
  124. template <class K = key_type, class... Args>
  125. iterator try_emplace(const_iterator, const key_arg<K>& k,
  126. Args&&... args) Y_ABSL_ATTRIBUTE_LIFETIME_BOUND {
  127. return try_emplace(k, std::forward<Args>(args)...).first;
  128. }
  129. template <class K = key_type, class P = Policy>
  130. MappedReference<P> at(const key_arg<K>& key) Y_ABSL_ATTRIBUTE_LIFETIME_BOUND {
  131. auto it = this->find(key);
  132. if (it == this->end()) {
  133. base_internal::ThrowStdOutOfRange(
  134. "y_absl::container_internal::raw_hash_map<>::at");
  135. }
  136. return Policy::value(&*it);
  137. }
  138. template <class K = key_type, class P = Policy>
  139. MappedConstReference<P> at(const key_arg<K>& key) const
  140. Y_ABSL_ATTRIBUTE_LIFETIME_BOUND {
  141. auto it = this->find(key);
  142. if (it == this->end()) {
  143. base_internal::ThrowStdOutOfRange(
  144. "y_absl::container_internal::raw_hash_map<>::at");
  145. }
  146. return Policy::value(&*it);
  147. }
  148. template <class K = key_type, class P = Policy, K* = nullptr>
  149. MappedReference<P> operator[](key_arg<K>&& key)
  150. Y_ABSL_ATTRIBUTE_LIFETIME_BOUND {
  151. return Policy::value(&*try_emplace(std::forward<K>(key)).first);
  152. }
  153. template <class K = key_type, class P = Policy>
  154. MappedReference<P> operator[](const key_arg<K>& key)
  155. Y_ABSL_ATTRIBUTE_LIFETIME_BOUND {
  156. return Policy::value(&*try_emplace(key).first);
  157. }
  158. private:
  159. template <class K, class V>
  160. std::pair<iterator, bool> insert_or_assign_impl(K&& k, V&& v)
  161. Y_ABSL_ATTRIBUTE_LIFETIME_BOUND {
  162. auto res = this->find_or_prepare_insert(k);
  163. if (res.second)
  164. this->emplace_at(res.first, std::forward<K>(k), std::forward<V>(v));
  165. else
  166. Policy::value(&*this->iterator_at(res.first)) = std::forward<V>(v);
  167. return {this->iterator_at(res.first), res.second};
  168. }
  169. template <class K = key_type, class... Args>
  170. std::pair<iterator, bool> try_emplace_impl(K&& k, Args&&... args)
  171. Y_ABSL_ATTRIBUTE_LIFETIME_BOUND {
  172. auto res = this->find_or_prepare_insert(k);
  173. if (res.second)
  174. this->emplace_at(res.first, std::piecewise_construct,
  175. std::forward_as_tuple(std::forward<K>(k)),
  176. std::forward_as_tuple(std::forward<Args>(args)...));
  177. return {this->iterator_at(res.first), res.second};
  178. }
  179. };
  180. } // namespace container_internal
  181. Y_ABSL_NAMESPACE_END
  182. } // namespace y_absl
  183. #endif // Y_ABSL_CONTAINER_INTERNAL_RAW_HASH_MAP_H_