[gcc(refs/users/ppalka/heads/libstdcxx-constrained-algos)] Add ranges::copy
Patrick Palka
ppalka@gcc.gnu.org
Sat Jan 18 04:46:00 GMT 2020
https://gcc.gnu.org/g:38d29b2ad4a73c287f6d6431094d1a938a323b86
commit 38d29b2ad4a73c287f6d6431094d1a938a323b86
Author: Patrick Palka <ppalka@gcc.gnu.org>
Date: Fri Jan 17 23:43:14 2020 -0500
Add ranges::copy
Diff:
---
libstdc++-v3/include/bits/ranges_algo.h | 73 ++++++++++++++++
.../testsuite/25_algorithms/copy/constrained.cc | 96 ++++++++++++++++++++++
2 files changed, 169 insertions(+)
diff --git a/libstdc++-v3/include/bits/ranges_algo.h b/libstdc++-v3/include/bits/ranges_algo.h
index c63b2a1..fd8fd3b 100644
--- a/libstdc++-v3/include/bits/ranges_algo.h
+++ b/libstdc++-v3/include/bits/ranges_algo.h
@@ -772,6 +772,79 @@ namespace ranges
std::move(__proj1), std::move(__proj2));
}
+ template<typename _Iter, typename _Out>
+ struct copy_result
+ {
+ [[no_unique_address]] _Iter in;
+ [[no_unique_address]] _Out out;
+
+ template<typename _Iter2, typename _Out2>
+ requires convertible_to<const _Iter&, _Iter2>
+ && convertible_to<const _Out&, _Out2>
+ operator copy_result<_Iter2, _Out2>() const &
+ {
+ return {in, out};
+ }
+
+ template<typename _Iter2, typename _Out2>
+ requires convertible_to<_Iter, _Iter2>
+ && convertible_to<_Out, _Out2>
+ operator copy_result<_Iter2, _Out2>() &&
+ {
+ return {std::move(in), std::move(out)};
+ }
+ };
+
+ template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
+ weakly_incrementable _Out>
+ requires indirectly_copyable<_Iter, _Out>
+ constexpr copy_result<_Iter, _Out>
+ copy(_Iter __first, _Sent __last, _Out __result)
+ {
+ // TODO: implement more specializations to be at least on par with
+ // std::copy.
+ if constexpr (sized_sentinel_for<_Sent, _Iter>)
+ {
+ using _ValueTypeI = iterator_traits<_Iter>::value_type;
+ using _ValueTypeO = iterator_traits<_Out>::value_type;
+ constexpr bool __use_memmove
+ = (is_trivially_copyable_v<_ValueTypeI>
+ && is_same_v<_ValueTypeI, _ValueTypeO>
+ && is_pointer_v<_Iter>
+ && is_pointer_v<_Out>);
+
+ if constexpr (__use_memmove)
+ {
+ static_assert(is_copy_assignable_v<_ValueTypeI>);
+ auto __num = __last - __first;
+ if (__num)
+ std::__memmove<false>(__result, __first, __num);
+ return {__first + __num, __result + __num};
+ }
+ else
+ {
+ for (auto __n = __last - __first; __n > 0; --__n)
+ *__result++ = *__first++;
+ return {__last, __result};
+ }
+ }
+ else
+ {
+ while (__first != __last)
+ *__result++ = *__first++;
+ return {__last, __result};
+ }
+ }
+
+ template<input_range _Range, weakly_incrementable _Out>
+ requires indirectly_copyable<iterator_t<_Range>, _Out>
+ constexpr copy_result<safe_iterator_t<_Range>, _Out>
+ copy(_Range&& __r, _Out __result)
+ {
+ return ranges::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/copy/constrained.cc b/libstdc++-v3/testsuite/25_algorithms/copy/constrained.cc
new file mode 100644
index 0000000..3400d26
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/copy/constrained.cc
@@ -0,0 +1,96 @@
+// 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;
+using __gnu_test::bidirectional_iterator_wrapper;
+
+namespace ranges = std::ranges;
+
+void
+test01()
+{
+ {
+ int x[7] = { 1, 2, 3, 4, 5, 6, 7 };
+ int y[7] = { 0 };
+ int z[7] = { 1, 2, 3, 4, 5, 6, 7 };
+ auto [in, out] = ranges::copy(x, y);
+ VERIFY( ranges::equal(x, y) && in == x+7 && out == y+7 );
+ VERIFY( ranges::equal(x, z) );
+ }
+
+ {
+ int x[3] = { 1, 2, 3 };
+ char y[4] = { 0 };
+ int z[3] = { 1, 2, 3 };
+ test_container<int, forward_iterator_wrapper> cx(x);
+ test_container<char, forward_iterator_wrapper> cy(y);
+ auto [in, out] = ranges::copy(x, y);
+ VERIFY( ranges::equal(x, x+3, y, y+3) && in == x+3 && out == y+3 );
+ VERIFY( ranges::equal(x, z) );
+ }
+
+ {
+ char x[3] = { 1, 2, 3 };
+ int y[4] = { 0 };
+ int z[3] = { 1, 2, 3 };
+ test_range<char, forward_iterator_wrapper> cx(x);
+ test_range<int, forward_iterator_wrapper> cy(y);
+ auto [in, out] = ranges::copy(x, y);
+ VERIFY( ranges::equal(x, x+3, y, y+3) && in == x+3 && out == y+3 );
+ VERIFY( ranges::equal(x, z) );
+ }
+}
+
+struct X
+{
+ int i;
+ constexpr X (int a) : i(a) { }
+};
+
+constexpr bool
+test02()
+{
+ bool ok = true;
+ int x[] = { {2}, {2}, {6}, {8}, {10} };
+ X y[] = { {2}, {6}, {8}, {10}, {11}, {2} };
+ int z[] = { {2}, {2}, {6}, {8}, {10} };
+ auto [in, out] = ranges::copy(x, y);
+ ok &= ranges::equal(x, x+5, y, y+5, {}, {}, &X::i);
+ ok &= (in == x+5);
+ ok &= (out == y+5);
+ ok &= (y[5].i == 2);
+ ok &= ranges::equal(x, z);
+ return ok;
+}
+
+int
+main()
+{
+ test01();
+ static_assert(test02());
+}
+
More information about the Libstdc++-cvs
mailing list