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 <__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>
26 
27 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
28 #  pragma GCC system_header
29 #endif
30 
31 _LIBCPP_BEGIN_NAMESPACE_STD
32 
33 template <class _AlgPolicy, class _Pred, class _Iter, class _Sent, class _SizeT, class _Type, class _Proj>
__search_n_forward_impl(_Iter __first,_Sent __last,_SizeT __count,const _Type & __value,_Pred & __pred,_Proj & __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) {
36   if (__count <= 0)
37     return std::make_pair(__first, __first);
38   while (true) {
39     // Find first element in sequence that matchs __value, with a mininum of loop checks
40     while (true) {
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);
44       }
45       if (std::__invoke(__pred, std::__invoke(__proj, *__first), __value))
46         break;
47       ++__first;
48     }
49     // *__first matches __value, now match elements after here
50     _Iter __m = __first;
51     _SizeT __c(0);
52     while (true) {
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);
58       }
59 
60       // if there is a mismatch, restart with a new __first
61       if (!std::__invoke(__pred, std::__invoke(__proj, *__m), __value)) {
62         __first = __m;
63         ++__first;
64         break;
65       } // else there is a match, check next elements
66     }
67   }
68 }
69 
70 template <class _AlgPolicy, class _Pred, class _Iter, class _Sent, class _SizeT, class _Type, class _Proj, class _DiffT>
__search_n_random_access_impl(_Iter __first,_Sent __last,_SizeT __count,const _Type & __value,_Pred & __pred,_Proj & __proj,_DiffT __size1)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;
74   if (__count == 0)
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);
79   }
80 
81   const auto __s = __first + __size1 - difference_type(__count - 1); // Start of pattern match can't go beyond here
82   while (true) {
83     // Find first element in sequence that matchs __value, with a mininum of loop checks
84     while (true) {
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);
88       }
89       if (std::__invoke(__pred, std::__invoke(__proj, *__first), __value))
90         break;
91       ++__first;
92     }
93     // *__first matches __value_, now match elements after here
94     auto __m = __first;
95     _SizeT __c(0);
96     while (true) {
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
100 
101       // if there is a mismatch, restart with a new __first
102       if (!std::__invoke(__pred, std::__invoke(__proj, *__m), __value)) {
103         __first = __m;
104         ++__first;
105         break;
106       } // else there is a match, check next elements
107     }
108   }
109 }
110 
111 template <class _Iter, class _Sent, class _DiffT, class _Type, class _Pred, class _Proj>
112 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter, _Iter> __search_n_impl(
113     _Iter __first,
114     _Sent __last,
115     _DiffT __count,
116     const _Type& __value,
117     _Pred& __pred,
118     _Proj& __proj,
119     __enable_if_t<__has_random_access_iterator_category<_Iter>::value>* = nullptr) {
120   return std::__search_n_random_access_impl<_ClassicAlgPolicy>(
121       __first, __last, __count, __value, __pred, __proj, __last - __first);
122 }
123 
124 template <class _Iter1, class _Sent1, class _DiffT, class _Type, class _Pred, class _Proj>
125 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter1, _Iter1> __search_n_impl(
126     _Iter1 __first,
127     _Sent1 __last,
128     _DiffT __count,
129     const _Type& __value,
130     _Pred& __pred,
131     _Proj& __proj,
132     __enable_if_t<__has_forward_iterator_category<_Iter1>::value &&
133                   !__has_random_access_iterator_category<_Iter1>::value>* = nullptr) {
134   return std::__search_n_forward_impl<_ClassicAlgPolicy>(__first, __last, __count, __value, __pred, __proj);
135 }
136 
137 template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
search_n(_ForwardIterator __first,_ForwardIterator __last,_Size __count,const _Tp & __value,_BinaryPredicate __pred)138 _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator search_n(
139     _ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value, _BinaryPredicate __pred) {
140   static_assert(
141       __is_callable<_BinaryPredicate, decltype(*__first), const _Tp&>::value, "BinaryPredicate has to be callable");
142   auto __proj = __identity();
143   return std::__search_n_impl(__first, __last, std::__convert_to_integral(__count), __value, __pred, __proj).first;
144 }
145 
146 template <class _ForwardIterator, class _Size, class _Tp>
147 _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
search_n(_ForwardIterator __first,_ForwardIterator __last,_Size __count,const _Tp & __value)148 search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value) {
149   return std::search_n(__first, __last, std::__convert_to_integral(__count), __value, __equal_to());
150 }
151 
152 _LIBCPP_END_NAMESPACE_STD
153 
154 #endif // _LIBCPP___ALGORITHM_SEARCH_N_H
155