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

Patrick Palka ppalka@gcc.gnu.org
Mon Jan 20 19:04:00 GMT 2020


https://gcc.gnu.org/g:05aa6fc3cf757160342d65d8f19e6afc93dc85fe

commit 05aa6fc3cf757160342d65d8f19e6afc93dc85fe
Author: Patrick Palka <ppalka@gcc.gnu.org>
Date:   Mon Jan 20 14:03:18 2020 -0500

    Add ranges::replace and ranges::replace_if

Diff:
---
 libstdc++-v3/include/bits/ranges_algo.h            |  57 +++++++++++
 .../testsuite/25_algorithms/replace/constrained.cc | 101 ++++++++++++++++++++
 .../25_algorithms/replace_if/constrained.cc        | 106 +++++++++++++++++++++
 3 files changed, 264 insertions(+)

diff --git a/libstdc++-v3/include/bits/ranges_algo.h b/libstdc++-v3/include/bits/ranges_algo.h
index 244465f..6818e2e 100644
--- a/libstdc++-v3/include/bits/ranges_algo.h
+++ b/libstdc++-v3/include/bits/ranges_algo.h
@@ -1058,6 +1058,63 @@ namespace ranges
 				 std::move(__proj1), std::move(__proj2));
       }
 
+    template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
+	     typename _Tp1, typename _Tp2, typename _Proj = identity>
+      requires writable<_Iter, const _Tp2&> &&
+	       indirect_binary_predicate<ranges::equal_to,
+					 projected<_Iter, _Proj>, const _Tp1*>
+      constexpr _Iter
+      replace(_Iter __first, _Sent __last,
+	      const _Tp1& __old_value, const _Tp2& __new_value,
+	      _Proj __proj = {})
+      {
+	for (; __first != __last; ++__first)
+	  if (std::__invoke(__proj, *__first) == __old_value)
+	    *__first = __new_value;
+	return __first;
+      }
+
+    template<input_range R,
+	     typename _Tp1, typename _Tp2, typename _Proj = identity>
+      requires writable<iterator_t<R>, const _Tp2&> &&
+	       indirect_binary_predicate<ranges::equal_to,
+					 projected<iterator_t<R>, _Proj>,
+						   const _Tp1*>
+      constexpr safe_iterator_t<R>
+      replace(R&& __r,
+	      const _Tp1& __old_value, const _Tp2& __new_value,
+	      _Proj __proj = {})
+      {
+	return ranges::replace(ranges::begin(__r), ranges::end(__r),
+			       __old_value, __new_value, std::move(__proj));
+      }
+
+    template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
+	     typename _Tp, typename _Proj = identity,
+	     indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
+      requires writable<_Iter, const _Tp&>
+      constexpr _Iter
+      replace_if(_Iter __first, _Sent __last,
+		 _Pred __pred, const _Tp& __new_value, _Proj __proj = {})
+      {
+	for (; __first != __last; ++__first)
+	  if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
+	    *__first = __new_value;
+	return __first;
+      }
+
+    template<input_range R, typename _Tp, typename _Proj = identity,
+	     indirect_unary_predicate<projected<iterator_t<R>, _Proj>> _Pred>
+      requires writable<iterator_t<R>, const _Tp&>
+      constexpr safe_iterator_t<R>
+      replace_if(R&& __r,
+		 _Pred __pred, const _Tp& __new_value, _Proj __proj = {})
+      {
+	return ranges::replace_if(ranges::begin(__r), ranges::end(__r),
+				  std::move(__pred), __new_value,
+				  std::move(__proj));
+      }
+
 } // namespace ranges
 _GLIBCXX_END_NAMESPACE_VERSION
 } // namespace std
