[gcc(refs/users/ppalka/heads/libstdcxx-constrained-algos)] Add ranges::remove and ranges::remove_if

Patrick Palka ppalka@gcc.gnu.org
Tue Jan 21 15:25:00 GMT 2020


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

commit e3f1b603e025dced755ba604d9113b770fcf9164
Author: Patrick Palka <ppalka@gcc.gnu.org>
Date:   Tue Jan 21 10:25:18 2020 -0500

    Add ranges::remove and ranges::remove_if

Diff:
---
 libstdc++-v3/include/bits/ranges_algo.h            | 58 +++++++++++++
 .../testsuite/25_algorithms/remove/constrained.cc  | 97 ++++++++++++++++++++++
 .../25_algorithms/remove_if/constrained.cc         | 97 ++++++++++++++++++++++
 3 files changed, 252 insertions(+)

diff --git a/libstdc++-v3/include/bits/ranges_algo.h b/libstdc++-v3/include/bits/ranges_algo.h
index 5de9314..4212353 100644
--- a/libstdc++-v3/include/bits/ranges_algo.h
+++ b/libstdc++-v3/include/bits/ranges_algo.h
@@ -1279,6 +1279,64 @@ namespace ranges
 				std::move(__gen));
       }
 
+    template<permutable _Iter, sentinel_for<_Iter> _Sent, class _Proj = identity,
+	     indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
+      constexpr subrange<_Iter>
+      remove_if(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {})
+      {
+	__first = ranges::find_if(__first, __last, __pred, __proj);
+	if (__first == __last)
+	  return {__first, __last};
+
+	auto __result = __first;
+	++__first;
+	for (; __first != __last; ++__first)
+	  if (!std::__invoke(__pred, std::__invoke(__proj, *__first)))
+	    {
+	      *__result = std::move(*__first);
+	      ++__result;
+	    }
+
+	return {__result, __last};
+      }
+
+    template<forward_range _Range, class _Proj = identity,
+	     indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred>
+      requires permutable<iterator_t<_Range>>
+      constexpr safe_subrange_t<_Range>
+      remove_if(_Range&& __r, _Pred __pred, _Proj __proj = {})
+      {
+	return ranges::remove_if(ranges::begin(__r), ranges::end(__r),
+				 std::move(__pred), std::move(__proj));
+      }
+
+    template<permutable _Iter, sentinel_for<_Iter> _Sent,
+	     class _Tp, class _Proj = identity>
+      requires indirect_binary_predicate<ranges::equal_to,
+					 projected<_Iter, _Proj>,
+					 const _Tp*>
+      constexpr subrange<_Iter>
+      remove(_Iter __first, _Sent __last, const _Tp& __value, _Proj __proj = {})
+      {
+	auto __pred = [&] (auto&& __arg) {
+	  return std::forward<decltype(__arg)>(__arg) == __value;
+	};
+	return ranges::remove_if(__first, __last,
+				 std::move(__pred), std::move(__proj));
+      }
+
+    template<forward_range _Range, class _Tp, class _Proj = identity>
+      requires permutable<iterator_t<_Range>> &&
+	       indirect_binary_predicate<ranges::equal_to,
+					 projected<iterator_t<_Range>, _Proj>,
+					 const _Tp*>
+      constexpr safe_subrange_t<_Range>
+      remove(_Range&& __r, const _Tp& __value, _Proj __proj = {})
+      {
+	return ranges::remove(ranges::begin(__r), ranges::end(__r),
+			      __value, std::move(__proj));
+      }
+
 } // namespace ranges
 _GLIBCXX_END_NAMESPACE_VERSION
 } // namespace std
