From 91e78fd746a9eddabf42e2f2ae5ad2f015a7403b Mon Sep 17 00:00:00 2001 From: Christian Mazakas Date: Mon, 25 Jul 2022 11:35:23 -0700 Subject: [PATCH 1/2] Add `erase_node()` function to table, creating an optimizer-friendly function --- .../boost/unordered/detail/implementation.hpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/include/boost/unordered/detail/implementation.hpp b/include/boost/unordered/detail/implementation.hpp index cec99c79..2cc27c5d 100644 --- a/include/boost/unordered/detail/implementation.hpp +++ b/include/boost/unordered/detail/implementation.hpp @@ -2928,6 +2928,23 @@ namespace boost { return 1; } + iterator erase_node(c_iterator pos) { + c_iterator next = pos; + ++next; + + bucket_iterator itb = pos.itb; + node_pointer* pp = boost::addressof(itb->next); + while (*pp != pos.p) { + pp = boost::addressof((*pp)->next); + } + + buckets_.extract_node_after(itb, pp); + this->delete_node(pos.p); + --size_; + + return iterator(next.p, next.itb); + } + iterator erase_nodes_range(c_iterator first, c_iterator last) { if (first == last) { From a31e894411c7f0504c1fe3279d08af07f35dd1e8 Mon Sep 17 00:00:00 2001 From: Christian Mazakas Date: Mon, 25 Jul 2022 11:35:38 -0700 Subject: [PATCH 2/2] Update implementation to use `erase_node()` where applicable --- include/boost/unordered/unordered_map.hpp | 16 ++++------------ include/boost/unordered/unordered_set.hpp | 9 ++------- 2 files changed, 6 insertions(+), 19 deletions(-) diff --git a/include/boost/unordered/unordered_map.hpp b/include/boost/unordered/unordered_map.hpp index 02fe1f9f..97908fb6 100644 --- a/include/boost/unordered/unordered_map.hpp +++ b/include/boost/unordered/unordered_map.hpp @@ -1856,18 +1856,14 @@ namespace boost { typename unordered_map::iterator unordered_map::erase(iterator position) { - const_iterator last = position; - ++last; - return table_.erase_nodes_range(position, last); + return table_.erase_node(position); } template typename unordered_map::iterator unordered_map::erase(const_iterator position) { - const_iterator last = position; - ++last; - return table_.erase_nodes_range(position, last); + return table_.erase_node(position); } template @@ -2340,9 +2336,7 @@ namespace boost { unordered_multimap::erase(iterator position) { BOOST_ASSERT(position != this->end()); - iterator next = position; - ++next; - return table_.erase_nodes_range(position, next); + return table_.erase_node(position); } template @@ -2350,9 +2344,7 @@ namespace boost { unordered_multimap::erase(const_iterator position) { BOOST_ASSERT(position != this->end()); - const_iterator next = position; - ++next; - return table_.erase_nodes_range(position, next); + return table_.erase_node(position); } template diff --git a/include/boost/unordered/unordered_set.hpp b/include/boost/unordered/unordered_set.hpp index d1ddc347..8721a68a 100644 --- a/include/boost/unordered/unordered_set.hpp +++ b/include/boost/unordered/unordered_set.hpp @@ -1457,9 +1457,7 @@ namespace boost { typename unordered_set::iterator unordered_set::erase(const_iterator position) { - const_iterator last = position; - ++last; - return table_.erase_nodes_range(position, last); + return table_.erase_node(position); } template @@ -1853,10 +1851,7 @@ namespace boost { unordered_multiset::erase(const_iterator position) { BOOST_ASSERT(position != this->end()); - iterator next = position; - ++next; - table_.erase_nodes_range(position, next); - return next; + return table_.erase_node(position); } template