On 11/26/18 6:18 AM, Jonathan Wakely wrote:
On 24/11/18 13:54 -0500, Ed Smith-Rowland wrote:
All,

I's very late but uniform container erasure is, I think, the last little tidbit to graduate from fundamentals/v2 to std at the last meeting.  I think it would be a shame not to nudge this into gcc-9.  The routines are very short so I just copied them. Ditto the testcases (with adjustments).  The node erasure tool was moved from experimental to std/bits and adjustments made to affected *set/*map headers.

The experimental code has been in our tree since 2015.

This builds and tests clean on x86_64-linux.

OK for trunk as it only touches experimental C++2a and TS material.
Thanks.

I pointed out to the committee that the erase_if functions added to
C++20 completely overlook P0646R1 "Improving the Return Value of
Erase-Like Algorithms" and so fail to preserve the useful information
returned by the members of the linked list containers.
Is *this* what you has in mind for this?  I basically return the number of erased items for all the things.

I expect that to get fixed as a defect. If you have time and
motivation, I think we should make that change proactively in
libstdc++. The Library Fundamentals versions can continue to return
void, but the ones in namespace std can return the number of elements
removed

Ed


2018-11-29  Edward Smith-Rowland  <3dw...@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.

Index: include/bits/erase_if.h
===================================================================
--- include/bits/erase_if.h     (revision 266623)
+++ include/bits/erase_if.h     (working copy)
@@ -41,17 +41,22 @@
   namespace __detail
   {
     template<typename _Container, typename _Predicate>
-      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
 
Index: include/std/forward_list
===================================================================
--- include/std/forward_list    (revision 266623)
+++ include/std/forward_list    (working copy)
@@ -66,16 +66,17 @@
 {
 _GLIBCXX_BEGIN_NAMESPACE_VERSION
   template<typename _Tp, typename _Alloc, typename _Predicate>
-    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<typename _Tp, typename _Alloc, typename _Up>
-    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
Index: include/std/list
===================================================================
--- include/std/list    (revision 266623)
+++ include/std/list    (working copy)
@@ -90,16 +90,17 @@
 {
 _GLIBCXX_BEGIN_NAMESPACE_VERSION
   template<typename _Tp, typename _Alloc, typename _Predicate>
-    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<typename _Tp, typename _Alloc, typename _Up>
-    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
Index: include/std/map
===================================================================
--- include/std/map     (revision 266623)
+++ include/std/map     (working copy)
@@ -97,15 +97,15 @@
 _GLIBCXX_BEGIN_NAMESPACE_VERSION
   template<typename _Key, typename _Tp, typename _Compare, typename _Alloc,
           typename _Predicate>
-    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<typename _Key, typename _Tp, typename _Compare, typename _Alloc,
           typename _Predicate>
-    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
Index: include/std/set
===================================================================
--- include/std/set     (revision 266623)
+++ include/std/set     (working copy)
@@ -93,15 +93,15 @@
 _GLIBCXX_BEGIN_NAMESPACE_VERSION
   template<typename _Key, typename _Compare, typename _Alloc,
           typename _Predicate>
-    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<typename _Key, typename _Compare, typename _Alloc,
           typename _Predicate>
-    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
Index: include/std/string
===================================================================
--- include/std/string  (revision 266624)
+++ include/std/string  (working copy)
@@ -81,19 +81,23 @@
 _GLIBCXX_BEGIN_NAMESPACE_VERSION
   template<typename _CharT, typename _Traits, typename _Alloc,
           typename _Predicate>
-    inline void
+    inline 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 __cont.size() - __osz;
     }
 
   template<typename _CharT, typename _Traits, typename _Alloc, typename _Up>
-    inline void
+    inline 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 __cont.size() - __osz;
     }
 _GLIBCXX_END_NAMESPACE_VERSION
 } // namespace std
Index: include/std/unordered_map
===================================================================
--- include/std/unordered_map   (revision 266623)
+++ include/std/unordered_map   (working copy)
@@ -84,17 +84,17 @@
 _GLIBCXX_BEGIN_NAMESPACE_VERSION
   template<typename _Key, typename _Tp, typename _Hash, typename _CPred,
           typename _Alloc, typename _Predicate>
