[gcc(refs/users/ppalka/heads/libstdcxx-constrained-algos)] Implement ranges [alg.partitions]
Patrick Palka
ppalka@gcc.gnu.org
Fri Jan 24 19:59:00 GMT 2020
https://gcc.gnu.org/g:60343c01212b855cd0b806394bd4b2051896c82d
commit 60343c01212b855cd0b806394bd4b2051896c82d
Author: Patrick Palka <ppalka@redhat.com>
Date: Fri Jan 24 14:55:21 2020 -0500
Implement ranges [alg.partitions]
Diff:
---
libstdc++-v3/include/bits/ranges_algo.h | 218 +++++++++++++++++++++
.../25_algorithms/is_partitioned/constrained.cc | 58 ++++++
.../25_algorithms/partition/constrained.cc | 71 +++++++
.../25_algorithms/partition_copy/constrained.cc | 81 ++++++++
.../25_algorithms/partition_point/constrained.cc | 67 +++++++
.../25_algorithms/stable_partition/constrained.cc | 76 +++++++
6 files changed, 571 insertions(+)
diff --git a/libstdc++-v3/include/bits/ranges_algo.h b/libstdc++-v3/include/bits/ranges_algo.h
index 24b4133..7865907 100644
--- a/libstdc++-v3/include/bits/ranges_algo.h
+++ b/libstdc++-v3/include/bits/ranges_algo.h
@@ -2290,6 +2290,224 @@ namespace ranges
std::move(__comp), std::move(__proj));
}
+ template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
+ typename _Proj = identity,
+ indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
+ constexpr bool
+ is_partitioned(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {})
+ {
+ __first = ranges::find_if_not(std::move(__first), __last, __pred, __proj);
+ if (__first == __last)
+ return true;
+ ++__first;
+ return ranges::none_of(std::move(__first), std::move(__last),
+ std::move(__pred), std::move(__proj));
+ }
+
+ template<input_range _Range, typename _Proj = identity,
+ indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred>
+ constexpr bool
+ is_partitioned(_Range&& __r, _Pred __pred, _Proj __proj = {})
+ {
+ return ranges::is_partitioned(ranges::begin(__r), ranges::end(__r),
+ std::move(__pred), std::move(__proj));
+ }
+
+ template<permutable _Iter, sentinel_for<_Iter> _Sent,
+ typename _Proj = identity,
+ indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
+ constexpr subrange<_Iter>
+ partition(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {})
+ {
+ if constexpr (bidirectional_iterator<_Iter>)
+ {
+ auto __lasti = ranges::next(__first, __last);
+ auto __tail = __lasti;
+ for (;;)
+ {
+ for (;;)
+ if (__first == __tail)
+ return {std::move(__first), std::move(__lasti)};
+ else if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
+ ++__first;
+ else
+ break;
+ --__tail;
+ for (;;)
+ if (__first == __tail)
+ return {std::move(__first), std::move(__lasti)};
+ else if (!(bool)std::__invoke(__pred,
+ std::__invoke(__proj, *__tail)))
+ --__tail;
+ else
+ break;
+ ranges::iter_swap(__first, __tail);
+ ++__first;
+ }
+ }
+ else
+ {
+ if (__first == __last)
+ return {std::move(__first), std::move(__first)};
+
+ while (std::__invoke(__pred, std::__invoke(__proj, *__first)))
+ if (++__first == __last)
+ return {std::move(__first), std::move(__first)};
+
+ auto __next = __first;
+ while (++__next != __last)
+ if (std::__invoke(__pred, std::__invoke(__proj, *__next)))
+ {
+ ranges::iter_swap(__first, __next);
+ ++__first;
+ }
+
+ return {std::move(__first), std::move(__next)};
+ }
+ }
+
+ template<forward_range _Range, typename _Proj = identity,
+ indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred>
+ requires permutable<iterator_t<_Range>>
+ constexpr safe_subrange_t<_Range>
+ partition(_Range&& __r, _Pred __pred, _Proj __proj = {})
+ {
+ return ranges::partition(ranges::begin(__r), ranges::end(__r),
+ std::move(__pred), std::move(__proj));
+ }
+
+ template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
+ typename _Proj = identity,
+ indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
+ requires permutable<_Iter>
+ subrange<_Iter>
+ stable_partition(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {})
+ {
+ // TODO: is it worth reimplementing std::stable_partition here?
+ auto __lasti = ranges::next(__first, __last);
+ auto __proj_comp = [&] (auto&& __arg) {
+ using _Tp = decltype(__arg);
+ return std::__invoke(__pred,
+ std::__invoke(__proj, std::forward<_Tp>(__arg)));
+ };
+ auto __middle = std::stable_partition(std::move(__first),
+ std::move(__lasti),
+ std::move(__proj_comp));
+ return {__middle, __lasti};
+ }
+
+ template<bidirectional_range _Range, typename _Proj = identity,
+ indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred>
+ requires permutable<iterator_t<_Range>>
+ safe_subrange_t<_Range>
+ stable_partition(_Range&& __r, _Pred __pred, _Proj __proj = {})
+ {
+ return ranges::stable_partition(ranges::begin(__r), ranges::end(__r),
+ std::move(__pred), std::move(__proj));
+ }
+
+ template<typename _Iter, typename _Out1, typename _O2>
+ struct partition_copy_result
+ {
+ [[no_unique_address]] _Iter in;
+ [[no_unique_address]] _Out1 out1;
+ [[no_unique_address]] _O2 out2;
+
+ template<typename _IIter, typename _OOut1, typename _OOut2>
+ requires convertible_to<const _Iter&, _IIter>
+ && convertible_to<const _Out1&, _OOut1>
+ && convertible_to<const _O2&, _OOut2>
+ operator partition_copy_result<_IIter, _OOut1, _OOut2>() const &
+ {
+ return {in, out1, out2};
+ }
+
+ template<typename _IIter, typename _OOut1, typename _OOut2>
+ requires convertible_to<_Iter, _IIter>
+ && convertible_to<_Out1, _OOut1>
+ && convertible_to<_O2, _OOut2>
+ operator partition_copy_result<_IIter, _OOut1, _OOut2>() &&
+ {
+ return {std::move(in), std::move(out1), std::move(out2)};
+ }
+ };
+
+ template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
+ weakly_incrementable _Out1, weakly_incrementable _O2,
+ typename _Proj = identity,
+ indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
+ requires indirectly_copyable<_Iter, _Out1>
+ && indirectly_copyable<_Iter, _O2>
+ constexpr partition_copy_result<_Iter, _Out1, _O2>
+ partition_copy(_Iter __first, _Sent __last,
+ _Out1 __out_true, _O2 __out_false,
+ _Pred __pred, _Proj __proj = {})
+ {
+ for (; __first != __last; ++__first)
+ if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
+ {
+ *__out_true = *__first;
+ ++__out_true;
+ }
+ else
+ {
+ *__out_false = *__first;
+ ++__out_false;
+ }
+
+ return {std::move(__first), std::move(__out_true), std::move(__out_false)};
+ }
+
+ template<input_range _Range, weakly_incrementable _Out1,
+ weakly_incrementable _O2,
+ typename _Proj = identity,
+ indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred>
+ requires indirectly_copyable<iterator_t<_Range>, _Out1>
+ && indirectly_copyable<iterator_t<_Range>, _O2>
+ constexpr partition_copy_result<safe_iterator_t<_Range>, _Out1, _O2>
+ partition_copy(_Range&& __r, _Out1 out_true, _O2 out_false,
+ _Pred __pred, _Proj __proj = {})
+ {
+ return ranges::partition_copy(ranges::begin(__r), ranges::end(__r),
+ std::move(out_true), std::move(out_false),
+ std::move(__pred), std::move(__proj));
+ }
+
+ template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
+ typename _Proj = identity,
+ indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
+ constexpr _Iter
+ partition_point(_Iter __first, _Sent __last,
+ _Pred __pred, _Proj __proj = {})
+ {
+ auto __len = ranges::distance(__first, __last);
+
+ while (__len > 0)
+ {
+ auto __half = __len / 2;
+ auto __middle = __first;
+ ranges::advance(__middle, __half);
+ if (std::__invoke(__pred, std::__invoke(__proj, *__middle)))
+ {
+ __first = __middle;
+ ++__first;
+ __len = __len - __half - 1;
+ }
+ else
+ __len = __half;
+ }
+ return __first;
+ }
+
+ template<forward_range _Range, typename _Proj = identity,
+ indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred>
+ constexpr safe_iterator_t<_Range>
+ partition_point(_Range&& __r, _Pred __pred, _Proj __proj = {})
+ {
+ return ranges::partition_point(ranges::begin(__r), ranges::end(__r),
+ std::move(__pred), std::move(__proj));
+ }
+
} // namespace ranges
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace std
diff --git a/libstdc++-v3/testsuite/25_algorithms/is_partitioned/constrained.cc b/libstdc++-v3/testsuite/25_algorithms/is_partitioned/constrained.cc
new file mode 100644
index 0000000..8035667
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/is_partitioned/constrained.cc
@@ -0,0 +1,58 @@
+// 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;
+
+void
+test01()
+{
+ int x[] = {2,4,6,1,3,5};
+ test_container<int, forward_iterator_wrapper> cx(x);
+ VERIFY( ranges::is_partitioned(cx, [] (int a) { return a%2==0; }) );
+
+ test_range<int, input_iterator_wrapper> rx(x);
+ VERIFY( ranges::is_partitioned(rx,
+ [] (int a) { return a%2==1; },
+ [] (int a) { return a+1; }) );
+}
+
+constexpr bool
+test02()
+{
+ int x[] = {1,2,3,4,5,6,1};
+ return (ranges::is_partitioned(x, x+6, [] (int a) { return a<=2; })
+ && !ranges::is_partitioned(x, x+7, [] (int a) { return a<=2; }));
+}
+
+int
+main()
+{
+ test01();
+ static_assert(test02());
+}
diff --git a/libstdc++-v3/testsuite/25_algorithms/partition/constrained.cc b/libstdc++-v3/testsuite/25_algorithms/partition/constrained.cc
new file mode 100644
index 0000000..4e5fa5e
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/partition/constrained.cc
@@ -0,0 +1,71 @@
+// 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::forward_iterator_wrapper;
+using __gnu_test::bidirectional_iterator_wrapper;
+
+namespace ranges = std::ranges;
+
+template<template<typename> typename wrapper>
+void
+test01()
+{
+ {
+ int x[] = {1,2,3,4,5,6,7,8,9,10};
+ test_container<int, wrapper> cx(x);
+ auto range = ranges::partition(cx, [] (int a) { return a%2==0; });
+ VERIFY( range.begin().ptr == x+5 );
+ VERIFY( range.end().ptr == x+10 );
+ VERIFY( ranges::is_partitioned(cx, [] (int a) { return a%2==0; }) );
+ }
+
+ {
+ int x[] = {1,2,3,4,5,6,7,8};
+ test_range<int, wrapper> rx(x);
+ auto range = ranges::partition(rx,
+ [] (int a) { return a%2==0; },
+ [] (int a) { return a+1; });
+ VERIFY( range.begin().ptr == x+4 );
+ VERIFY( range.end().ptr == x+8 );
+ VERIFY( ranges::is_partitioned(rx, [] (int a) { return a%2==1; }) );
+ }
+}
+
+constexpr bool
+test02()
+{
+ int x[] = {1,2,3,4,5,6,7,8,9,10};
+ auto range = ranges::partition(x, x+9, [] (int a) { return a < 100; });
+ return (range.begin() == x+9 && range.end() == x+9);
+}
+
+int
+main()
+{
+ test01<forward_iterator_wrapper>();
+ test01<bidirectional_iterator_wrapper>();
+ static_assert(test02());
+}
diff --git a/libstdc++-v3/testsuite/25_algorithms/partition_copy/constrained.cc b/libstdc++-v3/testsuite/25_algorithms/partition_copy/constrained.cc
new file mode 100644
index 0000000..8ed6e24
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/partition_copy/constrained.cc
@@ -0,0 +1,81 @@
+// 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()
+{
+ {
+ int x[] = {1,2,3,4,5,6,7,8,9,10,11};
+ int y[5], z[6];
+ test_container<int, forward_iterator_wrapper> cx(x);
+ test_container<int, forward_iterator_wrapper> cy(y), cz(z);
+ auto pred = [] (int a) { return a%2==0; };
+ auto [in,out_true,out_false]
+ = ranges::partition_copy(cx, cy.begin(), cz.begin(), pred);
+ VERIFY( in.ptr == x+11 );
+ VERIFY( out_true.ptr == y+5 );
+ VERIFY( out_false.ptr == z+6 );
+ VERIFY( ranges::all_of(cy, pred) );
+ VERIFY( ranges::none_of(cz, pred) );
+ }
+
+ {
+ int x[] = {1,2,3,4,5,6,7,8,9,10,11};
+ int y[6], z[5];
+ test_range<int, input_iterator_wrapper> cx(x);
+ test_range<int, output_iterator_wrapper> cy(y), cz(z);
+ auto pred = [] (int a) { return a%2==0; };
+ auto proj = [] (int a) { return a+1; };
+ auto [in,out_true,out_false]
+ = ranges::partition_copy(cx, cy.begin(), cz.begin(), pred, proj);
+ VERIFY( in.ptr == x+11 );
+ VERIFY( out_true.ptr == y+6 );
+ VERIFY( out_false.ptr == z+5 );
+ VERIFY( ranges::none_of(y, pred) );
+ VERIFY( ranges::all_of(z, pred) );
+ }
+}
+
+constexpr bool
+test02()
+{
+ int x[] = {1,2,3,4,5,6,7,8,9,10};
+ auto range = ranges::partition(x, x+9, [] (int a) { return a < 100; });
+ return (range.begin() == x+9 && range.end() == x+9);
+}
+
+int
+main()
+{
+ test01();
+ static_assert(test02());
+}
diff --git a/libstdc++-v3/testsuite/25_algorithms/partition_point/constrained.cc b/libstdc++-v3/testsuite/25_algorithms/partition_point/constrained.cc
new file mode 100644
index 0000000..2a430f2
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/partition_point/constrained.cc
@@ -0,0 +1,67 @@
+// 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::forward_iterator_wrapper;
+
+namespace ranges = std::ranges;
+
+void
+test01()
+{
+ for (int k = 1; k <= 7; k++)
+ {
+ int x[] = {1,2,3,4,5,6,7};
+ test_container<int, forward_iterator_wrapper> cx(x);
+ auto pred = [&] (int a) { return a <= k; };
+ auto middle = ranges::partition_point(cx, pred);
+ VERIFY( middle.ptr == x+k );
+ }
+
+ for (int k = 1; k <= 8; k++)
+ {
+ int x[] = {1,2,3,4,5,6,7,8};
+ test_range<int, forward_iterator_wrapper> rx(x);
+ auto pred = [&] (int a) { return a > -k; };
+ auto proj = [] (int a) { return -a; };
+ auto middle = ranges::partition_point(rx, pred, proj);
+ VERIFY( middle.ptr == x+k-1 );
+ }
+}
+
+constexpr bool
+test02()
+{
+ int x[] = {1,2,3,4,5};
+ return (ranges::partition_point(x, x+5, [] (int a) { return a < 6; }) == x+5
+ && ranges::partition_point(x, x+5, [] (int a) { return a < 0; }) == x);
+}
+
+int
+main()
+{
+ test01();
+ static_assert(test02());
+}
diff --git a/libstdc++-v3/testsuite/25_algorithms/stable_partition/constrained.cc b/libstdc++-v3/testsuite/25_algorithms/stable_partition/constrained.cc
new file mode 100644
index 0000000..761e3dd
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/stable_partition/constrained.cc
@@ -0,0 +1,76 @@
+// 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::bidirectional_iterator_wrapper;
+
+namespace ranges = std::ranges;
+
+void
+test01()
+{
+ {
+ int x[] = {1,2,3,4,5,6,7,8,9,10};
+ test_container<int, bidirectional_iterator_wrapper> cx(x);
+ auto pred = [] (int a) { return a%2==0; };
+ auto range = ranges::stable_partition(cx, pred);
+ VERIFY( ranges::all_of(cx.begin(), range.begin(), pred) );
+ VERIFY( ranges::none_of(range, pred) );
+ }
+
+ {
+ int x[] = {1,2,3,4,5,6,7,8,9,10,11};
+ test_range<int, bidirectional_iterator_wrapper> cx(x);
+ auto pred = [] (int a) { return a%2==0; };
+ auto range = ranges::stable_partition(cx, pred);
+ VERIFY( ranges::all_of(cx.begin(), range.begin(), pred) );
+ VERIFY( ranges::none_of(range, pred) );
+ }
+}
+
+void
+test02()
+{
+ for (int k = 1; k <= 10; k++)
+ {
+ int x[] = {1,2,3,4,5,6,7,8,9,10};
+ auto pred = [&] (int a) { return a >= k; };
+ auto proj = [] (int a) { return a-1; };
+ auto range = ranges::stable_partition(x, x+10, pred, proj);
+ VERIFY( ranges::all_of(x, range.begin(), pred, proj) );
+ VERIFY( ranges::none_of(range, pred, proj) );
+
+ int y[] = {0,1,2,3,4,5,6,7,8,9};
+ ranges::rotate(y, y+k);
+ VERIFY( ranges::equal(x, y, {}, proj) );
+ }
+}
+
+int
+main()
+{
+ test01();
+ test02();
+}
More information about the Libstdc++-cvs
mailing list