[gcc(refs/users/ppalka/heads/libstdcxx-constrained-algos)] Add ranges::partial_sort
Patrick Palka
ppalka@gcc.gnu.org
Thu Jan 23 22:14:00 GMT 2020
https://gcc.gnu.org/g:031e250992fa5a7120fbd7efeb54afd44027fa44
commit 031e250992fa5a7120fbd7efeb54afd44027fa44
Author: Patrick Palka <ppalka@redhat.com>
Date: Thu Jan 23 17:06:52 2020 -0500
Add ranges::partial_sort
Diff:
---
libstdc++-v3/include/bits/ranges_algo.h | 34 +++++++++
.../25_algorithms/partial_sort/constrained.cc | 84 ++++++++++++++++++++++
2 files changed, 118 insertions(+)
diff --git a/libstdc++-v3/include/bits/ranges_algo.h b/libstdc++-v3/include/bits/ranges_algo.h
index a43e7b8..a81fd972 100644
--- a/libstdc++-v3/include/bits/ranges_algo.h
+++ b/libstdc++-v3/include/bits/ranges_algo.h
@@ -2033,6 +2033,40 @@ namespace ranges
std::move(__comp), std::move(__proj));
}
+ template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
+ class _Comp = ranges::less, class _Proj = identity>
+ requires sortable<_Iter, _Comp, _Proj>
+ constexpr _Iter
+ partial_sort(_Iter __first, _Iter __middle, _Sent __last,
+ _Comp __comp = {}, _Proj __proj = {})
+ {
+ // TODO: is it worth reimplementing std::partial_sort here?
+ auto __lasti = ranges::next(__first, __last);
+ auto __proj_comp = [&] (auto&& __lhs, auto&& __rhs) {
+ using _TL = decltype(__lhs);
+ using _TR = decltype(__rhs);
+ return std::__invoke(__comp,
+ std::__invoke(__proj, std::forward<_TL>(__lhs)),
+ std::__invoke(__proj, std::forward<_TR>(__rhs)));
+ };
+ std::partial_sort(std::move(__first), std::move(__middle), __lasti,
+ std::move(__proj_comp));
+ return __lasti;
+ }
+
+ template<random_access_range _Range,
+ class _Comp = ranges::less, class _Proj = identity>
+ requires sortable<iterator_t<_Range>, _Comp, _Proj>
+ constexpr safe_iterator_t<_Range>
+ partial_sort(_Range&& __r, iterator_t<_Range> __middle,
+ _Comp __comp = {}, _Proj __proj = {})
+ {
+ return ranges::partial_sort(ranges::begin(__r),
+ std::move(__middle),
+ ranges::end(__r),
+ std::move(__comp), std::move(__proj));
+ }
+
} // namespace ranges
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace std
diff --git a/libstdc++-v3/testsuite/25_algorithms/partial_sort/constrained.cc b/libstdc++-v3/testsuite/25_algorithms/partial_sort/constrained.cc
new file mode 100644
index 0000000..430e3c0
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/partial_sort/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 } }
+// { dg-require-cstdint "" }
+
+#include <algorithm>
+#include <random>
+#include <vector>
+#include <testsuite_hooks.h>
+#include <testsuite_iterators.h>
+
+using __gnu_test::test_container;
+using __gnu_test::test_range;
+using __gnu_test::random_access_iterator_wrapper;
+
+namespace ranges = std::ranges;
+
+void
+test01()
+{
+ for (unsigned size = 0; size < 50; ++size)
+ {
+ std::vector<int> vref(size);
+ std::iota(vref.begin(), vref.end(), 0);
+ std::vector<int> v1(vref), v2(vref);
+ test_container<int, random_access_iterator_wrapper> c
+ = {&v1[0], &v1[0] + size};
+ test_range<int, random_access_iterator_wrapper> r
+ = {&v2[0], &v2[0] + size};
+
+ std::ranlux48_base g1(size), g2(size + 1);
+ ranges::shuffle(c, g1);
+ ranges::shuffle(ranges::begin(r), ranges::end(r), g2);
+
+ for (unsigned middle = 0; middle < std::min(size, 10U); ++middle)
+ {
+ auto res1 = ranges::partial_sort(c.begin(), c.begin()+middle, c.end(),
+ {}, std::negate<>{});
+ VERIFY( res1 == c.end() );
+
+ auto res2 = ranges::partial_sort(r,
+ ranges::begin(r)+middle,
+ ranges::greater{});
+ VERIFY( res2 == ranges::end(r) );
+
+ VERIFY( ranges::equal(c.begin(), c.begin()+middle,
+ r.begin(), r.begin()+middle) );
+ VERIFY( ranges::equal(c.begin(), c.begin()+middle,
+ vref.rbegin(), vref.rbegin()+middle) );
+ }
+ }
+}
+
+constexpr bool
+test02()
+{
+ int x[] = { 5,4,1,3,2 };
+ const int y[] = { 1,2,3 };
+ ranges::partial_sort(x, x+3, x+5);
+ return ranges::equal(x, x+3, y, y+3);
+}
+
+int
+main()
+{
+ test01();
+ static_assert(test02());
+}
More information about the Libstdc++-cvs
mailing list