From 7d77f1d4788dc9d82572e734548674167c31919c Mon Sep 17 00:00:00 2001 From: Christian Mazakas Date: Tue, 13 Dec 2022 12:33:07 -0800 Subject: [PATCH] Add transparent insert() overloads to unordered_[flat]_set --- .../boost/unordered/detail/implementation.hpp | 25 ++++++++++++++++--- .../boost/unordered/unordered_flat_set.hpp | 18 +++++++++++++ include/boost/unordered/unordered_set.hpp | 18 +++++++++++++ 3 files changed, 57 insertions(+), 4 deletions(-) diff --git a/include/boost/unordered/detail/implementation.hpp b/include/boost/unordered/detail/implementation.hpp index d953b313..13351171 100644 --- a/include/boost/unordered/detail/implementation.hpp +++ b/include/boost/unordered/detail/implementation.hpp @@ -1477,7 +1477,22 @@ namespace boost { } #endif - } + + template + inline typename boost::allocator_pointer::type + construct_node_from_key(T*, Alloc& alloc, BOOST_FWD_REF(Key) k) + { + return construct_node(alloc, boost::forward(k)); + } + + template + inline typename boost::allocator_pointer::type + construct_node_from_key( + std::pair*, Alloc& alloc, BOOST_FWD_REF(Key) k) + { + return construct_node_pair(alloc, boost::forward(k)); + } + } // namespace func } } } @@ -2640,8 +2655,10 @@ namespace boost { } else { node_allocator_type alloc = node_alloc(); - node_tmp tmp( - detail::func::construct_node_pair(alloc, boost::forward(k)), + value_type* dispatch = BOOST_NULLPTR; + + node_tmp tmp(detail::func::construct_node_from_key( + dispatch, alloc, boost::forward(k)), alloc); if (size_ + 1 > max_load_) { @@ -2660,7 +2677,7 @@ namespace boost { template iterator try_emplace_hint_unique(c_iterator hint, BOOST_FWD_REF(Key) k) { - if (hint.p && this->key_eq()(hint->first, k)) { + if (hint.p && this->key_eq()(extractor::extract(*hint), k)) { return iterator(hint.p, hint.itb); } else { return try_emplace_unique(k).first; diff --git a/include/boost/unordered/unordered_flat_set.hpp b/include/boost/unordered/unordered_flat_set.hpp index 29c53596..1599ba55 100644 --- a/include/boost/unordered/unordered_flat_set.hpp +++ b/include/boost/unordered/unordered_flat_set.hpp @@ -231,6 +231,15 @@ namespace boost { return table_.insert(std::move(value)); } + template + BOOST_FORCEINLINE typename std::enable_if< + detail::transparent_non_iterable::value, + std::pair >::type + insert(K&& k) + { + return table_.try_emplace(std::forward(k)); + } + BOOST_FORCEINLINE iterator insert(const_iterator, value_type const& value) { return table_.insert(value).first; @@ -241,6 +250,15 @@ namespace boost { return table_.insert(std::move(value)).first; } + template + BOOST_FORCEINLINE typename std::enable_if< + detail::transparent_non_iterable::value, + iterator>::type + insert(const_iterator, K&& k) + { + return table_.try_emplace(std::forward(k)).first; + } + template void insert(InputIterator first, InputIterator last) { diff --git a/include/boost/unordered/unordered_set.hpp b/include/boost/unordered/unordered_set.hpp index 9542b492..1bd4ba3d 100644 --- a/include/boost/unordered/unordered_set.hpp +++ b/include/boost/unordered/unordered_set.hpp @@ -390,6 +390,15 @@ namespace boost { return this->emplace(boost::move(x)); } + template + typename boost::enable_if_c< + detail::transparent_non_iterable::value, + std::pair >::type + insert(BOOST_FWD_REF(Key) k) + { + return table_.try_emplace_unique(boost::forward(k)); + } + iterator insert(const_iterator hint, value_type const& x) { return this->emplace_hint(hint, x); @@ -400,6 +409,15 @@ namespace boost { return this->emplace_hint(hint, boost::move(x)); } + template + typename boost::enable_if_c< + detail::transparent_non_iterable::value, + iterator>::type + insert(const_iterator hint, BOOST_FWD_REF(Key) k) + { + return table_.try_emplace_hint_unique(hint, boost::forward(k)); + } + template void insert(InputIt, InputIt); #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)