1*ee0f8c40SNikolas Klauser //===----------------------------------------------------------------------===//
2*ee0f8c40SNikolas Klauser //
3*ee0f8c40SNikolas Klauser // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*ee0f8c40SNikolas Klauser // See https://llvm.org/LICENSE.txt for license information.
5*ee0f8c40SNikolas Klauser // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*ee0f8c40SNikolas Klauser //
7*ee0f8c40SNikolas Klauser //===----------------------------------------------------------------------===//
8*ee0f8c40SNikolas Klauser
9*ee0f8c40SNikolas Klauser // <algorithm>
10*ee0f8c40SNikolas Klauser
11*ee0f8c40SNikolas Klauser // UNSUPPORTED: c++03, c++11, c++14, c++17
12*ee0f8c40SNikolas Klauser
13*ee0f8c40SNikolas Klauser // template<input_iterator I, sentinel_for<I> S, class Proj = identity,
14*ee0f8c40SNikolas Klauser // indirect_unary_predicate<projected<I, Proj>> Pred>
15*ee0f8c40SNikolas Klauser // constexpr I ranges::find_if_not(I first, S last, Pred pred, Proj proj = {});
16*ee0f8c40SNikolas Klauser // template<input_range R, class Proj = identity,
17*ee0f8c40SNikolas Klauser // indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
18*ee0f8c40SNikolas Klauser // constexpr borrowed_iterator_t<R>
19*ee0f8c40SNikolas Klauser // ranges::find_if_not(R&& r, Pred pred, Proj proj = {});
20*ee0f8c40SNikolas Klauser
21*ee0f8c40SNikolas Klauser #include <algorithm>
22*ee0f8c40SNikolas Klauser #include <array>
23*ee0f8c40SNikolas Klauser #include <cassert>
24*ee0f8c40SNikolas Klauser #include <ranges>
25*ee0f8c40SNikolas Klauser
26*ee0f8c40SNikolas Klauser #include "almost_satisfies_types.h"
27*ee0f8c40SNikolas Klauser #include "test_iterators.h"
28*ee0f8c40SNikolas Klauser
29*ee0f8c40SNikolas Klauser struct Predicate {
30*ee0f8c40SNikolas Klauser bool operator()(int);
31*ee0f8c40SNikolas Klauser };
32*ee0f8c40SNikolas Klauser
33*ee0f8c40SNikolas Klauser template <class It, class Sent = It>
34*ee0f8c40SNikolas Klauser concept HasFindIfNotIt = requires(It it, Sent sent) { std::ranges::find_if_not(it, sent, Predicate{}); };
35*ee0f8c40SNikolas Klauser static_assert(HasFindIfNotIt<int*>);
36*ee0f8c40SNikolas Klauser static_assert(!HasFindIfNotIt<InputIteratorNotDerivedFrom>);
37*ee0f8c40SNikolas Klauser static_assert(!HasFindIfNotIt<InputIteratorNotIndirectlyReadable>);
38*ee0f8c40SNikolas Klauser static_assert(!HasFindIfNotIt<InputIteratorNotInputOrOutputIterator>);
39*ee0f8c40SNikolas Klauser static_assert(!HasFindIfNotIt<cpp20_input_iterator<int*>, SentinelForNotSemiregular>);
40*ee0f8c40SNikolas Klauser static_assert(!HasFindIfNotIt<cpp20_input_iterator<int*>, InputRangeNotSentinelEqualityComparableWith>);
41*ee0f8c40SNikolas Klauser
42*ee0f8c40SNikolas Klauser static_assert(!HasFindIfNotIt<int*, int>);
43*ee0f8c40SNikolas Klauser static_assert(!HasFindIfNotIt<int, int*>);
44*ee0f8c40SNikolas Klauser
45*ee0f8c40SNikolas Klauser template <class Pred>
46*ee0f8c40SNikolas Klauser concept HasFindIfNotPred = requires(int* it, Pred pred) {std::ranges::find_if_not(it, it, pred); };
47*ee0f8c40SNikolas Klauser
48*ee0f8c40SNikolas Klauser static_assert(!HasFindIfNotPred<IndirectUnaryPredicateNotCopyConstructible>);
49*ee0f8c40SNikolas Klauser static_assert(!HasFindIfNotPred<IndirectUnaryPredicateNotPredicate>);
50*ee0f8c40SNikolas Klauser
51*ee0f8c40SNikolas Klauser template <class R>
52*ee0f8c40SNikolas Klauser concept HasFindIfNotR = requires(R r) { std::ranges::find_if_not(r, Predicate{}); };
53*ee0f8c40SNikolas Klauser static_assert(HasFindIfNotR<std::array<int, 0>>);
54*ee0f8c40SNikolas Klauser static_assert(!HasFindIfNotR<int>);
55*ee0f8c40SNikolas Klauser static_assert(!HasFindIfNotR<InputRangeNotDerivedFrom>);
56*ee0f8c40SNikolas Klauser static_assert(!HasFindIfNotR<InputRangeNotIndirectlyReadable>);
57*ee0f8c40SNikolas Klauser static_assert(!HasFindIfNotR<InputRangeNotInputOrOutputIterator>);
58*ee0f8c40SNikolas Klauser static_assert(!HasFindIfNotR<InputRangeNotSentinelSemiregular>);
59*ee0f8c40SNikolas Klauser static_assert(!HasFindIfNotR<InputRangeNotSentinelEqualityComparableWith>);
60*ee0f8c40SNikolas Klauser
61*ee0f8c40SNikolas Klauser template <class It, class Sent = It>
test_iterators()62*ee0f8c40SNikolas Klauser constexpr void test_iterators() {
63*ee0f8c40SNikolas Klauser {
64*ee0f8c40SNikolas Klauser int a[] = {1, 2, 3, 4};
65*ee0f8c40SNikolas Klauser std::same_as<It> auto ret = std::ranges::find_if_not(It(a), Sent(It(a + 4)), [c = 0](int) mutable { return c++ <= 2; });
66*ee0f8c40SNikolas Klauser assert(base(ret) == a + 3);
67*ee0f8c40SNikolas Klauser assert(*ret == 4);
68*ee0f8c40SNikolas Klauser }
69*ee0f8c40SNikolas Klauser {
70*ee0f8c40SNikolas Klauser int a[] = {1, 2, 3, 4};
71*ee0f8c40SNikolas Klauser auto range = std::ranges::subrange(It(a), Sent(It(a + 4)));
72*ee0f8c40SNikolas Klauser std::same_as<It> auto ret = std::ranges::find_if_not(range, [c = 0](int) mutable { return c++ <= 2; });
73*ee0f8c40SNikolas Klauser assert(base(ret) == a + 3);
74*ee0f8c40SNikolas Klauser assert(*ret == 4);
75*ee0f8c40SNikolas Klauser }
76*ee0f8c40SNikolas Klauser }
77*ee0f8c40SNikolas Klauser
78*ee0f8c40SNikolas Klauser struct NonConstComparableLValue {
operator ==(const NonConstComparableLValue &,const NonConstComparableLValue &)79*ee0f8c40SNikolas Klauser friend constexpr bool operator==(const NonConstComparableLValue&, const NonConstComparableLValue&) { return false; }
operator ==(NonConstComparableLValue &,NonConstComparableLValue &)80*ee0f8c40SNikolas Klauser friend constexpr bool operator==(NonConstComparableLValue&, NonConstComparableLValue&) { return false; }
operator ==(const NonConstComparableLValue &,NonConstComparableLValue &)81*ee0f8c40SNikolas Klauser friend constexpr bool operator==(const NonConstComparableLValue&, NonConstComparableLValue&) { return false; }
operator ==(NonConstComparableLValue &,const NonConstComparableLValue &)82*ee0f8c40SNikolas Klauser friend constexpr bool operator==(NonConstComparableLValue&, const NonConstComparableLValue&) { return true; }
83*ee0f8c40SNikolas Klauser };
84*ee0f8c40SNikolas Klauser
test()85*ee0f8c40SNikolas Klauser constexpr bool test() {
86*ee0f8c40SNikolas Klauser test_iterators<int*>();
87*ee0f8c40SNikolas Klauser test_iterators<const int*>();
88*ee0f8c40SNikolas Klauser test_iterators<cpp20_input_iterator<int*>, sentinel_wrapper<cpp20_input_iterator<int*>>>();
89*ee0f8c40SNikolas Klauser test_iterators<bidirectional_iterator<int*>>();
90*ee0f8c40SNikolas Klauser test_iterators<forward_iterator<int*>>();
91*ee0f8c40SNikolas Klauser test_iterators<random_access_iterator<int*>>();
92*ee0f8c40SNikolas Klauser test_iterators<contiguous_iterator<int*>>();
93*ee0f8c40SNikolas Klauser
94*ee0f8c40SNikolas Klauser { // check that projections are used properly and that they are called with the iterator directly
95*ee0f8c40SNikolas Klauser {
96*ee0f8c40SNikolas Klauser int a[] = {1, 2, 3, 4};
97*ee0f8c40SNikolas Klauser auto ret = std::ranges::find_if_not(a, a + 4, [&](int* i) { return i != a + 3; }, [](int& i) { return &i; });
98*ee0f8c40SNikolas Klauser assert(ret == a + 3);
99*ee0f8c40SNikolas Klauser }
100*ee0f8c40SNikolas Klauser {
101*ee0f8c40SNikolas Klauser int a[] = {1, 2, 3, 4};
102*ee0f8c40SNikolas Klauser auto ret = std::ranges::find_if_not(a, [&](int* i) { return i != a + 3; }, [](int& i) { return &i; });
103*ee0f8c40SNikolas Klauser assert(ret == a + 3);
104*ee0f8c40SNikolas Klauser }
105*ee0f8c40SNikolas Klauser }
106*ee0f8c40SNikolas Klauser
107*ee0f8c40SNikolas Klauser {
108*ee0f8c40SNikolas Klauser // check that the first element is returned
109*ee0f8c40SNikolas Klauser {
110*ee0f8c40SNikolas Klauser struct S {
111*ee0f8c40SNikolas Klauser int comp;
112*ee0f8c40SNikolas Klauser int other;
113*ee0f8c40SNikolas Klauser };
114*ee0f8c40SNikolas Klauser S a[] = { {0, 0}, {0, 2}, {0, 1} };
115*ee0f8c40SNikolas Klauser auto ret = std::ranges::find_if_not(a, [](int i){ return i != 0; }, &S::comp);
116*ee0f8c40SNikolas Klauser assert(ret == a);
117*ee0f8c40SNikolas Klauser assert(ret->comp == 0);
118*ee0f8c40SNikolas Klauser assert(ret->other == 0);
119*ee0f8c40SNikolas Klauser }
120*ee0f8c40SNikolas Klauser {
121*ee0f8c40SNikolas Klauser struct S {
122*ee0f8c40SNikolas Klauser int comp;
123*ee0f8c40SNikolas Klauser int other;
124*ee0f8c40SNikolas Klauser };
125*ee0f8c40SNikolas Klauser S a[] = { {0, 0}, {0, 2}, {0, 1} };
126*ee0f8c40SNikolas Klauser auto ret = std::ranges::find_if_not(a, a + 3, [](int i) { return i != 0; }, &S::comp);
127*ee0f8c40SNikolas Klauser assert(ret == a);
128*ee0f8c40SNikolas Klauser assert(ret->comp == 0);
129*ee0f8c40SNikolas Klauser assert(ret->other == 0);
130*ee0f8c40SNikolas Klauser }
131*ee0f8c40SNikolas Klauser }
132*ee0f8c40SNikolas Klauser
133*ee0f8c40SNikolas Klauser {
134*ee0f8c40SNikolas Klauser // check that end + 1 iterator is returned with no match
135*ee0f8c40SNikolas Klauser {
136*ee0f8c40SNikolas Klauser int a[] = {1, 1, 1};
137*ee0f8c40SNikolas Klauser auto ret = std::ranges::find_if(a, a + 3, [](int) { return false; });
138*ee0f8c40SNikolas Klauser assert(ret == a + 3);
139*ee0f8c40SNikolas Klauser }
140*ee0f8c40SNikolas Klauser {
141*ee0f8c40SNikolas Klauser int a[] = {1, 1, 1};
142*ee0f8c40SNikolas Klauser auto ret = std::ranges::find_if(a, [](int){ return false; });
143*ee0f8c40SNikolas Klauser assert(ret == a + 3);
144*ee0f8c40SNikolas Klauser }
145*ee0f8c40SNikolas Klauser }
146*ee0f8c40SNikolas Klauser
147*ee0f8c40SNikolas Klauser { // check that ranges::dangling is returned
148*ee0f8c40SNikolas Klauser [[maybe_unused]] std::same_as<std::ranges::dangling> auto ret =
149*ee0f8c40SNikolas Klauser std::ranges::find_if_not(std::array{1, 2}, [](int){ return true; });
150*ee0f8c40SNikolas Klauser }
151*ee0f8c40SNikolas Klauser
152*ee0f8c40SNikolas Klauser { // check that an iterator is returned with a borrowing range
153*ee0f8c40SNikolas Klauser int a[] = {1, 2, 3, 4};
154*ee0f8c40SNikolas Klauser std::same_as<int*> auto ret = std::ranges::find_if_not(std::views::all(a), [](int){ return false; });
155*ee0f8c40SNikolas Klauser assert(ret == a);
156*ee0f8c40SNikolas Klauser assert(*ret == 1);
157*ee0f8c40SNikolas Klauser }
158*ee0f8c40SNikolas Klauser
159*ee0f8c40SNikolas Klauser { // check that std::invoke is used
160*ee0f8c40SNikolas Klauser struct S { int i; };
161*ee0f8c40SNikolas Klauser S a[] = { S{1}, S{3}, S{2} };
162*ee0f8c40SNikolas Klauser std::same_as<S*> auto ret = std::ranges::find_if_not(a, [](int) { return true; }, &S::i);
163*ee0f8c40SNikolas Klauser assert(ret == a + 3);
164*ee0f8c40SNikolas Klauser }
165*ee0f8c40SNikolas Klauser
166*ee0f8c40SNikolas Klauser { // count projection and predicate invocation count
167*ee0f8c40SNikolas Klauser {
168*ee0f8c40SNikolas Klauser int a[] = {1, 2, 3, 4};
169*ee0f8c40SNikolas Klauser int predicate_count = 0;
170*ee0f8c40SNikolas Klauser int projection_count = 0;
171*ee0f8c40SNikolas Klauser auto ret = std::ranges::find_if_not(a, a + 4,
172*ee0f8c40SNikolas Klauser [&](int i) { ++predicate_count; return i != 2; },
173*ee0f8c40SNikolas Klauser [&](int i) { ++projection_count; return i; });
174*ee0f8c40SNikolas Klauser assert(ret == a + 1);
175*ee0f8c40SNikolas Klauser assert(*ret == 2);
176*ee0f8c40SNikolas Klauser assert(predicate_count == 2);
177*ee0f8c40SNikolas Klauser assert(projection_count == 2);
178*ee0f8c40SNikolas Klauser }
179*ee0f8c40SNikolas Klauser {
180*ee0f8c40SNikolas Klauser int a[] = {1, 2, 3, 4};
181*ee0f8c40SNikolas Klauser int predicate_count = 0;
182*ee0f8c40SNikolas Klauser int projection_count = 0;
183*ee0f8c40SNikolas Klauser auto ret = std::ranges::find_if_not(a,
184*ee0f8c40SNikolas Klauser [&](int i) { ++predicate_count; return i != 2; },
185*ee0f8c40SNikolas Klauser [&](int i) { ++projection_count; return i; });
186*ee0f8c40SNikolas Klauser assert(ret == a + 1);
187*ee0f8c40SNikolas Klauser assert(*ret == 2);
188*ee0f8c40SNikolas Klauser assert(predicate_count == 2);
189*ee0f8c40SNikolas Klauser assert(projection_count == 2);
190*ee0f8c40SNikolas Klauser }
191*ee0f8c40SNikolas Klauser }
192*ee0f8c40SNikolas Klauser
193*ee0f8c40SNikolas Klauser { // check that the return type of `iter::operator*` doesn't change
194*ee0f8c40SNikolas Klauser {
195*ee0f8c40SNikolas Klauser NonConstComparableLValue a[] = { NonConstComparableLValue{} };
196*ee0f8c40SNikolas Klauser auto ret = std::ranges::find_if_not(a, a + 1, [](auto&& e) { return e != NonConstComparableLValue{}; });
197*ee0f8c40SNikolas Klauser assert(ret == a);
198*ee0f8c40SNikolas Klauser }
199*ee0f8c40SNikolas Klauser {
200*ee0f8c40SNikolas Klauser NonConstComparableLValue a[] = { NonConstComparableLValue{} };
201*ee0f8c40SNikolas Klauser auto ret = std::ranges::find_if_not(a, [](auto&& e) { return e != NonConstComparableLValue{}; });
202*ee0f8c40SNikolas Klauser assert(ret == a);
203*ee0f8c40SNikolas Klauser }
204*ee0f8c40SNikolas Klauser }
205*ee0f8c40SNikolas Klauser
206*ee0f8c40SNikolas Klauser {
207*ee0f8c40SNikolas Klauser // check that an empty range works
208*ee0f8c40SNikolas Klauser {
209*ee0f8c40SNikolas Klauser std::array<int ,0> a = {};
210*ee0f8c40SNikolas Klauser auto ret = std::ranges::find_if_not(a.begin(), a.end(), [](int) { return true; });
211*ee0f8c40SNikolas Klauser assert(ret == a.begin());
212*ee0f8c40SNikolas Klauser }
213*ee0f8c40SNikolas Klauser {
214*ee0f8c40SNikolas Klauser std::array<int, 0> a = {};
215*ee0f8c40SNikolas Klauser auto ret = std::ranges::find_if_not(a, [](int) { return true; });
216*ee0f8c40SNikolas Klauser assert(ret == a.begin());
217*ee0f8c40SNikolas Klauser }
218*ee0f8c40SNikolas Klauser }
219*ee0f8c40SNikolas Klauser
220*ee0f8c40SNikolas Klauser return true;
221*ee0f8c40SNikolas Klauser }
222*ee0f8c40SNikolas Klauser
main(int,char **)223*ee0f8c40SNikolas Klauser int main(int, char**) {
224*ee0f8c40SNikolas Klauser test();
225*ee0f8c40SNikolas Klauser static_assert(test());
226*ee0f8c40SNikolas Klauser
227*ee0f8c40SNikolas Klauser return 0;
228*ee0f8c40SNikolas Klauser }
229