[gcc(refs/users/ppalka/heads/libstdcxx-constrained-algos)] Add ranges::replace_copy and ranges::replace_copy_if
Patrick Palka
ppalka@gcc.gnu.org
Mon Jan 20 21:56:00 GMT 2020
https://gcc.gnu.org/g:f92bcdd9e93fd476d8229fafc1a2c68c69cff0ea
commit f92bcdd9e93fd476d8229fafc1a2c68c69cff0ea
Author: Patrick Palka <ppalka@gcc.gnu.org>
Date: Mon Jan 20 14:47:25 2020 -0500
Add ranges::replace_copy and ranges::replace_copy_if
Diff:
---
libstdc++-v3/include/bits/ranges_algo.h | 72 +++++++++++++
.../25_algorithms/replace_copy/constrained.cc | 107 +++++++++++++++++++
.../25_algorithms/replace_copy_if/constrained.cc | 116 +++++++++++++++++++++
3 files changed, 295 insertions(+)
diff --git a/libstdc++-v3/include/bits/ranges_algo.h b/libstdc++-v3/include/bits/ranges_algo.h
index cef1f54..a950e99 100644
--- a/libstdc++-v3/include/bits/ranges_algo.h
+++ b/libstdc++-v3/include/bits/ranges_algo.h
@@ -1115,6 +1115,78 @@ namespace ranges
std::move(__proj));
}
+ template<typename _Iter, typename _Out>
+ using replace_copy_result = copy_result<_Iter, _Out>;
+
+ template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
+ typename _Tp1, typename _Tp2, output_iterator<const _Tp2&> _Out,
+ typename _Proj = identity>
+ requires indirectly_copyable<_Iter, _Out>
+ && indirect_binary_predicate<ranges::equal_to,
+ projected<_Iter, _Proj>, const _Tp1*>
+ constexpr replace_copy_result<_Iter, _Out>
+ replace_copy(_Iter __first, _Sent __last, _Out __result,
+ const _Tp1& __old_value, const _Tp2& __new_value,
+ _Proj __proj = {})
+ {
+ for (; __first != __last; ++__first, (void)++__result)
+ if (std::__invoke(__proj, *__first) == __old_value)
+ *__result = __new_value;
+ else
+ *__result = *__first;
+ return {__first, __result};
+ }
+
+ template<input_range _Range, typename _Tp1, typename _Tp2,
+ output_iterator<const _Tp2&> _Out, typename _Proj = identity>
+ requires indirectly_copyable<iterator_t<_Range>, _Out>
+ && indirect_binary_predicate<ranges::equal_to,
+ projected<iterator_t<_Range>, _Proj>,
+ const _Tp1*>
+ constexpr replace_copy_result<safe_iterator_t<_Range>, _Out>
+ replace_copy(_Range&& __r, _Out __result,
+ const _Tp1& __old_value, const _Tp2& __new_value,
+ _Proj __proj = {})
+ {
+ return ranges::replace_copy(ranges::begin(__r), ranges::end(__r),
+ std::move(__result), __old_value,
+ __new_value, std::move(__proj));
+ }
+
+ template<typename _Iter, typename _Out>
+ using replace_copy_if_result = copy_result<_Iter, _Out>;
+
+ template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
+ typename _Tp, output_iterator<const _Tp&> _Out,
+ typename _Proj = identity,
+ indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
+ requires indirectly_copyable<_Iter, _Out>
+ constexpr replace_copy_if_result<_Iter, _Out>
+ replace_copy_if(_Iter __first, _Sent __last, _Out __result,
+ _Pred __pred, const _Tp& __new_value, _Proj __proj = {})
+ {
+ for (; __first != __last; ++__first, (void)++__result)
+ if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
+ *__result = __new_value;
+ else
+ *__result = *__first;
+ return {__first, __result};
+ }
+
+ template<input_range _Range,
+ typename _Tp, output_iterator<const _Tp&> _Out,
+ typename _Proj = identity,
+ indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred>
+ requires indirectly_copyable<iterator_t<_Range>, _Out>
+ constexpr replace_copy_if_result<safe_iterator_t<_Range>, _Out>
+ replace_copy_if(_Range&& __r, _Out __result,
+ _Pred __pred, const _Tp& __new_value, _Proj __proj = {})
+ {
+ return ranges::replace_copy_if(ranges::begin(__r), ranges::end(__r),
+ std::move(__result), 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_copy/constrained.cc b/libstdc++-v3/testsuite/25_algorithms/replace_copy/constrained.cc
new file mode 100644
index 0000000..0d792bc
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/replace_copy/constrained.cc
@@ -0,0 +1,107 @@
+// 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()
+{
+ {
+ const X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} };
+ X y[5];
+ X z[5] = { {2}, {2}, {6}, {9}, {10} };
+ auto [in,out] = ranges::replace_copy(x, x+5, y, 8, X{9}, &X::i);
+ VERIFY( in == x+5 && out == y+5 );
+ VERIFY( ranges::equal(y, z) );
+ }
+
+ {
+ const X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} };
+ X y[5];
+ X z[5] = { {2}, {2}, {6}, {8}, {10} };
+ auto [in,out] = ranges::replace_copy(x, x+5, y, 7, X{9}, &X::i);
+ VERIFY( in == x+5 && out == y+5 );
+ VERIFY( ranges::equal(x, x+5, y, y+5) && ranges::equal(y, z) );
+ }
+
+ {
+ X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} };
+ X y[6];
+ X z[6] = { {7}, {7}, {6}, {8}, {10}, {11} };
+ test_container<X, forward_iterator_wrapper> cx(x), cy(y), cz(z);
+ auto [in, out] = ranges::replace_copy(cx, cy.begin(), 2, X{7}, &X::i);
+ VERIFY( in == cx.end() && out == cy.end() );
+ VERIFY( ranges::equal(cy, cz) );
+ }
+
+ {
+ X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} };
+ X y[6];
+ X z[6] = { {7}, {7}, {6}, {8}, {10}, {11} };
+ test_range<X, forward_iterator_wrapper> cx(x), cy(y), cz(z);
+ auto [in, out] = ranges::replace_copy(cx, cy.begin(), 2, X{7}, &X::i);
+ VERIFY( in == cx.end() && out == cy.end() );
+ VERIFY( ranges::equal(cy, cz) );
+ }
+}
+
+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} };
+ Y z[] = { {4,5}, {2,4}, {4,5} };
+ auto [in, out] = ranges::replace_copy(x, y, 3, Y{4,5}, &Y::i);
+ ok &= in == x+3;
+ ok &= out == y+3;
+ ok &= ranges::equal(y, z, {}, &Y::i, &Y::i);
+ ok &= ranges::equal(y, z, {}, &Y::j, &Y::j);
+ return ok;
+}
+
+int
+main()
+{
+ test01();
+ static_assert(test02());
+}
diff --git a/libstdc++-v3/testsuite/25_algorithms/replace_copy_if/constrained.cc b/libstdc++-v3/testsuite/25_algorithms/replace_copy_if/constrained.cc
new file mode 100644
index 0000000..599fdfe
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/replace_copy_if/constrained.cc
@@ -0,0 +1,116 @@
+// 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_negative_p = [] (int a) { return a < 0; };
+ auto is_two_p = [] (int a) { return a == 2; };
+
+ {
+ const X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} };
+ X y[5];
+ X z[5] = { {9}, {9}, {6}, {8}, {10} };
+ auto [in, out] = ranges::replace_copy_if(x, x+5, y,
+ is_two_p, X{9}, &X::i);
+ VERIFY( in == x+5 && out == y+5 );
+ VERIFY( ranges::equal(y, z) );
+ }
+
+ {
+ const X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} };
+ X y[5];
+ X z[5] = { {2}, {2}, {6}, {8}, {10} };
+ auto [in, out] = ranges::replace_copy_if(x, x+5, y,
+ is_negative_p, X{9}, &X::i);
+ VERIFY( in == x+5 && out == y+5 );
+ VERIFY( ranges::equal(x, x+5, y, y+5) && ranges::equal(y, z) );
+ }
+
+ {
+ X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} };
+ X y[6];
+ X z[6] = { {7}, {7}, {6}, {8}, {10}, {11} };
+ test_container<X, forward_iterator_wrapper> cx(x), cy(y), cz(z);
+ auto [in, out] = ranges::replace_copy_if(cx, cy.begin(),
+ is_two_p, X{7}, &X::i);
+ VERIFY( in == cx.end() && out == cy.end() );
+ VERIFY( ranges::equal(cy, cz) );
+ }
+
+ {
+ X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} };
+ X y[6];
+ X z[6] = { {7}, {7}, {6}, {8}, {10}, {11} };
+ test_range<X, forward_iterator_wrapper> cx(x), cy(y), cz(z);
+ auto [in, out] = ranges::replace_copy_if(cx, cy.begin(),
+ is_two_p, X{7}, &X::i);
+ VERIFY( in == cx.end() && out == cy.end() );
+ VERIFY( ranges::equal(cy, cz) );
+ }
+}
+
+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} };
+ Y z[] = { {4,5}, {2,4}, {4,5} };
+ auto [in, out]
+ = ranges::replace_copy_if(x, y,
+ [] (int a) { return a%2 == 1; }, Y{4,5}, &Y::i);
+ ok &= in == x+3;
+ ok &= out == y+3;
+ ok &= ranges::equal(y, z, {}, &Y::i, &Y::i);
+ ok &= ranges::equal(y, z, {}, &Y::j, &Y::j);
+ return ok;
+}
+
+int
+main()
+{
+ test01();
+ static_assert(test02());
+}
More information about the Libstdc++-cvs
mailing list