public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: "Franथईois Dumont" <fdumont@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org
Subject: [gcc r12-5840] libstdc++: [_GLIBCXX_DEBUG] Enhance std::erase_if for vector/deque
Date: Wed,  8 Dec 2021 18:10:43 +0000 (GMT)	[thread overview]
Message-ID: <20211208181043.A42E23858D3C@sourceware.org> (raw)

https://gcc.gnu.org/g:e7fac1e1a5858abc9737518ddbdac780fd2ad8b8

commit r12-5840-ge7fac1e1a5858abc9737518ddbdac780fd2ad8b8
Author: François Dumont <fdumont@gcc.gnu.org>
Date:   Sun Nov 21 11:56:40 2021 +0100

    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.

Diff:
---
 libstdc++-v3/include/std/deque  | 30 ++++++++++++++++++++++--------
 libstdc++-v3/include/std/vector | 30 ++++++++++++++++++++++--------
 2 files changed, 44 insertions(+), 16 deletions(-)

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


                 reply	other threads:[~2021-12-08 18:10 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211208181043.A42E23858D3C@sourceware.org \
    --to=fdumont@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    --cc=libstdc++-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).