[gcc(refs/users/ppalka/heads/libstdcxx-constrained-algos)] Add ranges::transform
Patrick Palka
ppalka@gcc.gnu.org
Mon Jan 20 19:04:00 GMT 2020
https://gcc.gnu.org/g:a29ae06cd7476e9c81b69e67c9ad4b7f98edebef
commit a29ae06cd7476e9c81b69e67c9ad4b7f98edebef
Author: Patrick Palka <ppalka@gcc.gnu.org>
Date: Mon Jan 20 12:48:27 2020 -0500
Add ranges::transform
Diff:
---
libstdc++-v3/include/bits/ranges_algo.h | 90 +++++++++++++
.../25_algorithms/transform/constrained.cc | 147 +++++++++++++++++++++
2 files changed, 237 insertions(+)
diff --git a/libstdc++-v3/include/bits/ranges_algo.h b/libstdc++-v3/include/bits/ranges_algo.h
index 788837d..244465f 100644
--- a/libstdc++-v3/include/bits/ranges_algo.h
+++ b/libstdc++-v3/include/bits/ranges_algo.h
@@ -968,6 +968,96 @@ namespace ranges
ranges::begin(__r2), ranges::end(__r2));
}
+ template<typename _Iter, typename _Out>
+ using unary_transform_result = copy_result<_Iter, _Out>;
+
+ template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
+ weakly_incrementable _Out,
+ copy_constructible _F, typename _Proj = identity>
+ requires writable<_Out, indirect_result_t<_F&, projected<_Iter, _Proj>>>
+ constexpr unary_transform_result<_Iter, _Out>
+ transform(_Iter __first1, _Sent __last1, _Out __result,
+ _F __op, _Proj __proj = {})
+ {
+ for (; __first1 != __last1; ++__first1, (void)++__result)
+ *__result = std::__invoke(__op, std::__invoke(__proj, *__first1));
+ return {__first1, __result};
+ }
+
+ template<input_range _Range, weakly_incrementable _Out,
+ copy_constructible _F, typename _Proj = identity>
+ requires writable<_Out,
+ indirect_result_t<_F&, projected<iterator_t<_Range>,
+ _Proj>>>
+ constexpr unary_transform_result<safe_iterator_t<_Range>, _Out>
+ transform(_Range&& __r, _Out __result, _F __op, _Proj __proj = {})
+ {
+ return ranges::transform(ranges::begin(__r), ranges::end(__r),
+ std::move(__result),
+ std::move(__op), std::move(__proj));
+ }
+
+ template<typename _Iter1, typename _Iter2, typename _Out>
+ struct binary_transform_result {
+ [[no_unique_address]] _Iter1 in1;
+ [[no_unique_address]] _Iter2 in2;
+ [[no_unique_address]] _Out out;
+
+ template<typename _IIter1, typename _IIter2, typename _OOut>
+ requires convertible_to<const _Iter1&, _IIter1> &&
+ && convertible_to<const _Iter2&, _IIter2>
+ && convertible_to<const _Out&, _OOut>
+ operator binary_transform_result<_IIter1, _IIter2, _OOut>() const & {
+ return {in1, in2, out};
+ }
+
+ template<typename _IIter1, typename _IIter2, typename _OOut>
+ requires convertible_to<_Iter1, _IIter1>
+ && convertible_to<_Iter2, _IIter2>
+ && convertible_to<_Out, _OOut>
+ operator binary_transform_result<_IIter1, _IIter2, _OOut>() && {
+ return {std::move(in1), std::move(in2), std::move(out)};
+ }
+ };
+
+ template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
+ input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
+ weakly_incrementable _Out, copy_constructible _F,
+ typename _Proj1 = identity, typename _Proj2 = identity>
+ requires writable<_Out, indirect_result_t<_F&, projected<_Iter1, _Proj1>,
+ projected<_Iter2, _Proj2>>>
+ constexpr binary_transform_result<_Iter1, _Iter2, _Out>
+ transform(_Iter1 __first1, _Sent1 __last1, _Iter2 __first2, _Sent2 __last2,
+ _Out __result, _F __binary_op,
+ _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
+ {
+ for (; __first1 != __last1 && __first2 != __last2;
+ ++__first1, (void)++__first2, ++__result)
+ *__result = std::__invoke(__binary_op,
+ std::__invoke(__proj1, *__first1),
+ std::__invoke(__proj2, *__first2));
+ return {__first1, __first2, __result};
+ }
+
+ template<input_range _Range1, input_range _Range2,
+ weakly_incrementable _Out, copy_constructible _F,
+ typename _Proj1 = identity, typename _Proj2 = identity>
+ requires writable<_Out, indirect_result_t<_F&,
+ projected<iterator_t<_Range1>,
+ _Proj1>,
+ projected<iterator_t<_Range2>,
+ _Proj2>>>
+ constexpr binary_transform_result<safe_iterator_t<_Range1>,
+ safe_iterator_t<_Range2>, _Out>
+ transform(_Range1&& __r1, _Range2&& __r2, _Out __result,
+ _F __binary_op, _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
+ {
+ return ranges::transform(ranges::begin(__r1), ranges::end(__r1),
+ ranges::begin(__r2), ranges::end(__r2),
+ std::move(__result), std::move(__binary_op),
+ std::move(__proj1), std::move(__proj2));
+ }
+
} // namespace ranges
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace std
diff --git a/libstdc++-v3/testsuite/25_algorithms/transform/constrained.cc b/libstdc++-v3/testsuite/25_algorithms/transform/constrained.cc
new file mode 100644
index 0000000..2af2f38
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/transform/constrained.cc
@@ -0,0 +1,147 @@
+// 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; };
+
+void
+test01()
+{
+ {
+ int x[6] = { {2}, {4}, {6}, {8}, {10}, {11} };
+ int y[6] = { {3}, {5}, {7}, {9}, {11}, {12} };
+ auto [in, out] = ranges::transform(x, x, [] (int a) { return a+1; });
+ VERIFY( in == x+6 && out == x+6 );
+ VERIFY( ranges::equal(x, y) );
+ }
+
+ {
+ X x[6] = { {2}, {4}, {6}, {8}, {10}, {11} };
+ int y[7] = { {3}, {5}, {7}, {9}, {11}, {12}, {0} };
+ int z[7] = { {0}, {0}, {0}, {0}, {0}, {0}, {0} };
+ test_container<X, forward_iterator_wrapper> cx(x);
+ test_container<int, forward_iterator_wrapper> cy(y), cz(z);
+ auto [in, out]
+ = ranges::transform(cx, cz.begin(), [] (int a) { return a+1; }, &X::i);
+ VERIFY( ranges::equal(cy, cz) );
+ VERIFY( in == cx.end() && ++out == cz.end() );
+ }
+
+ {
+ X x[6] = { {2}, {4}, {6}, {8}, {10}, {11} };
+ X y[7] = { {3}, {5}, {7}, {9}, {11}, {12}, {0} };
+ int z[7] = { {0}, {0}, {0}, {0}, {0}, {0}, {0} };
+ test_range<X, input_iterator_wrapper> rx(x), ry(y);
+ test_range<int, forward_iterator_wrapper> rz(z);
+ auto [in, out]
+ = ranges::transform(rx, rz.begin(), [] (int a) { return a+1; }, &X::i);
+ VERIFY( ranges::equal(ry, rz, {}, &X::i) );
+ VERIFY( in == rx.end() && ++out == rz.end() );
+ }
+}
+
+struct Y { int i; int j; };
+
+constexpr bool
+test02()
+{
+ int x[] = { 1, 2, 3 };
+ Y y[] = { {1,2}, {2,4}, {3,6} };
+ ranges::transform(y, y+3, x, [] (int a) { return -a; }, &Y::i);
+ return x[0] == -1 && x[1] == -2 && x[2] == -3;
+}
+
+void
+test03()
+{
+ {
+ int x[6] = { {2}, {4}, {6}, {8}, {10}, {11} };
+ const int y[6] = { {3}, {5}, {7}, {9}, {11}, {12} };
+ int z[6] = { {5}, {9}, {13}, {17}, {21}, {23} };
+ auto [in1, in2, out] = ranges::transform(x, y, x, std::plus<>{});
+ VERIFY( in1 == x+6 && in2 == y+6 && out == x+6 );
+ VERIFY( ranges::equal(x, z) );
+ }
+
+ {
+ int x[6] = { {2}, {4}, {6}, {8}, {10}, {11} };
+ const int y[6] = { {3}, {5}, {7}, {9}, {11}, {12} };
+ int z[6] = { {5}, {9}, {13}, {17}, {21}, {23} };
+ auto [in1, in2, out] = ranges::transform(y, x, x, std::plus<>{});
+ VERIFY( in1 == y+6 && in2 == x+6 && out == x+6 );
+ VERIFY( ranges::equal(x, z) );
+ }
+
+ {
+ X x[6] = { {2}, {4}, {6}, {8}, {10}, {11} };
+ int y[7] = { {3}, {5}, {7}, {9}, {11}, {12}, {0} };
+ int z[6] = { {5}, {9}, {13}, {17}, {21}, {23} };
+ int w[6];
+ test_container<X, forward_iterator_wrapper> cx(x);
+ test_container<int, forward_iterator_wrapper> cy(y), cz(z), cw(w);
+ auto [in1, in2, out]
+ = ranges::transform(cx, cy, cw.begin(), std::plus<>{}, &X::i);
+ VERIFY( in1 == cx.end() && ++in2 == cy.end() && out == cw.end() );
+ VERIFY( ranges::equal(cw, cz) );
+ }
+
+ {
+ X x[6] = { {2}, {4}, {6}, {8}, {10}, {11} };
+ int y[7] = { {3}, {5}, {7}, {9}, {11}, {12}, {0} };
+ int z[6] = { {5}, {9}, {13}, {17}, {21}, {23} };
+ int w[6];
+ test_range<X, input_iterator_wrapper> rx(x);
+ test_range<int, input_iterator_wrapper> ry(y), rz(z);
+ test_range<int, forward_iterator_wrapper> rw(w);
+ auto [in1, in2, out]
+ = ranges::transform(rx, ry, rw.begin(), std::plus<>{}, &X::i);
+ VERIFY( in1 == rx.end() && ++in2 == ry.end() && out == rw.end() );
+ VERIFY( ranges::equal(rw, rz) );
+ }
+}
+
+constexpr bool
+test04()
+{
+ int x[3];
+ const Y y[3] = { {1,2}, {2,4}, {3,6} };
+ ranges::transform(y, y+3, y, y+3, x, std::plus<>{}, &Y::i, &Y::j);
+ return x[0] == 3 && x[1] == 6 && x[2] == 9;
+}
+
+int
+main()
+{
+ test01();
+ static_assert(test02());
+ test03();
+ static_assert(test04());
+}
+
More information about the Libstdc++-cvs
mailing list