diff --git a/libstdc++-v3/testsuite/25_algorithms/replace/constrained.cc b/libstdc++-v3/testsuite/25_algorithms/replace/constrained.cc
new file mode 100644
index 0000000..b36a53a
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/replace/constrained.cc
@@ -0,0 +1,101 @@
+// 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::forward_iterator_wrapper;
+
+namespace ranges = std::ranges;
+
+struct X
+{
+  int i;
+
+  friend constexpr bool
+  operator==(const X& a, const X& b)
+  {
+    return a.i == b.i;
+  }
+};
+
+void
+test01()
+{
+    {
+      X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} };
+      X y[6] = { {2}, {2}, {6}, {9}, {10}, {11} };
+      auto res = ranges::replace(x, x+5, 8, X{9}, &X::i);
+      VERIFY( res == x+5 );
+      VERIFY( ranges::equal(x, y) );
+    }
+
+    {
+      X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} };
+      X y[6] = { {2}, {2}, {6}, {8}, {10}, {11} };
+      auto res = ranges::replace(x, x+5, 7, X{9}, &X::i);
+      VERIFY( res == x+5 );
+      VERIFY( ranges::equal(x, y) );
+    }
+
+    {
+      X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} };
+      X y[6] = { {7}, {7}, {6}, {8}, {10}, {11} };
+      test_container<X, forward_iterator_wrapper> cx(x), cy(y);
+      auto res = ranges::replace(cx, 2, X{7}, &X::i);
+      VERIFY( res == cx.end() );
+      VERIFY( ranges::equal(cx, cy) );
+    }
+
+    {
+      int x[6] = { {2}, {2}, {6}, {8}, {10}, {2} };
+      int y[6] = { {7}, {7}, {6}, {8}, {10}, {7} };
+      test_range<int, input_iterator_wrapper> rx(x), ry(y);
+      auto res = ranges::replace(rx, 2, 7);
+      VERIFY( res == rx.end() );
+      VERIFY( ranges::equal(rx, ry) );
+    }
+}
+
+struct Y { int i; int j; };
+
+constexpr bool
+test02()
+{
+  bool ok = true;
+  Y x[] = { {3,2}, {2,4}, {3,6} };
+  Y y[] = { {4,5}, {2,4}, {4,5} };
+  auto res = ranges::replace(x, 3, Y{4,5}, &Y::i);
+  ok &= res == x+3;
+  ok &= ranges::equal(x, y, {}, &Y::i, &Y::i);
+  ok &= ranges::equal(x, y, {}, &Y::j, &Y::j);
+  return ok;
+}
+
+int
+main()
+{
+  test01();
+  static_assert(test02());
+}
diff --git a/libstdc++-v3/testsuite/25_algorithms/replace_if/constrained.cc b/libstdc++-v3/testsuite/25_algorithms/replace_if/constrained.cc
new file mode 100644
index 0000000..ed95829
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/replace_if/constrained.cc
@@ -0,0 +1,106 @@
+// 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::forward_iterator_wrapper;
+
+namespace ranges = std::ranges;
+
+struct X
+{
+  int i;
+
+  friend constexpr bool
+  operator==(const X& a, const X& b)
+  {
+    return a.i == b.i;
+  }
+};
+
+void
+test01()
+{
+  auto is_even_p = [] (int a) { return a%2 == 0; };
+  auto is_negative_p = [] (int a) { return a < 0; };
+  auto is_two_p = [] (int a) { return a == 2; };
+    {
+      X x[6] = { {1}, {2}, {6}, {8}, {10}, {11} };
+      X y[6] = { {1}, {9}, {9}, {9}, {9}, {11} };
+      auto res = ranges::replace_if(x, x+5, is_even_p, X{9}, &X::i);
+      VERIFY( res == x+5 );
+      VERIFY( ranges::equal(x, y) );
+    }
+
+    {
+      X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} };
+      X y[6] = { {2}, {2}, {6}, {8}, {10}, {11} };
+      auto res = ranges::replace_if(x, x+5, is_negative_p, X{9}, &X::i);
+      VERIFY( res == x+5 );
+      VERIFY( ranges::equal(x, y) );
+    }
+
+    {
+      X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} };
+      X y[6] = { {7}, {7}, {6}, {8}, {10}, {11} };
+      test_container<X, forward_iterator_wrapper> cx(x), cy(y);
+      auto res = ranges::replace_if(cx, is_two_p, X{7}, &X::i);
+      VERIFY( res == cx.end() );
+      VERIFY( ranges::equal(cx, cy) );
+    }
+
+    {
+      int x[6] = { {2}, {2}, {6}, {8}, {10}, {2} };
+      int y[6] = { {7}, {7}, {6}, {8}, {10}, {7} };
+      test_range<int, input_iterator_wrapper> rx(x), ry(y);
+      auto res = ranges::replace_if(rx, is_two_p, 7);
+      VERIFY( res == rx.end() );
+      VERIFY( ranges::equal(rx, ry) );
+    }
+}
+
+struct Y { int i; int j; };
+
+constexpr bool
+test02()
+{
+  bool ok = true;
+  Y x[] = { {3,2}, {2,4}, {3,6} };
+  Y y[] = { {4,5}, {2,4}, {4,5} };
+  auto res = ranges::replace_if(x, [] (int a) { return a%2 == 1; },
+				Y{4,5}, &Y::i);
+  ok &= res == x+3;
+  ok &= ranges::equal(x, y, {}, &Y::i, &Y::i);
+  ok &= ranges::equal(x, y, {}, &Y::j, &Y::j);
+  return ok;
+}
+
+int
+main()
+{
+  test01();
+  static_assert(test02());
+}
+



More information about the Libstdc++-cvs mailing list