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

Patrick Palka ppalka@gcc.gnu.org
Fri Jan 17 21:44:00 GMT 2020


https://gcc.gnu.org/g:f7447b4a96228ea79f0a6ee93d7f9ed53e4bbbc0

commit f7447b4a96228ea79f0a6ee93d7f9ed53e4bbbc0
Author: Patrick Palka <ppalka@gcc.gnu.org>
Date:   Wed Jan 15 11:30:58 2020 -0500

    Add ranges::find_end

Diff:
---
 libstdc++-v3/include/bits/ranges_algo.h            | 50 ++++++++++++
 .../25_algorithms/find_end/constrained.cc          | 88 ++++++++++++++++++++++
 2 files changed, 138 insertions(+)

diff --git a/libstdc++-v3/include/bits/ranges_algo.h b/libstdc++-v3/include/bits/ranges_algo.h
index 36640dd..965fe85 100644
--- a/libstdc++-v3/include/bits/ranges_algo.h
+++ b/libstdc++-v3/include/bits/ranges_algo.h
@@ -418,6 +418,56 @@ namespace ranges
 			    std::move(__proj1), std::move(__proj2));
     }
 
+  template<forward_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
+	   forward_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
+	   class _Pred = ranges::equal_to,
+	   class _Proj1 = identity, class _Proj2 = identity>
+    requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
+    constexpr subrange<_Iter1>
+    find_end(_Iter1 __first1, _Sent1 __last1,
+	     _Iter2 __first2, _Sent2 __last2,
+	     _Pred __pred = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
+    {
+      if (__first2 == __last2)
+	return {__last1, __last1};
+
+      auto __res_first = __last1;
+      auto __res_last = __last1;
+      for (;;)
+	{
+	  auto __new_range = ranges::search(__first1, __last1,
+					    __first2, __last2,
+					    __pred, __proj1, __proj2);
+	  auto __new_res_first = ranges::begin(__new_range);
+	  auto __new_res_last = ranges::end(__new_range);
+	  if (__new_res_first == __last1)
+	    return {__res_first, __res_last};
+	  else
+	    {
+	      __res_first = __new_res_first;
+	      __res_last = __new_res_last;
+	      __first1 = __res_first;
+	      ++__first1;
+	    }
+	}
+    }
+
+  template<forward_range _Range1, forward_range _Range2,
+	   class _Pred = ranges::equal_to,
+	   class _Proj1 = identity, class _Proj2 = identity>
+    requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>,
+				   _Pred, _Proj1, _Proj2>
+    constexpr safe_subrange_t<_Range1>
+    find_end(_Range1&& __r1, _Range2&& __r2,
+	     _Pred __pred = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
+    {
+      return ranges::find_end(ranges::begin(__r1), ranges::end(__r1),
+			      ranges::begin(__r2), ranges::end(__r2),
+			      std::move(__pred),
+			      std::move(__proj1), std::move(__proj2));
+    }
+
+
 } // namespace ranges
 _GLIBCXX_END_NAMESPACE_VERSION
 } // namespace std
diff --git a/libstdc++-v3/testsuite/25_algorithms/find_end/constrained.cc b/libstdc++-v3/testsuite/25_algorithms/find_end/constrained.cc
new file mode 100644
index 0000000..70b44bb
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/find_end/constrained.cc
@@ -0,0 +1,88 @@
+// 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;
+
+struct X { int i; };
+
+void
+test01()
+{
+  X x[] = { {10}, {11}, {2}, {6}, {8}, {10}, {11} };
+  X y[] = { {10}, {11} };
+  {
+
+    test_container<X, forward_iterator_wrapper> c(x);
+    auto res = ranges::find_end(c, y, {}, &X::i, &X::i);
+    VERIFY( std::get<0>(res)->i == 10 && std::get<1>(res) == ranges::end(c) );
+    res = ranges::find_end(c, c, {}, &X::i, &X::i);
+    VERIFY( std::get<0>(res) == ranges::begin(c)
+	    && std::get<1>(res) == ranges::end(c) );
+  }
+
+  {
+    test_range<X, forward_iterator_wrapper> r(x);
+    auto res = ranges::find_end(r, y, {}, &X::i, &X::i);
+    VERIFY( std::get<0>(res)->i == 10 && std::get<1>(res) == ranges::end(r) );
+    res = ranges::find_end(r, r, {}, &X::i, &X::i);
+    VERIFY( std::get<0>(res) == ranges::begin(r)
+	    && std::get<1>(res) == ranges::end(r) );
+  }
+}
+
+void
+test02()
+{
+  static constexpr X x[] = { {2}, {2}, {6}, {8}, {10}, {6}, {8}, {11} };
+  static constexpr X y[] = { {6}, {8} };
+  static constexpr int z[] = { 2, 8 };
+  static constexpr int w[] = { 2 };
+
+  static_assert(std::get<0>(ranges::find_end(x, y, {}, &X::i, &X::i)) == x+5);
+  static_assert(std::get<1>(ranges::find_end(x, y, {}, &X::i, &X::i)) == x+7);
+
+  static_assert(std::get<0>(ranges::find_end(x, z, {}, &X::i)) == x+8);
+  static_assert(std::get<1>(ranges::find_end(x, z, {}, &X::i)) == x+8);
+
+  static_assert(std::get<0>(ranges::find_end(x, w, {}, &X::i)) == x+1);
+  static_assert(std::get<1>(ranges::find_end(x, w, {}, &X::i)) == x+2);
+
+  static_assert(std::get<0>(ranges::find_end(x, x+6, w, w, {}, &X::i)) == x+6);
+  static_assert(std::get<1>(ranges::find_end(x, x+6, w, w, {}, &X::i)) == x+6);
+
+  static_assert(std::get<0>(ranges::find_end(x, x, w, w+1, {}, &X::i)) == x+0);
+  static_assert(std::get<1>(ranges::find_end(x, x, w, w+1, {}, &X::i)) == x+0);
+}
+
+int
+main()
+{
+  test01();
+  test02();
+}



More information about the Libstdc++-cvs mailing list