From 0b44b4b807a2d7bee4d2a0e4c37794db2aeeb44f Mon Sep 17 00:00:00 2001 From: Edward Smith-Rowland <3dw4rd@verizon.net> Date: Fri, 30 Nov 2018 16:12:13 +0000 Subject: [PATCH] Pre-emptively support P0646R1 for std container erasure. 2018-11-30 Edward Smith-Rowland <3dw4rd@verizon.net> Pre-emptively support P0646R1 for std container erasure. * include/bits/erase_if.h: Accumulate and return number of erased nodes. * include/std/forward_list (): Return number of erased items. * include/std/list (): Ditto. * include/std/map (): Ditto. * include/std/set (): Ditto. * include/std/string (): Ditto. * include/std/unordered_map (): Ditto. * include/std/unordered_set (): Ditto. * include/std/vector (): Ditto. * testsuite/21_strings/basic_string/erasure.cc: Test number of erasures. * testsuite/23_containers/deque/erasure.cc: Ditto. * testsuite/23_containers/forward_list/erasure.cc: Ditto. * testsuite/23_containers/list/erasure.cc: Ditto. * testsuite/23_containers/map/erasure.cc: Ditto. * testsuite/23_containers/set/erasure.cc: Ditto. * testsuite/23_containers/unordered_map/erasure.cc: Ditto. * testsuite/23_containers/unordered_set/erasure.cc: Ditto. * testsuite/23_containers/vector/erasure.cc: Ditto. From-SVN: r266672 --- libstdc++-v3/ChangeLog | 22 +++++++++++++++++++ libstdc++-v3/include/bits/erase_if.h | 19 ++++++++++------ libstdc++-v3/include/std/forward_list | 9 ++++---- libstdc++-v3/include/std/list | 9 ++++---- libstdc++-v3/include/std/map | 8 +++---- libstdc++-v3/include/std/set | 8 +++---- libstdc++-v3/include/std/string | 8 +++++-- libstdc++-v3/include/std/unordered_map | 9 ++++---- libstdc++-v3/include/std/unordered_set | 8 +++---- libstdc++-v3/include/std/vector | 8 +++++-- .../21_strings/basic_string/erasure.cc | 9 +++++--- .../testsuite/23_containers/deque/erasure.cc | 9 +++++--- .../23_containers/forward_list/erasure.cc | 9 +++++--- .../testsuite/23_containers/list/erasure.cc | 6 +++-- .../testsuite/23_containers/map/erasure.cc | 6 +++-- .../testsuite/23_containers/set/erasure.cc | 6 +++-- .../23_containers/unordered_map/erasure.cc | 6 +++-- .../23_containers/unordered_set/erasure.cc | 6 +++-- .../testsuite/23_containers/vector/erasure.cc | 9 +++++--- 19 files changed, 117 insertions(+), 57 deletions(-) diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 037325ea554..7b75b10b5ce 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,25 @@ +2018-11-30 Edward Smith-Rowland <3dw4rd@verizon.net> + + Pre-emptively support P0646R1 for std container erasure. + * include/bits/erase_if.h: Accumulate and return number of erased nodes. + * include/std/forward_list (): Return number of erased items. + * include/std/list (): Ditto. + * include/std/map (): Ditto. + * include/std/set (): Ditto. + * include/std/string (): Ditto. + * include/std/unordered_map (): Ditto. + * include/std/unordered_set (): Ditto. + * include/std/vector (): Ditto. + * testsuite/21_strings/basic_string/erasure.cc: Test number of erasures. + * testsuite/23_containers/deque/erasure.cc: Ditto. + * testsuite/23_containers/forward_list/erasure.cc: Ditto. + * testsuite/23_containers/list/erasure.cc: Ditto. + * testsuite/23_containers/map/erasure.cc: Ditto. + * testsuite/23_containers/set/erasure.cc: Ditto. + * testsuite/23_containers/unordered_map/erasure.cc: Ditto. + * testsuite/23_containers/unordered_set/erasure.cc: Ditto. + * testsuite/23_containers/vector/erasure.cc: Ditto. + 2018-11-29 Edward Smith-Rowland <3dw4rd@verizon.net> Only include bits/stl_algo.h for C++20. diff --git a/libstdc++-v3/include/bits/erase_if.h b/libstdc++-v3/include/bits/erase_if.h index de4601f0173..d0fd13a3049 100644 --- a/libstdc++-v3/include/bits/erase_if.h +++ b/libstdc++-v3/include/bits/erase_if.h @@ -41,17 +41,22 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION namespace __detail { template - void + typename _Container::size_type __erase_nodes_if(_Container& __cont, _Predicate __pred) { + typename _Container::size_type __num = 0; for (auto __iter = __cont.begin(), __last = __cont.end(); __iter != __last;) - { - if (__pred(*__iter)) - __iter = __cont.erase(__iter); - else - ++__iter; - } + { + if (__pred(*__iter)) + { + __iter = __cont.erase(__iter); + ++__num; + } + else + ++__iter; + } + return __num; } } // namespace __detail diff --git a/libstdc++-v3/include/std/forward_list b/libstdc++-v3/include/std/forward_list index a2cc90387ad..b094d882048 100644 --- a/libstdc++-v3/include/std/forward_list +++ b/libstdc++-v3/include/std/forward_list @@ -66,16 +66,17 @@ namespace std _GLIBCXX_VISIBILITY(default) { _GLIBCXX_BEGIN_NAMESPACE_VERSION template - inline void + inline typename forward_list<_Tp, _Alloc>::size_type erase_if(forward_list<_Tp, _Alloc>& __cont, _Predicate __pred) - { __cont.remove_if(__pred); } + { return __cont.remove_if(__pred); } template - inline void + inline typename forward_list<_Tp, _Alloc>::size_type erase(forward_list<_Tp, _Alloc>& __cont, const _Up& __value) { using __elem_type = typename forward_list<_Tp, _Alloc>::value_type; - erase_if(__cont, [&](__elem_type& __elem) { return __elem == __value; }); + return erase_if(__cont, + [&](__elem_type& __elem) { return __elem == __value; }); } _GLIBCXX_END_NAMESPACE_VERSION } // namespace std diff --git a/libstdc++-v3/include/std/list b/libstdc++-v3/include/std/list index 58ad461b666..b2ced427642 100644 --- a/libstdc++-v3/include/std/list +++ b/libstdc++-v3/include/std/list @@ -90,16 +90,17 @@ namespace std _GLIBCXX_VISIBILITY(default) { _GLIBCXX_BEGIN_NAMESPACE_VERSION template - inline void + inline typename list<_Tp, _Alloc>::size_type erase_if(list<_Tp, _Alloc>& __cont, _Predicate __pred) - { __cont.remove_if(__pred); } + { return __cont.remove_if(__pred); } template - inline void + inline typename list<_Tp, _Alloc>::size_type erase(list<_Tp, _Alloc>& __cont, const _Up& __value) { using __elem_type = typename list<_Tp, _Alloc>::value_type; - erase_if(__cont, [&](__elem_type& __elem) { return __elem == __value; }); + return erase_if(__cont, + [&](__elem_type& __elem) { return __elem == __value; }); } _GLIBCXX_END_NAMESPACE_VERSION } // namespace std diff --git a/libstdc++-v3/include/std/map b/libstdc++-v3/include/std/map index 26b2f0ed08a..7b01fd19e69 100644 --- a/libstdc++-v3/include/std/map +++ b/libstdc++-v3/include/std/map @@ -97,15 +97,15 @@ namespace std _GLIBCXX_VISIBILITY(default) _GLIBCXX_BEGIN_NAMESPACE_VERSION template - inline void + inline typename map<_Key, _Tp, _Compare, _Alloc>::size_type erase_if(map<_Key, _Tp, _Compare, _Alloc>& __cont, _Predicate __pred) - { __detail::__erase_nodes_if(__cont, __pred); } + { return __detail::__erase_nodes_if(__cont, __pred); } template - inline void + inline typename multimap<_Key, _Tp, _Compare, _Alloc>::size_type erase_if(multimap<_Key, _Tp, _Compare, _Alloc>& __cont, _Predicate __pred) - { __detail::__erase_nodes_if(__cont, __pred); } + { return __detail::__erase_nodes_if(__cont, __pred); } _GLIBCXX_END_NAMESPACE_VERSION } // namespace std #endif // C++20 diff --git a/libstdc++-v3/include/std/set b/libstdc++-v3/include/std/set index 432af90978d..2423b2f4513 100644 --- a/libstdc++-v3/include/std/set +++ b/libstdc++-v3/include/std/set @@ -93,15 +93,15 @@ namespace std _GLIBCXX_VISIBILITY(default) _GLIBCXX_BEGIN_NAMESPACE_VERSION template - inline void + inline typename set<_Key, _Compare, _Alloc>::size_type erase_if(set<_Key, _Compare, _Alloc>& __cont, _Predicate __pred) - { __detail::__erase_nodes_if(__cont, __pred); } + { return __detail::__erase_nodes_if(__cont, __pred); } template - inline void + inline typename multiset<_Key, _Compare, _Alloc>::size_type erase_if(multiset<_Key, _Compare, _Alloc>& __cont, _Predicate __pred) - { __detail::__erase_nodes_if(__cont, __pred); } + { return __detail::__erase_nodes_if(__cont, __pred); } _GLIBCXX_END_NAMESPACE_VERSION } // namespace std #endif // C++20 diff --git a/libstdc++-v3/include/std/string b/libstdc++-v3/include/std/string index 87b31839fcf..e1f0304f8b6 100644 --- a/libstdc++-v3/include/std/string +++ b/libstdc++-v3/include/std/string @@ -81,19 +81,23 @@ namespace std _GLIBCXX_VISIBILITY(default) _GLIBCXX_BEGIN_NAMESPACE_VERSION template - inline void + inline typename basic_string<_CharT, _Traits, _Alloc>::size_type erase_if(basic_string<_CharT, _Traits, _Alloc>& __cont, _Predicate __pred) { + const auto __osz = __cont.size(); __cont.erase(std::remove_if(__cont.begin(), __cont.end(), __pred), __cont.end()); + return __osz - __cont.size(); } template - inline void + inline typename basic_string<_CharT, _Traits, _Alloc>::size_type erase(basic_string<_CharT, _Traits, _Alloc>& __cont, const _Up& __value) { + const auto __osz = __cont.size(); __cont.erase(std::remove(__cont.begin(), __cont.end(), __value), __cont.end()); + return __osz - __cont.size(); } _GLIBCXX_END_NAMESPACE_VERSION } // namespace std diff --git a/libstdc++-v3/include/std/unordered_map b/libstdc++-v3/include/std/unordered_map index 8cb82144fd2..bb1e1b3e101 100644 --- a/libstdc++-v3/include/std/unordered_map +++ b/libstdc++-v3/include/std/unordered_map @@ -84,17 +84,18 @@ namespace std _GLIBCXX_VISIBILITY(default) _GLIBCXX_BEGIN_NAMESPACE_VERSION template - inline void + inline typename unordered_map<_Key, _Tp, _Hash, _CPred, _Alloc>::size_type erase_if(unordered_map<_Key, _Tp, _Hash, _CPred, _Alloc>& __cont, _Predicate __pred) - { __detail::__erase_nodes_if(__cont, __pred); } + { return __detail::__erase_nodes_if(__cont, __pred); } template - inline void + inline typename unordered_multimap<_Key, _Tp, _Hash, _CPred, _Alloc>:: + size_type erase_if(unordered_multimap<_Key, _Tp, _Hash, _CPred, _Alloc>& __cont, _Predicate __pred) - { __detail::__erase_nodes_if(__cont, __pred); } + { return __detail::__erase_nodes_if(__cont, __pred); } _GLIBCXX_END_NAMESPACE_VERSION } // namespace std #endif // C++20 diff --git a/libstdc++-v3/include/std/unordered_set b/libstdc++-v3/include/std/unordered_set index a5e1e41815b..e7441a7ba79 100644 --- a/libstdc++-v3/include/std/unordered_set +++ b/libstdc++-v3/include/std/unordered_set @@ -84,17 +84,17 @@ namespace std _GLIBCXX_VISIBILITY(default) _GLIBCXX_BEGIN_NAMESPACE_VERSION template - inline void + inline typename unordered_set<_Key, _Hash, _CPred, _Alloc>::size_type erase_if(unordered_set<_Key, _Hash, _CPred, _Alloc>& __cont, _Predicate __pred) - { std::__detail::__erase_nodes_if(__cont, __pred); } + { return __detail::__erase_nodes_if(__cont, __pred); } template - inline void + inline typename unordered_multiset<_Key, _Hash, _CPred, _Alloc>::size_type erase_if(unordered_multiset<_Key, _Hash, _CPred, _Alloc>& __cont, _Predicate __pred) - { std::__detail::__erase_nodes_if(__cont, __pred); } + { return __detail::__erase_nodes_if(__cont, __pred); } _GLIBCXX_END_NAMESPACE_VERSION } // namespace std #endif // C++20 diff --git a/libstdc++-v3/include/std/vector b/libstdc++-v3/include/std/vector index ef04d759d53..167d33637ff 100644 --- a/libstdc++-v3/include/std/vector +++ b/libstdc++-v3/include/std/vector @@ -98,19 +98,23 @@ namespace std _GLIBCXX_VISIBILITY(default) { _GLIBCXX_BEGIN_NAMESPACE_VERSION template - inline void + inline typename vector<_Tp, _Alloc>::size_type erase_if(vector<_Tp, _Alloc>& __cont, _Predicate __pred) { + const auto __osz = __cont.size(); __cont.erase(std::remove_if(__cont.begin(), __cont.end(), __pred), __cont.end()); + return __osz - __cont.size(); } template - inline void + inline typename vector<_Tp, _Alloc>::size_type erase(vector<_Tp, _Alloc>& __cont, const _Up& __value) { + const auto __osz = __cont.size(); __cont.erase(std::remove(__cont.begin(), __cont.end(), __value), __cont.end()); + return __osz - __cont.size(); } _GLIBCXX_END_NAMESPACE_VERSION } // namespace std diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/erasure.cc b/libstdc++-v3/testsuite/21_strings/basic_string/erasure.cc index d8ac6a45297..2e928279e89 100644 --- a/libstdc++-v3/testsuite/21_strings/basic_string/erasure.cc +++ b/libstdc++-v3/testsuite/21_strings/basic_string/erasure.cc @@ -30,18 +30,21 @@ test01() }; std::string str("cute fluffy kittens"); - std::erase_if(str, is_vowel); + auto num = std::erase_if(str, is_vowel); VERIFY( str == "ct flffy kttns" ); + VERIFY( num == 5 ); } void test02() { std::string str = "cute fluffy kittens"; - std::erase(str, 'f'); + auto num = std::erase(str, 'f'); VERIFY( str == "cute luy kittens" ); - std::erase(str, 'z'); + VERIFY( num == 3 ); + num = std::erase(str, 'z'); VERIFY( str == "cute luy kittens" ); + VERIFY( num == 0 ); } int diff --git a/libstdc++-v3/testsuite/23_containers/deque/erasure.cc b/libstdc++-v3/testsuite/23_containers/deque/erasure.cc index 1bb0b95d200..3c28a3d70dd 100644 --- a/libstdc++-v3/testsuite/23_containers/deque/erasure.cc +++ b/libstdc++-v3/testsuite/23_containers/deque/erasure.cc @@ -27,20 +27,23 @@ test01() auto is_odd = [](const int i) { return i % 2 != 0; }; std::deque d{ 10, 11, 12, 14, 15, 17, 18, 19 }; - std::erase_if(d, is_odd); + auto num = std::erase_if(d, is_odd); std::deque t{ 10, 12, 14, 18 }; VERIFY( d == t ); + VERIFY( num == 4 ); } void test02() { std::deque d{ 10, 11, 12, 14, 15, 17, 18, 19 }; - std::erase(d, 14); + auto num = std::erase(d, 14); std::deque t{ 10, 11, 12, 15, 17, 18, 19 }; VERIFY( d == t ); - std::erase(d, 20); + VERIFY( num == 1 ); + num = std::erase(d, 20); VERIFY( d == t ); + VERIFY( num == 0 ); } int diff --git a/libstdc++-v3/testsuite/23_containers/forward_list/erasure.cc b/libstdc++-v3/testsuite/23_containers/forward_list/erasure.cc index 9d06ebe37e4..e137716a818 100644 --- a/libstdc++-v3/testsuite/23_containers/forward_list/erasure.cc +++ b/libstdc++-v3/testsuite/23_containers/forward_list/erasure.cc @@ -27,20 +27,23 @@ test01() auto is_odd = [](const int i) { return i % 2 != 0; }; std::forward_list fl{ 10, 11, 12, 14, 15, 17, 18, 19 }; - std::erase_if(fl, is_odd); + auto num = std::erase_if(fl, is_odd); std::forward_list t{ 10, 12, 14, 18 }; VERIFY( fl == t ); + VERIFY( num == 4 ); } void test02() { std::forward_list fl{ 10, 11, 12, 14, 15, 17, 18, 19 }; - std::erase(fl, 14); + auto num = std::erase(fl, 14); std::forward_list t{ 10, 11, 12, 15, 17, 18, 19 }; VERIFY( fl == t ); - std::erase(fl, 20); + VERIFY( num == 1 ); + num = std::erase(fl, 20); VERIFY( fl == t ); + VERIFY( num == 0 ); } int diff --git a/libstdc++-v3/testsuite/23_containers/list/erasure.cc b/libstdc++-v3/testsuite/23_containers/list/erasure.cc index ff4dbcf778b..bae54421cd4 100644 --- a/libstdc++-v3/testsuite/23_containers/list/erasure.cc +++ b/libstdc++-v3/testsuite/23_containers/list/erasure.cc @@ -36,11 +36,13 @@ void test02() { std::list l{ 0, 11, 0, 0, 22, 33, 0, 0, 44, 0 }; - std::erase(l, 0); + auto num = std::erase(l, 0); std::list t{ 11, 22, 33, 44 }; VERIFY( l == t ); - std::erase(l, 55); + VERIFY( num == 6 ); + num = std::erase(l, 55); VERIFY( l == t ); + VERIFY( num == 0 ); } int diff --git a/libstdc++-v3/testsuite/23_containers/map/erasure.cc b/libstdc++-v3/testsuite/23_containers/map/erasure.cc index 58706a1be01..09d40b5cd35 100644 --- a/libstdc++-v3/testsuite/23_containers/map/erasure.cc +++ b/libstdc++-v3/testsuite/23_containers/map/erasure.cc @@ -33,10 +33,11 @@ test01() { 12, "C" }, { 14, "D" }, { 15, "E" }, { 17, "F" }, { 18, "G" }, { 19, "H" } }; - std::erase_if(m, is_odd_pair); + auto num = std::erase_if(m, is_odd_pair); std::map t{ { 10, "A" }, { 12, "C" }, { 14, "D" }, { 18, "G" } }; VERIFY( m == t ); + VERIFY( num == 4 ); } void @@ -46,10 +47,11 @@ test02() { 22, "U" }, { 22, "V" }, { 23, "W" }, { 23, "X" }, { 24, "Y" }, { 25, "Z" } }; - std::erase_if(mm, is_odd_pair); + auto num = std::erase_if(mm, is_odd_pair); std::multimap t{ { 20, "S" }, { 22, "U" }, { 22, "V" }, { 24, "Y" } }; VERIFY( mm == t ); + VERIFY( num == 4 ); } int diff --git a/libstdc++-v3/testsuite/23_containers/set/erasure.cc b/libstdc++-v3/testsuite/23_containers/set/erasure.cc index 1dccde8147e..f168ad3df84 100644 --- a/libstdc++-v3/testsuite/23_containers/set/erasure.cc +++ b/libstdc++-v3/testsuite/23_containers/set/erasure.cc @@ -27,18 +27,20 @@ void test01() { std::set s{ 10, 11, 12, 14, 15, 17, 18, 19 }; - std::erase_if(s, is_odd); + auto num = std::erase_if(s, is_odd); std::set t{ 10, 12, 14, 18 }; VERIFY( s == t ); + VERIFY( num == 4 ); } void test02() { std::multiset ms{ 20, 21, 22, 22, 23, 23, 24, 25 }; - std::erase_if(ms, is_odd); + auto num = std::erase_if(ms, is_odd); std::multiset t{ 20, 22, 22, 24 }; VERIFY( ms == t ); + VERIFY( num == 4 ); } int diff --git a/libstdc++-v3/testsuite/23_containers/unordered_map/erasure.cc b/libstdc++-v3/testsuite/23_containers/unordered_map/erasure.cc index 8a475482802..d8eb3acb983 100644 --- a/libstdc++-v3/testsuite/23_containers/unordered_map/erasure.cc +++ b/libstdc++-v3/testsuite/23_containers/unordered_map/erasure.cc @@ -33,10 +33,11 @@ test01() { 12, "C" }, { 14, "D" }, { 15, "E" }, { 17, "F" }, { 18, "G" }, { 19, "H" } }; - std::erase_if(um, is_odd_pair); + auto num = std::erase_if(um, is_odd_pair); std::unordered_map t{ { 10, "A" }, { 12, "C" }, { 14, "D" }, { 18, "G" } }; VERIFY( um == t ); + VERIFY( num == 4 ); } void @@ -46,10 +47,11 @@ test02() { 22, "U" }, { 22, "V" }, { 23, "W" }, { 23, "X" }, { 24, "Y" }, { 25, "Z" } }; - std::erase_if(umm, is_odd_pair); + auto num = std::erase_if(umm, is_odd_pair); std::unordered_multimap t{ { 20, "S" }, { 22, "U" }, { 22, "V" }, { 24, "Y" } }; VERIFY( umm == t ); + VERIFY( num == 4 ); } int diff --git a/libstdc++-v3/testsuite/23_containers/unordered_set/erasure.cc b/libstdc++-v3/testsuite/23_containers/unordered_set/erasure.cc index e5fcde00c01..f1c9632ad59 100644 --- a/libstdc++-v3/testsuite/23_containers/unordered_set/erasure.cc +++ b/libstdc++-v3/testsuite/23_containers/unordered_set/erasure.cc @@ -27,9 +27,10 @@ test01() auto is_odd = [](const int i) { return i % 2 != 0; }; std::unordered_set us{ 10, 11, 12, 14, 15, 17, 18, 19 }; - std::erase_if(us, is_odd); + auto num = std::erase_if(us, is_odd); std::unordered_set t{ 10, 12, 14, 18 }; VERIFY( us == t ); + VERIFY( num == 4 ); } void @@ -38,9 +39,10 @@ test02() auto is_odd = [](const int i) { return i % 2 != 0; }; std::unordered_multiset ums{ 20, 21, 22, 22, 23, 23, 24, 25 }; - std::erase_if(ums, is_odd); + auto num = std::erase_if(ums, is_odd); std::unordered_multiset t{ 20, 22, 22, 24 }; VERIFY( ums == t ); + VERIFY( num == 4 ); } int diff --git a/libstdc++-v3/testsuite/23_containers/vector/erasure.cc b/libstdc++-v3/testsuite/23_containers/vector/erasure.cc index 57567d3338a..8934ee5a3a1 100644 --- a/libstdc++-v3/testsuite/23_containers/vector/erasure.cc +++ b/libstdc++-v3/testsuite/23_containers/vector/erasure.cc @@ -27,20 +27,23 @@ test01() auto is_odd = [](const int i) { return i % 2 != 0; }; std::vector v{ 10, 11, 12, 14, 15, 17, 18, 19 }; - std::erase_if(v, is_odd); + auto num = std::erase_if(v, is_odd); std::vector t{ 10, 12, 14, 18 }; VERIFY( v == t ); + VERIFY( num == 4 ); } void test02() { std::vector v{ 0, 11, 0, 0, 22, 33, 0, 0, 44, 0 }; - std::erase(v, 0); + auto num = std::erase(v, 0); std::vector t{ 11, 22, 33, 44 }; VERIFY( v == t ); - std::erase(v, 55); + VERIFY( num == 6 ); + num = std::erase(v, 55); VERIFY( v == t ); + VERIFY( num == 0 ); } int -- 2.30.2