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_H
11 #define _LIBCPP___ALGORITHM_SEARCH_H
13 #include <__algorithm/comp.h>
14 #include <__algorithm/iterator_operations.h>
16 #include <__functional/identity.h>
17 #include <__iterator/advance.h>
18 #include <__iterator/concepts.h>
19 #include <__iterator/iterator_traits.h>
20 #include <__type_traits/enable_if.h>
21 #include <__type_traits/invoke.h>
22 #include <__type_traits/is_callable.h>
23 #include <__utility/pair.h>
25 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
26 # pragma GCC system_header
29 _LIBCPP_BEGIN_NAMESPACE_STD
31 template <class _AlgPolicy
,
39 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair
<_Iter1
, _Iter1
> __search_forward_impl(
40 _Iter1 __first1
, _Sent1 __last1
, _Iter2 __first2
, _Sent2 __last2
, _Pred
& __pred
, _Proj1
& __proj1
, _Proj2
& __proj2
) {
41 if (__first2
== __last2
)
42 return std::make_pair(__first1
, __first1
); // Everything matches an empty sequence
44 // Find first element in sequence 1 that matchs *__first2, with a mininum of loop checks
46 if (__first1
== __last1
) { // return __last1 if no element matches *__first2
47 _IterOps
<_AlgPolicy
>::__advance_to(__first1
, __last1
);
48 return std::make_pair(__first1
, __first1
);
50 if (std::__invoke(__pred
, std::__invoke(__proj1
, *__first1
), std::__invoke(__proj2
, *__first2
)))
54 // *__first1 matches *__first2, now match elements after here
55 _Iter1 __m1
= __first1
;
56 _Iter2 __m2
= __first2
;
58 if (++__m2
== __last2
) // If pattern exhausted, __first1 is the answer (works for 1 element pattern)
59 return std::make_pair(__first1
, ++__m1
);
60 if (++__m1
== __last1
) { // Otherwise if source exhaused, pattern not found
61 return std::make_pair(__m1
, __m1
);
64 // if there is a mismatch, restart with a new __first1
65 if (!std::__invoke(__pred
, std::__invoke(__proj1
, *__m1
), std::__invoke(__proj2
, *__m2
))) {
68 } // else there is a match, check next elements
73 template <class _AlgPolicy
,
83 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair
<_Iter1
, _Iter1
> __search_random_access_impl(
93 const _Iter1 __s
= __first1
+ __size1
- _DiffT1(__size2
- 1); // Start of pattern match can't go beyond here
97 if (__first1
== __s
) {
98 _IterOps
<_AlgPolicy
>::__advance_to(__first1
, __last1
);
99 return std::make_pair(__first1
, __first1
);
101 if (std::__invoke(__pred
, std::__invoke(__proj1
, *__first1
), std::__invoke(__proj2
, *__first2
)))
106 _Iter1 __m1
= __first1
;
107 _Iter2 __m2
= __first2
;
109 if (++__m2
== __last2
)
110 return std::make_pair(__first1
, __first1
+ _DiffT1(__size2
));
111 ++__m1
; // no need to check range on __m1 because __s guarantees we have enough source
112 if (!std::__invoke(__pred
, std::__invoke(__proj1
, *__m1
), std::__invoke(__proj2
, *__m2
))) {
120 template <class _Iter1
,
127 __enable_if_t
<__has_random_access_iterator_category
<_Iter1
>::value
&&
128 __has_random_access_iterator_category
<_Iter2
>::value
,
130 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair
<_Iter1
, _Iter1
> __search_impl(
131 _Iter1 __first1
, _Sent1 __last1
, _Iter2 __first2
, _Sent2 __last2
, _Pred
& __pred
, _Proj1
& __proj1
, _Proj2
& __proj2
) {
132 auto __size2
= __last2
- __first2
;
134 return std::make_pair(__first1
, __first1
);
136 auto __size1
= __last1
- __first1
;
137 if (__size1
< __size2
) {
138 return std::make_pair(__last1
, __last1
);
141 return std::__search_random_access_impl
<_ClassicAlgPolicy
>(
142 __first1
, __last1
, __first2
, __last2
, __pred
, __proj1
, __proj2
, __size1
, __size2
);
153 __enable_if_t
<__has_forward_iterator_category
<_Iter1
>::value
&& __has_forward_iterator_category
<_Iter2
>::value
&&
154 !(__has_random_access_iterator_category
<_Iter1
>::value
&&
155 __has_random_access_iterator_category
<_Iter2
>::value
),
157 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair
<_Iter1
, _Iter1
> __search_impl(
158 _Iter1 __first1
, _Sent1 __last1
, _Iter2 __first2
, _Sent2 __last2
, _Pred
& __pred
, _Proj1
& __proj1
, _Proj2
& __proj2
) {
159 return std::__search_forward_impl
<_ClassicAlgPolicy
>(__first1
, __last1
, __first2
, __last2
, __pred
, __proj1
, __proj2
);
162 template <class _ForwardIterator1
, class _ForwardIterator2
, class _BinaryPredicate
>
163 [[__nodiscard__
]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator1
164 search(_ForwardIterator1 __first1
,
165 _ForwardIterator1 __last1
,
166 _ForwardIterator2 __first2
,
167 _ForwardIterator2 __last2
,
168 _BinaryPredicate __pred
) {
169 static_assert(__is_callable
<_BinaryPredicate
&, decltype(*__first1
), decltype(*__first2
)>::value
,
170 "The comparator has to be callable");
171 auto __proj
= __identity();
172 return std::__search_impl(__first1
, __last1
, __first2
, __last2
, __pred
, __proj
, __proj
).first
;
175 template <class _ForwardIterator1
, class _ForwardIterator2
>
176 [[__nodiscard__
]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator1
177 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
, _ForwardIterator2 __first2
, _ForwardIterator2 __last2
) {
178 return std::search(__first1
, __last1
, __first2
, __last2
, __equal_to());
181 #if _LIBCPP_STD_VER >= 17
182 template <class _ForwardIterator
, class _Searcher
>
183 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
184 search(_ForwardIterator __f
, _ForwardIterator __l
, const _Searcher
& __s
) {
185 return __s(__f
, __l
).first
;
190 _LIBCPP_END_NAMESPACE_STD
192 #endif // _LIBCPP___ALGORITHM_SEARCH_H