raw_hash_map.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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_RAW_HASH_MAP_H_
  15. #define ABSL_CONTAINER_INTERNAL_RAW_HASH_MAP_H_
  16. #include <tuple>
  17. #include <type_traits>
  18. #include <utility>
  19. #include "absl/base/internal/throw_delegate.h"
  20. #include "absl/container/internal/container_memory.h"
  21. #include "absl/container/internal/raw_hash_set.h" // IWYU pragma: export
  22. namespace absl {
  23. 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. return insert_or_assign_impl(std::forward<K>(k), std::forward<V>(v));
  63. }
  64. template <class K = key_type, class V = mapped_type, K* = nullptr>
  65. std::pair<iterator, bool> insert_or_assign(key_arg<K>&& k, const V& v) {
  66. return insert_or_assign_impl(std::forward<K>(k), v);
  67. }
  68. template <class K = key_type, class V = mapped_type, V* = nullptr>
  69. std::pair<iterator, bool> insert_or_assign(const key_arg<K>& k, V&& v) {
  70. return insert_or_assign_impl(k, std::forward<V>(v));
  71. }
  72. template <class K = key_type, class V = mapped_type>
  73. std::pair<iterator, bool> insert_or_assign(const key_arg<K>& k, const V& v) {
  74. return insert_or_assign_impl(k, v);
  75. }
  76. template <class K = key_type, class V = mapped_type, K* = nullptr,
  77. V* = nullptr>
  78. iterator insert_or_assign(const_iterator, key_arg<K>&& k, V&& v) {
  79. return insert_or_assign(std::forward<K>(k), std::forward<V>(v)).first;
  80. }
  81. template <class K = key_type, class V = mapped_type, K* = nullptr>
  82. iterator insert_or_assign(const_iterator, key_arg<K>&& k, const V& v) {
  83. return insert_or_assign(std::forward<K>(k), v).first;
  84. }
  85. template <class K = key_type, class V = mapped_type, V* = nullptr>
  86. iterator insert_or_assign(const_iterator, const key_arg<K>& k, V&& v) {
  87. return insert_or_assign(k, std::forward<V>(v)).first;
  88. }
  89. template <class K = key_type, class V = mapped_type>
  90. iterator insert_or_assign(const_iterator, const key_arg<K>& k, const V& v) {
  91. return insert_or_assign(k, v).first;
  92. }
  93. // All `try_emplace()` overloads make the same guarantees regarding rvalue
  94. // arguments as `std::unordered_map::try_emplace()`, namely that these
  95. // functions will not move from rvalue arguments if insertions do not happen.
  96. template <class K = key_type, class... Args,
  97. typename std::enable_if<
  98. !std::is_convertible<K, const_iterator>::value, int>::type = 0,
  99. K* = nullptr>
  100. std::pair<iterator, bool> try_emplace(key_arg<K>&& k, Args&&... args) {
  101. return try_emplace_impl(std::forward<K>(k), std::forward<Args>(args)...);
  102. }
  103. template <class K = key_type, class... Args,
  104. typename std::enable_if<
  105. !std::is_convertible<K, const_iterator>::value, int>::type = 0>
  106. std::pair<iterator, bool> try_emplace(const key_arg<K>& k, Args&&... args) {
  107. return try_emplace_impl(k, std::forward<Args>(args)...);
  108. }
  109. template <class K = key_type, class... Args, K* = nullptr>
  110. iterator try_emplace(const_iterator, key_arg<K>&& k, Args&&... args) {
  111. return try_emplace(std::forward<K>(k), std::forward<Args>(args)...).first;
  112. }
  113. template <class K = key_type, class... Args>
  114. iterator try_emplace(const_iterator, const key_arg<K>& k, Args&&... args) {
  115. return try_emplace(k, std::forward<Args>(args)...).first;
  116. }
  117. template <class K = key_type, class P = Policy>
  118. MappedReference<P> at(const key_arg<K>& key) {
  119. auto it = this->find(key);
  120. if (it == this->end()) {
  121. base_internal::ThrowStdOutOfRange(
  122. "absl::container_internal::raw_hash_map<>::at");
  123. }
  124. return Policy::value(&*it);
  125. }
  126. template <class K = key_type, class P = Policy>
  127. MappedConstReference<P> at(const key_arg<K>& key) const {
  128. auto it = this->find(key);
  129. if (it == this->end()) {
  130. base_internal::ThrowStdOutOfRange(
  131. "absl::container_internal::raw_hash_map<>::at");
  132. }
  133. return Policy::value(&*it);
  134. }
  135. template <class K = key_type, class P = Policy, K* = nullptr>
  136. MappedReference<P> operator[](key_arg<K>&& key) {
  137. return Policy::value(&*try_emplace(std::forward<K>(key)).first);
  138. }
  139. template <class K = key_type, class P = Policy>
  140. MappedReference<P> operator[](const key_arg<K>& key) {
  141. return Policy::value(&*try_emplace(key).first);
  142. }
  143. private:
  144. template <class K, class V>
  145. std::pair<iterator, bool> insert_or_assign_impl(K&& k, V&& v) {
  146. auto res = this->find_or_prepare_insert(k);
  147. if (res.second)
  148. this->emplace_at(res.first, std::forward<K>(k), std::forward<V>(v));
  149. else
  150. Policy::value(&*this->iterator_at(res.first)) = std::forward<V>(v);
  151. return {this->iterator_at(res.first), res.second};
  152. }
  153. template <class K = key_type, class... Args>
  154. std::pair<iterator, bool> try_emplace_impl(K&& k, Args&&... args) {
  155. auto res = this->find_or_prepare_insert(k);
  156. if (res.second)
  157. this->emplace_at(res.first, std::piecewise_construct,
  158. std::forward_as_tuple(std::forward<K>(k)),
  159. std::forward_as_tuple(std::forward<Args>(args)...));
  160. return {this->iterator_at(res.first), res.second};
  161. }
  162. };
  163. } // namespace container_internal
  164. ABSL_NAMESPACE_END
  165. } // namespace absl
  166. #endif // ABSL_CONTAINER_INTERNAL_RAW_HASH_MAP_H_