[gcc(refs/users/ppalka/heads/libstdcxx-constrained-algos)] Add ranges::stable_sort

Patrick Palka ppalka@gcc.gnu.org
Thu Jan 23 22:14:00 GMT 2020


https://gcc.gnu.org/g:0ae4c05773bac88d04d981b90fbb8b889b6c099e

commit 0ae4c05773bac88d04d981b90fbb8b889b6c099e
Author: Patrick Palka <ppalka@redhat.com>
Date:   Thu Jan 23 16:40:02 2020 -0500

    Add ranges::stable_sort

Diff:
---
 libstdc++-v3/include/bits/ranges_algo.h            | 30 ++++++++++
 .../25_algorithms/stable_sort/constrained.cc       | 70 ++++++++++++++++++++++
 2 files changed, 100 insertions(+)

diff --git a/libstdc++-v3/include/bits/ranges_algo.h b/libstdc++-v3/include/bits/ranges_algo.h
index c64cb3c..a43e7b8 100644
--- a/libstdc++-v3/include/bits/ranges_algo.h
+++ b/libstdc++-v3/include/bits/ranges_algo.h
@@ -2003,6 +2003,36 @@ namespace ranges
 			  std::move(__comp), std::move(__proj));
     }
 
+  template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
+	   typename _Comp = ranges::less, typename _Proj = identity>
+    requires sortable<_Iter, _Comp, _Proj>
+    _Iter
+    stable_sort(_Iter __first, _Sent __last,
+		_Comp __comp = {}, _Proj __proj = {})
+    {
+      // TODO: is it worth reimplementing std::stable_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::stable_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>
+    safe_iterator_t<_Range>
+    stable_sort(_Range&& __r, _Comp __comp = {}, _Proj __proj = {})
+    {
+      return ranges::stable_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/stable_sort/constrained.cc b/libstdc++-v3/testsuite/25_algorithms/stable_sort/constrained.cc
new file mode 100644
index 0000000..23a8c03
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/stable_sort/constrained.cc
@@ -0,0 +1,70 @@
+// 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;
+
+// This test doesn't verify the stability property of ranges::stable_sort,
+// because at the moment it's just defined to be a wrapper over
+// std::stable_sort.
+
+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);
+
+      auto res1 = ranges::stable_sort(c.begin(), c.end(), {}, std::negate<>{});
+      VERIFY( res1 == c.end() );
+
+      auto res2 = ranges::stable_sort(r, ranges::greater{});
+      VERIFY( res2 == ranges::end(r) );
+
+      VERIFY( ranges::equal(c, r) );
+      VERIFY( ranges::equal(c.begin(), c.end(), vref.rbegin(), vref.rend()) );
+    }
+}
+
+int
+main()
+{
+  test01();
+}



More information about the Libstdc++-cvs mailing list