forked from OSchip/llvm-project
[libc++] Implement ranges::adjacent_find
Reviewed By: Mordante, var-const, #libc Spies: libcxx-commits, mgorny Differential Revision: https://reviews.llvm.org/D126610
This commit is contained in:
parent
10ac235b07
commit
916e9052ba
|
@ -66,6 +66,7 @@ set(files
|
|||
__algorithm/pop_heap.h
|
||||
__algorithm/prev_permutation.h
|
||||
__algorithm/push_heap.h
|
||||
__algorithm/ranges_adjacent_find.h
|
||||
__algorithm/ranges_all_of.h
|
||||
__algorithm/ranges_any_of.h
|
||||
__algorithm/ranges_binary_search.h
|
||||
|
|
|
@ -0,0 +1,78 @@
|
|||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
||||
// See https://llvm.org/LICENSE.txt for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#ifndef _LIBCPP___ALGORITHM_RANGES_ADJACENT_FIND_H
|
||||
#define _LIBCPP___ALGORITHM_RANGES_ADJACENT_FIND_H
|
||||
|
||||
#include <__config>
|
||||
#include <__functional/identity.h>
|
||||
#include <__functional/invoke.h>
|
||||
#include <__functional/ranges_operations.h>
|
||||
#include <__iterator/concepts.h>
|
||||
#include <__iterator/projected.h>
|
||||
#include <__ranges/access.h>
|
||||
#include <__ranges/concepts.h>
|
||||
#include <__ranges/dangling.h>
|
||||
#include <__utility/move.h>
|
||||
|
||||
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
|
||||
# pragma GCC system_header
|
||||
#endif
|
||||
|
||||
#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
|
||||
|
||||
_LIBCPP_BEGIN_NAMESPACE_STD
|
||||
|
||||
namespace ranges {
|
||||
namespace __adjacent_find {
|
||||
struct __fn {
|
||||
|
||||
template <class _Iter, class _Sent, class _Proj, class _Pred>
|
||||
_LIBCPP_HIDE_FROM_ABI constexpr static
|
||||
_Iter __adjacent_find_impl(_Iter __first, _Sent __last, _Pred& __pred, _Proj& __proj) {
|
||||
if (__first == __last)
|
||||
return __first;
|
||||
|
||||
auto __i = __first;
|
||||
while (++__i != __last) {
|
||||
if (std::invoke(__pred, std::invoke(__proj, *__first), std::invoke(__proj, *__i)))
|
||||
return __first;
|
||||
__first = __i;
|
||||
}
|
||||
return __i;
|
||||
}
|
||||
|
||||
template <forward_iterator _Iter, sentinel_for<_Iter> _Sent,
|
||||
class _Proj = identity,
|
||||
indirect_binary_predicate<projected<_Iter, _Proj>, projected<_Iter, _Proj>> _Pred = ranges::equal_to>
|
||||
_LIBCPP_HIDE_FROM_ABI constexpr
|
||||
_Iter operator()(_Iter __first, _Sent __last, _Pred __pred = {}, _Proj __proj = {}) const {
|
||||
return __adjacent_find_impl(std::move(__first), std::move(__last), __pred, __proj);
|
||||
}
|
||||
|
||||
template <forward_range _Range,
|
||||
class _Proj = identity,
|
||||
indirect_binary_predicate<projected<iterator_t<_Range>, _Proj>,
|
||||
projected<iterator_t<_Range>, _Proj>> _Pred = ranges::equal_to>
|
||||
_LIBCPP_HIDE_FROM_ABI constexpr
|
||||
borrowed_iterator_t<_Range> operator()(_Range&& __range, _Pred __pred = {}, _Proj __proj = {}) const {
|
||||
return __adjacent_find_impl(ranges::begin(__range), ranges::end(__range), __pred, __proj);
|
||||
}
|
||||
};
|
||||
} // namespace __adjacent_find
|
||||
|
||||
inline namespace __cpo {
|
||||
inline constexpr auto adjacent_find = __adjacent_find::__fn{};
|
||||
} // namespace __cpo
|
||||
} // namespace ranges
|
||||
|
||||
_LIBCPP_END_NAMESPACE_STD
|
||||
|
||||
#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
|
||||
|
||||
#endif // _LIBCPP___ALGORITHM_RANGES_ADJACENT_FIND_H
|
|
@ -391,6 +391,16 @@ namespace ranges {
|
|||
Pred pred = {},
|
||||
Proj1 proj1 = {}, Proj2 proj2 = {}); // since C++20
|
||||
|
||||
template<forward_iterator I, sentinel_for<I> S, class Proj = identity,
|
||||
indirect_binary_predicate<projected<I, Proj>,
|
||||
projected<I, Proj>> Pred = ranges::equal_to>
|
||||
constexpr I ranges::adjacent_find(I first, S last, Pred pred = {}, Proj proj = {}); // since C+20
|
||||
|
||||
template<forward_range R, class Proj = identity,
|
||||
indirect_binary_predicate<projected<iterator_t<R>, Proj>,
|
||||
projected<iterator_t<R>, Proj>> Pred = ranges::equal_to>
|
||||
constexpr borrowed_iterator_t<R> ranges::adjacent_find(R&& r, Pred pred = {}, Proj proj = {}); // since C++20
|
||||
|
||||
}
|
||||
|
||||
constexpr bool // constexpr in C++20
|
||||
|
@ -1107,6 +1117,7 @@ template <class BidirectionalIterator, class Compare>
|
|||
#include <__algorithm/pop_heap.h>
|
||||
#include <__algorithm/prev_permutation.h>
|
||||
#include <__algorithm/push_heap.h>
|
||||
#include <__algorithm/ranges_adjacent_find.h>
|
||||
#include <__algorithm/ranges_all_of.h>
|
||||
#include <__algorithm/ranges_any_of.h>
|
||||
#include <__algorithm/ranges_binary_search.h>
|
||||
|
|
|
@ -298,6 +298,7 @@ module std [system] {
|
|||
module pop_heap { private header "__algorithm/pop_heap.h" }
|
||||
module prev_permutation { private header "__algorithm/prev_permutation.h" }
|
||||
module push_heap { private header "__algorithm/push_heap.h" }
|
||||
module ranges_adjacent_find { private header "__algorithm/ranges_adjacent_find.h" }
|
||||
module ranges_all_of { private header "__algorithm/ranges_all_of.h" }
|
||||
module ranges_any_of { private header "__algorithm/ranges_any_of.h" }
|
||||
module ranges_binary_search { private header "__algorithm/ranges_binary_search.h" }
|
||||
|
|
|
@ -90,8 +90,8 @@ constexpr bool all_the_algorithms()
|
|||
int count = 1;
|
||||
|
||||
int copies = 0;
|
||||
//(void)std::ranges::adjacent_find(first, last, Equal(&copies)); assert(copies == 0);
|
||||
//(void)std::ranges::adjacent_find(a, Equal(&copies)); assert(copies == 0);
|
||||
(void)std::ranges::adjacent_find(first, last, Equal(&copies)); assert(copies == 0);
|
||||
(void)std::ranges::adjacent_find(a, Equal(&copies)); assert(copies == 0);
|
||||
(void)std::ranges::all_of(first, last, UnaryTrue(&copies)); assert(copies == 0);
|
||||
(void)std::ranges::all_of(a, UnaryTrue(&copies)); assert(copies == 0);
|
||||
(void)std::ranges::any_of(first, last, UnaryTrue(&copies)); assert(copies == 0);
|
||||
|
|
|
@ -72,8 +72,8 @@ constexpr bool all_the_algorithms()
|
|||
int count = 1;
|
||||
|
||||
int copies = 0;
|
||||
//(void)std::ranges::adjacent_find(first, last, Equal(), Proj(&copies)); assert(copies == 0);
|
||||
//(void)std::ranges::adjacent_find(a, Equal(), Proj(&copies)); assert(copies == 0);
|
||||
(void)std::ranges::adjacent_find(first, last, Equal(), Proj(&copies)); assert(copies == 0);
|
||||
(void)std::ranges::adjacent_find(a, Equal(), Proj(&copies)); assert(copies == 0);
|
||||
(void)std::ranges::all_of(first, last, UnaryTrue(), Proj(&copies)); assert(copies == 0);
|
||||
(void)std::ranges::all_of(a, UnaryTrue(), Proj(&copies)); assert(copies == 0);
|
||||
(void)std::ranges::any_of(first, last, UnaryTrue(), Proj(&copies)); assert(copies == 0);
|
||||
|
|
|
@ -103,6 +103,7 @@ END-SCRIPT
|
|||
#include <__algorithm/pop_heap.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/pop_heap.h'}}
|
||||
#include <__algorithm/prev_permutation.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/prev_permutation.h'}}
|
||||
#include <__algorithm/push_heap.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/push_heap.h'}}
|
||||
#include <__algorithm/ranges_adjacent_find.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_adjacent_find.h'}}
|
||||
#include <__algorithm/ranges_all_of.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_all_of.h'}}
|
||||
#include <__algorithm/ranges_any_of.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_any_of.h'}}
|
||||
#include <__algorithm/ranges_binary_search.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_binary_search.h'}}
|
||||
|
|
|
@ -0,0 +1,197 @@
|
|||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
||||
// See https://llvm.org/LICENSE.txt for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <algorithm>
|
||||
|
||||
// UNSUPPORTED: c++03, c++11, c++14, c++17
|
||||
// UNSUPPORTED: libcpp-has-no-incomplete-ranges
|
||||
|
||||
// template<forward_iterator I, sentinel_for<I> S, class Proj = identity,
|
||||
// indirect_binary_predicate<projected<I, Proj>,
|
||||
// projected<I, Proj>> Pred = ranges::equal_to>
|
||||
// constexpr I ranges::adjacent_find(I first, S last, Pred pred = {}, Proj proj = {});
|
||||
// template<forward_range R, class Proj = identity,
|
||||
// indirect_binary_predicate<projected<iterator_t<R>, Proj>,
|
||||
// projected<iterator_t<R>, Proj>> Pred = ranges::equal_to>
|
||||
// constexpr borrowed_iterator_t<R> ranges::adjacent_find(R&& r, Pred pred = {}, Proj proj = {});
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
#include <ranges>
|
||||
|
||||
#include "almost_satisfies_types.h"
|
||||
#include "boolean_testable.h"
|
||||
#include "test_iterators.h"
|
||||
|
||||
template <class Iter, class Sent = Iter>
|
||||
concept HasAdjacentFindIt = requires (Iter iter, Sent sent) { std::ranges::adjacent_find(iter, sent); };
|
||||
|
||||
struct NotComparable {};
|
||||
|
||||
static_assert(HasAdjacentFindIt<int*>);
|
||||
static_assert(!HasAdjacentFindIt<ForwardIteratorNotDerivedFrom>);
|
||||
static_assert(!HasAdjacentFindIt<ForwardIteratorNotIncrementable>);
|
||||
static_assert(!HasAdjacentFindIt<int*, SentinelForNotSemiregular>);
|
||||
static_assert(!HasAdjacentFindIt<int*, SentinelForNotWeaklyEqualityComparableWith>);
|
||||
static_assert(!HasAdjacentFindIt<NotComparable*>);
|
||||
|
||||
template <class Range>
|
||||
concept HasAdjacentFindR = requires (Range range) { std::ranges::adjacent_find(range); };
|
||||
|
||||
static_assert(HasAdjacentFindR<UncheckedRange<int*>>);
|
||||
static_assert(!HasAdjacentFindR<ForwardRangeNotDerivedFrom>);
|
||||
static_assert(!HasAdjacentFindR<ForwardRangeNotIncrementable>);
|
||||
static_assert(!HasAdjacentFindR<ForwardRangeNotSentinelSemiregular>);
|
||||
static_assert(!HasAdjacentFindR<ForwardRangeNotSentinelEqualityComparableWith>);
|
||||
static_assert(!HasAdjacentFindR<UncheckedRange<NotComparable>>);
|
||||
|
||||
template <size_t N>
|
||||
struct Data {
|
||||
std::array<int, N> input;
|
||||
int expected;
|
||||
};
|
||||
|
||||
template <class Iter, class Sent, size_t N>
|
||||
constexpr void test(Data<N> d) {
|
||||
{
|
||||
std::same_as<Iter> decltype(auto) ret =
|
||||
std::ranges::adjacent_find(Iter(d.input.data()), Sent(Iter(d.input.data() + d.input.size())));
|
||||
assert(base(ret) == d.input.data() + d.expected);
|
||||
}
|
||||
{
|
||||
auto range = std::ranges::subrange(Iter(d.input.data()), Sent(Iter(d.input.data() + d.input.size())));
|
||||
std::same_as<Iter> decltype(auto) ret = std::ranges::adjacent_find(range);
|
||||
assert(base(ret) == d.input.data() + d.expected);
|
||||
}
|
||||
}
|
||||
|
||||
template <class Iter, class Sent = Iter>
|
||||
constexpr void test_iterators() {
|
||||
// simple test
|
||||
test<Iter, Sent, 4>({.input = {1, 2, 2, 4}, .expected = 1});
|
||||
// last is returned with no match
|
||||
test<Iter, Sent, 4>({.input = {1, 2, 3, 4}, .expected = 4});
|
||||
// first elements match
|
||||
test<Iter, Sent, 4>({.input = {1, 1, 3, 4}, .expected = 0});
|
||||
// the first match is returned
|
||||
test<Iter, Sent, 7>({.input = {1, 1, 3, 4, 4, 4, 4}, .expected = 0});
|
||||
// two element range works
|
||||
test<Iter, Sent, 2>({.input = {3, 3}, .expected = 0});
|
||||
// single element range works
|
||||
test<Iter, Sent, 1>({.input = {1}, .expected = 1});
|
||||
// empty range works
|
||||
test<Iter, Sent, 0>({.input = {}, .expected = 0});
|
||||
}
|
||||
|
||||
constexpr bool test() {
|
||||
test_iterators<forward_iterator<int*>, sentinel_wrapper<forward_iterator<int*>>>();
|
||||
test_iterators<forward_iterator<int*>>();
|
||||
test_iterators<bidirectional_iterator<int*>>();
|
||||
test_iterators<random_access_iterator<int*>>();
|
||||
test_iterators<contiguous_iterator<int*>>();
|
||||
test_iterators<int*>();
|
||||
test_iterators<const int*>();
|
||||
|
||||
{ // check that ranges::dangling is returned
|
||||
[[maybe_unused]] std::same_as<std::ranges::dangling> decltype(auto) ret =
|
||||
std::ranges::adjacent_find(std::array{1, 2, 3, 4});
|
||||
}
|
||||
|
||||
{ // check that the complexity requirements are met with no match
|
||||
{
|
||||
int predicateCount = 0;
|
||||
auto pred = [&](int, int) { ++predicateCount; return false; };
|
||||
auto projectionCount = 0;
|
||||
auto proj = [&](int i) { ++projectionCount; return i; };
|
||||
int a[] = {1, 2, 3, 4, 5};
|
||||
auto ret = std::ranges::adjacent_find(a, a + 5, pred, proj);
|
||||
assert(ret == a + 5);
|
||||
assert(predicateCount == 4);
|
||||
assert(projectionCount == 8);
|
||||
}
|
||||
{
|
||||
int predicateCount = 0;
|
||||
auto pred = [&](int, int) { ++predicateCount; return false; };
|
||||
auto projectionCount = 0;
|
||||
auto proj = [&](int i) { ++projectionCount; return i; };
|
||||
int a[] = {1, 2, 3, 4, 5};
|
||||
auto ret = std::ranges::adjacent_find(a, pred, proj);
|
||||
assert(ret == a + 5);
|
||||
assert(predicateCount == 4);
|
||||
assert(projectionCount == 8);
|
||||
}
|
||||
}
|
||||
|
||||
{ // check that the complexity requirements are met with a match
|
||||
{
|
||||
int predicateCount = 0;
|
||||
auto pred = [&](int i, int j) { ++predicateCount; return i == j; };
|
||||
auto projectionCount = 0;
|
||||
auto proj = [&](int i) { ++projectionCount; return i; };
|
||||
int a[] = {1, 2, 4, 4, 5};
|
||||
auto ret = std::ranges::adjacent_find(a, a + 5, pred, proj);
|
||||
assert(ret == a + 2);
|
||||
assert(predicateCount == 3);
|
||||
assert(projectionCount == 6);
|
||||
}
|
||||
{
|
||||
int predicateCount = 0;
|
||||
auto pred = [&](int i, int j) { ++predicateCount; return i == j; };
|
||||
auto projectionCount = 0;
|
||||
auto proj = [&](int i) { ++projectionCount; return i; };
|
||||
int a[] = {1, 2, 4, 4, 5};
|
||||
auto ret = std::ranges::adjacent_find(a, pred, proj);
|
||||
assert(ret == a + 2);
|
||||
assert(predicateCount == 3);
|
||||
assert(projectionCount == 6);
|
||||
}
|
||||
}
|
||||
|
||||
{ // check that std::invoke is used
|
||||
struct S {
|
||||
constexpr S(int i_) : i(i_) {}
|
||||
constexpr bool compare(const S& j) const { return j.i == i; }
|
||||
constexpr const S& identity() const { return *this; }
|
||||
int i;
|
||||
};
|
||||
{
|
||||
S a[] = {1, 2, 3, 4};
|
||||
auto ret = std::ranges::adjacent_find(std::begin(a), std::end(a), &S::compare, &S::identity);
|
||||
assert(ret == a + 4);
|
||||
}
|
||||
{
|
||||
S a[] = {1, 2, 3, 4};
|
||||
auto ret = std::ranges::adjacent_find(a, &S::compare, &S::identity);
|
||||
assert(ret == a + 4);
|
||||
}
|
||||
}
|
||||
|
||||
{ // check that the implicit conversion to bool works
|
||||
{
|
||||
int a[] = {1, 2, 2, 4};
|
||||
auto ret = std::ranges::adjacent_find(a, a + 4, [](int i, int j) { return BooleanTestable{i == j}; });
|
||||
assert(ret == a + 1);
|
||||
}
|
||||
{
|
||||
int a[] = {1, 2, 2, 4};
|
||||
auto ret = std::ranges::adjacent_find(a, [](int i, int j) { return BooleanTestable{i == j}; });
|
||||
assert(ret == a + 1);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int, char**) {
|
||||
test();
|
||||
static_assert(test());
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -60,7 +60,7 @@ auto triple = [](int x) { return 3*x; };
|
|||
|
||||
// [algorithm.syn]
|
||||
|
||||
//static_assert(test(std::ranges::adjacent_find, a));
|
||||
static_assert(test(std::ranges::adjacent_find, a));
|
||||
static_assert(test(std::ranges::all_of, a, odd));
|
||||
static_assert(test(std::ranges::any_of, a, odd));
|
||||
static_assert(test(std::ranges::binary_search, a, 42));
|
||||
|
|
Loading…
Reference in New Issue