// Copyright (C) 2022-2023 Christian Mazakas // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #ifndef BOOST_UNORDERED_UNORDERED_NODE_MAP_HPP_INCLUDED #define BOOST_UNORDERED_UNORDERED_NODE_MAP_HPP_INCLUDED #include #if defined(BOOST_HAS_PRAGMA_ONCE) #pragma once #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace boost { namespace unordered { #if defined(BOOST_MSVC) #pragma warning(push) #pragma warning(disable : 4714) /* marked as __forceinline not inlined */ #endif namespace detail { template struct node_map_handle : public detail::foa::node_handle_base { private: using base_type = detail::foa::node_handle_base; using typename base_type::type_policy; template friend class boost::unordered::unordered_node_map; public: using key_type = typename TypePolicy::key_type; using mapped_type = typename TypePolicy::mapped_type; constexpr node_map_handle() noexcept = default; node_map_handle(node_map_handle&& nh) noexcept = default; node_map_handle& operator=(node_map_handle&&) noexcept = default; key_type& key() const { BOOST_ASSERT(!this->empty()); return const_cast(this->data().first); } mapped_type& mapped() const { BOOST_ASSERT(!this->empty()); return const_cast(this->data().second); } }; } // namespace detail template class unordered_node_map { using map_types = detail::foa::node_map_types::type>; using table_type = detail::foa::table >::type>; table_type table_; template bool friend operator==(unordered_node_map const& lhs, unordered_node_map const& rhs); template typename unordered_node_map::size_type friend erase_if( unordered_node_map& set, Pred pred); public: using key_type = Key; using mapped_type = T; using value_type = typename map_types::value_type; using init_type = typename map_types::init_type; using size_type = std::size_t; using difference_type = std::ptrdiff_t; using hasher = typename boost::unordered::detail::type_identity::type; using key_equal = typename boost::unordered::detail::type_identity::type; using allocator_type = typename boost::unordered::detail::type_identity::type; using reference = value_type&; using const_reference = value_type const&; using pointer = typename boost::allocator_pointer::type; using const_pointer = typename boost::allocator_const_pointer::type; using iterator = typename table_type::iterator; using const_iterator = typename table_type::const_iterator; using node_type = detail::node_map_handle::type>; using insert_return_type = detail::foa::insert_return_type; #if defined(BOOST_UNORDERED_ENABLE_STATS) using stats = typename table_type::stats; #endif unordered_node_map() : unordered_node_map(0) {} explicit unordered_node_map(size_type n, hasher const& h = hasher(), key_equal const& pred = key_equal(), allocator_type const& a = allocator_type()) : table_(n, h, pred, a) { } unordered_node_map(size_type n, allocator_type const& a) : unordered_node_map(n, hasher(), key_equal(), a) { } unordered_node_map(size_type n, hasher const& h, allocator_type const& a) : unordered_node_map(n, h, key_equal(), a) { } template unordered_node_map( InputIterator f, InputIterator l, allocator_type const& a) : unordered_node_map(f, l, size_type(0), hasher(), key_equal(), a) { } explicit unordered_node_map(allocator_type const& a) : unordered_node_map(0, a) { } template unordered_node_map(Iterator first, Iterator last, size_type n = 0, hasher const& h = hasher(), key_equal const& pred = key_equal(), allocator_type const& a = allocator_type()) : unordered_node_map(n, h, pred, a) { this->insert(first, last); } template unordered_node_map( Iterator first, Iterator last, size_type n, allocator_type const& a) : unordered_node_map(first, last, n, hasher(), key_equal(), a) { } template unordered_node_map(Iterator first, Iterator last, size_type n, hasher const& h, allocator_type const& a) : unordered_node_map(first, last, n, h, key_equal(), a) { } unordered_node_map(unordered_node_map const& other) : table_(other.table_) { } unordered_node_map( unordered_node_map const& other, allocator_type const& a) : table_(other.table_, a) { } unordered_node_map(unordered_node_map&& other) noexcept(std::is_nothrow_move_constructible::value) : table_(std::move(other.table_)) { } unordered_node_map(unordered_node_map&& other, allocator_type const& al) : table_(std::move(other.table_), al) { } unordered_node_map(std::initializer_list ilist, size_type n = 0, hasher const& h = hasher(), key_equal const& pred = key_equal(), allocator_type const& a = allocator_type()) : unordered_node_map(ilist.begin(), ilist.end(), n, h, pred, a) { } unordered_node_map( std::initializer_list il, allocator_type const& a) : unordered_node_map(il, size_type(0), hasher(), key_equal(), a) { } unordered_node_map(std::initializer_list init, size_type n, allocator_type const& a) : unordered_node_map(init, n, hasher(), key_equal(), a) { } unordered_node_map(std::initializer_list init, size_type n, hasher const& h, allocator_type const& a) : unordered_node_map(init, n, h, key_equal(), a) { } ~unordered_node_map() = default; unordered_node_map& operator=(unordered_node_map const& other) { table_ = other.table_; return *this; } unordered_node_map& operator=(unordered_node_map&& other) noexcept( noexcept(std::declval() = std::declval())) { table_ = std::move(other.table_); return *this; } allocator_type get_allocator() const noexcept { return table_.get_allocator(); } /// Iterators /// iterator begin() noexcept { return table_.begin(); } const_iterator begin() const noexcept { return table_.begin(); } const_iterator cbegin() const noexcept { return table_.cbegin(); } iterator end() noexcept { return table_.end(); } const_iterator end() const noexcept { return table_.end(); } const_iterator cend() const noexcept { return table_.cend(); } /// Capacity /// BOOST_ATTRIBUTE_NODISCARD bool empty() const noexcept { return table_.empty(); } size_type size() const noexcept { return table_.size(); } size_type max_size() const noexcept { return table_.max_size(); } /// Modifiers /// void clear() noexcept { table_.clear(); } template BOOST_FORCEINLINE auto insert(Ty&& value) -> decltype(table_.insert(std::forward(value))) { return table_.insert(std::forward(value)); } BOOST_FORCEINLINE std::pair insert(init_type&& value) { return table_.insert(std::move(value)); } template BOOST_FORCEINLINE auto insert(const_iterator, Ty&& value) -> decltype(table_.insert(std::forward(value)).first) { return table_.insert(std::forward(value)).first; } BOOST_FORCEINLINE iterator insert(const_iterator, init_type&& value) { return table_.insert(std::move(value)).first; } template BOOST_FORCEINLINE void insert(InputIterator first, InputIterator last) { for (auto pos = first; pos != last; ++pos) { table_.emplace(*pos); } } void insert(std::initializer_list ilist) { this->insert(ilist.begin(), ilist.end()); } insert_return_type insert(node_type&& nh) { if (nh.empty()) { return {end(), false, node_type{}}; } BOOST_ASSERT(get_allocator() == nh.get_allocator()); auto itp = table_.insert(std::move(nh.element())); if (itp.second) { nh.reset(); return {itp.first, true, node_type{}}; } else { return {itp.first, false, std::move(nh)}; } } iterator insert(const_iterator, node_type&& nh) { if (nh.empty()) { return end(); } BOOST_ASSERT(get_allocator() == nh.get_allocator()); auto itp = table_.insert(std::move(nh.element())); if (itp.second) { nh.reset(); return itp.first; } else { return itp.first; } } template std::pair insert_or_assign(key_type const& key, M&& obj) { auto ibp = table_.try_emplace(key, std::forward(obj)); if (ibp.second) { return ibp; } ibp.first->second = std::forward(obj); return ibp; } template std::pair insert_or_assign(key_type&& key, M&& obj) { auto ibp = table_.try_emplace(std::move(key), std::forward(obj)); if (ibp.second) { return ibp; } ibp.first->second = std::forward(obj); return ibp; } template typename std::enable_if< boost::unordered::detail::are_transparent::value, std::pair >::type insert_or_assign(K&& k, M&& obj) { auto ibp = table_.try_emplace(std::forward(k), std::forward(obj)); if (ibp.second) { return ibp; } ibp.first->second = std::forward(obj); return ibp; } template iterator insert_or_assign(const_iterator, key_type const& key, M&& obj) { return this->insert_or_assign(key, std::forward(obj)).first; } template iterator insert_or_assign(const_iterator, key_type&& key, M&& obj) { return this->insert_or_assign(std::move(key), std::forward(obj)) .first; } template typename std::enable_if< boost::unordered::detail::are_transparent::value, iterator>::type insert_or_assign(const_iterator, K&& k, M&& obj) { return this->insert_or_assign(std::forward(k), std::forward(obj)) .first; } template BOOST_FORCEINLINE std::pair emplace(Args&&... args) { return table_.emplace(std::forward(args)...); } template BOOST_FORCEINLINE iterator emplace_hint(const_iterator, Args&&... args) { return table_.emplace(std::forward(args)...).first; } template BOOST_FORCEINLINE std::pair try_emplace( key_type const& key, Args&&... args) { return table_.try_emplace(key, std::forward(args)...); } template BOOST_FORCEINLINE std::pair try_emplace( key_type&& key, Args&&... args) { return table_.try_emplace(std::move(key), std::forward(args)...); } template BOOST_FORCEINLINE typename std::enable_if< boost::unordered::detail::transparent_non_iterable::value, std::pair >::type try_emplace(K&& key, Args&&... args) { return table_.try_emplace( std::forward(key), std::forward(args)...); } template BOOST_FORCEINLINE iterator try_emplace( const_iterator, key_type const& key, Args&&... args) { return table_.try_emplace(key, std::forward(args)...).first; } template BOOST_FORCEINLINE iterator try_emplace( const_iterator, key_type&& key, Args&&... args) { return table_.try_emplace(std::move(key), std::forward(args)...) .first; } template BOOST_FORCEINLINE typename std::enable_if< boost::unordered::detail::transparent_non_iterable::value, iterator>::type try_emplace(const_iterator, K&& key, Args&&... args) { return table_ .try_emplace(std::forward(key), std::forward(args)...) .first; } BOOST_FORCEINLINE typename table_type::erase_return_type erase( iterator pos) { return table_.erase(pos); } BOOST_FORCEINLINE typename table_type::erase_return_type erase( const_iterator pos) { return table_.erase(pos); } iterator erase(const_iterator first, const_iterator last) { while (first != last) { this->erase(first++); } return iterator{detail::foa::const_iterator_cast_tag{}, last}; } BOOST_FORCEINLINE size_type erase(key_type const& key) { return table_.erase(key); } template BOOST_FORCEINLINE typename std::enable_if< detail::transparent_non_iterable::value, size_type>::type erase(K const& key) { return table_.erase(key); } void swap(unordered_node_map& rhs) noexcept( noexcept(std::declval().swap(std::declval()))) { table_.swap(rhs.table_); } node_type extract(const_iterator pos) { BOOST_ASSERT(pos != end()); node_type nh; auto elem = table_.extract(pos); nh.emplace(std::move(elem), get_allocator()); return nh; } node_type extract(key_type const& key) { auto pos = find(key); return pos != end() ? extract(pos) : node_type(); } template typename std::enable_if< boost::unordered::detail::transparent_non_iterable::value, node_type>::type extract(K const& key) { auto pos = find(key); return pos != end() ? extract(pos) : node_type(); } template void merge( unordered_node_map& source) { BOOST_ASSERT(get_allocator() == source.get_allocator()); table_.merge(source.table_); } template void merge( unordered_node_map&& source) { BOOST_ASSERT(get_allocator() == source.get_allocator()); table_.merge(std::move(source.table_)); } /// Lookup /// mapped_type& at(key_type const& key) { auto pos = table_.find(key); if (pos != table_.end()) { return pos->second; } // TODO: someday refactor this to conditionally serialize the key and // include it in the error message // boost::unordered::detail::throw_out_of_range( "key was not found in unordered_node_map"); } mapped_type const& at(key_type const& key) const { auto pos = table_.find(key); if (pos != table_.end()) { return pos->second; } boost::unordered::detail::throw_out_of_range( "key was not found in unordered_node_map"); } template typename std::enable_if< boost::unordered::detail::are_transparent::value, mapped_type&>::type at(K&& key) { auto pos = table_.find(std::forward(key)); if (pos != table_.end()) { return pos->second; } boost::unordered::detail::throw_out_of_range( "key was not found in unordered_node_map"); } template typename std::enable_if< boost::unordered::detail::are_transparent::value, mapped_type const&>::type at(K&& key) const { auto pos = table_.find(std::forward(key)); if (pos != table_.end()) { return pos->second; } boost::unordered::detail::throw_out_of_range( "key was not found in unordered_node_map"); } BOOST_FORCEINLINE mapped_type& operator[](key_type const& key) { return table_.try_emplace(key).first->second; } BOOST_FORCEINLINE mapped_type& operator[](key_type&& key) { return table_.try_emplace(std::move(key)).first->second; } template typename std::enable_if< boost::unordered::detail::are_transparent::value, mapped_type&>::type operator[](K&& key) { return table_.try_emplace(std::forward(key)).first->second; } BOOST_FORCEINLINE size_type count(key_type const& key) const { auto pos = table_.find(key); return pos != table_.end() ? 1 : 0; } template BOOST_FORCEINLINE typename std::enable_if< detail::are_transparent::value, size_type>::type count(K const& key) const { auto pos = table_.find(key); return pos != table_.end() ? 1 : 0; } BOOST_FORCEINLINE iterator find(key_type const& key) { return table_.find(key); } BOOST_FORCEINLINE const_iterator find(key_type const& key) const { return table_.find(key); } template BOOST_FORCEINLINE typename std::enable_if< boost::unordered::detail::are_transparent::value, iterator>::type find(K const& key) { return table_.find(key); } template BOOST_FORCEINLINE typename std::enable_if< boost::unordered::detail::are_transparent::value, const_iterator>::type find(K const& key) const { return table_.find(key); } BOOST_FORCEINLINE bool contains(key_type const& key) const { return this->find(key) != this->end(); } template BOOST_FORCEINLINE typename std::enable_if< boost::unordered::detail::are_transparent::value, bool>::type contains(K const& key) const { return this->find(key) != this->end(); } std::pair equal_range(key_type const& key) { auto pos = table_.find(key); if (pos == table_.end()) { return {pos, pos}; } auto next = pos; ++next; return {pos, next}; } std::pair equal_range( key_type const& key) const { auto pos = table_.find(key); if (pos == table_.end()) { return {pos, pos}; } auto next = pos; ++next; return {pos, next}; } template typename std::enable_if< detail::are_transparent::value, std::pair >::type equal_range(K const& key) { auto pos = table_.find(key); if (pos == table_.end()) { return {pos, pos}; } auto next = pos; ++next; return {pos, next}; } template typename std::enable_if< detail::are_transparent::value, std::pair >::type equal_range(K const& key) const { auto pos = table_.find(key); if (pos == table_.end()) { return {pos, pos}; } auto next = pos; ++next; return {pos, next}; } /// Hash Policy /// size_type bucket_count() const noexcept { return table_.capacity(); } float load_factor() const noexcept { return table_.load_factor(); } float max_load_factor() const noexcept { return table_.max_load_factor(); } void max_load_factor(float) {} size_type max_load() const noexcept { return table_.max_load(); } void rehash(size_type n) { table_.rehash(n); } void reserve(size_type n) { table_.reserve(n); } #if defined(BOOST_UNORDERED_ENABLE_STATS) /// Stats /// stats get_stats() const { return table_.get_stats(); } void reset_stats() noexcept { table_.reset_stats(); } #endif /// Observers /// hasher hash_function() const { return table_.hash_function(); } key_equal key_eq() const { return table_.key_eq(); } }; template bool operator==( unordered_node_map const& lhs, unordered_node_map const& rhs) { return lhs.table_ == rhs.table_; } template bool operator!=( unordered_node_map const& lhs, unordered_node_map const& rhs) { return !(lhs == rhs); } template void swap(unordered_node_map& lhs, unordered_node_map& rhs) noexcept(noexcept(lhs.swap(rhs))) { lhs.swap(rhs); } template typename unordered_node_map::size_type erase_if( unordered_node_map& map, Pred pred) { return erase_if(map.table_, pred); } template void serialize(Archive& ar, unordered_node_map& map, unsigned int version) { detail::serialize_container(ar, map, version); } #if defined(BOOST_MSVC) #pragma warning(pop) /* C4714 */ #endif #if BOOST_UNORDERED_TEMPLATE_DEDUCTION_GUIDES template >, class Pred = std::equal_to >, class Allocator = std::allocator< boost::unordered::detail::iter_to_alloc_t >, class = std::enable_if_t >, class = std::enable_if_t >, class = std::enable_if_t >, class = std::enable_if_t > > unordered_node_map(InputIterator, InputIterator, std::size_t = boost::unordered::detail::foa::default_bucket_count, Hash = Hash(), Pred = Pred(), Allocator = Allocator()) -> unordered_node_map, boost::unordered::detail::iter_val_t, Hash, Pred, Allocator>; template >, class Pred = std::equal_to >, class Allocator = std::allocator >, class = std::enable_if_t >, class = std::enable_if_t >, class = std::enable_if_t > > unordered_node_map(std::initializer_list >, std::size_t = boost::unordered::detail::foa::default_bucket_count, Hash = Hash(), Pred = Pred(), Allocator = Allocator()) -> unordered_node_map, T, Hash, Pred, Allocator>; template >, class = std::enable_if_t > > unordered_node_map(InputIterator, InputIterator, std::size_t, Allocator) -> unordered_node_map, boost::unordered::detail::iter_val_t, boost::hash >, std::equal_to >, Allocator>; template >, class = std::enable_if_t > > unordered_node_map(InputIterator, InputIterator, Allocator) -> unordered_node_map, boost::unordered::detail::iter_val_t, boost::hash >, std::equal_to >, Allocator>; template >, class = std::enable_if_t >, class = std::enable_if_t > > unordered_node_map( InputIterator, InputIterator, std::size_t, Hash, Allocator) -> unordered_node_map, boost::unordered::detail::iter_val_t, Hash, std::equal_to >, Allocator>; template > > unordered_node_map(std::initializer_list >, std::size_t, Allocator) -> unordered_node_map, T, boost::hash >, std::equal_to >, Allocator>; template > > unordered_node_map(std::initializer_list >, Allocator) -> unordered_node_map, T, boost::hash >, std::equal_to >, Allocator>; template >, class = std::enable_if_t > > unordered_node_map(std::initializer_list >, std::size_t, Hash, Allocator) -> unordered_node_map, T, Hash, std::equal_to >, Allocator>; #endif } // namespace unordered } // namespace boost #endif