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>
15 #include <__algorithm/search.h>
17 #include <__functional/identity.h>
18 #include <__iterator/advance.h>
19 #include <__iterator/iterator_traits.h>
20 #include <__iterator/next.h>
21 #include <__iterator/reverse_iterator.h>
22 #include <__utility/pair.h>
23 #include <type_traits>
25 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
26 # pragma GCC system_header
29 _LIBCPP_BEGIN_NAMESPACE_STD
40 _LIBCPP_HIDE_FROM_ABI inline _LIBCPP_CONSTEXPR_AFTER_CXX11 pair<_Iter1, _Iter1> __find_end_impl(
49 forward_iterator_tag) {
50 // modeled after search algorithm
51 _Iter1 __match_first = _IterOps<_AlgPolicy>::next(__first1, __last1); // __last1 is the "default" answer
52 _Iter1 __match_last = __match_first;
53 if (__first2 == __last2)
54 return pair<_Iter1, _Iter1>(__match_last, __match_last);
57 if (__first1 == __last1) // if source exhausted return last correct answer (or __last1 if never found)
58 return pair<_Iter1, _Iter1>(__match_first, __match_last);
59 if (std::__invoke(__pred, std::__invoke(__proj1, *__first1), std::__invoke(__proj2, *__first2)))
63 // *__first1 matches *__first2, now match elements after here
64 _Iter1 __m1 = __first1;
65 _Iter2 __m2 = __first2;
67 if (++__m2 == __last2) { // Pattern exhaused, record answer and search for another one
68 __match_first = __first1;
69 __match_last = ++__m1;
73 if (++__m1 == __last1) // Source exhausted, return last answer
74 return pair<_Iter1, _Iter1>(__match_first, __match_last);
75 // mismatch, restart with a new __first
76 if (!std::__invoke(__pred, std::__invoke(__proj1, *__m1), std::__invoke(__proj2, *__m2)))
80 } // else there is a match, check next elements
94 _LIBCPP_CONSTEXPR_AFTER_CXX17 _Iter1 __find_end(
102 bidirectional_iterator_tag,
103 bidirectional_iterator_tag) {
104 auto __last1 = _IterOps::next(__first1, __sent1);
105 auto __last2 = _IterOps::next(__first2, __sent2);
106 // modeled after search algorithm (in reverse)
107 if (__first2 == __last2)
108 return __last1; // Everything matches an empty sequence
109 _Iter1 __l1 = __last1;
110 _Iter2 __l2 = __last2;
113 // Find last element in sequence 1 that matchs *(__last2-1), with a mininum of loop checks
115 if (__first1 == __l1) // return __last1 if no element matches *__first2
117 if (std::__invoke(__pred, std::__invoke(__proj1, *--__l1), std::__invoke(__proj2, *__l2)))
120 // *__l1 matches *__l2, now match elements before here
124 if (__m2 == __first2) // If pattern exhausted, __m1 is the answer (works for 1 element pattern)
126 if (__m1 == __first1) // Otherwise if source exhaused, pattern not found
129 // if there is a mismatch, restart with a new __l1
130 if (!std::__invoke(__pred, std::__invoke(__proj1, *--__m1), std::__invoke(__proj2, *--__m2)))
133 } // else there is a match, check next elements
147 _LIBCPP_CONSTEXPR_AFTER_CXX11 _Iter1 __find_end(
155 random_access_iterator_tag,
156 random_access_iterator_tag) {
157 typedef typename iterator_traits<_Iter1>::difference_type _D1;
158 auto __last1 = _IterOps<_AlgPolicy>::next(__first1, __sent1);
159 auto __last2 = _IterOps<_AlgPolicy>::next(__first2, __sent2);
160 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
161 auto __len2 = __last2 - __first2;
164 auto __len1 = __last1 - __first1;
167 const _Iter1 __s = __first1 + _D1(__len2 - 1); // End of pattern match can't go before here
168 _Iter1 __l1 = __last1;
169 _Iter2 __l2 = __last2;
175 if (std::__invoke(__pred, std::__invoke(__proj1, *--__l1), std::__invoke(__proj2, *__l2)))
181 if (__m2 == __first2)
183 // no need to check range on __m1 because __s guarantees we have enough source
184 if (!std::__invoke(__pred, std::__invoke(__proj1, *--__m1), std::__invoke(*--__m2))) {
191 template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
192 _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
193 _ForwardIterator1 __find_end_classic(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
194 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
195 _BinaryPredicate& __pred) {
196 auto __proj = __identity();
197 return std::__find_end_impl<_ClassicAlgPolicy>(
205 typename iterator_traits<_ForwardIterator1>::iterator_category(),
206 typename iterator_traits<_ForwardIterator2>::iterator_category())
210 template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
211 _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
212 _ForwardIterator1 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
213 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
214 _BinaryPredicate __pred) {
215 return std::__find_end_classic(__first1, __last1, __first2, __last2, __pred);
218 template <class _ForwardIterator1, class _ForwardIterator2>
219 _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
220 _ForwardIterator1 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
221 _ForwardIterator2 __first2, _ForwardIterator2 __last2) {
222 using __v1 = typename iterator_traits<_ForwardIterator1>::value_type;
223 using __v2 = typename iterator_traits<_ForwardIterator2>::value_type;
224 return std::find_end(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
227 _LIBCPP_END_NAMESPACE_STD
229 #endif // _LIBCPP___ALGORITHM_FIND_END_OF_H