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

Patrick Palka ppalka@gcc.gnu.org
Wed Jan 15 15:40:00 GMT 2020


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

commit d70aa3b3535aadeabfd4121ff0f6588c701139b4
Author: Patrick Palka <ppalka@gcc.gnu.org>
Date:   Tue Jan 14 14:43:43 2020 -0500

    Add ranges::find_first_of

Diff:
---
 libstdc++-v3/include/bits/ranges_algo.h            | 34 +++++++++
 .../25_algorithms/find_first_of/constrained.cc     | 82 ++++++++++++++++++++++
 2 files changed, 116 insertions(+)

diff --git a/libstdc++-v3/include/bits/ranges_algo.h b/libstdc++-v3/include/bits/ranges_algo.h
index 2f25355..72b87ab 100644
--- a/libstdc++-v3/include/bits/ranges_algo.h
+++ b/libstdc++-v3/include/bits/ranges_algo.h
@@ -219,6 +219,40 @@ namespace ranges
 				 std::move(__pred), std::move(__proj));
     }
 
+  template<input_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 _Iter1
+    find_first_of(_Iter1 __first1, _Sent1 __last1,
+		  _Iter2 __first2, _Sent2 __last2,
+		  _Pred __pred = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
+    {
+      for (; __first1 != __last1; ++__first1)
+	for (auto __iter = __first2; __iter != __last2; ++__iter)
+	  if (std::__invoke(__pred,
+			    std::__invoke(__proj1, *__first1),
+			    std::__invoke(__proj2, *__iter)))
+	    return __first1;
+      return __last1;
+    }
+
+  template<input_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_iterator_t<_Range1>
+    find_first_of(_Range1&& __r1, _Range2&& __r2,
+		  _Pred __pred = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
+    {
+      return ranges::find_first_of(ranges::begin(__r1), ranges::end(__r1),
+				   ranges::begin(__r2), ranges::end(__r2),
+				   std::move(__pred),
+				   std::move(__proj1), std::move(__proj2));
+    }
+
   template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
 	   typename _Tp, typename _Proj = identity>
     requires indirect_binary_predicate<ranges::equal_to,
diff --git a/libstdc++-v3/testsuite/25_algorithms/find_first_of/constrained.cc b/libstdc++-v3/testsuite/25_algorithms/find_first_of/constrained.cc
new file mode 100644
index 0000000..ccb4558
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/find_first_of/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}, {2}, {6}, {8}, {10}, {11} };
+  int y[] = { 2, 7, 8, 9 };
+  X w[] = { {2}, {7}, {8}, {9} };
+
+  auto res = ranges::find_first_of(x, x+6, y+1, y+4, {}, &X::i);
+  VERIFY( res == x+3 );
+  res = ranges::find_first_of(x, x+6, w, w+4, {}, &X::i, &X::i);
+  VERIFY( res == x+0 );
+  res = ranges::find_first_of(x, x+6, y+3, y+4, {}, &X::i);
+  VERIFY( res == x+6 );
+
+  test_container<X, forward_iterator_wrapper> c(x);
+  test_container<int, forward_iterator_wrapper> d1(y+1, y+4);
+  auto res2 = ranges::find_first_of(c, d1, {}, &X::i);
+  VERIFY( res2 != ranges::end(c) && res2->i == 8 );
+
+  test_container<X, forward_iterator_wrapper> d2(w+3, w+4);
+  res2 = ranges::find_first_of(c, d2, {}, &X::i, &X::i);
+  VERIFY( res2 == ranges::end(c) );
+
+  test_range<X, input_iterator_wrapper> r(x);
+  test_range<int, forward_iterator_wrapper> s1(y+1, y+4);
+  auto res3 = ranges::find_first_of(r, s1, {}, &X::i);
+  VERIFY( res3 != ranges::end(r) && res3->i == 8 );
+
+  test_range<X, forward_iterator_wrapper> s2(w+3, w+4);
+  res3 = ranges::find_first_of(r, s2, {}, &X::i, &X::i);
+  VERIFY( res3 == ranges::end(r) );
+}
+
+struct Y { int i; int j; };
+
+void
+test02()
+{
+  static constexpr Y y[] = { {1,2}, {2,4}, {3,6} };
+  static_assert(ranges::find_first_of(y, y, {}, &Y::j, &Y::i) == y);
+  static_assert(ranges::find_first_of(y, y, {}, &Y::i, &Y::j) == y+1);
+}
+
+int
+main()
+{
+  test01();
+  test02();
+}



More information about the Libstdc++-cvs mailing list