xref: /freebsd/contrib/llvm-project/libcxx/include/__algorithm/search_n.h (revision 5036d9652a5701d00e9e40ea942c278e9f77d33d)
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>
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>
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,
112           class _Sent,
113           class _DiffT,
114           class _Type,
115           class _Pred,
116           class _Proj,
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);
122 }
123 
124 template <class _Iter1,
125           class _Sent1,
126           class _DiffT,
127           class _Type,
128           class _Pred,
129           class _Proj,
130           __enable_if_t<__has_forward_iterator_category<_Iter1>::value &&
131                             !__has_random_access_iterator_category<_Iter1>::value,
132                         int> = 0>
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);
136 }
137 
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) {
141   static_assert(
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;
145 }
146 
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());
151 }
152 
153 _LIBCPP_END_NAMESPACE_STD
154 
155 #endif // _LIBCPP___ALGORITHM_SEARCH_N_H
156