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 <__cxx03/__algorithm/comp.h>
14 #include <__cxx03/__algorithm/iterator_operations.h>
15 #include <__cxx03/__config>
16 #include <__cxx03/__functional/identity.h>
17 #include <__cxx03/__functional/invoke.h>
18 #include <__cxx03/__iterator/advance.h>
19 #include <__cxx03/__iterator/concepts.h>
20 #include <__cxx03/__iterator/distance.h>
21 #include <__cxx03/__iterator/iterator_traits.h>
22 #include <__cxx03/__ranges/concepts.h>
23 #include <__cxx03/__type_traits/is_callable.h>
24 #include <__cxx03/__utility/convert_to_integral.h>
25 #include <__cxx03/__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 pair
<_Iter
, _Iter
> __search_n_forward_impl(
35 _Iter __first
, _Sent __last
, _SizeT __count
, const _Type
& __value
, _Pred
& __pred
, _Proj
& __proj
) {
37 return std::make_pair(__first
, __first
);
39 // Find first element in sequence that matchs __value, with a mininum of loop checks
41 if (__first
== __last
) { // return __last if no element matches __value
42 _IterOps
<_AlgPolicy
>::__advance_to(__first
, __last
);
43 return std::make_pair(__first
, __first
);
45 if (std::__invoke(__pred
, std::__invoke(__proj
, *__first
), __value
))
49 // *__first matches __value, now match elements after here
53 if (++__c
== __count
) // If pattern exhausted, __first is the answer (works for 1 element pattern)
54 return std::make_pair(__first
, ++__m
);
55 if (++__m
== __last
) { // Otherwise if source exhaused, pattern not found
56 _IterOps
<_AlgPolicy
>::__advance_to(__first
, __last
);
57 return std::make_pair(__first
, __first
);
60 // if there is a mismatch, restart with a new __first
61 if (!std::__invoke(__pred
, std::__invoke(__proj
, *__m
), __value
)) {
65 } // else there is a match, check next elements
70 template <class _AlgPolicy
, class _Pred
, class _Iter
, class _Sent
, class _SizeT
, class _Type
, class _Proj
, class _DiffT
>
71 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
std::pair
<_Iter
, _Iter
> __search_n_random_access_impl(
72 _Iter __first
, _Sent __last
, _SizeT __count
, const _Type
& __value
, _Pred
& __pred
, _Proj
& __proj
, _DiffT __size1
) {
73 using difference_type
= typename iterator_traits
<_Iter
>::difference_type
;
75 return std::make_pair(__first
, __first
);
76 if (__size1
< static_cast<_DiffT
>(__count
)) {
77 _IterOps
<_AlgPolicy
>::__advance_to(__first
, __last
);
78 return std::make_pair(__first
, __first
);
81 const auto __s
= __first
+ __size1
- difference_type(__count
- 1); // Start of pattern match can't go beyond here
83 // Find first element in sequence that matchs __value, with a mininum of loop checks
85 if (__first
>= __s
) { // return __last if no element matches __value
86 _IterOps
<_AlgPolicy
>::__advance_to(__first
, __last
);
87 return std::make_pair(__first
, __first
);
89 if (std::__invoke(__pred
, std::__invoke(__proj
, *__first
), __value
))
93 // *__first matches __value_, now match elements after here
97 if (++__c
== __count
) // If pattern exhausted, __first is the answer (works for 1 element pattern)
98 return std::make_pair(__first
, __first
+ _DiffT(__count
));
99 ++__m
; // no need to check range on __m because __s guarantees we have enough source
101 // if there is a mismatch, restart with a new __first
102 if (!std::__invoke(__pred
, std::__invoke(__proj
, *__m
), __value
)) {
106 } // else there is a match, check next elements
111 template <class _Iter
,
117 __enable_if_t
<__has_random_access_iterator_category
<_Iter
>::value
, int> = 0>
118 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair
<_Iter
, _Iter
>
119 __search_n_impl(_Iter __first
, _Sent __last
, _DiffT __count
, const _Type
& __value
, _Pred
& __pred
, _Proj
& __proj
) {
120 return std::__search_n_random_access_impl
<_ClassicAlgPolicy
>(
121 __first
, __last
, __count
, __value
, __pred
, __proj
, __last
- __first
);
124 template <class _Iter1
,
130 __enable_if_t
<__has_forward_iterator_category
<_Iter1
>::value
&&
131 !__has_random_access_iterator_category
<_Iter1
>::value
,
133 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair
<_Iter1
, _Iter1
>
134 __search_n_impl(_Iter1 __first
, _Sent1 __last
, _DiffT __count
, const _Type
& __value
, _Pred
& __pred
, _Proj
& __proj
) {
135 return std::__search_n_forward_impl
<_ClassicAlgPolicy
>(__first
, __last
, __count
, __value
, __pred
, __proj
);
138 template <class _ForwardIterator
, class _Size
, class _Tp
, class _BinaryPredicate
>
139 _LIBCPP_NODISCARD
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
search_n(
140 _ForwardIterator __first
, _ForwardIterator __last
, _Size __count
, const _Tp
& __value
, _BinaryPredicate __pred
) {
142 __is_callable
<_BinaryPredicate
, decltype(*__first
), const _Tp
&>::value
, "BinaryPredicate has to be callable");
143 auto __proj
= __identity();
144 return std::__search_n_impl(__first
, __last
, std::__convert_to_integral(__count
), __value
, __pred
, __proj
).first
;
147 template <class _ForwardIterator
, class _Size
, class _Tp
>
148 _LIBCPP_NODISCARD
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
149 search_n(_ForwardIterator __first
, _ForwardIterator __last
, _Size __count
, const _Tp
& __value
) {
150 return std::search_n(__first
, __last
, std::__convert_to_integral(__count
), __value
, __equal_to());
153 _LIBCPP_END_NAMESPACE_STD
155 #endif // _LIBCPP___ALGORITHM_SEARCH_N_H