From 6911e6adce3e1d714889b8c144569e3a2ebb23a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ion=20Gazta=C3=B1aga?= Date: Fri, 31 Oct 2025 15:37:07 +0100 Subject: [PATCH] Implement erase/erase_if for list and slist --- .../container/detail/compare_functors.hpp | 7 +++-- include/boost/container/list.hpp | 22 ++++++++++++++++ include/boost/container/slist.hpp | 22 ++++++++++++++++ test/list_test.hpp | 26 +++++++++++++++++++ 4 files changed, 75 insertions(+), 2 deletions(-) diff --git a/include/boost/container/detail/compare_functors.hpp b/include/boost/container/detail/compare_functors.hpp index 9a35274..e15320b 100644 --- a/include/boost/container/detail/compare_functors.hpp +++ b/include/boost/container/detail/compare_functors.hpp @@ -36,8 +36,11 @@ class equal_to_value : t_(t) {} - inline bool operator()(const value_type &t)const - { return t_ == t; } + template + inline bool operator()(const U &t)const + { + return t_ == t; + } }; template diff --git a/include/boost/container/list.hpp b/include/boost/container/list.hpp index 1346537..1d70898 100644 --- a/include/boost/container/list.hpp +++ b/include/boost/container/list.hpp @@ -1487,6 +1487,28 @@ class list }; +//! Effects: Erases all elements that compare equal to v from the container c. +//! +//! Complexity: Linear. +template +inline typename list::size_type erase(list& c, const U& v) +{ + typename list::size_type old_size = c.size(); + c.remove_if(equal_to_value(v)); + return old_size - c.size(); +} + +//! Effects: Erases all elements that satisfy the predicate pred from the container c. +//! +//! Complexity: Linear. +template +inline typename list::size_type erase_if(list& c, Pred pred) +{ + typename list::size_type old_size = c.size(); + c.remove_if(pred); + return old_size - c.size(); +} + #ifndef BOOST_CONTAINER_NO_CXX17_CTAD template list(InputIterator, InputIterator) -> diff --git a/include/boost/container/slist.hpp b/include/boost/container/slist.hpp index 849f929..d6ecf61 100644 --- a/include/boost/container/slist.hpp +++ b/include/boost/container/slist.hpp @@ -1658,6 +1658,28 @@ class slist #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED }; +//! Effects: Erases all elements that compare equal to v from the container c. +//! +//! Complexity: Linear. +template +inline typename slist::size_type erase(slist& c, const U& v) +{ + typename slist::size_type old_size = c.size(); + c.remove_if(equal_to_value(v)); + return old_size - c.size(); +} + +//! Effects: Erases all elements that satisfy the predicate pred from the container c. +//! +//! Complexity: Linear. +template +inline typename slist::size_type erase_if(slist& c, Pred pred) +{ + typename slist::size_type old_size = c.size(); + c.remove_if(pred); + return old_size - c.size(); +} + #ifndef BOOST_CONTAINER_NO_CXX17_CTAD template diff --git a/test/list_test.hpp b/test/list_test.hpp index 404c5b1..7b53dbc 100644 --- a/test/list_test.hpp +++ b/test/list_test.hpp @@ -83,6 +83,32 @@ bool list_copyable_only(V1 &boostlist, V2 &stdlist, boost::container::dtl::true_ boostlist.assign(v1.begin(), v1.end()); stdlist.assign(v2.begin(), v2.end()); if(!test::CheckEqualContainers(boostlist, stdlist)) return 1; + + //test erase/erase_if + IntType aux_vect[50]; + for(int i = 0; i < 50; ++i){ + aux_vect[i] = i; + } + int aux_vect2[50]; + for(int i = 0; i < 50; ++i){ + aux_vect2[i] = i; + } + + boostlist.clear(); + stdlist.clear(); + boostlist.insert(boostlist.end() + ,boost::make_move_iterator(&aux_vect[0]) + ,boost::make_move_iterator(aux_vect + 50)); + stdlist.insert(stdlist.end(), aux_vect2, aux_vect2 + 50); + if(!test::CheckEqualContainers(boostlist, stdlist)) return false; + + if (1 != erase(boostlist, 25)) + return 1; + stdlist.erase(boost::container::find(stdlist.begin(), stdlist.end(), 25)); + if(!test::CheckEqualContainers(boostlist, stdlist)) return false; + + if (0 != erase(boostlist, 25)) + return 1; } { //List(const List &, alloc) ::boost::movelib::unique_ptr const pv1 = ::boost::movelib::make_unique(boostlist, typename V1::allocator_type());