120517557Szijunzhao //===----------------------------------------------------------------------===//
220517557Szijunzhao //
320517557Szijunzhao // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
420517557Szijunzhao // See https://llvm.org/LICENSE.txt for license information.
520517557Szijunzhao // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
620517557Szijunzhao //
720517557Szijunzhao //===----------------------------------------------------------------------===//
820517557Szijunzhao
920517557Szijunzhao // <algorithm>
1020517557Szijunzhao
1120517557Szijunzhao // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
1220517557Szijunzhao
1320517557Szijunzhao // template<input_iterator I1, sentinel_for<I1> S1, input_iterator I2, sentinel_for<I2> S2,
1420517557Szijunzhao // class Pred = ranges::equal_to, class Proj1 = identity, class Proj2 = identity>
1520517557Szijunzhao // requires indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
1620517557Szijunzhao // constexpr bool ranges::starts_with(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {},
1720517557Szijunzhao // Proj1 proj1 = {}, Proj2 proj2 = {});
1820517557Szijunzhao // template<input_range R1, input_range R2, class Pred = ranges::equal_to, class Proj1 = identity,
1920517557Szijunzhao // class Proj2 = identity>
2020517557Szijunzhao // requires indirectly_comparable<iterator_t<R1>, iterator_t<R2>, Pred, Proj1, Proj2>
2120517557Szijunzhao // constexpr bool ranges::starts_with(R1&& r1, R2&& r2, Pred pred = {},
2220517557Szijunzhao // Proj1 proj1 = {}, Proj2 proj2 = {});
2320517557Szijunzhao
2420517557Szijunzhao #include <algorithm>
2520517557Szijunzhao #include <array>
2620517557Szijunzhao #include <ranges>
2720517557Szijunzhao
2820517557Szijunzhao #include "almost_satisfies_types.h"
2920517557Szijunzhao #include "test_iterators.h"
3020517557Szijunzhao
3120517557Szijunzhao template <class Iter1, class Sent1 = Iter1, class Iter2 = int*, class Sent2 = Iter2>
3220517557Szijunzhao concept HasStartsWithIt = requires(Iter1 first1, Sent1 last1, Iter2 first2, Sent2 last2) {
3320517557Szijunzhao std::ranges::starts_with(first1, last1, first2, last2);
3420517557Szijunzhao };
3520517557Szijunzhao
3620517557Szijunzhao static_assert(HasStartsWithIt<int*>);
3720517557Szijunzhao static_assert(HasStartsWithIt<ForwardIteratorNotDerivedFrom>);
3820517557Szijunzhao static_assert(HasStartsWithIt<ForwardIteratorNotIncrementable>);
3920517557Szijunzhao static_assert(!HasStartsWithIt<int*, SentinelForNotSemiregular>);
4020517557Szijunzhao static_assert(!HasStartsWithIt<int*, int*, int**>); // not indirectly comparable
4120517557Szijunzhao static_assert(!HasStartsWithIt<int*, SentinelForNotWeaklyEqualityComparableWith>);
4220517557Szijunzhao static_assert(HasStartsWithIt<int*, int*, ForwardIteratorNotDerivedFrom>);
4320517557Szijunzhao static_assert(HasStartsWithIt<int*, int*, ForwardIteratorNotIncrementable>);
4420517557Szijunzhao static_assert(!HasStartsWithIt<int*, int*, int*, SentinelForNotSemiregular>);
4520517557Szijunzhao static_assert(!HasStartsWithIt<int*, int*, int*, SentinelForNotWeaklyEqualityComparableWith>);
4620517557Szijunzhao
4720517557Szijunzhao template <class Range1, class Range2 = UncheckedRange<int*>>
485b28e4d7SLouis Dionne concept HasStartsWithR = requires(Range1 range1, Range2 range2) { std::ranges::starts_with(range1, range2); };
4920517557Szijunzhao
5020517557Szijunzhao static_assert(HasStartsWithR<UncheckedRange<int*>>);
5120517557Szijunzhao static_assert(HasStartsWithR<ForwardRangeNotDerivedFrom>);
5220517557Szijunzhao static_assert(!HasStartsWithR<ForwardIteratorNotIncrementable>);
5320517557Szijunzhao static_assert(!HasStartsWithR<ForwardRangeNotSentinelSemiregular>);
5420517557Szijunzhao static_assert(!HasStartsWithR<ForwardRangeNotSentinelEqualityComparableWith>);
5520517557Szijunzhao static_assert(!HasStartsWithR<UncheckedRange<int*>, UncheckedRange<int**>>); // not indirectly comparable
5620517557Szijunzhao static_assert(HasStartsWithR<UncheckedRange<int*>, ForwardRangeNotDerivedFrom>);
5720517557Szijunzhao static_assert(HasStartsWithR<UncheckedRange<int*>, ForwardRangeNotIncrementable>);
5820517557Szijunzhao static_assert(!HasStartsWithR<UncheckedRange<int*>, ForwardRangeNotSentinelSemiregular>);
5920517557Szijunzhao static_assert(!HasStartsWithR<UncheckedRange<int*>, ForwardRangeNotSentinelEqualityComparableWith>);
6020517557Szijunzhao
61f9f1f9c2Szijunzhao // clang-format off
6220517557Szijunzhao template <class Iter1, class Sent1 = Iter1, class Iter2, class Sent2 = Iter2>
test_iterators()6320517557Szijunzhao constexpr void test_iterators() {
64f9f1f9c2Szijunzhao { // simple tests
655b28e4d7SLouis Dionne {
665b28e4d7SLouis Dionne int a[] = {1, 2, 3, 4, 5, 6};
6720517557Szijunzhao int p[] = {1, 2};
6820517557Szijunzhao std::same_as<bool> decltype(auto) ret =
6920517557Szijunzhao std::ranges::starts_with(Iter1(a), Sent1(Iter1(a + 6)), Iter2(p), Sent2(Iter2(p + 2)));
7020517557Szijunzhao assert(ret);
7120517557Szijunzhao }
7220517557Szijunzhao {
7320517557Szijunzhao int a[] = {1, 2, 3, 4, 5, 6};
7420517557Szijunzhao int p[] = {1, 2};
7520517557Szijunzhao auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a + 6)));
7620517557Szijunzhao auto prefix = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 2)));
7720517557Szijunzhao std::same_as<bool> decltype(auto) ret = std::ranges::starts_with(whole, prefix);
7820517557Szijunzhao assert(ret);
7920517557Szijunzhao }
8020517557Szijunzhao }
8120517557Szijunzhao
82f9f1f9c2Szijunzhao { // prefix doesn't match
8320517557Szijunzhao {
8420517557Szijunzhao int a[] = {1, 2, 3, 4, 5, 6};
8520517557Szijunzhao int p[] = {4, 5, 6};
8620517557Szijunzhao std::same_as<bool> decltype(auto) ret =
8720517557Szijunzhao std::ranges::starts_with(Iter1(a), Sent1(Iter1(a + 6)), Iter2(p), Sent2(Iter2(p + 3)));
8820517557Szijunzhao assert(!ret);
8920517557Szijunzhao }
9020517557Szijunzhao {
9120517557Szijunzhao int a[] = {1, 2, 3, 4, 5, 6};
9220517557Szijunzhao int p[] = {4, 5, 6};
9320517557Szijunzhao auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a + 6)));
9420517557Szijunzhao auto prefix = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 3)));
9520517557Szijunzhao std::same_as<bool> decltype(auto) ret = std::ranges::starts_with(whole, prefix);
9620517557Szijunzhao assert(!ret);
9720517557Szijunzhao }
9820517557Szijunzhao }
9920517557Szijunzhao
100f9f1f9c2Szijunzhao { // range and prefix are identical
10120517557Szijunzhao {
10220517557Szijunzhao int a[] = {1, 2, 3, 4, 5, 6};
10320517557Szijunzhao int p[] = {1, 2, 3, 4, 5, 6};
10420517557Szijunzhao std::same_as<bool> decltype(auto) ret =
10520517557Szijunzhao std::ranges::starts_with(Iter1(a), Sent1(Iter1(a + 6)), Iter2(p), Sent2(Iter2(p + 6)));
10620517557Szijunzhao assert(ret);
10720517557Szijunzhao }
10820517557Szijunzhao {
10920517557Szijunzhao int a[] = {1, 2, 3, 4, 5, 6};
11020517557Szijunzhao int p[] = {1, 2, 3, 4, 5, 6};
11120517557Szijunzhao auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a + 6)));
11220517557Szijunzhao auto prefix = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 6)));
11320517557Szijunzhao std::same_as<bool> decltype(auto) ret = std::ranges::starts_with(whole, prefix);
11420517557Szijunzhao assert(ret);
11520517557Szijunzhao }
11620517557Szijunzhao }
11720517557Szijunzhao
118f9f1f9c2Szijunzhao { // prefix is longer than range
11920517557Szijunzhao {
12020517557Szijunzhao int a[] = {1, 2, 3, 4, 5, 6};
12120517557Szijunzhao int p[] = {1, 2, 3, 4, 5, 6, 7, 8};
12220517557Szijunzhao std::same_as<bool> decltype(auto) ret =
12320517557Szijunzhao std::ranges::starts_with(Iter1(a), Sent1(Iter1(a + 6)), Iter2(p), Sent2(Iter2(p + 8)));
12420517557Szijunzhao assert(!ret);
12520517557Szijunzhao }
12620517557Szijunzhao {
12720517557Szijunzhao int a[] = {1, 2, 3, 4, 5, 6};
12820517557Szijunzhao int p[] = {1, 2, 3, 4, 5, 6, 7, 8};
12920517557Szijunzhao auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a + 6)));
13020517557Szijunzhao auto prefix = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 8)));
13120517557Szijunzhao std::same_as<bool> decltype(auto) ret = std::ranges::starts_with(whole, prefix);
13220517557Szijunzhao assert(!ret);
13320517557Szijunzhao }
13420517557Szijunzhao }
13520517557Szijunzhao
136f9f1f9c2Szijunzhao { // prefix has zero length
13720517557Szijunzhao {
13820517557Szijunzhao int a[] = {1, 2, 3, 4, 5, 6};
139*c000f754SStephan T. Lavavej std::array<int, 0> p = {};
14020517557Szijunzhao std::same_as<bool> decltype(auto) ret =
141*c000f754SStephan T. Lavavej std::ranges::starts_with(Iter1(a), Sent1(Iter1(a + 6)), Iter2(p.data()), Sent2(Iter2(p.data())));
14220517557Szijunzhao assert(ret);
14320517557Szijunzhao }
14420517557Szijunzhao {
14520517557Szijunzhao int a[] = {1, 2, 3, 4, 5, 6};
146*c000f754SStephan T. Lavavej std::array<int, 0> p = {};
14720517557Szijunzhao auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a + 6)));
148*c000f754SStephan T. Lavavej auto prefix = std::ranges::subrange(Iter2(p.data()), Sent2(Iter2(p.data())));
14920517557Szijunzhao std::same_as<bool> decltype(auto) ret = std::ranges::starts_with(whole, prefix);
15020517557Szijunzhao assert(ret);
15120517557Szijunzhao }
15220517557Szijunzhao }
15320517557Szijunzhao
154f9f1f9c2Szijunzhao { // range has zero length
15520517557Szijunzhao {
156*c000f754SStephan T. Lavavej std::array<int, 0> a = {};
15720517557Szijunzhao int p[] = {1, 2, 3, 4, 5, 6, 7, 8};
15820517557Szijunzhao std::same_as<bool> decltype(auto) ret =
159*c000f754SStephan T. Lavavej std::ranges::starts_with(Iter1(a.data()), Sent1(Iter1(a.data())), Iter2(p), Sent2(Iter2(p + 8)));
16020517557Szijunzhao assert(!ret);
16120517557Szijunzhao }
16220517557Szijunzhao {
163*c000f754SStephan T. Lavavej std::array<int, 0> a = {};
16420517557Szijunzhao int p[] = {1, 2, 3, 4, 5, 6, 7, 8};
165*c000f754SStephan T. Lavavej auto whole = std::ranges::subrange(Iter1(a.data()), Sent1(Iter1(a.data())));
16620517557Szijunzhao auto prefix = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 8)));
16720517557Szijunzhao std::same_as<bool> decltype(auto) ret = std::ranges::starts_with(whole, prefix);
16820517557Szijunzhao assert(!ret);
16920517557Szijunzhao }
17020517557Szijunzhao }
17120517557Szijunzhao
172f9f1f9c2Szijunzhao { // check that the predicate is used
17320517557Szijunzhao {
17420517557Szijunzhao int a[] = {11, 8, 3, 4, 0, 6};
17520517557Szijunzhao int p[] = {1, 12};
17620517557Szijunzhao std::same_as<bool> decltype(auto) ret = std::ranges::starts_with(
17720517557Szijunzhao Iter1(a), Sent1(Iter1(a + 6)), Iter2(p), Sent2(Iter2(p + 2)), [](int l, int r) { return l > r; });
17820517557Szijunzhao assert(!ret);
17920517557Szijunzhao }
18020517557Szijunzhao {
18120517557Szijunzhao int a[] = {11, 8, 3, 4, 0, 6};
18220517557Szijunzhao int p[] = {1, 12};
18320517557Szijunzhao auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a + 6)));
18420517557Szijunzhao auto prefix = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 2)));
18520517557Szijunzhao std::same_as<bool> decltype(auto) ret = std::ranges::starts_with(whole, prefix, [](int l, int r) { return l > r; });
18620517557Szijunzhao assert(!ret);
18720517557Szijunzhao }
18820517557Szijunzhao }
18920517557Szijunzhao
190f9f1f9c2Szijunzhao { // check that the projections are used
19120517557Szijunzhao {
19220517557Szijunzhao int a[] = {1, 3, 5, 1, 5, 6};
19320517557Szijunzhao int p[] = {2, 3, 4};
19420517557Szijunzhao std::same_as<bool> decltype(auto) ret = std::ranges::starts_with(
19520517557Szijunzhao Iter1(a),
19620517557Szijunzhao Sent1(Iter1(a + 6)),
19720517557Szijunzhao Iter2(p),
19820517557Szijunzhao Sent2(Iter2(p + 3)),
19920517557Szijunzhao {},
20020517557Szijunzhao [](int i) { return i + 3; },
20120517557Szijunzhao [](int i) { return i * 2; });
20220517557Szijunzhao assert(ret);
20320517557Szijunzhao }
20420517557Szijunzhao {
20520517557Szijunzhao int a[] = {1, 3, 5, 1, 5, 6};
20620517557Szijunzhao int p[] = {2, 3, 4};
20720517557Szijunzhao auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a + 6)));
20820517557Szijunzhao auto prefix = std::ranges::subrange(Iter2(p), Sent2(Iter2(p + 3)));
20920517557Szijunzhao std::same_as<bool> decltype(auto) ret = std::ranges::starts_with(
21020517557Szijunzhao whole, prefix, {}, [](int i) { return i + 3; }, [](int i) { return i * 2; });
21120517557Szijunzhao assert(ret);
21220517557Szijunzhao }
21320517557Szijunzhao }
21420517557Szijunzhao }
21520517557Szijunzhao
test()21620517557Szijunzhao constexpr bool test() {
217f9f1f9c2Szijunzhao types::for_each(types::cpp20_input_iterator_list<int*>{}, []<class Iter2>() {
218f9f1f9c2Szijunzhao types::for_each(types::cpp20_input_iterator_list<int*>{}, []<class Iter1>() {
219f9f1f9c2Szijunzhao if constexpr (std::forward_iterator<Iter1> && std::forward_iterator<Iter2>)
220f9f1f9c2Szijunzhao test_iterators<Iter1, Iter1, Iter2, Iter2>();
221f9f1f9c2Szijunzhao if constexpr (std::forward_iterator<Iter2>)
222f9f1f9c2Szijunzhao test_iterators<Iter1, sized_sentinel<Iter1>, Iter2, Iter2>();
223f9f1f9c2Szijunzhao if constexpr (std::forward_iterator<Iter1>)
224f9f1f9c2Szijunzhao test_iterators<Iter1, Iter1, Iter2, sized_sentinel<Iter2>>();
225f9f1f9c2Szijunzhao test_iterators<Iter1, sized_sentinel<Iter1>, Iter2, sized_sentinel<Iter2>>();
22620517557Szijunzhao });
22720517557Szijunzhao });
22820517557Szijunzhao
229f9f1f9c2Szijunzhao { // check that std::invoke is used
23020517557Szijunzhao struct S {
23120517557Szijunzhao int i;
23220517557Szijunzhao
23320517557Szijunzhao constexpr S identity() { return *this; }
23420517557Szijunzhao
23520517557Szijunzhao constexpr bool compare(const S& s) { return i == s.i; }
23620517557Szijunzhao };
23720517557Szijunzhao {
23820517557Szijunzhao S a[] = {{1}, {2}, {3}, {4}};
23920517557Szijunzhao S p[] = {{1}, {2}};
24020517557Szijunzhao auto ret = std::ranges::starts_with(a, a + 4, p, p + 2, &S::compare, &S::identity, &S::identity);
24120517557Szijunzhao assert(ret);
24220517557Szijunzhao }
24320517557Szijunzhao {
24420517557Szijunzhao S a[] = {{1}, {2}, {3}, {4}};
24520517557Szijunzhao S p[] = {{1}, {2}};
24620517557Szijunzhao auto ret = std::ranges::starts_with(a, p, &S::compare, &S::identity, &S::identity);
24720517557Szijunzhao assert(ret);
24820517557Szijunzhao }
24920517557Szijunzhao }
25020517557Szijunzhao
25120517557Szijunzhao return true;
25220517557Szijunzhao }
25320517557Szijunzhao
main(int,char **)25420517557Szijunzhao int main(int, char**) {
25520517557Szijunzhao test();
25620517557Szijunzhao static_assert(test());
257f9f1f9c2Szijunzhao
25820517557Szijunzhao return 0;
25920517557Szijunzhao }
260