[gcc(refs/users/ppalka/heads/libstdcxx-constrained-algos)] Add ranges::generate and ranges::generate_n
Patrick Palka
ppalka@gcc.gnu.org
Mon Jan 20 21:56:00 GMT 2020
https://gcc.gnu.org/g:ab6b00691cc74a16faedfef691b267563d5ca679
commit ab6b00691cc74a16faedfef691b267563d5ca679
Author: Patrick Palka <ppalka@gcc.gnu.org>
Date: Mon Jan 20 16:46:38 2020 -0500
Add ranges::generate and ranges::generate_n
Diff:
---
libstdc++-v3/include/bits/ranges_algo.h | 30 ++++++++
.../25_algorithms/generate/constrained.cc | 77 ++++++++++++++++++++
.../25_algorithms/generate_n/constrained.cc | 84 ++++++++++++++++++++++
3 files changed, 191 insertions(+)
diff --git a/libstdc++-v3/include/bits/ranges_algo.h b/libstdc++-v3/include/bits/ranges_algo.h
index 6856439..da81196 100644
--- a/libstdc++-v3/include/bits/ranges_algo.h
+++ b/libstdc++-v3/include/bits/ranges_algo.h
@@ -1250,6 +1250,36 @@ namespace ranges
return ranges::fill(ranges::begin(__r), ranges::end(__r), __value);
}
+ template<input_or_output_iterator _Out, copy_constructible _F>
+ requires invocable<_F&> && writable<_Out, invoke_result_t<_F&>>
+ constexpr _Out
+ generate_n(_Out __first, iter_difference_t<_Out> __n, _F __gen)
+ {
+ for (; __n > 0; --__n, (void)++__first)
+ *__first = std::__invoke(__gen);
+ return __first;
+ }
+
+ template<input_or_output_iterator _Out, sentinel_for<_Out> _Sent,
+ copy_constructible _F>
+ requires invocable<_F&> && writable<_Out, invoke_result_t<_F&>>
+ constexpr _Out
+ generate(_Out __first, _Sent __last, _F __gen)
+ {
+ for (; __first != __last; ++__first)
+ *__first = std::__invoke(__gen);
+ return __first;
+ }
+
+ template<typename _Range, copy_constructible _F>
+ requires invocable<_F&> && output_range<_Range, invoke_result_t<_F&>>
+ constexpr safe_iterator_t<_Range>
+ generate(_Range&& __r, _F __gen)
+ {
+ return ranges::generate(ranges::begin(__r), ranges::end(__r),
+ std::move(__gen));
+ }
+
} // namespace ranges
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace std
diff --git a/libstdc++-v3/testsuite/25_algorithms/generate/constrained.cc b/libstdc++-v3/testsuite/25_algorithms/generate/constrained.cc
new file mode 100644
index 0000000..71bcbaa
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/generate/constrained.cc
@@ -0,0 +1,77 @@
+// 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::output_iterator_wrapper;
+using __gnu_test::forward_iterator_wrapper;
+
+namespace ranges = std::ranges;
+
+void
+test01()
+{
+ const int c[6] = { 1, 2, 3, 4, 5, 6 };
+
+ {
+ int x[6];
+ int a = 1;
+ VERIFY( ranges::generate(x, [&] { return a++; }) == x+6 );
+ VERIFY( ranges::equal(x, c) );
+ }
+
+ {
+ int x[6];
+ int a = 1;
+ test_container<int, forward_iterator_wrapper> cx(x);
+ VERIFY( ranges::generate(cx, [&] { return a++; }) == cx.end() );
+ VERIFY( ranges::equal(cx, c) );
+ }
+
+ {
+ int x[6];
+ int a = 1;
+ test_range<int, output_iterator_wrapper> rx(x);
+ VERIFY( ranges::generate(rx, [&] { return a++; }) == rx.end() );
+ VERIFY( ranges::equal(x, c) );
+ }
+}
+
+constexpr bool
+test02()
+{
+ const int c[6] = { 1, 2, 3, 4, 5, 6 };
+ int x[6];
+ int a = 1;
+ ranges::generate(x, [&] { return a++; });
+ return ranges::equal(x, c);
+}
+
+int
+main()
+{
+ test01();
+ static_assert(test02());
+}
diff --git a/libstdc++-v3/testsuite/25_algorithms/generate_n/constrained.cc b/libstdc++-v3/testsuite/25_algorithms/generate_n/constrained.cc
new file mode 100644
index 0000000..ff894ad
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/generate_n/constrained.cc
@@ -0,0 +1,84 @@
+// 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::output_iterator_wrapper;
+using __gnu_test::forward_iterator_wrapper;
+
+namespace ranges = std::ranges;
+
+void
+test01()
+{
+ const int c[6] = { 1, 2, 3, 4, 5, 6 };
+
+ {
+ int x[6] = { 7, 8, 9, 4, 5, 6 };
+ int a = 1;
+ VERIFY( ranges::generate_n(x, 3, [&] { return a++; }) == x+3 );
+ VERIFY( ranges::equal(x, c) );
+ }
+
+ {
+ int x[6] = { 7, 8, 9, 4, 5, 6 };
+ int a = 1;
+ test_container<int, forward_iterator_wrapper> cx(x);
+ VERIFY( *ranges::generate_n(cx.begin(), 3, [&] { return a++; })
+ == 4 );
+ VERIFY( ranges::equal(cx, c) );
+ }
+
+ {
+ int x[6] = { 7, 8, 9, 4, 5, 6 };
+ int a = 1;
+ test_range<int, output_iterator_wrapper> rx(x);
+ ranges::generate_n(ranges::begin(rx), 3, [&] { return a++; });
+ VERIFY( ranges::equal(x, c) );
+ }
+}
+
+constexpr bool
+test02()
+{
+ bool ok = true;
+ int c[6] = { 1, 2, 3, 4, 5, 6 };
+ int x[6];
+ int a = 1;
+ ranges::generate_n(x, 6, [&] { return a++; });
+ ok &= ranges::equal(x, c);
+ ranges::generate_n(c, 0, [] { return -1; });
+ ok &= ranges::equal(x, c);
+ ranges::generate_n(c, -2, [] { return -1; });
+ ok &= ranges::equal(x, c);
+ return ok;
+}
+
+int
+main()
+{
+ test01();
+ static_assert(test02());
+}
More information about the Libstdc++-cvs
mailing list