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 <__utility/convert_to_integral.h>
22#include <__utility/pair.h>
23
24#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
25# pragma GCC system_header
26#endif
27
28_LIBCPP_BEGIN_NAMESPACE_STD
29
30template <class _AlgPolicy, class _Pred, class _Iter, class _Sent, class _SizeT, class _Type, class _Proj>
31_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter, _Iter> __search_n_forward_impl(
32 _Iter __first, _Sent __last, _SizeT __count, const _Type& __value, _Pred& __pred, _Proj& __proj) {
33 if (__count <= 0)
34 return std::make_pair(__first, __first);
35 while (true) {
36 // Find first element in sequence that matchs __value, with a mininum of loop checks
37 while (true) {
38 if (__first == __last) { // return __last if no element matches __value
39 _IterOps<_AlgPolicy>::__advance_to(__first, __last);
40 return std::make_pair(__first, __first);
41 }
42 if (std::__invoke(__pred, std::__invoke(__proj, *__first), __value))
43 break;
44 ++__first;
45 }
46 // *__first matches __value, now match elements after here
47 _Iter __m = __first;
48 _SizeT __c(0);
49 while (true) {
50 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
51 return std::make_pair(__first, ++__m);
52 if (++__m == __last) { // Otherwise if source exhaused, pattern not found
53 _IterOps<_AlgPolicy>::__advance_to(__first, __last);
54 return std::make_pair(__first, __first);
55 }
56
57 // if there is a mismatch, restart with a new __first
58 if (!std::__invoke(__pred, std::__invoke(__proj, *__m), __value)) {
59 __first = __m;
60 ++__first;
61 break;
62 } // else there is a match, check next elements
63 }
64 }
65}
66
67// Finds the longest suffix in [__first, __last) where each element satisfies __pred.
68template <class _RAIter, class _Pred, class _Proj, class _ValueT>
69_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _RAIter
70__find_longest_suffix(_RAIter __first, _RAIter __last, const _ValueT& __value, _Pred& __pred, _Proj& __proj) {
71 while (__first != __last) {
72 if (!std::__invoke(__pred, std::__invoke(__proj, *--__last), __value)) {
73 return ++__last;
74 }
75 }
76 return __first;
77}
78
79template <class _AlgPolicy, class _Pred, class _Iter, class _SizeT, class _Type, class _Proj, class _DiffT>
80_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 std::pair<_Iter, _Iter> __search_n_random_access_impl(
81 _Iter __first, _SizeT __count_in, const _Type& __value, _Pred& __pred, _Proj& __proj, _DiffT __size) {
82 auto __last = __first + __size;
83 auto __count = static_cast<_DiffT>(__count_in);
84
85 if (__count == 0)
86 return std::make_pair(__first, __first);
87 if (__size < __count)
88 return std::make_pair(__last, __last);
89
90 // [__match_start, __match_start + __count) is the subrange which we currently check whether it only contains matching
91 // elements. This subrange is returned in case all the elements match.
92 // [__match_start, __matched_until) is the longest subrange where all elements are known to match at any given point
93 // in time.
94 // [__matched_until, __match_start + __count) is the subrange where we don't know whether the elements match.
95
96 // This algorithm tries to expand the subrange [__match_start, __matched_until) into a range of sufficient length.
97 // When we fail to do that because we find a mismatching element, we move it forward to the beginning of the next
98 // consecutive sequence that is not known not to match.
99
100 const _Iter __try_match_until = __last - __count;
101 _Iter __match_start = __first;
102 _Iter __matched_until = __first;
103
104 while (true) {
105 // There's no chance of expanding the subrange into a sequence of sufficient length, since we don't have enough
106 // elements in the haystack anymore.
107 if (__match_start > __try_match_until)
108 return std::make_pair(__last, __last);
109
110 auto __mismatch = std::__find_longest_suffix(__matched_until, __match_start + __count, __value, __pred, __proj);
111
112 // If all elements in [__matched_until, __match_start + __count) match, we know that
113 // [__match_start, __match_start + __count) is a full sequence of matching elements, so we're done.
114 if (__mismatch == __matched_until)
115 return std::make_pair(__match_start, __match_start + __count);
116
117 // Otherwise, we have to move the [__match_start, __matched_until) subrange forward past the point where we know for
118 // sure a match is impossible.
119 __matched_until = __match_start + __count;
120 __match_start = __mismatch;
121 }
122}
123
124template <class _Iter,
125 class _Sent,
126 class _DiffT,
127 class _Type,
128 class _Pred,
129 class _Proj,
130 __enable_if_t<__has_random_access_iterator_category<_Iter>::value, int> = 0>
131_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter, _Iter>
132__search_n_impl(_Iter __first, _Sent __last, _DiffT __count, const _Type& __value, _Pred& __pred, _Proj& __proj) {
133 return std::__search_n_random_access_impl<_ClassicAlgPolicy>(
134 __first, __count, __value, __pred, __proj, __last - __first);
135}
136
137template <class _Iter1,
138 class _Sent1,
139 class _DiffT,
140 class _Type,
141 class _Pred,
142 class _Proj,
143 __enable_if_t<__has_forward_iterator_category<_Iter1>::value &&
144 !__has_random_access_iterator_category<_Iter1>::value,
145 int> = 0>
146_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter1, _Iter1>
147__search_n_impl(_Iter1 __first, _Sent1 __last, _DiffT __count, const _Type& __value, _Pred& __pred, _Proj& __proj) {
148 return std::__search_n_forward_impl<_ClassicAlgPolicy>(__first, __last, __count, __value, __pred, __proj);
149}
150
151template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
152[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator search_n(
153 _ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value, _BinaryPredicate __pred) {
154 static_assert(
155 __is_callable<_BinaryPredicate&, decltype(*__first), const _Tp&>::value, "The comparator has to be callable");
156 auto __proj = __identity();
157 return std::__search_n_impl(__first, __last, std::__convert_to_integral(__count), __value, __pred, __proj).first;
158}
159
160template <class _ForwardIterator, class _Size, class _Tp>
161[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
162search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value) {
163 return std::search_n(__first, __last, std::__convert_to_integral(__count), __value, __equal_to());
164}
165
166_LIBCPP_END_NAMESPACE_STD
167
168#endif // _LIBCPP___ALGORITHM_SEARCH_N_H
169