xref: /freebsd/contrib/llvm-project/libcxx/include/__algorithm/search_n.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1fe6060f1SDimitry Andric // -*- C++ -*-
2fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
3fe6060f1SDimitry Andric //
4fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
6fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7fe6060f1SDimitry Andric //
8fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
9fe6060f1SDimitry Andric 
10fe6060f1SDimitry Andric #ifndef _LIBCPP___ALGORITHM_SEARCH_N_H
11fe6060f1SDimitry Andric #define _LIBCPP___ALGORITHM_SEARCH_N_H
12fe6060f1SDimitry Andric 
13fe6060f1SDimitry Andric #include <__algorithm/comp.h>
14753f127fSDimitry Andric #include <__algorithm/iterator_operations.h>
1504eeddc0SDimitry Andric #include <__config>
16753f127fSDimitry Andric #include <__functional/identity.h>
1706c3fb27SDimitry Andric #include <__functional/invoke.h>
18753f127fSDimitry Andric #include <__iterator/advance.h>
19753f127fSDimitry Andric #include <__iterator/concepts.h>
20753f127fSDimitry Andric #include <__iterator/distance.h>
21fe6060f1SDimitry Andric #include <__iterator/iterator_traits.h>
22753f127fSDimitry Andric #include <__ranges/concepts.h>
2306c3fb27SDimitry Andric #include <__type_traits/is_callable.h>
24bdd1243dSDimitry Andric #include <__utility/convert_to_integral.h>
25753f127fSDimitry Andric #include <__utility/pair.h>
26fe6060f1SDimitry Andric 
27fe6060f1SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
28fe6060f1SDimitry Andric #  pragma GCC system_header
29fe6060f1SDimitry Andric #endif
30fe6060f1SDimitry Andric 
31fe6060f1SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
32fe6060f1SDimitry Andric 
33753f127fSDimitry Andric 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)34cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter, _Iter> __search_n_forward_impl(
35cb14a3feSDimitry Andric     _Iter __first, _Sent __last, _SizeT __count, const _Type& __value, _Pred& __pred, _Proj& __proj) {
36fe6060f1SDimitry Andric   if (__count <= 0)
37753f127fSDimitry Andric     return std::make_pair(__first, __first);
38fe6060f1SDimitry Andric   while (true) {
39753f127fSDimitry Andric     // Find first element in sequence that matchs __value, with a mininum of loop checks
40fe6060f1SDimitry Andric     while (true) {
41753f127fSDimitry Andric       if (__first == __last) { // return __last if no element matches __value
42753f127fSDimitry Andric         _IterOps<_AlgPolicy>::__advance_to(__first, __last);
43753f127fSDimitry Andric         return std::make_pair(__first, __first);
44753f127fSDimitry Andric       }
45753f127fSDimitry Andric       if (std::__invoke(__pred, std::__invoke(__proj, *__first), __value))
46fe6060f1SDimitry Andric         break;
47fe6060f1SDimitry Andric       ++__first;
48fe6060f1SDimitry Andric     }
49753f127fSDimitry Andric     // *__first matches __value, now match elements after here
50753f127fSDimitry Andric     _Iter __m = __first;
51753f127fSDimitry Andric     _SizeT __c(0);
52fe6060f1SDimitry Andric     while (true) {
53fe6060f1SDimitry Andric       if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
54753f127fSDimitry Andric         return std::make_pair(__first, ++__m);
55753f127fSDimitry Andric       if (++__m == __last) { // Otherwise if source exhaused, pattern not found
56753f127fSDimitry Andric         _IterOps<_AlgPolicy>::__advance_to(__first, __last);
57753f127fSDimitry Andric         return std::make_pair(__first, __first);
58753f127fSDimitry Andric       }
59753f127fSDimitry Andric 
60753f127fSDimitry Andric       // if there is a mismatch, restart with a new __first
61cb14a3feSDimitry Andric       if (!std::__invoke(__pred, std::__invoke(__proj, *__m), __value)) {
62fe6060f1SDimitry Andric         __first = __m;
63fe6060f1SDimitry Andric         ++__first;
64fe6060f1SDimitry Andric         break;
65fe6060f1SDimitry Andric       } // else there is a match, check next elements
66fe6060f1SDimitry Andric     }
67fe6060f1SDimitry Andric   }
68fe6060f1SDimitry Andric }
69fe6060f1SDimitry Andric 
70753f127fSDimitry Andric 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)71cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 std::pair<_Iter, _Iter> __search_n_random_access_impl(
72cb14a3feSDimitry Andric     _Iter __first, _Sent __last, _SizeT __count, const _Type& __value, _Pred& __pred, _Proj& __proj, _DiffT __size1) {
73753f127fSDimitry Andric   using difference_type = typename iterator_traits<_Iter>::difference_type;
74753f127fSDimitry Andric   if (__count == 0)
75753f127fSDimitry Andric     return std::make_pair(__first, __first);
76753f127fSDimitry Andric   if (__size1 < static_cast<_DiffT>(__count)) {
77753f127fSDimitry Andric     _IterOps<_AlgPolicy>::__advance_to(__first, __last);
78753f127fSDimitry Andric     return std::make_pair(__first, __first);
79753f127fSDimitry Andric   }
80753f127fSDimitry Andric 
81753f127fSDimitry Andric   const auto __s = __first + __size1 - difference_type(__count - 1); // Start of pattern match can't go beyond here
82fe6060f1SDimitry Andric   while (true) {
83753f127fSDimitry Andric     // Find first element in sequence that matchs __value, with a mininum of loop checks
84fe6060f1SDimitry Andric     while (true) {
85753f127fSDimitry Andric       if (__first >= __s) { // return __last if no element matches __value
86753f127fSDimitry Andric         _IterOps<_AlgPolicy>::__advance_to(__first, __last);
87753f127fSDimitry Andric         return std::make_pair(__first, __first);
88753f127fSDimitry Andric       }
89753f127fSDimitry Andric       if (std::__invoke(__pred, std::__invoke(__proj, *__first), __value))
90fe6060f1SDimitry Andric         break;
91fe6060f1SDimitry Andric       ++__first;
92fe6060f1SDimitry Andric     }
93fe6060f1SDimitry Andric     // *__first matches __value_, now match elements after here
94753f127fSDimitry Andric     auto __m = __first;
95753f127fSDimitry Andric     _SizeT __c(0);
96fe6060f1SDimitry Andric     while (true) {
97fe6060f1SDimitry Andric       if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
98753f127fSDimitry Andric         return std::make_pair(__first, __first + _DiffT(__count));
99fe6060f1SDimitry Andric       ++__m; // no need to check range on __m because __s guarantees we have enough source
100753f127fSDimitry Andric 
101753f127fSDimitry Andric       // if there is a mismatch, restart with a new __first
102cb14a3feSDimitry Andric       if (!std::__invoke(__pred, std::__invoke(__proj, *__m), __value)) {
103fe6060f1SDimitry Andric         __first = __m;
104fe6060f1SDimitry Andric         ++__first;
105fe6060f1SDimitry Andric         break;
106fe6060f1SDimitry Andric       } // else there is a match, check next elements
107fe6060f1SDimitry Andric     }
108fe6060f1SDimitry Andric   }
109fe6060f1SDimitry Andric }
110fe6060f1SDimitry Andric 
111*0fca6ea1SDimitry Andric template <class _Iter,
112*0fca6ea1SDimitry Andric           class _Sent,
113*0fca6ea1SDimitry Andric           class _DiffT,
114*0fca6ea1SDimitry Andric           class _Type,
115*0fca6ea1SDimitry Andric           class _Pred,
116*0fca6ea1SDimitry Andric           class _Proj,
117*0fca6ea1SDimitry Andric           __enable_if_t<__has_random_access_iterator_category<_Iter>::value, int> = 0>
118*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter, _Iter>
__search_n_impl(_Iter __first,_Sent __last,_DiffT __count,const _Type & __value,_Pred & __pred,_Proj & __proj)119*0fca6ea1SDimitry Andric __search_n_impl(_Iter __first, _Sent __last, _DiffT __count, const _Type& __value, _Pred& __pred, _Proj& __proj) {
120cb14a3feSDimitry Andric   return std::__search_n_random_access_impl<_ClassicAlgPolicy>(
121cb14a3feSDimitry Andric       __first, __last, __count, __value, __pred, __proj, __last - __first);
122753f127fSDimitry Andric }
123753f127fSDimitry Andric 
124*0fca6ea1SDimitry Andric template <class _Iter1,
125*0fca6ea1SDimitry Andric           class _Sent1,
126*0fca6ea1SDimitry Andric           class _DiffT,
127*0fca6ea1SDimitry Andric           class _Type,
128*0fca6ea1SDimitry Andric           class _Pred,
129*0fca6ea1SDimitry Andric           class _Proj,
130cb14a3feSDimitry Andric           __enable_if_t<__has_forward_iterator_category<_Iter1>::value &&
131*0fca6ea1SDimitry Andric                             !__has_random_access_iterator_category<_Iter1>::value,
132*0fca6ea1SDimitry Andric                         int> = 0>
133*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter1, _Iter1>
__search_n_impl(_Iter1 __first,_Sent1 __last,_DiffT __count,const _Type & __value,_Pred & __pred,_Proj & __proj)134*0fca6ea1SDimitry Andric __search_n_impl(_Iter1 __first, _Sent1 __last, _DiffT __count, const _Type& __value, _Pred& __pred, _Proj& __proj) {
135cb14a3feSDimitry Andric   return std::__search_n_forward_impl<_ClassicAlgPolicy>(__first, __last, __count, __value, __pred, __proj);
136753f127fSDimitry Andric }
137753f127fSDimitry Andric 
138fe6060f1SDimitry Andric template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
search_n(_ForwardIterator __first,_ForwardIterator __last,_Size __count,const _Tp & __value,_BinaryPredicate __pred)139*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator search_n(
140cb14a3feSDimitry Andric     _ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value, _BinaryPredicate __pred) {
141cb14a3feSDimitry Andric   static_assert(
142cb14a3feSDimitry Andric       __is_callable<_BinaryPredicate, decltype(*__first), const _Tp&>::value, "BinaryPredicate has to be callable");
143753f127fSDimitry Andric   auto __proj = __identity();
144753f127fSDimitry Andric   return std::__search_n_impl(__first, __last, std::__convert_to_integral(__count), __value, __pred, __proj).first;
145fe6060f1SDimitry Andric }
146fe6060f1SDimitry Andric 
147fe6060f1SDimitry Andric template <class _ForwardIterator, class _Size, class _Tp>
148*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
search_n(_ForwardIterator __first,_ForwardIterator __last,_Size __count,const _Tp & __value)149cb14a3feSDimitry Andric search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value) {
150bdd1243dSDimitry Andric   return std::search_n(__first, __last, std::__convert_to_integral(__count), __value, __equal_to());
151fe6060f1SDimitry Andric }
152fe6060f1SDimitry Andric 
153fe6060f1SDimitry Andric _LIBCPP_END_NAMESPACE_STD
154fe6060f1SDimitry Andric 
155fe6060f1SDimitry Andric #endif // _LIBCPP___ALGORITHM_SEARCH_N_H
156