2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___ALGORITHM_SEARCH_N_H
11 #define _LIBCPP___ALGORITHM_SEARCH_N_H
13 #include <__algorithm/comp.h>
14 #include <__algorithm/iterator_operations.h>
16 #include <__functional/identity.h>
17 #include <__functional/invoke.h>
18 #include <__iterator/advance.h>
19 #include <__iterator/concepts.h>
20 #include <__iterator/distance.h>
21 #include <__iterator/iterator_traits.h>
22 #include <__ranges/concepts.h>
23 #include <__type_traits/is_callable.h>
24 #include <__utility/convert_to_integral.h>
25 #include <__utility/pair.h>
27 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
28 # pragma GCC system_header
31 _LIBCPP_BEGIN_NAMESPACE_STD
33 template <class _AlgPolicy
, class _Pred
, class _Iter
, class _Sent
, class _SizeT
, class _Type
, class _Proj
>
34 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
35 pair
<_Iter
, _Iter
> __search_n_forward_impl(_Iter __first
, _Sent __last
,
41 return std::make_pair(__first
, __first
);
43 // Find first element in sequence that matchs __value, with a mininum of loop checks
45 if (__first
== __last
) { // return __last if no element matches __value
46 _IterOps
<_AlgPolicy
>::__advance_to(__first
, __last
);
47 return std::make_pair(__first
, __first
);
49 if (std::__invoke(__pred
, std::__invoke(__proj
, *__first
), __value
))
53 // *__first matches __value, now match elements after here
57 if (++__c
== __count
) // If pattern exhausted, __first is the answer (works for 1 element pattern)
58 return std::make_pair(__first
, ++__m
);
59 if (++__m
== __last
) { // Otherwise if source exhaused, pattern not found
60 _IterOps
<_AlgPolicy
>::__advance_to(__first
, __last
);
61 return std::make_pair(__first
, __first
);
64 // if there is a mismatch, restart with a new __first
65 if (!std::__invoke(__pred
, std::__invoke(__proj
, *__m
), __value
))
70 } // else there is a match, check next elements
75 template <class _AlgPolicy
, class _Pred
, class _Iter
, class _Sent
, class _SizeT
, class _Type
, class _Proj
, class _DiffT
>
76 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
77 std::pair
<_Iter
, _Iter
> __search_n_random_access_impl(_Iter __first
, _Sent __last
,
83 using difference_type
= typename iterator_traits
<_Iter
>::difference_type
;
85 return std::make_pair(__first
, __first
);
86 if (__size1
< static_cast<_DiffT
>(__count
)) {
87 _IterOps
<_AlgPolicy
>::__advance_to(__first
, __last
);
88 return std::make_pair(__first
, __first
);
91 const auto __s
= __first
+ __size1
- difference_type(__count
- 1); // Start of pattern match can't go beyond here
93 // Find first element in sequence that matchs __value, with a mininum of loop checks
95 if (__first
>= __s
) { // return __last if no element matches __value
96 _IterOps
<_AlgPolicy
>::__advance_to(__first
, __last
);
97 return std::make_pair(__first
, __first
);
99 if (std::__invoke(__pred
, std::__invoke(__proj
, *__first
), __value
))
103 // *__first matches __value_, now match elements after here
107 if (++__c
== __count
) // If pattern exhausted, __first is the answer (works for 1 element pattern)
108 return std::make_pair(__first
, __first
+ _DiffT(__count
));
109 ++__m
; // no need to check range on __m because __s guarantees we have enough source
111 // if there is a mismatch, restart with a new __first
112 if (!std::__invoke(__pred
, std::__invoke(__proj
, *__m
), __value
))
117 } // else there is a match, check next elements
122 template <class _Iter
, class _Sent
,
127 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
128 pair
<_Iter
, _Iter
> __search_n_impl(_Iter __first
, _Sent __last
,
130 const _Type
& __value
,
133 __enable_if_t
<__has_random_access_iterator_category
<_Iter
>::value
>* = nullptr) {
134 return std::__search_n_random_access_impl
<_ClassicAlgPolicy
>(__first
, __last
,
142 template <class _Iter1
, class _Sent1
,
147 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
148 pair
<_Iter1
, _Iter1
> __search_n_impl(_Iter1 __first
, _Sent1 __last
,
150 const _Type
& __value
,
153 __enable_if_t
<__has_forward_iterator_category
<_Iter1
>::value
154 && !__has_random_access_iterator_category
<_Iter1
>::value
>* = nullptr) {
155 return std::__search_n_forward_impl
<_ClassicAlgPolicy
>(__first
, __last
,
162 template <class _ForwardIterator
, class _Size
, class _Tp
, class _BinaryPredicate
>
163 _LIBCPP_NODISCARD_EXT
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
164 _ForwardIterator
search_n(_ForwardIterator __first
, _ForwardIterator __last
,
167 _BinaryPredicate __pred
) {
168 static_assert(__is_callable
<_BinaryPredicate
, decltype(*__first
), const _Tp
&>::value
,
169 "BinaryPredicate has to be callable");
170 auto __proj
= __identity();
171 return std::__search_n_impl(__first
, __last
, std::__convert_to_integral(__count
), __value
, __pred
, __proj
).first
;
174 template <class _ForwardIterator
, class _Size
, class _Tp
>
175 _LIBCPP_NODISCARD_EXT
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
176 _ForwardIterator
search_n(_ForwardIterator __first
, _ForwardIterator __last
, _Size __count
, const _Tp
& __value
) {
177 return std::search_n(__first
, __last
, std::__convert_to_integral(__count
), __value
, __equal_to());
180 _LIBCPP_END_NAMESPACE_STD
182 #endif // _LIBCPP___ALGORITHM_SEARCH_N_H