1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP___ALGORITHM_SEARCH_N_H
11#define _LIBCPP___ALGORITHM_SEARCH_N_H
12
13#include <__algorithm/comp.h>
14#include <__algorithm/iterator_operations.h>
15#include <__config>
16#include <__functional/identity.h>
17#include <__iterator/iterator_traits.h>
18#include <__type_traits/enable_if.h>
19#include <__type_traits/invoke.h>
20#include <__type_traits/is_callable.h>
21#include <__type_traits/make_unsigned.h>
22#include <__utility/convert_to_integral.h>
23#include <__utility/pair.h>
24
25#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
26# pragma GCC system_header
27#endif
28
29_LIBCPP_BEGIN_NAMESPACE_STD
30
31template <class _AlgPolicy, class _Pred, class _Iter, class _Sent, class _SizeT, class _Type, class _Proj>
32_LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter, _Iter> __search_n_forward_impl(
33 _Iter __first, _Sent __last, _SizeT __count, const _Type& __value, _Pred& __pred, _Proj& __proj) {
34 if (__count <= 0)
35 return std::make_pair(__first, __first);
36 while (true) {
37 // Find first element in sequence that matchs __value, with a mininum of loop checks
38 while (true) {
39 if (__first == __last) { // return __last if no element matches __value
40 _IterOps<_AlgPolicy>::__advance_to(__first, __last);
41 return std::make_pair(__first, __first);
42 }
43 if (std::__invoke(__pred, std::__invoke(__proj, *__first), __value))
44 break;
45 ++__first;
46 }
47 // *__first matches __value, now match elements after here
48 _Iter __m = __first;
49 _SizeT __c(0);
50 while (true) {
51 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
52 return std::make_pair(__first, ++__m);
53 if (++__m == __last) { // Otherwise if source exhaused, pattern not found
54 _IterOps<_AlgPolicy>::__advance_to(__first, __last);
55 return std::make_pair(__first, __first);
56 }
57
58 // if there is a mismatch, restart with a new __first
59 if (!std::__invoke(__pred, std::__invoke(__proj, *__m), __value)) {
60 __first = __m;
61 ++__first;
62 break;
63 } // else there is a match, check next elements
64 }
65 }
66}
67
68// Finds the longest suffix in [__first, __last) where each element satisfies __pred.
69template <class _RAIter, class _Pred, class _Proj, class _ValueT>
70_LIBCPP_CONSTEXPR_SINCE_CXX14 _RAIter
71__find_longest_suffix(_RAIter __first, _RAIter __last, const _ValueT& __value, _Pred& __pred, _Proj& __proj) {
72 while (__first != __last) {
73 if (!std::__invoke(__pred, std::__invoke(__proj, *--__last), __value)) {
74 return ++__last;
75 }
76 }
77 return __first;
78}
79
80template <class _AlgPolicy, class _Pred, class _Iter, class _SizeT, class _Type, class _Proj, class _DiffT>
81_LIBCPP_CONSTEXPR_SINCE_CXX14 std::pair<_Iter, _Iter> __search_n_random_access_impl(
82 _Iter __first, _SizeT __count_in, const _Type& __value, _Pred& __pred, _Proj& __proj, _DiffT __size) {
83 // Trivially match if count is non-positive. This must be checked before we perform arithmetic on the count.
84 if (__count_in <= 0)
85 return std::make_pair(__first, __first);
86
87 auto __last = __first + __size;
88
89 // If count is larger than the sequence size, no match is possible. Compare as unsigned to prevent
90 // truncation between _DiffT and _SizeT.
91 if (std::__to_unsigned_like(__size) < std::__to_unsigned_like(__count_in))
92 return std::make_pair(__last, __last);
93
94 auto __count = static_cast<_DiffT>(__count_in); // representable as _DiffT since `0 < __count_in <= __size`
95
96 // [__match_start, __match_start + __count) is the subrange which we currently check whether it only contains matching
97 // elements. This subrange is returned in case all the elements match.
98 // [__match_start, __matched_until) is the longest subrange where all elements are known to match at any given point
99 // in time.
100 // [__matched_until, __match_start + __count) is the subrange where we don't know whether the elements match.
101
102 // This algorithm tries to expand the subrange [__match_start, __matched_until) into a range of sufficient length.
103 // When we fail to do that because we find a mismatching element, we move it forward to the beginning of the next
104 // consecutive sequence that is not known not to match.
105
106 const _Iter __try_match_until = __last - __count;
107 _Iter __match_start = __first;
108 _Iter __matched_until = __first;
109
110 while (true) {
111 // There's no chance of expanding the subrange into a sequence of sufficient length, since we don't have enough
112 // elements in the haystack anymore.
113 if (__match_start > __try_match_until)
114 return std::make_pair(__last, __last);
115
116 auto __mismatch = std::__find_longest_suffix(__matched_until, __match_start + __count, __value, __pred, __proj);
117
118 // If all elements in [__matched_until, __match_start + __count) match, we know that
119 // [__match_start, __match_start + __count) is a full sequence of matching elements, so we're done.
120 if (__mismatch == __matched_until)
121 return std::make_pair(__match_start, __match_start + __count);
122
123 // Otherwise, we have to move the [__match_start, __matched_until) subrange forward past the point where we know for
124 // sure a match is impossible.
125 __matched_until = __match_start + __count;
126 __match_start = __mismatch;
127 }
128}
129
130template <class _Iter,
131 class _Sent,
132 class _DiffT,
133 class _Type,
134 class _Pred,
135 class _Proj,
136 __enable_if_t<__has_random_access_iterator_category<_Iter>::value, int> = 0>
137_LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter, _Iter>
138__search_n_impl(_Iter __first, _Sent __last, _DiffT __count, const _Type& __value, _Pred& __pred, _Proj& __proj) {
139 return std::__search_n_random_access_impl<_ClassicAlgPolicy>(
140 __first, __count, __value, __pred, __proj, __last - __first);
141}
142
143template <class _Iter1,
144 class _Sent1,
145 class _DiffT,
146 class _Type,
147 class _Pred,
148 class _Proj,
149 __enable_if_t<__has_forward_iterator_category<_Iter1>::value &&
150 !__has_random_access_iterator_category<_Iter1>::value,
151 int> = 0>
152_LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter1, _Iter1>
153__search_n_impl(_Iter1 __first, _Sent1 __last, _DiffT __count, const _Type& __value, _Pred& __pred, _Proj& __proj) {
154 return std::__search_n_forward_impl<_ClassicAlgPolicy>(__first, __last, __count, __value, __pred, __proj);
155}
156
157template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
158[[__nodiscard__]] inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator search_n(
159 _ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value, _BinaryPredicate __pred) {
160 static_assert(
161 __is_callable<_BinaryPredicate&, decltype(*__first), const _Tp&>::value, "The comparator has to be callable");
162 auto __proj = __identity();
163 return std::__search_n_impl(__first, __last, std::__convert_to_integral(__count), __value, __pred, __proj).first;
164}
165
166template <class _ForwardIterator, class _Size, class _Tp>
167[[__nodiscard__]] inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
168search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value) {
169 return std::search_n(__first, __last, std::__convert_to_integral(__count), __value, __equal_to());
170}
171
172_LIBCPP_END_NAMESPACE_STD
173
174#endif // _LIBCPP___ALGORITHM_SEARCH_N_H
175