[gcc(refs/users/ppalka/heads/libstdcxx-constrained-algos)] Add ranges::fill and ranges::fill_n
Patrick Palka
ppalka@gcc.gnu.org
Mon Jan 20 21:56:00 GMT 2020
https://gcc.gnu.org/g:37b4f28bfae72b4b77f017529e4f19f517640091
commit 37b4f28bfae72b4b77f017529e4f19f517640091
Author: Patrick Palka <ppalka@gcc.gnu.org>
Date: Mon Jan 20 15:58:43 2020 -0500
Add ranges::fill and ranges::fill_n
Diff:
---
libstdc++-v3/include/bits/ranges_algo.h | 63 ++++++++++++++
.../testsuite/25_algorithms/fill/constrained.cc | 92 ++++++++++++++++++++
.../testsuite/25_algorithms/fill_n/constrained.cc | 98 ++++++++++++++++++++++
3 files changed, 253 insertions(+)
diff --git a/libstdc++-v3/include/bits/ranges_algo.h b/libstdc++-v3/include/bits/ranges_algo.h
index a950e99..6856439 100644
--- a/libstdc++-v3/include/bits/ranges_algo.h
+++ b/libstdc++-v3/include/bits/ranges_algo.h
@@ -1187,6 +1187,69 @@ namespace ranges
__new_value, std::move(__proj));
}
+ template<typename _Tp, output_iterator<const _Tp&> _Out>
+ constexpr _Out
+ fill_n(_Out __first, iter_difference_t<_Out> __n, const _Tp& __value)
+ {
+ // TODO: implement more specializations to be at least on par with
+ // std::fill_n
+ if (__n <= 0)
+ return __first;
+
+ if constexpr (is_pointer_v<_Out> && __is_byte<_Tp>::__value)
+ {
+ __builtin_memset(__first, static_cast<unsigned char>(__value), __n);
+ return __first + __n;
+ }
+ else if constexpr (is_scalar_v<_Tp>)
+ {
+ const auto __tmp = __value;
+ for (; __n > 0; --__n, (void)++__first)
+ *__first = __tmp;
+ return __first;
+ }
+ else
+ {
+ for (; __n > 0; --__n, (void)++__first)
+ *__first = __value;
+ return __first;
+ }
+ }
+
+ template<typename _Tp,
+ output_iterator<const _Tp&> _Out, sentinel_for<_Out> _Sent>
+ constexpr _Out
+ fill(_Out __first, _Sent __last, const _Tp& __value)
+ {
+ // TODO: implement more specializations to be at least on par with
+ // std::fill
+ if constexpr (sized_sentinel_for<_Sent, _Out>)
+ {
+ const auto __len = __last - __first;
+ return ranges::fill_n(__first, __len, __value);
+ }
+ else if constexpr (is_scalar_v<_Tp>)
+ {
+ const auto __tmp = __value;
+ for (; __first != __last; ++__first)
+ *__first = __tmp;
+ return __first;
+ }
+ else
+ {
+ for (; __first != __last; ++__first)
+ *__first = __value;
+ return __first;
+ }
+ }
+
+ template<typename _Tp, output_range<const _Tp&> _Range>
+ constexpr safe_iterator_t<_Range>
+ fill(_Range&& __r, const _Tp& __value)
+ {
+ return ranges::fill(ranges::begin(__r), ranges::end(__r), __value);
+ }
+
} // namespace ranges
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace std
diff --git a/libstdc++-v3/testsuite/25_algorithms/fill/constrained.cc b/libstdc++-v3/testsuite/25_algorithms/fill/constrained.cc
new file mode 100644
index 0000000..4813b83
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/fill/constrained.cc
@@ -0,0 +1,92 @@
+// 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>
+#include <list>
+
+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;
+
+struct X
+{
+ int i;
+};
+
+void
+test01()
+{
+ const int c[6] = { 17, 17, 17, 17, 17, 17 };
+ {
+ X x[6];
+ VERIFY( ranges::fill(x, X{17}) == x+6 );
+ VERIFY( ranges::equal(x, c, {}, &X::i) );
+ }
+
+ {
+ char x[6];
+ VERIFY( ranges::fill(x, 17) == x+6 );
+ VERIFY( ranges::equal(x, c) );
+ }
+
+ {
+ X x[6];
+ test_container<X, forward_iterator_wrapper> cx(x);
+ VERIFY( ranges::fill(cx, X{17}) == cx.end() );
+ VERIFY( ranges::equal(cx, c, {}, &X::i) );
+ }
+
+ {
+ int x[6];
+ test_range<int, output_iterator_wrapper> rx(x);
+ VERIFY( ranges::fill(rx, 17) == rx.end() );
+ VERIFY( ranges::equal(x, c) );
+ }
+
+ {
+ std::list<int> list(6);
+ ranges::fill(list, 17);
+ VERIFY( ranges::equal(list, c) );
+ }
+}
+
+constexpr bool
+test02()
+{
+ bool ok = true;
+ int x[5];
+ ranges::fill(x, 17);
+ for (auto v : x)
+ ok &= v == 17;
+ return ok;
+}
+
+int
+main()
+{
+ test01();
+ static_assert(test02());
+}
diff --git a/libstdc++-v3/testsuite/25_algorithms/fill_n/constrained.cc b/libstdc++-v3/testsuite/25_algorithms/fill_n/constrained.cc
new file mode 100644
index 0000000..e9ce8e8
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/fill_n/constrained.cc
@@ -0,0 +1,98 @@
+// 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>
+#include <list>
+
+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;
+
+struct X
+{
+ int i;
+};
+
+void
+test01()
+{
+ const int c[6] = { 17, 17, 17, 4, 5, 6 };
+ {
+ X x[6] = { {1}, {2}, {3}, {4}, {5}, {6} };
+ VERIFY( ranges::fill_n(x, 3, X{17}) == x+3 );
+ VERIFY( ranges::equal(x, c, {}, &X::i) );
+ }
+
+ {
+ char x[6];
+ VERIFY( ranges::fill_n(x, 3, 17) == x+3 );
+ VERIFY( ranges::equal(x, x+3, c, c+3) );
+ }
+
+ {
+ X x[6] = { 1, 2, 3, 4, 5, 6 };
+ test_container<X, forward_iterator_wrapper> cx(x);
+ VERIFY( ranges::fill_n(cx.begin(), 3, X{17})->i == 4 );
+ VERIFY( ranges::equal(cx, c, {}, &X::i) );
+ }
+
+ {
+ int x[6] = { 1, 2, 3, 4, 5, 6 };;
+ test_range<int, output_iterator_wrapper> rx(x);
+ ranges::fill_n(ranges::begin(rx), 3, 17);
+ VERIFY( ranges::equal(x, c) );
+ }
+
+ {
+ std::list<int> list({1, 2, 3, 4, 5, 6});
+ ranges::fill_n(list.begin(), 3, 17);
+ VERIFY( ranges::equal(list, c) );
+ }
+}
+
+constexpr bool
+test02()
+{
+ bool ok = true;
+ int x[6] = { 1, 2, 3, 4, 5, 6 };
+ const int y[6] = { 1, 2, 3, 4, 5, 6 };
+ const int z[6] = { 17, 17, 17, 4, 5, 6 };
+
+ ranges::fill_n(x, 0, 17);
+ ranges::fill_n(x, -1, 17);
+ ok &= ranges::equal(x, y);
+
+ ranges::fill_n(x, 3, 17);
+ ok &= ranges::equal(x, z);
+ return ok;
+}
+
+int
+main()
+{
+ test01();
+ static_assert(test02());
+}
More information about the Libstdc++-cvs
mailing list