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

Patrick Palka ppalka@gcc.gnu.org
Wed Jan 22 18:44:00 GMT 2020


https://gcc.gnu.org/g:042767b3b786a21c7d25511f82434bada281165e

commit 042767b3b786a21c7d25511f82434bada281165e
Author: Patrick Palka <ppalka@redhat.com>
Date:   Wed Jan 22 11:47:03 2020 -0500

    Add ranges::reverse

Diff:
---
 libstdc++-v3/include/bits/ranges_algo.h            | 44 ++++++++++++
 .../testsuite/25_algorithms/reverse/constrained.cc | 78 ++++++++++++++++++++++
 2 files changed, 122 insertions(+)

diff --git a/libstdc++-v3/include/bits/ranges_algo.h b/libstdc++-v3/include/bits/ranges_algo.h
index 291d0ac..bcf9442 100644
--- a/libstdc++-v3/include/bits/ranges_algo.h
+++ b/libstdc++-v3/include/bits/ranges_algo.h
@@ -1520,6 +1520,50 @@ namespace ranges
 				   std::move(__comp), std::move(__proj));
       }
 
+    template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent>
+      requires permutable<_Iter>
+      constexpr _Iter
+      reverse(_Iter __first, _Sent __last)
+      {
+	auto __i = ranges::next(__first, __last);
+	auto __tail = __i;
+
+	if constexpr (random_access_iterator<_Iter>)
+	  {
+	    if (__first != __last)
+	      {
+		--__tail;
+		while (__first < __tail)
+		  {
+		    ranges::iter_swap(__first, __tail);
+		    ++__first;
+		    --__tail;
+		  }
+	      }
+	    return __i;
+	  }
+	else
+	  {
+	    for (;;)
+	      if (__first == __tail || __first == --__tail)
+		break;
+	      else
+		{
+		  ranges::iter_swap(__first, __tail);
+		  ++__first;
+		}
+	    return __i;
+	  }
+      }
+
+    template<bidirectional_range _Range>
+      requires permutable<iterator_t<_Range>>
+      constexpr safe_iterator_t<_Range>
+      reverse(_Range&& __r)
+      {
+	return ranges::reverse(ranges::begin(__r), ranges::end(__r));
+      }
+
 } // namespace ranges
 _GLIBCXX_END_NAMESPACE_VERSION
 } // namespace std
diff --git a/libstdc++-v3/testsuite/25_algorithms/reverse/constrained.cc b/libstdc++-v3/testsuite/25_algorithms/reverse/constrained.cc
new file mode 100644
index 0000000..da1bbc6
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/reverse/constrained.cc
@@ -0,0 +1,78 @@
+// 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 <list>
+#include <algorithm>
+#include <testsuite_hooks.h>
+#include <testsuite_iterators.h>
+
+using __gnu_test::test_range;
+using __gnu_test::test_container;
+using __gnu_test::bidirectional_iterator_wrapper;
+using __gnu_test::random_access_iterator_wrapper;
+
+namespace ranges = std::ranges;
+
+template<template<typename> typename wrapper>
+void
+test01()
+{
+  int x[] = { 1, 2, 3, 4 };
+  test_container<int, wrapper> cx(x);
+  const int y[] = { 4, 3, 2, 1 };
+
+  auto res = ranges::reverse(cx);
+  VERIFY( res == ranges::end(cx) );
+  VERIFY( ranges::equal(cx, y) );
+}
+
+template<template<typename> typename wrapper>
+void
+test02()
+{
+  int x[] = { 1, 2, 3, 4, 5 };
+  test_range<int, wrapper> rx(x);
+  const int y[] = { 5, 4, 3, 2, 1 };
+
+  auto res = ranges::reverse(rx);
+  VERIFY( res == ranges::end(rx) );
+  VERIFY( ranges::equal(rx, y) );
+}
+
+constexpr bool
+test03()
+{
+  int x[] = { 1, 2, 3 };
+  const int y[] = { 2, 1, 3 };
+  ranges::reverse(x, x+2);
+  return ranges::equal(x, y);
+}
+
+int
+main()
+{
+  test01<bidirectional_iterator_wrapper>();
+  test02<bidirectional_iterator_wrapper>();
+
+  test01<random_access_iterator_wrapper>();
+  test02<random_access_iterator_wrapper>();
+
+  static_assert(test03());
+}



More information about the Libstdc++-cvs mailing list