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

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


https://gcc.gnu.org/g:809ce45f134c7d42cf5b20cfed2240b3fae1ed98

commit 809ce45f134c7d42cf5b20cfed2240b3fae1ed98
Author: Patrick Palka <ppalka@gcc.gnu.org>
Date:   Wed Jan 15 10:32:35 2020 -0500

    Add ranges::search

Diff:
---
 libstdc++-v3/include/bits/ranges_algo.h            | 55 +++++++++++++++
 .../testsuite/25_algorithms/search/constrained.cc  | 82 ++++++++++++++++++++++
 2 files changed, 137 insertions(+)

diff --git a/libstdc++-v3/include/bits/ranges_algo.h b/libstdc++-v3/include/bits/ranges_algo.h
index 72b87ab..e17f71d 100644
--- a/libstdc++-v3/include/bits/ranges_algo.h
+++ b/libstdc++-v3/include/bits/ranges_algo.h
@@ -360,6 +360,61 @@ 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>
+    search(_Iter1 __first1, _Sent1 __last1, _Iter2 __first2, _Sent2 __last2,
+	   _Pred __pred = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
+    {
+      for (;;)
+	{
+	  for (;;)
+	    {
+	      if (__first1 == __last1)
+		return {__last1, __last1};
+	      if (std::__invoke(__pred,
+				std::__invoke(__proj1, *__first1),
+				std::__invoke(__proj2, *__first2)))
+		break;
+	      ++__first1;
+	    }
+	  auto __cur1 = __first1;
+	  auto __cur2 = __first2;
+	  for (;;)
+	    {
+	      if (++__cur2 == __last2)
+		return {__first1, ++__cur1};
+	      if (++__cur1 == __last1)
+		return {__last1, __last1};
+	      if (!std::__invoke(__pred,
+				 std::__invoke(__proj1, *__cur1),
+				 std::__invoke(__proj2, *__cur2)))
+		{
+		  ++__first1;
+		  break;
+		}
+	    }
+	}
+    }
+
+  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>
+    search(_Range1&& __r1, _Range2&& __r2,
+	   _Pred __pred = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
+    {
+      return ranges::search(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/search/constrained.cc b/libstdc++-v3/testsuite/25_algorithms/search/constrained.cc
new file mode 100644
index 0000000..85987a5
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/search/constrained.cc
@@ -0,0 +1,82 @@
+// 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[] = { {2}, {6}, {8}, {10}, {11} };
+  X y[] = { {10}, {11} };
+  {
+
+    test_container<X, forward_iterator_wrapper> c(x);
+    auto res = ranges::search(c, y, {}, &X::i, &X::i);
+    VERIFY( std::get<0>(res)->i == 10 && std::get<1>(res) == ranges::end(c) );
+    res = ranges::search(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::search(r, y, {}, &X::i, &X::i);
+    VERIFY( std::get<0>(res)->i == 10 && std::get<1>(res) == ranges::end(r) );
+    res = ranges::search(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}, {11} };
+  static constexpr X y[] = { {6}, {8} };
+  static constexpr int z[] = { 2, 8 };
+  static constexpr int w[] = { 2 };
+
+  static_assert(std::get<0>(ranges::search(x, y, {}, &X::i, &X::i)) == x+2);
+  static_assert(std::get<1>(ranges::search(x, y, {}, &X::i, &X::i)) == x+4);
+
+  static_assert(std::get<0>(ranges::search(x, z, {}, &X::i)) == x+6);
+  static_assert(std::get<1>(ranges::search(x, z, {}, &X::i)) == x+6);
+
+  static_assert(std::get<0>(ranges::search(x, w, {}, &X::i)) == x+0);
+  static_assert(std::get<1>(ranges::search(x, w, {}, &X::i)) == x+1);
+}
+
+int
+main()
+{
+  test01();
+  test02();
+}



More information about the Libstdc++-cvs mailing list