Skip to content

Commit 6911e6a

Browse files
committed
Implement erase/erase_if for list and slist
1 parent ec6364a commit 6911e6a

4 files changed

Lines changed: 75 additions & 2 deletions

File tree

include/boost/container/detail/compare_functors.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@ class equal_to_value
3636
: t_(t)
3737
{}
3838

39-
inline bool operator()(const value_type &t)const
40-
{ return t_ == t; }
39+
template <class U>
40+
inline bool operator()(const U &t)const
41+
{
42+
return t_ == t;
43+
}
4144
};
4245

4346
template<class Node, class Pred, class Ret = bool>

include/boost/container/list.hpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,6 +1487,28 @@ class list
14871487

14881488
};
14891489

1490+
//! <b>Effects</b>: Erases all elements that compare equal to v from the container c.
1491+
//!
1492+
//! <b>Complexity</b>: Linear.
1493+
template <class T, class A, class U>
1494+
inline typename list<T, A>::size_type erase(list<T, A>& c, const U& v)
1495+
{
1496+
typename list<T, A>::size_type old_size = c.size();
1497+
c.remove_if(equal_to_value<U>(v));
1498+
return old_size - c.size();
1499+
}
1500+
1501+
//! <b>Effects</b>: Erases all elements that satisfy the predicate pred from the container c.
1502+
//!
1503+
//! <b>Complexity</b>: Linear.
1504+
template <class T, class A, class Pred>
1505+
inline typename list<T, A>::size_type erase_if(list<T, A>& c, Pred pred)
1506+
{
1507+
typename list<T, A>::size_type old_size = c.size();
1508+
c.remove_if(pred);
1509+
return old_size - c.size();
1510+
}
1511+
14901512
#ifndef BOOST_CONTAINER_NO_CXX17_CTAD
14911513
template <typename InputIterator>
14921514
list(InputIterator, InputIterator) ->

include/boost/container/slist.hpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,6 +1658,28 @@ class slist
16581658
#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
16591659
};
16601660

1661+
//! <b>Effects</b>: Erases all elements that compare equal to v from the container c.
1662+
//!
1663+
//! <b>Complexity</b>: Linear.
1664+
template <class T, class A, class U>
1665+
inline typename slist<T, A>::size_type erase(slist<T, A>& c, const U& v)
1666+
{
1667+
typename slist<T, A>::size_type old_size = c.size();
1668+
c.remove_if(equal_to_value<U>(v));
1669+
return old_size - c.size();
1670+
}
1671+
1672+
//! <b>Effects</b>: Erases all elements that satisfy the predicate pred from the container c.
1673+
//!
1674+
//! <b>Complexity</b>: Linear.
1675+
template <class T, class A, class Pred>
1676+
inline typename slist<T, A>::size_type erase_if(slist<T, A>& c, Pred pred)
1677+
{
1678+
typename slist<T, A>::size_type old_size = c.size();
1679+
c.remove_if(pred);
1680+
return old_size - c.size();
1681+
}
1682+
16611683
#ifndef BOOST_CONTAINER_NO_CXX17_CTAD
16621684

16631685
template <typename InpIt>

test/list_test.hpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,32 @@ bool list_copyable_only(V1 &boostlist, V2 &stdlist, boost::container::dtl::true_
8383
boostlist.assign(v1.begin(), v1.end());
8484
stdlist.assign(v2.begin(), v2.end());
8585
if(!test::CheckEqualContainers(boostlist, stdlist)) return 1;
86+
87+
//test erase/erase_if
88+
IntType aux_vect[50];
89+
for(int i = 0; i < 50; ++i){
90+
aux_vect[i] = i;
91+
}
92+
int aux_vect2[50];
93+
for(int i = 0; i < 50; ++i){
94+
aux_vect2[i] = i;
95+
}
96+
97+
boostlist.clear();
98+
stdlist.clear();
99+
boostlist.insert(boostlist.end()
100+
,boost::make_move_iterator(&aux_vect[0])
101+
,boost::make_move_iterator(aux_vect + 50));
102+
stdlist.insert(stdlist.end(), aux_vect2, aux_vect2 + 50);
103+
if(!test::CheckEqualContainers(boostlist, stdlist)) return false;
104+
105+
if (1 != erase(boostlist, 25))
106+
return 1;
107+
stdlist.erase(boost::container::find(stdlist.begin(), stdlist.end(), 25));
108+
if(!test::CheckEqualContainers(boostlist, stdlist)) return false;
109+
110+
if (0 != erase(boostlist, 25))
111+
return 1;
86112
}
87113
{ //List(const List &, alloc)
88114
::boost::movelib::unique_ptr<V1> const pv1 = ::boost::movelib::make_unique<V1>(boostlist, typename V1::allocator_type());

0 commit comments

Comments
 (0)