raw_hash_map.h 8.7 KB

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