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