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_FIND_END_OF_H
11 #define _LIBCPP___ALGORITHM_FIND_END_OF_H
13 #include <__algorithm/comp.h>
14 #include <__algorithm/iterator_operations.h>
16 #include <__functional/identity.h>
17 #include <__iterator/iterator_traits.h>
18 #include <__iterator/next.h>
19 #include <__type_traits/invoke.h>
20 #include <__utility/pair.h>
22 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
23 # pragma GCC system_header
26 _LIBCPP_BEGIN_NAMESPACE_STD
28 template < class _AlgPolicy
,
36 _LIBCPP_HIDE_FROM_ABI
inline _LIBCPP_CONSTEXPR_SINCE_CXX14 pair
<_Iter1
, _Iter1
> __find_end_impl(
45 forward_iterator_tag
) {
46 // modeled after search algorithm
47 _Iter1 __match_first
= _IterOps
<_AlgPolicy
>::next(__first1
, __last1
); // __last1 is the "default" answer
48 _Iter1 __match_last
= __match_first
;
49 if (__first2
== __last2
)
50 return pair
<_Iter1
, _Iter1
>(__match_last
, __match_last
);
53 if (__first1
== __last1
) // if source exhausted return last correct answer (or __last1 if never found)
54 return pair
<_Iter1
, _Iter1
>(__match_first
, __match_last
);
55 if (std::__invoke(__pred
, std::__invoke(__proj1
, *__first1
), std::__invoke(__proj2
, *__first2
)))
59 // *__first1 matches *__first2, now match elements after here
60 _Iter1 __m1
= __first1
;
61 _Iter2 __m2
= __first2
;
63 if (++__m2
== __last2
) { // Pattern exhaused, record answer and search for another one
64 __match_first
= __first1
;
65 __match_last
= ++__m1
;
69 if (++__m1
== __last1
) // Source exhausted, return last answer
70 return pair
<_Iter1
, _Iter1
>(__match_first
, __match_last
);
71 // mismatch, restart with a new __first
72 if (!std::__invoke(__pred
, std::__invoke(__proj1
, *__m1
), std::__invoke(__proj2
, *__m2
))) {
75 } // else there is a match, check next elements
80 template <class _ForwardIterator1
, class _ForwardIterator2
, class _BinaryPredicate
>
81 [[__nodiscard__
]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _ForwardIterator1
__find_end_classic(
82 _ForwardIterator1 __first1
,
83 _ForwardIterator1 __last1
,
84 _ForwardIterator2 __first2
,
85 _ForwardIterator2 __last2
,
86 _BinaryPredicate
& __pred
) {
87 auto __proj
= __identity();
88 return std::__find_end_impl
<_ClassicAlgPolicy
>(
96 typename iterator_traits
<_ForwardIterator1
>::iterator_category(),
97 typename iterator_traits
<_ForwardIterator2
>::iterator_category())
101 template <class _ForwardIterator1
, class _ForwardIterator2
, class _BinaryPredicate
>
102 [[__nodiscard__
]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator1
find_end(
103 _ForwardIterator1 __first1
,
104 _ForwardIterator1 __last1
,
105 _ForwardIterator2 __first2
,
106 _ForwardIterator2 __last2
,
107 _BinaryPredicate __pred
) {
108 return std::__find_end_classic(__first1
, __last1
, __first2
, __last2
, __pred
);
111 template <class _ForwardIterator1
, class _ForwardIterator2
>
112 [[__nodiscard__
]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator1
113 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
, _ForwardIterator2 __first2
, _ForwardIterator2 __last2
) {
114 return std::find_end(__first1
, __last1
, __first2
, __last2
, __equal_to());
117 _LIBCPP_END_NAMESPACE_STD
119 #endif // _LIBCPP___ALGORITHM_FIND_END_OF_H