diff --git a/libstdc++-v3/testsuite/25_algorithms/remove/constrained.cc b/libstdc++-v3/testsuite/25_algorithms/remove/constrained.cc
new file mode 100644
index 0000000..39a002f
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/remove/constrained.cc
@@ -0,0 +1,97 @@
+// Copyright (C) 2020 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do run { target c++2a } }
+
+#include <algorithm>
+#include <testsuite_hooks.h>
+#include <testsuite_iterators.h>
+
+using __gnu_test::test_container;
+using __gnu_test::test_range;
+using __gnu_test::input_iterator_wrapper;
+using __gnu_test::output_iterator_wrapper;
+using __gnu_test::forward_iterator_wrapper;
+
+namespace ranges = std::ranges;
+
+struct X
+{
+  int i;
+};
+
+void
+test01()
+{
+  int x[5] = { 1, 2, 3, 4, 5 };
+  const int y[4] = { 1, 2, 4, 5 };
+  auto res = ranges::remove(x, 3);
+  VERIFY( res.begin() == x+4 && res.end() == x+5 );
+  VERIFY( ranges::equal(x, x+4, y, y+4) );
+}
+
+void
+test02()
+{
+  int x[1];
+  test_container<int, forward_iterator_wrapper> c(x, x);
+  auto res = ranges::remove(c, 1);
+  VERIFY( res.begin().ptr == x && res.end().ptr == x );
+}
+
+void
+test03()
+{
+  int x[1] = {1};
+  test_container<int, forward_iterator_wrapper> c(x);
+  auto res = ranges::remove(c, 0);
+  VERIFY( res.begin().ptr == x+1 && res.end().ptr == x+1 );
+  res = ranges::remove(c, 1);
+  VERIFY( res.begin().ptr == x && res.end().ptr == x+1 );
+}
+
+void
+test04()
+{
+  X x[8] = { {0}, {1}, {0}, {1}, {0}, {0}, {1}, {1} };
+  const int y[4] = { 0, 0, 0, 0 };
+  test_container<X, forward_iterator_wrapper> c(x);
+  auto res = ranges::remove(c, 1, &X::i);
+  VERIFY( res.begin().ptr == x+4 && res.end().ptr == x+8 );
+  VERIFY( ranges::equal(x, x+4, y, y+4, {}, &X::i) );
+}
+
+constexpr bool
+test05()
+{
+  int x[6] = { 3, 2, 3, 3, 5, 3 };
+  const int y[2] = { 2, 5 };
+  auto res = ranges::remove(x, 3);
+  return ranges::equal(x, res.begin(), y, y+2);
+}
+
+
+int
+main()
+{
+  test01();
+  test02();
+  test03();
+  test04();
+  static_assert(test05());
+}
diff --git a/libstdc++-v3/testsuite/25_algorithms/remove_if/constrained.cc b/libstdc++-v3/testsuite/25_algorithms/remove_if/constrained.cc
new file mode 100644
index 0000000..25a99a0
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/remove_if/constrained.cc
@@ -0,0 +1,97 @@
+// Copyright (C) 2020 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do run { target c++2a } }
+
+#include <algorithm>
+#include <testsuite_hooks.h>
+#include <testsuite_iterators.h>
+
+using __gnu_test::test_container;
+using __gnu_test::test_range;
+using __gnu_test::input_iterator_wrapper;
+using __gnu_test::output_iterator_wrapper;
+using __gnu_test::forward_iterator_wrapper;
+
+namespace ranges = std::ranges;
+
+struct X
+{
+  int i;
+};
+
+void
+test01()
+{
+  int x[5] = { 1, 2, 3, 4, 5 };
+  const int y[4] = { 1, 2, 4, 5 };
+  auto res = ranges::remove_if(x, [] (int a) { return a == 3; });
+  VERIFY( res.begin() == x+4 && res.end() == x+5 );
+  VERIFY( ranges::equal(x, x+4, y, y+4) );
+}
+
+void
+test02()
+{
+  int x[1];
+  test_container<int, forward_iterator_wrapper> c(x, x);
+  auto res = ranges::remove_if(c, [] (int a) { return a == 1; });
+  VERIFY( res.begin().ptr == x && res.end().ptr == x );
+}
+
+void
+test03()
+{
+  int x[1] = {1};
+  test_container<int, forward_iterator_wrapper> c(x);
+  auto res = ranges::remove_if(c, [] (int a) { return a == 0; });
+  VERIFY( res.begin().ptr == x+1 && res.end().ptr == x+1 );
+  res = ranges::remove_if(c, [] (int a) { return a == 1; });
+  VERIFY( res.begin().ptr == x && res.end().ptr == x+1 );
+}
+
+void
+test04()
+{
+  X x[8] = { {0}, {1}, {0}, {1}, {0}, {0}, {1}, {1} };
+  const int y[4] = { 0, 0, 0, 0 };
+  test_container<X, forward_iterator_wrapper> c(x);
+  auto res = ranges::remove_if(c, [] (int a) { return a == 1; }, &X::i);
+  VERIFY( res.begin().ptr == x+4 && res.end().ptr == x+8 );
+  VERIFY( ranges::equal(x, x+4, y, y+4, {}, &X::i) );
+}
+
+constexpr bool
+test05()
+{
+  int x[6] = { 3, 2, 3, 3, 5, 3 };
+  const int y[2] = { 2, 5 };
+  auto res = ranges::remove_if(x, [] (int a) { return a == 3; });
+  return ranges::equal(x, res.begin(), y, y+2);
+}
+
+
+int
+main()
+{
+  test01();
+  test02();
+  test03();
+  test04();
+  static_assert(test05());
+}



More information about the Libstdc++-cvs mailing list