-    inline void
+    inline 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<typename _Key, typename _Tp, typename _Hash, typename _CPred,
           typename _Alloc, typename _Predicate>
-    inline void
+    inline 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
Index: include/std/unordered_set
===================================================================
--- include/std/unordered_set   (revision 266623)
+++ include/std/unordered_set   (working copy)
@@ -84,17 +84,17 @@
 _GLIBCXX_BEGIN_NAMESPACE_VERSION
   template<typename _Key, typename _Hash, typename _CPred, typename _Alloc,
           typename _Predicate>
-    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<typename _Key, typename _Hash, typename _CPred, typename _Alloc,
           typename _Predicate>
-    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
Index: include/std/vector
===================================================================
--- include/std/vector  (revision 266624)
+++ include/std/vector  (working copy)
@@ -98,19 +98,23 @@
 {
 _GLIBCXX_BEGIN_NAMESPACE_VERSION
   template<typename _Tp, typename _Alloc, typename _Predicate>
-    inline void
+    inline 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 __cont.size() - __osz;
     }
 
   template<typename _Tp, typename _Alloc, typename _Up>
-    inline void
+    inline 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 __cont.size() - __osz;
     }
 _GLIBCXX_END_NAMESPACE_VERSION
 } // namespace std
Index: testsuite/21_strings/basic_string/erasure.cc
===================================================================
--- testsuite/21_strings/basic_string/erasure.cc        (revision 266623)
+++ testsuite/21_strings/basic_string/erasure.cc        (working copy)
@@ -30,8 +30,9 @@
   };
 
   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
@@ -38,10 +39,12 @@
 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
Index: testsuite/23_containers/deque/erasure.cc
===================================================================
--- testsuite/23_containers/deque/erasure.cc    (revision 266623)
+++ testsuite/23_containers/deque/erasure.cc    (working copy)
@@ -27,9 +27,10 @@
   auto is_odd = [](const int i) { return i % 2 != 0; };
 
   std::deque<int> 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<int> t{ 10, 12, 14, 18 };
   VERIFY( d == t );
+  VERIFY( num == 4 );
 }
 
 void
@@ -36,11 +37,13 @@
 test02()
 {
   std::deque<int> d{ 10, 11, 12, 14, 15, 17, 18, 19 };
-  std::erase(d, 14);
+  auto num = std::erase(d, 14);
   std::deque<int> t{ 10, 11, 12, 15, 17, 18, 19 };
   VERIFY( d == t );
-  std::erase(d, 20);
+  VERIFY( num == 4 );
+  num = std::erase(d, 20);
   VERIFY( d == t );
+  VERIFY( num == 0 );
 }
 
 int
Index: testsuite/23_containers/forward_list/erasure.cc
===================================================================
--- testsuite/23_containers/forward_list/erasure.cc     (revision 266623)
+++ testsuite/23_containers/forward_list/erasure.cc     (working copy)
@@ -27,9 +27,10 @@
   auto is_odd = [](const int i) { return i % 2 != 0; };
 
   std::forward_list<int> 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<int> t{ 10, 12, 14, 18 };
   VERIFY( fl == t );
+  VERIFY( num == 4 );
 }
 
 void
