// Copyright 2018 The Abseil Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #ifndef ABSL_CONTAINER_INTERNAL_RAW_HASH_MAP_H_ #define ABSL_CONTAINER_INTERNAL_RAW_HASH_MAP_H_ #include #include #include #include "absl/base/attributes.h" #include "absl/base/config.h" #include "absl/base/internal/throw_delegate.h" #include "absl/container/internal/container_memory.h" #include "absl/container/internal/raw_hash_set.h" // IWYU pragma: export namespace absl { ABSL_NAMESPACE_BEGIN namespace container_internal { template class raw_hash_map : public raw_hash_set { // P is Policy. It's passed as a template argument to support maps that have // incomplete types as values, as in unordered_map. // MappedReference<> may be a non-reference type. template using MappedReference = decltype(P::value( std::addressof(std::declval()))); // MappedConstReference<> may be a non-reference type. template using MappedConstReference = decltype(P::value( std::addressof(std::declval()))); using KeyArgImpl = KeyArg::value && IsTransparent::value>; public: using key_type = typename Policy::key_type; using mapped_type = typename Policy::mapped_type; template using key_arg = typename KeyArgImpl::template type; static_assert(!std::is_reference::value, ""); // TODO(b/187807849): Evaluate whether to support reference mapped_type and // remove this assertion if/when it is supported. static_assert(!std::is_reference::value, ""); using iterator = typename raw_hash_map::raw_hash_set::iterator; using const_iterator = typename raw_hash_map::raw_hash_set::const_iterator; raw_hash_map() {} using raw_hash_map::raw_hash_set::raw_hash_set; // The last two template parameters ensure that both arguments are rvalues // (lvalue arguments are handled by the overloads below). This is necessary // for supporting bitfield arguments. // // union { int n : 1; }; // flat_hash_map m; // m.insert_or_assign(n, n); template std::pair insert_or_assign(key_arg&& k, V&& v) ABSL_ATTRIBUTE_LIFETIME_BOUND { return insert_or_assign_impl(std::forward(k), std::forward(v)); } template std::pair insert_or_assign(key_arg&& k, const V& v) ABSL_ATTRIBUTE_LIFETIME_BOUND { return insert_or_assign_impl(std::forward(k), v); } template std::pair insert_or_assign(const key_arg& k, V&& v) ABSL_ATTRIBUTE_LIFETIME_BOUND { return insert_or_assign_impl(k, std::forward(v)); } template std::pair insert_or_assign(const key_arg& k, const V& v) ABSL_ATTRIBUTE_LIFETIME_BOUND { return insert_or_assign_impl(k, v); } template iterator insert_or_assign(const_iterator, key_arg&& k, V&& v) ABSL_ATTRIBUTE_LIFETIME_BOUND { return insert_or_assign(std::forward(k), std::forward(v)).first; } template iterator insert_or_assign(const_iterator, key_arg&& k, const V& v) ABSL_ATTRIBUTE_LIFETIME_BOUND { return insert_or_assign(std::forward(k), v).first; } template iterator insert_or_assign(const_iterator, const key_arg& k, V&& v) ABSL_ATTRIBUTE_LIFETIME_BOUND { return insert_or_assign(k, std::forward(v)).first; } template iterator insert_or_assign(const_iterator, const key_arg& k, const V& v) ABSL_ATTRIBUTE_LIFETIME_BOUND { return insert_or_assign(k, v).first; } // All `try_emplace()` overloads make the same guarantees regarding rvalue // arguments as `std::unordered_map::try_emplace()`, namely that these // functions will not move from rvalue arguments if insertions do not happen. template ::value, int>::type = 0, K* = nullptr> std::pair try_emplace(key_arg&& k, Args&&... args) ABSL_ATTRIBUTE_LIFETIME_BOUND { return try_emplace_impl(std::forward(k), std::forward(args)...); } template ::value, int>::type = 0> std::pair try_emplace(const key_arg& k, Args&&... args) ABSL_ATTRIBUTE_LIFETIME_BOUND { return try_emplace_impl(k, std::forward(args)...); } template iterator try_emplace(const_iterator, key_arg&& k, Args&&... args) ABSL_ATTRIBUTE_LIFETIME_BOUND { return try_emplace(std::forward(k), std::forward(args)...).first; } template iterator try_emplace(const_iterator, const key_arg& k, Args&&... args) ABSL_ATTRIBUTE_LIFETIME_BOUND { return try_emplace(k, std::forward(args)...).first; } template MappedReference

at(const key_arg& key) ABSL_ATTRIBUTE_LIFETIME_BOUND { auto it = this->find(key); if (it == this->end()) { base_internal::ThrowStdOutOfRange( "absl::container_internal::raw_hash_map<>::at"); } return Policy::value(&*it); } template MappedConstReference

at(const key_arg& key) const ABSL_ATTRIBUTE_LIFETIME_BOUND { auto it = this->find(key); if (it == this->end()) { base_internal::ThrowStdOutOfRange( "absl::container_internal::raw_hash_map<>::at"); } return Policy::value(&*it); } template MappedReference

operator[](key_arg&& key) ABSL_ATTRIBUTE_LIFETIME_BOUND { // It is safe to use unchecked_deref here because try_emplace // will always return an iterator pointing to a valid item in the table, // since it inserts if nothing is found for the given key. return Policy::value( &this->unchecked_deref(try_emplace(std::forward(key)).first)); } template MappedReference

operator[](const key_arg& key) ABSL_ATTRIBUTE_LIFETIME_BOUND { // It is safe to use unchecked_deref here because try_emplace // will always return an iterator pointing to a valid item in the table, // since it inserts if nothing is found for the given key. return Policy::value(&this->unchecked_deref(try_emplace(key).first)); } private: template std::pair insert_or_assign_impl(K&& k, V&& v) ABSL_ATTRIBUTE_LIFETIME_BOUND { auto res = this->find_or_prepare_insert(k); if (res.second) this->emplace_at(res.first, std::forward(k), std::forward(v)); else Policy::value(&*this->iterator_at(res.first)) = std::forward(v); return {this->iterator_at(res.first), res.second}; } template std::pair try_emplace_impl(K&& k, Args&&... args) ABSL_ATTRIBUTE_LIFETIME_BOUND { auto res = this->find_or_prepare_insert(k); if (res.second) this->emplace_at(res.first, std::piecewise_construct, std::forward_as_tuple(std::forward(k)), std::forward_as_tuple(std::forward(args)...)); return {this->iterator_at(res.first), res.second}; } }; } // namespace container_internal ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_CONTAINER_INTERNAL_RAW_HASH_MAP_H_