[gcc(refs/users/ppalka/heads/libstdcxx-constrained-algos)] Add ranges::reverse_copy
Patrick Palka
ppalka@gcc.gnu.org
Wed Jan 22 18:45:00 GMT 2020
https://gcc.gnu.org/g:0c78fdb6ff67edcc8122d51dad825091a2b47254
commit 0c78fdb6ff67edcc8122d51dad825091a2b47254
Author: Patrick Palka <ppalka@redhat.com>
Date: Wed Jan 22 12:30:50 2020 -0500
Add ranges::reverse_copy
Diff:
---
libstdc++-v3/include/bits/ranges_algo.h | 29 +++++++++
.../25_algorithms/reverse_copy/constrained.cc | 74 ++++++++++++++++++++++
2 files changed, 103 insertions(+)
diff --git a/libstdc++-v3/include/bits/ranges_algo.h b/libstdc++-v3/include/bits/ranges_algo.h
index e0c13c3..3fa945d 100644
--- a/libstdc++-v3/include/bits/ranges_algo.h
+++ b/libstdc++-v3/include/bits/ranges_algo.h
@@ -1565,6 +1565,35 @@ namespace ranges
return ranges::reverse(ranges::begin(__r), ranges::end(__r));
}
+ template<typename _Iter, typename _Out>
+ using reverse_copy_result = copy_result<_Iter, _Out>;
+
+ template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
+ weakly_incrementable _Out>
+ requires indirectly_copyable<_Iter, _Out>
+ constexpr reverse_copy_result<_Iter, _Out>
+ reverse_copy(_Iter __first, _Sent __last, _Out __result)
+ {
+ auto __i = ranges::next(__first, __last);
+ auto __tail = __i;
+ while (__first != __tail)
+ {
+ --__tail;
+ *__result = *__tail;
+ ++__result;
+ }
+ return {__i, __result};
+ }
+
+ template<bidirectional_range _Range, weakly_incrementable _Out>
+ requires indirectly_copyable<iterator_t<_Range>, _Out>
+ constexpr reverse_copy_result<safe_iterator_t<_Range>, _Out>
+ reverse_copy(_Range&& __r, _Out __result)
+ {
+ return ranges::reverse_copy(ranges::begin(__r), ranges::end(__r),
+ std::move(__result));
+ }
+
} // namespace ranges
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace std
diff --git a/libstdc++-v3/testsuite/25_algorithms/reverse_copy/constrained.cc b/libstdc++-v3/testsuite/25_algorithms/reverse_copy/constrained.cc
new file mode 100644
index 0000000..1ee40be
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/reverse_copy/constrained.cc
@@ -0,0 +1,74 @@
+// 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_range;
+using __gnu_test::test_container;
+using __gnu_test::bidirectional_iterator_wrapper;
+
+namespace ranges = std::ranges;
+
+void
+test01()
+{
+ int x[] = { 1, 2, 3, 4 };
+ int w[4];
+ test_container<int, bidirectional_iterator_wrapper> cx(x), cw(w);
+ const int y[] = { 4, 3, 2, 1 };
+
+ auto [in,out] = ranges::reverse_copy(cx, cw.begin());
+ VERIFY( in == ranges::end(cx) && out == cw.end() );
+ VERIFY( ranges::equal(cw, y) );
+}
+
+void
+test02()
+{
+ int x[] = { 1, 2, 3, 4, 5 };
+ int w[5];
+ test_range<int, bidirectional_iterator_wrapper> rx(x), rw(w);
+ const int y[] = { 5, 4, 3, 2, 1 };
+
+ auto [in,out] = ranges::reverse_copy(rx, ranges::begin(rw));
+ VERIFY( in == ranges::end(rx) && out == ranges::end(rw) );
+ VERIFY( ranges::equal(rw, y) );
+}
+
+constexpr bool
+test03()
+{
+ const int x[] = { 1, 2, 3 };
+ int w[2];
+ const int y[] = { 2, 1 };
+ ranges::reverse_copy(x, x+2, w);
+ return ranges::equal(w, y);
+}
+
+int
+main()
+{
+ test01();
+ test02();
+
+ static_assert(test03());
+}
More information about the Libstdc++-cvs
mailing list