[gcc(refs/users/ppalka/heads/libstdcxx-constrained-algos)] Add ranges::adjacent_find
Patrick Palka
ppalka@gcc.gnu.org
Fri Jan 17 21:44:00 GMT 2020
https://gcc.gnu.org/g:e9d0c93beb62909d18038f5109d986868ff9aedd
commit e9d0c93beb62909d18038f5109d986868ff9aedd
Author: Patrick Palka <ppalka@gcc.gnu.org>
Date: Fri Jan 17 12:37:48 2020 -0500
Add ranges::adjacent_find
Diff:
---
libstdc++-v3/include/bits/ranges_algo.h | 33 +++++++++++
.../25_algorithms/adjacent_find/constrained.cc | 68 ++++++++++++++++++++++
2 files changed, 101 insertions(+)
diff --git a/libstdc++-v3/include/bits/ranges_algo.h b/libstdc++-v3/include/bits/ranges_algo.h
index b89f19a..0c53a54 100644
--- a/libstdc++-v3/include/bits/ranges_algo.h
+++ b/libstdc++-v3/include/bits/ranges_algo.h
@@ -500,6 +500,39 @@ namespace ranges
std::move(__proj1), std::move(__proj2));
}
+ template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
+ class _Proj = identity,
+ indirect_binary_predicate<projected<_Iter, _Proj>,
+ projected<_Iter, _Proj>> _Pred
+ = ranges::equal_to>
+ constexpr _Iter
+ adjacent_find(_Iter __first, _Sent __last,
+ _Pred __pred = {}, _Proj __proj = {})
+ {
+ if (__first == __last)
+ return __last;
+ auto __next = __first;
+ while (++__next != __last)
+ {
+ if (std::__invoke(__pred,
+ std::__invoke(__proj, *__first),
+ std::__invoke(__proj, *__next)))
+ return __first;
+ __first = __next;
+ }
+ return __last;
+ }
+
+ template<forward_range _Range, class _Proj = identity,
+ indirect_binary_predicate<
+ projected<iterator_t<_Range>, _Proj>,
+ projected<iterator_t<_Range>, _Proj>> _Pred = ranges::equal_to>
+ constexpr safe_iterator_t<_Range>
+ adjacent_find(_Range&& __r, _Pred __pred = {}, _Proj __proj = {})
+ {
+ return ranges::adjacent_find(ranges::begin(__r), ranges::end(__r),
+ std::move(__pred), std::move(__proj));
+ }
} // namespace ranges
_GLIBCXX_END_NAMESPACE_VERSION
diff --git a/libstdc++-v3/testsuite/25_algorithms/adjacent_find/constrained.cc b/libstdc++-v3/testsuite/25_algorithms/adjacent_find/constrained.cc
new file mode 100644
index 0000000..d56ac5a
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/adjacent_find/constrained.cc
@@ -0,0 +1,68 @@
+// 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, 8, 9 };
+
+ VERIFY( ranges::adjacent_find(x, x+6, {}, &X::i) == x+0 );
+ VERIFY( ranges::adjacent_find(x+1, x+6, {}, &X::i) == x+6 );
+ VERIFY( ranges::adjacent_find(y) == y+2 );
+ VERIFY( ranges::adjacent_find(y, y+4) == y+2 );
+
+ test_container<X, forward_iterator_wrapper> c(x);
+ VERIFY( ranges::adjacent_find(c, {}, &X::i) == ranges::begin(c) );
+
+ test_range<int, forward_iterator_wrapper> r(y);
+ auto res = ranges::adjacent_find(r);
+ VERIFY( *res == 8 && *++res == 8 );
+}
+
+void
+test02()
+{
+ static constexpr X x[] = { {2}, {2}, {6}, {8}, {10}, {11} };
+ static constexpr X y[] = { {2}, {6}, {8}, {10}, {11} };
+ static_assert(ranges::adjacent_find(x, {}, &X::i) == x+0);
+ static_assert(ranges::adjacent_find(y, {}, &X::i) == y+5);
+}
+
+int
+main()
+{
+ test01();
+ test02();
+}
+
More information about the Libstdc++-cvs
mailing list