@@ -36,11 +37,13 @@
 test02()
 {
   std::forward_list<int> fl{ 10, 11, 12, 14, 15, 17, 18, 19 };
-  std::erase(fl, 14);
+  auto num = std::erase(fl, 14);
   std::forward_list<int> 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
Index: testsuite/23_containers/list/erasure.cc
===================================================================
--- testsuite/23_containers/list/erasure.cc     (revision 266623)
+++ testsuite/23_containers/list/erasure.cc     (working copy)
@@ -36,11 +36,13 @@
 test02()
 {
   std::list<int> l{ 0, 11, 0, 0, 22, 33, 0, 0, 44, 0 };
-  std::erase(l, 0);
+  auto num = std::erase(l, 0);
   std::list<int> 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
Index: testsuite/23_containers/map/erasure.cc
===================================================================
--- testsuite/23_containers/map/erasure.cc      (revision 266623)
+++ testsuite/23_containers/map/erasure.cc      (working copy)
@@ -33,10 +33,11 @@
                                { 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<int, std::string> t{ { 10, "A" }, { 12, "C" },
                                { 14, "D" }, { 18, "G" } };
   VERIFY( m == t );
+  VERIFY( num == 4 );
 }
 
 void
@@ -46,10 +47,11 @@
                                      { 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<int, std::string> t{ { 20, "S" }, { 22, "U" },
                                     { 22, "V" }, { 24, "Y" } };
   VERIFY( mm == t );
+  VERIFY( num == 4 );
 }
 
 int
Index: testsuite/23_containers/set/erasure.cc
===================================================================
--- testsuite/23_containers/set/erasure.cc      (revision 266623)
+++ testsuite/23_containers/set/erasure.cc      (working copy)
@@ -27,9 +27,10 @@
 test01()
 {
   std::set<int> 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<int> t{ 10, 12, 14, 18 };
   VERIFY( s == t );
+  VERIFY( num == 4 );
 }
 
 void
@@ -36,9 +37,10 @@
 test02()
 {
   std::multiset<int> 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<int> t{ 20, 22, 22, 24 };
   VERIFY( ms == t );
+  VERIFY( num == 4 );
 }
 
 int
Index: testsuite/23_containers/unordered_map/erasure.cc
===================================================================
--- testsuite/23_containers/unordered_map/erasure.cc    (revision 266623)
+++ testsuite/23_containers/unordered_map/erasure.cc    (working copy)
@@ -33,10 +33,11 @@
                                           { 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<int, std::string> t{ { 10, "A" }, { 12, "C" },
                                          { 14, "D" }, { 18, "G" } };
   VERIFY( um == t );
+  VERIFY( num == 4 );
 }
 
 void
@@ -46,10 +47,11 @@
                                                 { 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<int, std::string> t{ { 20, "S" }, { 22, "U" },
                                               { 22, "V" }, { 24, "Y" } };
   VERIFY( umm == t );
+  VERIFY( num == 4 );
 }
 
 int
Index: testsuite/23_containers/unordered_set/erasure.cc
===================================================================
--- testsuite/23_containers/unordered_set/erasure.cc    (revision 266623)
+++ testsuite/23_containers/unordered_set/erasure.cc    (working copy)
@@ -27,9 +27,10 @@
   auto is_odd = [](const int i) { return i % 2 != 0; };
 
   std::unordered_set<int> 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<int> t{ 10, 12, 14, 18 };
   VERIFY( us == t );
+  VERIFY( num == 4 );
 }
 
 void
@@ -38,9 +39,10 @@
   auto is_odd = [](const int i) { return i % 2 != 0; };
 
   std::unordered_multiset<int> 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<int> t{ 20, 22, 22, 24 };
   VERIFY( ums == t );
+  VERIFY( num == 4 );
 }
 
 int
Index: testsuite/23_containers/vector/erasure.cc
===================================================================
--- testsuite/23_containers/vector/erasure.cc   (revision 266623)
+++ testsuite/23_containers/vector/erasure.cc   (working copy)
@@ -27,9 +27,10 @@
   auto is_odd = [](const int i) { return i % 2 != 0; };
 
   std::vector<int> 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<int> t{ 10, 12, 14, 18 };
   VERIFY( v == t );
+  VERIFY( num == 4 );
 }
 
 void
@@ -36,11 +37,13 @@
 test02()
 {
   std::vector<int> v{ 0, 11, 0, 0, 22, 33, 0, 0, 44, 0 };
-  std::erase(v, 0);
+  auto num = std::erase(v, 0);
   std::vector<int> t{ 11, 22, 33, 44 };
   VERIFY( v == t );
-  std::erase(v, 55);
+  VERIFY( num == 4 );
+  num = std::erase(v, 55);
   VERIFY( v == t );
+  VERIFY( num == 0 );
 }
 
 int

Reply via email to