public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH][_GLIBCXX_DEBUG] Enhance std::erase_if for vector/deque
@ 2021-11-21 11:24 François Dumont
  2021-12-08 16:17 ` Jonathan Wakely
  0 siblings, 1 reply; 2+ messages in thread
From: François Dumont @ 2021-11-21 11:24 UTC (permalink / raw)
  To: libstdc++; +Cc: gcc-patches

[-- Attachment #1: Type: text/plain, Size: 586 bytes --]

I tried to use the same approach I used for node based containers but 
got ambiguity on erase calls. I think this simple version will do the work.

     libstdc++: [_GLIBCXX_DEBUG] Enhance std::erase_if for vector/deque

     libstdc++-v3/ChangeLog:

             * include/std/deque (erase_if): Use _GLIBCXX_STD_C 
container reference and
             __niter_wrap to limit _GLIBCXX_DEBUG mode impact.
             * include/std/vector (erase_if): Likewise.

Tested under Linux x86_64 normal and _GLIBCXX_DEBUG modes.

Ok to commit ?

François


[-- Attachment #2: erase_if.patch --]
[-- Type: text/x-patch, Size: 3684 bytes --]

diff --git a/libstdc++-v3/include/std/deque b/libstdc++-v3/include/std/deque
index 473479c44ac..0a3541af554 100644
--- a/libstdc++-v3/include/std/deque
+++ b/libstdc++-v3/include/std/deque
@@ -96,12 +96,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     erase_if(deque<_Tp, _Alloc>& __cont, _Predicate __pred)
     {
       using namespace __gnu_cxx;
+      _GLIBCXX_STD_C::deque<_Tp, _Alloc>& __ucont = __cont;
       const auto __osz = __cont.size();
-      const auto __end = __cont.end();
-      auto __removed = std::__remove_if(__cont.begin(), __end,
+      const auto __end = __ucont.end();
+      auto __removed = std::__remove_if(__ucont.begin(), __end,
 					__ops::__pred_iter(std::ref(__pred)));
-      __cont.erase(__removed, __end);
-      return __osz - __cont.size();
+      if (__removed != __end)
+	{
+	  __cont.erase(__niter_wrap(__cont.begin(), __removed),
+		       __cont.end());
+	  return __osz - __cont.size();
+	}
+
+      return 0;
     }
 
   template<typename _Tp, typename _Alloc, typename _Up>
@@ -109,12 +116,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     erase(deque<_Tp, _Alloc>& __cont, const _Up& __value)
     {
       using namespace __gnu_cxx;
+      _GLIBCXX_STD_C::deque<_Tp, _Alloc>& __ucont = __cont;
       const auto __osz = __cont.size();
-      const auto __end = __cont.end();
-      auto __removed = std::__remove_if(__cont.begin(), __end,
+      const auto __end = __ucont.end();
+      auto __removed = std::__remove_if(__ucont.begin(), __end,
 					__ops::__iter_equals_val(__value));
-      __cont.erase(__removed, __end);
-      return __osz - __cont.size();
+      if (__removed != __end)
+	{
+	  __cont.erase(__niter_wrap(__cont.begin(), __removed),
+		       __cont.end());
+	  return __osz - __cont.size();
+	}
+
+      return 0;
     }
 _GLIBCXX_END_NAMESPACE_VERSION
 } // namespace std
diff --git a/libstdc++-v3/include/std/vector b/libstdc++-v3/include/std/vector
index 890b0ddb3eb..b648b3d7309 100644
--- a/libstdc++-v3/include/std/vector
+++ b/libstdc++-v3/include/std/vector
@@ -107,12 +107,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     erase_if(vector<_Tp, _Alloc>& __cont, _Predicate __pred)
     {
       using namespace __gnu_cxx;
+      _GLIBCXX_STD_C::vector<_Tp, _Alloc>& __ucont = __cont;
       const auto __osz = __cont.size();
-      const auto __end = __cont.end();
-      auto __removed = std::__remove_if(__cont.begin(), __end,
+      const auto __end = __ucont.end();
+      auto __removed = std::__remove_if(__ucont.begin(), __end,
 					__ops::__pred_iter(std::ref(__pred)));
-      __cont.erase(__removed, __end);
-      return __osz - __cont.size();
+      if (__removed != __end)
+	{
+	  __cont.erase(__niter_wrap(__cont.begin(), __removed),
+		       __cont.end());
+	  return __osz - __cont.size();
+	}
+
+      return 0;
     }
 
   template<typename _Tp, typename _Alloc, typename _Up>
@@ -121,12 +128,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     erase(vector<_Tp, _Alloc>& __cont, const _Up& __value)
     {
       using namespace __gnu_cxx;
+      _GLIBCXX_STD_C::vector<_Tp, _Alloc>& __ucont = __cont;
       const auto __osz = __cont.size();
-      const auto __end = __cont.end();
-      auto __removed = std::__remove_if(__cont.begin(), __end,
+      const auto __end = __ucont.end();
+      auto __removed = std::__remove_if(__ucont.begin(), __end,
 					__ops::__iter_equals_val(__value));
-      __cont.erase(__removed, __end);
-      return __osz - __cont.size();
+      if (__removed != __end)
+	{
+	  __cont.erase(__niter_wrap(__cont.begin(), __removed),
+		       __cont.end());
+	  return __osz - __cont.size();
+	}
+
+      return 0;
     }
 _GLIBCXX_END_NAMESPACE_VERSION
 } // namespace std

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH][_GLIBCXX_DEBUG] Enhance std::erase_if for vector/deque
  2021-11-21 11:24 [PATCH][_GLIBCXX_DEBUG] Enhance std::erase_if for vector/deque François Dumont
@ 2021-12-08 16:17 ` Jonathan Wakely
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Wakely @ 2021-12-08 16:17 UTC (permalink / raw)
  To: François Dumont; +Cc: libstdc++, gcc-patches

On Sun, 21 Nov 2021 at 11:26, François Dumont via Libstdc++
<libstdc++@gcc.gnu.org> wrote:
>
> I tried to use the same approach I used for node based containers but
> got ambiguity on erase calls. I think this simple version will do the work.
>
>      libstdc++: [_GLIBCXX_DEBUG] Enhance std::erase_if for vector/deque
>
>      libstdc++-v3/ChangeLog:
>
>              * include/std/deque (erase_if): Use _GLIBCXX_STD_C
> container reference and
>              __niter_wrap to limit _GLIBCXX_DEBUG mode impact.
>              * include/std/vector (erase_if): Likewise.
>
> Tested under Linux x86_64 normal and _GLIBCXX_DEBUG modes.
>
> Ok to commit ?

OK, thanks.


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-12-08 16:17 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-21 11:24 [PATCH][_GLIBCXX_DEBUG] Enhance std::erase_if for vector/deque François Dumont
2021-12-08 16:17 ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).