Skip to content

Commit 22bc502

Browse files
committed
Implement erase_if for map and flat_map
1 parent a7873e9 commit 22bc502

4 files changed

Lines changed: 118 additions & 0 deletions

File tree

include/boost/container/detail/compare_functors.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,24 @@ class equal_to_value
4343
}
4444
};
4545

46+
template<class ValueType>
47+
class equal_to_value_first
48+
{
49+
typedef ValueType value_type;
50+
const value_type &t_;
51+
52+
public:
53+
inline explicit equal_to_value_first(const value_type &t)
54+
: t_(t)
55+
{}
56+
57+
template <class U>
58+
inline bool operator()(const U &t)const
59+
{
60+
return t_ == t.first;
61+
}
62+
};
63+
4664
template<class Node, class Pred, class Ret = bool>
4765
struct value_to_node_compare
4866
: Pred

include/boost/container/flat_map.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1717,6 +1717,15 @@ class flat_map
17171717
#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
17181718
};
17191719

1720+
//! <b>Effects</b>: Erases all elements that satisfy the predicate pred from the container c.
1721+
//!
1722+
//! <b>Complexity</b>: Linear.
1723+
template <class K, class M, class C, class A, class Pred>
1724+
inline typename flat_map<K, M, C, A>::size_type erase_if(flat_map<K, M, C, A>& c, Pred pred)
1725+
{
1726+
return container_erase_if(c, pred);
1727+
}
1728+
17201729
#ifndef BOOST_CONTAINER_NO_CXX17_CTAD
17211730

17221731
template <typename InputIterator>
@@ -3074,6 +3083,15 @@ class flat_multimap
30743083
#pragma GCC pop_options
30753084
#endif
30763085

3086+
//! <b>Effects</b>: Erases all elements that satisfy the predicate pred from the container c.
3087+
//!
3088+
//! <b>Complexity</b>: Linear.
3089+
template <class K, class M, class C, class A, class Pred>
3090+
inline typename flat_multimap<K, M, C, A>::size_type erase_if(flat_multimap<K, M, C, A>& c, Pred pred)
3091+
{
3092+
return container_erase_if(c, pred);
3093+
}
3094+
30773095
#ifndef BOOST_CONTAINER_NO_CXX17_CTAD
30783096

30793097
template <typename InputIterator>

include/boost/container/map.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <boost/container/detail/value_init.hpp>
3333
#include <boost/container/detail/pair.hpp>
3434
#include <boost/container/detail/pair_key_mapped_of_value.hpp>
35+
#include <boost/container/detail/algorithm.hpp>
3536

3637
// move
3738
#include <boost/move/traits.hpp>
@@ -1318,6 +1319,15 @@ class map
13181319
#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
13191320
};
13201321

1322+
//! <b>Effects</b>: Erases all elements that satisfy the predicate pred from the container c.
1323+
//!
1324+
//! <b>Complexity</b>: Linear.
1325+
template <class K, class M, class C, class A, class O, class Pred>
1326+
inline typename map<K, M, C, A, O>::size_type erase_if(map<K, M, C, A, O>& c, Pred pred)
1327+
{
1328+
return container_erase_if(c, pred);
1329+
}
1330+
13211331
#ifndef BOOST_CONTAINER_NO_CXX17_CTAD
13221332

13231333
template <typename InputIterator>
@@ -2242,6 +2252,15 @@ class multimap
22422252
#endif //#if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
22432253
};
22442254

2255+
//! <b>Effects</b>: Erases all elements that satisfy the predicate pred from the container c.
2256+
//!
2257+
//! <b>Complexity</b>: Linear.
2258+
template <class K, class M, class C, class A, class O, class Pred>
2259+
inline typename multimap<K, M, C, A, O>::size_type erase_if(multimap<K, M, C, A, O>& c, Pred pred)
2260+
{
2261+
return container_erase_if(c, pred);
2262+
}
2263+
22452264
#ifndef BOOST_CONTAINER_NO_CXX17_CTAD
22462265

22472266
template <typename InputIterator>

test/map_test.hpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "print_container.hpp"
1717
#include "movable_int.hpp"
1818
#include <boost/container/detail/pair.hpp>
19+
#include <boost/container/detail/compare_functors.hpp>
1920
#include <boost/move/iterator.hpp>
2021
#include <boost/move/utility_core.hpp>
2122
#include <boost/move/make_unique.hpp>
@@ -647,6 +648,68 @@ int map_test_erase(MyBoostMap &boostmap, MyStdMap &stdmap, MyBoostMultiMap &boos
647648
if(!CheckEqualPairContainers(boostmap, stdmap)) return 1;
648649
if(!CheckEqualPairContainers(boostmultimap, stdmultimap)) return 1;
649650
}
651+
652+
{ //erase_if
653+
boostmap.clear();
654+
boostmultimap.clear();
655+
stdmap.clear();
656+
stdmultimap.clear();
657+
658+
{
659+
IntPairType aux_vect[(std::size_t)MaxElem];
660+
IntPairType aux_vect2[(std::size_t)MaxElem];
661+
IntPairType aux_vect3[(std::size_t)MaxElem];
662+
663+
for(int i = 0; i < MaxElem; ++i){
664+
IntType i1(i);
665+
IntType i2(i);
666+
new(&aux_vect[i])IntPairType(boost::move(i1), boost::move(i2));
667+
}
668+
669+
for(int i = 0; i < MaxElem; ++i){
670+
IntType i1(i);
671+
IntType i2(i);
672+
new(&aux_vect2[i])IntPairType(boost::move(i1), boost::move(i2));
673+
}
674+
675+
for(int i = 0; i < MaxElem; ++i){
676+
IntType i1(i);
677+
IntType i2(i);
678+
new(&aux_vect3[i])IntPairType(boost::move(i1), boost::move(i2));
679+
}
680+
681+
boostmap. insert(boost::make_move_iterator(&aux_vect[0]), boost::make_move_iterator(&aux_vect[0] + MaxElem));
682+
boostmultimap.insert(boost::make_move_iterator(&aux_vect2[0]), boost::make_move_iterator(&aux_vect2[0] + MaxElem));
683+
boostmultimap.insert(boost::make_move_iterator(&aux_vect3[0]), boost::make_move_iterator(&aux_vect3[0] + MaxElem));
684+
}
685+
686+
for(int i = 0; i < MaxElem; ++i){
687+
stdmap.insert((StdPairType(i, i)));
688+
stdmultimap.insert((StdPairType(i, i)));
689+
stdmultimap.insert((StdPairType(i, i)));
690+
}
691+
692+
for(int i = 0; i < MaxElem; ++i) {
693+
//erase_if
694+
const int key = (i + MaxElem/2) % MaxElem;
695+
696+
if (1 != erase_if(boostmap, equal_to_value_first<int>(key)))
697+
return 1;
698+
if (0 != erase_if(boostmap, equal_to_value_first<int>(key)))
699+
return 1;
700+
stdmap.erase(key);
701+
if(!test::CheckEqualContainers(boostmap, stdmap)) return false;
702+
703+
//erase_if
704+
if (2 != erase_if(boostmultimap, equal_to_value_first<int>(key)))
705+
return 1;
706+
if (0 != erase_if(boostmultimap, equal_to_value_first<int>(key)))
707+
return 1;
708+
stdmultimap.erase(key);
709+
if(!test::CheckEqualContainers(boostmultimap, stdmultimap)) return false;
710+
}
711+
}
712+
650713
return 0;
651714
}
652715

0 commit comments

Comments
 (0)