xref: /llvm-project/libcxx/include/__algorithm/search_n.h (revision 09e3a360581dc36d0820d3fb6da9bd7cfed87b5d)
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/advance.h>
18 #include <__iterator/concepts.h>
19 #include <__iterator/distance.h>
20 #include <__iterator/iterator_traits.h>
21 #include <__ranges/concepts.h>
22 #include <__type_traits/enable_if.h>
23 #include <__type_traits/invoke.h>
24 #include <__type_traits/is_callable.h>
25 #include <__utility/convert_to_integral.h>
26 #include <__utility/pair.h>
27 
28 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
29 #  pragma GCC system_header
30 #endif
31 
32 _LIBCPP_BEGIN_NAMESPACE_STD
33 
34 template <class _AlgPolicy, class _Pred, class _Iter, class _Sent, class _SizeT, class _Type, class _Proj>
35 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter, _Iter> __search_n_forward_impl(
36     _Iter __first, _Sent __last, _SizeT __count, const _Type& __value, _Pred& __pred, _Proj& __proj) {
37   if (__count <= 0)
38     return std::make_pair(__first, __first);
39   while (true) {
40     // Find first element in sequence that matchs __value, with a mininum of loop checks
41     while (true) {
42       if (__first == __last) { // return __last if no element matches __value
43         _IterOps<_AlgPolicy>::__advance_to(__first, __last);
44         return std::make_pair(__first, __first);
45       }
46       if (std::__invoke(__pred, std::__invoke(__proj, *__first), __value))
47         break;
48       ++__first;
49     }
50     // *__first matches __value, now match elements after here
51     _Iter __m = __first;
52     _SizeT __c(0);
53     while (true) {
54       if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
55         return std::make_pair(__first, ++__m);
56       if (++__m == __last) { // Otherwise if source exhaused, pattern not found
57         _IterOps<_AlgPolicy>::__advance_to(__first, __last);
58         return std::make_pair(__first, __first);
59       }
60 
61       // if there is a mismatch, restart with a new __first
62       if (!std::__invoke(__pred, std::__invoke(__proj, *__m), __value)) {
63         __first = __m;
64         ++__first;
65         break;
66       } // else there is a match, check next elements
67     }
68   }
69 }
70 
71 template <class _AlgPolicy, class _Pred, class _Iter, class _Sent, class _SizeT, class _Type, class _Proj, class _DiffT>
72 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 std::pair<_Iter, _Iter> __search_n_random_access_impl(
73     _Iter __first, _Sent __last, _SizeT __count, const _Type& __value, _Pred& __pred, _Proj& __proj, _DiffT __size1) {
74   using difference_type = typename iterator_traits<_Iter>::difference_type;
75   if (__count == 0)
76     return std::make_pair(__first, __first);
77   if (__size1 < static_cast<_DiffT>(__count)) {
78     _IterOps<_AlgPolicy>::__advance_to(__first, __last);
79     return std::make_pair(__first, __first);
80   }
81 
82   const auto __s = __first + __size1 - difference_type(__count - 1); // Start of pattern match can't go beyond here
83   while (true) {
84     // Find first element in sequence that matchs __value, with a mininum of loop checks
85     while (true) {
86       if (__first >= __s) { // return __last if no element matches __value
87         _IterOps<_AlgPolicy>::__advance_to(__first, __last);
88         return std::make_pair(__first, __first);
89       }
90       if (std::__invoke(__pred, std::__invoke(__proj, *__first), __value))
91         break;
92       ++__first;
93     }
94     // *__first matches __value_, now match elements after here
95     auto __m = __first;
96     _SizeT __c(0);
97     while (true) {
98       if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
99         return std::make_pair(__first, __first + _DiffT(__count));
100       ++__m; // no need to check range on __m because __s guarantees we have enough source
101 
102       // if there is a mismatch, restart with a new __first
103       if (!std::__invoke(__pred, std::__invoke(__proj, *__m), __value)) {
104         __first = __m;
105         ++__first;
106         break;
107       } // else there is a match, check next elements
108     }
109   }
110 }
111 
112 template <class _Iter,
113           class _Sent,
114           class _DiffT,
115           class _Type,
116           class _Pred,
117           class _Proj,
118           __enable_if_t<__has_random_access_iterator_category<_Iter>::value, int> = 0>
119 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter, _Iter>
120 __search_n_impl(_Iter __first, _Sent __last, _DiffT __count, const _Type& __value, _Pred& __pred, _Proj& __proj) {
121   return std::__search_n_random_access_impl<_ClassicAlgPolicy>(
122       __first, __last, __count, __value, __pred, __proj, __last - __first);
123 }
124 
125 template <class _Iter1,
126           class _Sent1,
127           class _DiffT,
128           class _Type,
129           class _Pred,
130           class _Proj,
131           __enable_if_t<__has_forward_iterator_category<_Iter1>::value &&
132                             !__has_random_access_iterator_category<_Iter1>::value,
133                         int> = 0>
134 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter1, _Iter1>
135 __search_n_impl(_Iter1 __first, _Sent1 __last, _DiffT __count, const _Type& __value, _Pred& __pred, _Proj& __proj) {
136   return std::__search_n_forward_impl<_ClassicAlgPolicy>(__first, __last, __count, __value, __pred, __proj);
137 }
138 
139 template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
140 [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator search_n(
141     _ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value, _BinaryPredicate __pred) {
142   static_assert(
143       __is_callable<_BinaryPredicate&, decltype(*__first), const _Tp&>::value, "The comparator has to be callable");
144   auto __proj = __identity();
145   return std::__search_n_impl(__first, __last, std::__convert_to_integral(__count), __value, __pred, __proj).first;
146 }
147 
148 template <class _ForwardIterator, class _Size, class _Tp>
149 [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
150 search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value) {
151   return std::search_n(__first, __last, std::__convert_to_integral(__count), __value, __equal_to());
152 }
153 
154 _LIBCPP_END_NAMESPACE_STD
155 
156 #endif // _LIBCPP___ALGORITHM_SEARCH_N_H
157