From c15bd0092dbd6f55eb796cad16e51f2d3f0a2419 Mon Sep 17 00:00:00 2001 From: joaquintides Date: Wed, 5 Oct 2022 18:09:57 +0200 Subject: [PATCH] introduced init_type/value_type --- include/boost/unordered/detail/foa.hpp | 25 ++++++++++++++++--- .../boost/unordered/unordered_flat_map.hpp | 8 ++++++ .../boost/unordered/unordered_flat_set.hpp | 4 +++ 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/include/boost/unordered/detail/foa.hpp b/include/boost/unordered/detail/foa.hpp index 1777da49..81698d09 100644 --- a/include/boost/unordered/detail/foa.hpp +++ b/include/boost/unordered/detail/foa.hpp @@ -828,6 +828,7 @@ table:empty_value,empty_value,empty_value public: using key_type=typename type_policy::key_type; + using init_type=typename type_policy::value_type; using value_type=typename type_policy::value_type; private: @@ -1042,12 +1043,12 @@ public: std::forward_as_tuple(std::forward(args)...)); } - BOOST_FORCEINLINE std::pair insert(const value_type& x) + BOOST_FORCEINLINE std::pair insert(const init_type& x) { return emplace_impl(x); } - BOOST_FORCEINLINE std::pair insert(value_type&& x) + BOOST_FORCEINLINE std::pair insert(init_type&& x) { return emplace_impl(std::move(x)); } @@ -1346,8 +1347,9 @@ private: void nosize_transfer_element(value_type* p,const arrays_type& arrays_) { auto hash=h()(key_from(*p)); - nosize_unchecked_emplace_at( - arrays_,position_for(hash,arrays_),hash,std::move(*p)); + type_policy::move_parts_to( + *p, + bind_unchecked_emplace_at{this,arrays_,position_for(hash,arrays_),hash}); destroy_element(p); } @@ -1413,6 +1415,21 @@ private: } #endif + struct bind_unchecked_emplace_at + { + template + iterator operator()(Args&&... args)const + { + return this_->nosize_unchecked_emplace_at( + arrays,pos0,hash,std::forward(args)...); + } + + table* this_; + const arrays_type& arrays; + std::size_t pos0; + std::size_t hash; + }; + template void for_all_elements(F f)const { diff --git a/include/boost/unordered/unordered_flat_map.hpp b/include/boost/unordered/unordered_flat_map.hpp index 2add8d34..864a33b8 100644 --- a/include/boost/unordered/unordered_flat_map.hpp +++ b/include/boost/unordered/unordered_flat_map.hpp @@ -28,8 +28,16 @@ namespace boost { struct map_types { using key_type = Key; + using init_type = std::pair; using value_type = std::pair; static Key const& extract(value_type const& kv) { return kv.first; } + + template + static void move_parts_to(value_type& x,F f) + { + // TODO: we probably need to launder here + f(std::move(const_cast(x.first)), std::move(x.second)); + } }; using table_type = detail::foa::table + static void move_parts_to(value_type& x,F f) { f(std::move(x)); } }; using table_type = detail::foa::table