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