xref: /freebsd/contrib/llvm-project/libcxx/include/__algorithm/find_end.h (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
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
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef _LIBCPP___ALGORITHM_FIND_END_OF_H
11 #define _LIBCPP___ALGORITHM_FIND_END_OF_H
12 
13 #include <__algorithm/comp.h>
14 #include <__config>
15 #include <__iterator/iterator_traits.h>
16 
17 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
18 #pragma GCC system_header
19 #endif
20 
21 _LIBCPP_BEGIN_NAMESPACE_STD
22 
23 template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
24 _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator1 __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
25                                                            _ForwardIterator2 __first2, _ForwardIterator2 __last2,
26                                                            _BinaryPredicate __pred, forward_iterator_tag,
27                                                            forward_iterator_tag) {
28   // modeled after search algorithm
29   _ForwardIterator1 __r = __last1; // __last1 is the "default" answer
30   if (__first2 == __last2)
31     return __r;
32   while (true) {
33     while (true) {
34       if (__first1 == __last1) // if source exhausted return last correct answer
35         return __r;            //    (or __last1 if never found)
36       if (__pred(*__first1, *__first2))
37         break;
38       ++__first1;
39     }
40     // *__first1 matches *__first2, now match elements after here
41     _ForwardIterator1 __m1 = __first1;
42     _ForwardIterator2 __m2 = __first2;
43     while (true) {
44       if (++__m2 == __last2) { // Pattern exhaused, record answer and search for another one
45         __r = __first1;
46         ++__first1;
47         break;
48       }
49       if (++__m1 == __last1) // Source exhausted, return last answer
50         return __r;
51       if (!__pred(*__m1, *__m2)) // mismatch, restart with a new __first
52       {
53         ++__first1;
54         break;
55       } // else there is a match, check next elements
56     }
57   }
58 }
59 
60 template <class _BinaryPredicate, class _BidirectionalIterator1, class _BidirectionalIterator2>
61 _LIBCPP_CONSTEXPR_AFTER_CXX17 _BidirectionalIterator1 __find_end(
62     _BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1, _BidirectionalIterator2 __first2,
63     _BidirectionalIterator2 __last2, _BinaryPredicate __pred, bidirectional_iterator_tag, bidirectional_iterator_tag) {
64   // modeled after search algorithm (in reverse)
65   if (__first2 == __last2)
66     return __last1; // Everything matches an empty sequence
67   _BidirectionalIterator1 __l1 = __last1;
68   _BidirectionalIterator2 __l2 = __last2;
69   --__l2;
70   while (true) {
71     // Find last element in sequence 1 that matchs *(__last2-1), with a mininum of loop checks
72     while (true) {
73       if (__first1 == __l1) // return __last1 if no element matches *__first2
74         return __last1;
75       if (__pred(*--__l1, *__l2))
76         break;
77     }
78     // *__l1 matches *__l2, now match elements before here
79     _BidirectionalIterator1 __m1 = __l1;
80     _BidirectionalIterator2 __m2 = __l2;
81     while (true) {
82       if (__m2 == __first2) // If pattern exhausted, __m1 is the answer (works for 1 element pattern)
83         return __m1;
84       if (__m1 == __first1) // Otherwise if source exhaused, pattern not found
85         return __last1;
86       if (!__pred(*--__m1, *--__m2)) // if there is a mismatch, restart with a new __l1
87       {
88         break;
89       } // else there is a match, check next elements
90     }
91   }
92 }
93 
94 template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
95 _LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator1 __find_end(
96     _RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, _RandomAccessIterator2 __first2,
97     _RandomAccessIterator2 __last2, _BinaryPredicate __pred, random_access_iterator_tag, random_access_iterator_tag) {
98   typedef typename iterator_traits<_RandomAccessIterator1>::difference_type _D1;
99   typedef typename iterator_traits<_RandomAccessIterator2>::difference_type _D2;
100   // Take advantage of knowing source and pattern lengths.  Stop short when source is smaller than pattern
101   _D2 __len2 = __last2 - __first2;
102   if (__len2 == 0)
103     return __last1;
104   _D1 __len1 = __last1 - __first1;
105   if (__len1 < __len2)
106     return __last1;
107   const _RandomAccessIterator1 __s = __first1 + _D1(__len2 - 1); // End of pattern match can't go before here
108   _RandomAccessIterator1 __l1 = __last1;
109   _RandomAccessIterator2 __l2 = __last2;
110   --__l2;
111   while (true) {
112     while (true) {
113       if (__s == __l1)
114         return __last1;
115       if (__pred(*--__l1, *__l2))
116         break;
117     }
118     _RandomAccessIterator1 __m1 = __l1;
119     _RandomAccessIterator2 __m2 = __l2;
120     while (true) {
121       if (__m2 == __first2)
122         return __m1;
123       // no need to check range on __m1 because __s guarantees we have enough source
124       if (!__pred(*--__m1, *--__m2)) {
125         break;
126       }
127     }
128   }
129 }
130 
131 template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
132 _LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator1
133 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, _ForwardIterator2 __last2,
134          _BinaryPredicate __pred) {
135   return _VSTD::__find_end<_BinaryPredicate&>(
136       __first1, __last1, __first2, __last2, __pred, typename iterator_traits<_ForwardIterator1>::iterator_category(),
137       typename iterator_traits<_ForwardIterator2>::iterator_category());
138 }
139 
140 template <class _ForwardIterator1, class _ForwardIterator2>
141 _LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator1
142 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, _ForwardIterator2 __last2) {
143   typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
144   typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
145   return _VSTD::find_end(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
146 }
147 
148 _LIBCPP_END_NAMESPACE_STD
149 
150 #endif // _LIBCPP___ALGORITHM_FIND_END_OF_H
151