From bf2b5217892f87f7ad117dcec491d38a7a9507dd Mon Sep 17 00:00:00 2001 From: Christian Mazakas Date: Mon, 14 Nov 2022 15:17:32 -0800 Subject: [PATCH] Add transparent try_emplace() --- .../boost/unordered/unordered_flat_map.hpp | 23 +++++ include/boost/unordered/unordered_map.hpp | 95 +++++++++++++++++++ 2 files changed, 118 insertions(+) diff --git a/include/boost/unordered/unordered_flat_map.hpp b/include/boost/unordered/unordered_flat_map.hpp index ce09af90..0d38f145 100644 --- a/include/boost/unordered/unordered_flat_map.hpp +++ b/include/boost/unordered/unordered_flat_map.hpp @@ -337,6 +337,17 @@ namespace boost { 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) @@ -352,6 +363,18 @@ namespace boost { .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 void erase(iterator pos) { table_.erase(pos); } BOOST_FORCEINLINE void erase(const_iterator pos) { diff --git a/include/boost/unordered/unordered_map.hpp b/include/boost/unordered/unordered_map.hpp index 9d80d743..3e07ed5e 100644 --- a/include/boost/unordered/unordered_map.hpp +++ b/include/boost/unordered/unordered_map.hpp @@ -486,6 +486,16 @@ namespace boost { boost::move(k), boost::forward(args)...); } + template + typename boost::enable_if_c< + detail::transparent_non_iterable::value, + std::pair >::type + try_emplace(Key&& k, Args&&... args) + { + return table_.try_emplace_unique( + boost::forward(k), boost::forward(args)...); + } + template iterator try_emplace( const_iterator hint, key_type const& k, BOOST_FWD_REF(Args)... args) @@ -502,6 +512,16 @@ namespace boost { hint, boost::move(k), boost::forward(args)...); } + template + typename boost::enable_if_c< + detail::transparent_non_iterable::value, + iterator>::type + try_emplace(const_iterator hint, Key&& k, Args&&... args) + { + return table_.try_emplace_hint_unique( + hint, boost::forward(k), boost::forward(args)...); + } + #else // In order to make this a template, this handles both: @@ -582,6 +602,43 @@ namespace boost { boost::forward(a1), boost::forward(a2))); } + // try_emplace(Key&&, Args&&...) + + template + typename boost::enable_if_c< + detail::transparent_non_iterable::value, + std::pair >::type + try_emplace(BOOST_FWD_REF(Key) k, BOOST_FWD_REF(A0) a0) + { + return table_.try_emplace_unique( + boost::forward(k), boost::unordered::detail::create_emplace_args( + boost::forward(a0))); + } + + template + typename boost::enable_if_c< + detail::transparent_non_iterable::value, + std::pair >::type + try_emplace( + BOOST_FWD_REF(Key) k, BOOST_FWD_REF(A0) a0, BOOST_FWD_REF(A1) a1) + { + return table_.try_emplace_unique(boost::forward(k), + boost::unordered::detail::create_emplace_args( + boost::forward(a0), boost::forward(a1))); + } + + template + typename boost::enable_if_c< + detail::transparent_non_iterable::value, + std::pair >::type + try_emplace(BOOST_FWD_REF(Key) k, BOOST_FWD_REF(A0) a0, + BOOST_FWD_REF(A1) a1, BOOST_FWD_REF(A2) a2) + { + return table_.try_emplace_unique(boost::forward(k), + boost::unordered::detail::create_emplace_args(boost::forward(a0), + boost::forward(a1), boost::forward(a2))); + } + // try_emplace(const_iterator hint, key const&, Args&&...) template @@ -640,6 +697,44 @@ namespace boost { boost::forward(a1), boost::forward(a2))); } + // try_emplace(const_iterator hint, Key&&, Args&&...) + + template + typename boost::enable_if_c< + detail::transparent_non_iterable::value, + iterator>::type + try_emplace( + const_iterator hint, BOOST_FWD_REF(Key) k, BOOST_FWD_REF(A0) a0) + { + return table_.try_emplace_hint_unique(hint, boost::forward(k), + boost::unordered::detail::create_emplace_args( + boost::forward(a0))); + } + + template + typename boost::enable_if_c< + detail::transparent_non_iterable::value, + iterator>::type + try_emplace(const_iterator hint, BOOST_FWD_REF(Key) k, + BOOST_FWD_REF(A0) a0, BOOST_FWD_REF(A1) a1) + { + return table_.try_emplace_hint_unique(hint, boost::forward(k), + boost::unordered::detail::create_emplace_args( + boost::forward(a0), boost::forward(a1))); + } + + template + typename boost::enable_if_c< + detail::transparent_non_iterable::value, + iterator>::type + try_emplace(const_iterator hint, BOOST_FWD_REF(Key) k, + BOOST_FWD_REF(A0) a0, BOOST_FWD_REF(A1) a1, BOOST_FWD_REF(A2) a2) + { + return table_.try_emplace_hint_unique(hint, boost::forward(k), + boost::unordered::detail::create_emplace_args(boost::forward(a0), + boost::forward(a1), boost::forward(a2))); + } + #define BOOST_UNORDERED_TRY_EMPLACE(z, n, _) \ \ template \