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 <__functional/invoke.h>
18 #include <__iterator/advance.h>
19 #include <__iterator/concepts.h>
20 #include <__iterator/iterator_traits.h>
21 #include <__type_traits/enable_if.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
,
32 class _Iter1
, class _Sent1
,
33 class _Iter2
, class _Sent2
,
37 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
38 pair
<_Iter1
, _Iter1
> __search_forward_impl(_Iter1 __first1
, _Sent1 __last1
,
39 _Iter2 __first2
, _Sent2 __last2
,
43 if (__first2
== __last2
)
44 return std::make_pair(__first1
, __first1
); // Everything matches an empty sequence
46 // Find first element in sequence 1 that matchs *__first2, with a mininum of loop checks
48 if (__first1
== __last1
) { // return __last1 if no element matches *__first2
49 _IterOps
<_AlgPolicy
>::__advance_to(__first1
, __last1
);
50 return std::make_pair(__first1
, __first1
);
52 if (std::__invoke(__pred
, std::__invoke(__proj1
, *__first1
), std::__invoke(__proj2
, *__first2
)))
56 // *__first1 matches *__first2, now match elements after here
57 _Iter1 __m1
= __first1
;
58 _Iter2 __m2
= __first2
;
60 if (++__m2
== __last2
) // If pattern exhausted, __first1 is the answer (works for 1 element pattern)
61 return std::make_pair(__first1
, ++__m1
);
62 if (++__m1
== __last1
) { // Otherwise if source exhaused, pattern not found
63 return std::make_pair(__m1
, __m1
);
66 // if there is a mismatch, restart with a new __first1
67 if (!std::__invoke(__pred
, std::__invoke(__proj1
, *__m1
), std::__invoke(__proj2
, *__m2
)))
71 } // else there is a match, check next elements
76 template <class _AlgPolicy
,
77 class _Iter1
, class _Sent1
,
78 class _Iter2
, class _Sent2
,
84 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
85 pair
<_Iter1
, _Iter1
> __search_random_access_impl(_Iter1 __first1
, _Sent1 __last1
,
86 _Iter2 __first2
, _Sent2 __last2
,
92 const _Iter1 __s
= __first1
+ __size1
- _DiffT1(__size2
- 1); // Start of pattern match can't go beyond here
96 if (__first1
== __s
) {
97 _IterOps
<_AlgPolicy
>::__advance_to(__first1
, __last1
);
98 return std::make_pair(__first1
, __first1
);
100 if (std::__invoke(__pred
, std::__invoke(__proj1
, *__first1
), std::__invoke(__proj2
, *__first2
)))
105 _Iter1 __m1
= __first1
;
106 _Iter2 __m2
= __first2
;
108 if (++__m2
== __last2
)
109 return std::make_pair(__first1
, __first1
+ _DiffT1(__size2
));
110 ++__m1
; // no need to check range on __m1 because __s guarantees we have enough source
111 if (!std::__invoke(__pred
, std::__invoke(__proj1
, *__m1
), std::__invoke(__proj2
, *__m2
))) {
119 template <class _Iter1
, class _Sent1
,
120 class _Iter2
, class _Sent2
,
124 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
125 pair
<_Iter1
, _Iter1
> __search_impl(_Iter1 __first1
, _Sent1 __last1
,
126 _Iter2 __first2
, _Sent2 __last2
,
130 __enable_if_t
<__has_random_access_iterator_category
<_Iter1
>::value
131 && __has_random_access_iterator_category
<_Iter2
>::value
>* = nullptr) {
133 auto __size2
= __last2
- __first2
;
135 return std::make_pair(__first1
, __first1
);
137 auto __size1
= __last1
- __first1
;
138 if (__size1
< __size2
) {
139 return std::make_pair(__last1
, __last1
);
142 return std::__search_random_access_impl
<_ClassicAlgPolicy
>(__first1
, __last1
,
151 template <class _Iter1
, class _Sent1
,
152 class _Iter2
, class _Sent2
,
156 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
157 pair
<_Iter1
, _Iter1
> __search_impl(_Iter1 __first1
, _Sent1 __last1
,
158 _Iter2 __first2
, _Sent2 __last2
,
162 __enable_if_t
<__has_forward_iterator_category
<_Iter1
>::value
163 && __has_forward_iterator_category
<_Iter2
>::value
164 && !(__has_random_access_iterator_category
<_Iter1
>::value
165 && __has_random_access_iterator_category
<_Iter2
>::value
)>* = nullptr) {
166 return std::__search_forward_impl
<_ClassicAlgPolicy
>(__first1
, __last1
,
173 template <class _ForwardIterator1
, class _ForwardIterator2
, class _BinaryPredicate
>
174 _LIBCPP_NODISCARD_EXT
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
175 _ForwardIterator1
search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
176 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
177 _BinaryPredicate __pred
) {
178 static_assert(__is_callable
<_BinaryPredicate
, decltype(*__first1
), decltype(*__first2
)>::value
,
179 "BinaryPredicate has to be callable");
180 auto __proj
= __identity();
181 return std::__search_impl(__first1
, __last1
, __first2
, __last2
, __pred
, __proj
, __proj
).first
;
184 template <class _ForwardIterator1
, class _ForwardIterator2
>
185 _LIBCPP_NODISCARD_EXT
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
186 _ForwardIterator1
search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
187 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
) {
188 return std::search(__first1
, __last1
, __first2
, __last2
, __equal_to());
191 #if _LIBCPP_STD_VER >= 17
192 template <class _ForwardIterator
, class _Searcher
>
193 _LIBCPP_NODISCARD_EXT _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
194 search(_ForwardIterator __f
, _ForwardIterator __l
, const _Searcher
& __s
) {
195 return __s(__f
, __l
).first
;
200 _LIBCPP_END_NAMESPACE_STD
202 #endif // _LIBCPP___ALGORITHM_SEARCH_H