[gcc(refs/users/ppalka/heads/libstdcxx-constrained-algos)] Add ranges::sort
Patrick Palka
ppalka@gcc.gnu.org
Thu Jan 23 22:14:00 GMT 2020
https://gcc.gnu.org/g:3232df7404b6ac6e2ae7052f099f8dc5aa23f785
commit 3232df7404b6ac6e2ae7052f099f8dc5aa23f785
Author: Patrick Palka <ppalka@redhat.com>
Date: Thu Jan 23 16:01:07 2020 -0500
Add ranges::sort
Diff:
---
libstdc++-v3/include/bits/ranges_algo.h | 29 ++++++++
.../testsuite/25_algorithms/sort/constrained.cc | 81 ++++++++++++++++++++++
2 files changed, 110 insertions(+)
diff --git a/libstdc++-v3/include/bits/ranges_algo.h b/libstdc++-v3/include/bits/ranges_algo.h
index 4dc05c5..c64cb3c 100644
--- a/libstdc++-v3/include/bits/ranges_algo.h
+++ b/libstdc++-v3/include/bits/ranges_algo.h
@@ -1974,6 +1974,35 @@ namespace ranges
std::forward<_Gen>(__g));
}
+ template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
+ typename _Comp = ranges::less, typename _Proj = identity>
+ requires sortable<_Iter, _Comp, _Proj>
+ constexpr _Iter
+ sort(_Iter __first, _Sent __last, _Comp __comp = {}, _Proj __proj = {})
+ {
+ // TODO: is it worth reimplementing std::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::sort(std::move(__first), __lasti, std::move(__proj_comp));
+ return __lasti;
+ }
+
+ template<random_access_range _Range,
+ typename _Comp = ranges::less, typename _Proj = identity>
+ requires sortable<iterator_t<_Range>, _Comp, _Proj>
+ constexpr safe_iterator_t<_Range>
+ sort(_Range&& __r, _Comp __comp = {}, _Proj __proj = {})
+ {
+ return ranges::sort(ranges::begin(__r), 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/sort/constrained.cc b/libstdc++-v3/testsuite/25_algorithms/sort/constrained.cc
new file mode 100644
index 0000000..d822777
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/sort/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 } }
+// { 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);
+
+ VERIFY( ranges::sort(c) == c.end() );
+ VERIFY( ranges::sort(r) == ranges::end(r) );
+
+ VERIFY( ranges::equal(c, vref) );
+ VERIFY( ranges::equal(r, vref) );
+ }
+}
+
+struct X
+{
+ int i;
+ constexpr X(int a) : i(a) { }
+};
+
+constexpr bool
+test02()
+{
+ X x[] = {3,4,2,1,5};
+ const X y[] = {4,3,2,1,5};
+
+ auto res = ranges::sort(x, x+4, ranges::greater{}, &X::i);
+ return (res == x+4
+ && ranges::equal(x, y, {}, &X::i, &X::i));
+}
+
+int
+main()
+{
+ test01();
+ static_assert(test02());
+}
More information about the Libstdc++-cvs
mailing list