| 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 <__algorithm/iterator_operations.h> |
| 15 | #include <__config> |
| 16 | #include <__functional/identity.h> |
| 17 | #include <__iterator/iterator_traits.h> |
| 18 | #include <__type_traits/invoke.h> |
| 19 | #include <__utility/pair.h> |
| 20 | |
| 21 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 22 | # pragma GCC system_header |
| 23 | #endif |
| 24 | |
| 25 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 26 | |
| 27 | template < class _AlgPolicy, |
| 28 | class _Iter1, |
| 29 | class _Sent1, |
| 30 | class _Iter2, |
| 31 | class _Sent2, |
| 32 | class _Pred, |
| 33 | class _Proj1, |
| 34 | class _Proj2> |
| 35 | _LIBCPP_HIDE_FROM_ABI inline _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter1, _Iter1> __find_end_impl( |
| 36 | _Iter1 __first1, |
| 37 | _Sent1 __last1, |
| 38 | _Iter2 __first2, |
| 39 | _Sent2 __last2, |
| 40 | _Pred& __pred, |
| 41 | _Proj1& __proj1, |
| 42 | _Proj2& __proj2, |
| 43 | forward_iterator_tag, |
| 44 | forward_iterator_tag) { |
| 45 | // modeled after search algorithm |
| 46 | _Iter1 __match_first = _IterOps<_AlgPolicy>::next(__first1, __last1); // __last1 is the "default" answer |
| 47 | _Iter1 __match_last = __match_first; |
| 48 | if (__first2 == __last2) |
| 49 | return pair<_Iter1, _Iter1>(__match_last, __match_last); |
| 50 | while (true) { |
| 51 | while (true) { |
| 52 | if (__first1 == __last1) // if source exhausted return last correct answer (or __last1 if never found) |
| 53 | return pair<_Iter1, _Iter1>(__match_first, __match_last); |
| 54 | if (std::__invoke(__pred, std::__invoke(__proj1, *__first1), std::__invoke(__proj2, *__first2))) |
| 55 | break; |
| 56 | ++__first1; |
| 57 | } |
| 58 | // *__first1 matches *__first2, now match elements after here |
| 59 | _Iter1 __m1 = __first1; |
| 60 | _Iter2 __m2 = __first2; |
| 61 | while (true) { |
| 62 | if (++__m2 == __last2) { // Pattern exhaused, record answer and search for another one |
| 63 | __match_first = __first1; |
| 64 | __match_last = ++__m1; |
| 65 | ++__first1; |
| 66 | break; |
| 67 | } |
| 68 | if (++__m1 == __last1) // Source exhausted, return last answer |
| 69 | return pair<_Iter1, _Iter1>(__match_first, __match_last); |
| 70 | // mismatch, restart with a new __first |
| 71 | if (!std::__invoke(__pred, std::__invoke(__proj1, *__m1), std::__invoke(__proj2, *__m2))) { |
| 72 | ++__first1; |
| 73 | break; |
| 74 | } // else there is a match, check next elements |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | template <class _AlgPolicy, |
| 80 | class _Pred, |
| 81 | class _Iter1, |
| 82 | class _Sent1, |
| 83 | class _Iter2, |
| 84 | class _Sent2, |
| 85 | class _Proj1, |
| 86 | class _Proj2> |
| 87 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_Iter1, _Iter1> __find_end_impl( |
| 88 | _Iter1 __first1, |
| 89 | _Sent1 __sent1, |
| 90 | _Iter2 __first2, |
| 91 | _Sent2 __sent2, |
| 92 | _Pred& __pred, |
| 93 | _Proj1& __proj1, |
| 94 | _Proj2& __proj2, |
| 95 | bidirectional_iterator_tag, |
| 96 | bidirectional_iterator_tag) { |
| 97 | auto __last1 = _IterOps<_AlgPolicy>::next(__first1, __sent1); |
| 98 | auto __last2 = _IterOps<_AlgPolicy>::next(__first2, __sent2); |
| 99 | // modeled after search algorithm (in reverse) |
| 100 | if (__first2 == __last2) |
| 101 | return std::make_pair(__last1, __last1); // Everything matches an empty sequence |
| 102 | _Iter1 __l1 = __last1; |
| 103 | _Iter2 __l2 = __last2; |
| 104 | --__l2; |
| 105 | while (true) { |
| 106 | // Find last element in sequence 1 that matches *(__last2-1), with a mininum of loop checks |
| 107 | while (true) { |
| 108 | if (__first1 == __l1) // return __last1 if no element matches *__first2 |
| 109 | return std::make_pair(__last1, __last1); |
| 110 | if (std::__invoke(__pred, std::__invoke(__proj1, *--__l1), std::__invoke(__proj2, *__l2))) |
| 111 | break; |
| 112 | } |
| 113 | // *__l1 matches *__l2, now match elements before here |
| 114 | _Iter1 __match_last = __l1; |
| 115 | _Iter1 __m1 = __l1; |
| 116 | _Iter2 __m2 = __l2; |
| 117 | while (true) { |
| 118 | if (__m2 == __first2) // If pattern exhausted, __m1 is the answer (works for 1 element pattern) |
| 119 | return std::make_pair(__m1, ++__match_last); |
| 120 | if (__m1 == __first1) // Otherwise if source exhaused, pattern not found |
| 121 | return std::make_pair(__last1, __last1); |
| 122 | |
| 123 | // if there is a mismatch, restart with a new __l1 |
| 124 | if (!std::__invoke(__pred, std::__invoke(__proj1, *--__m1), std::__invoke(__proj2, *--__m2))) { |
| 125 | break; |
| 126 | } // else there is a match, check next elements |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | template <class _AlgPolicy, |
| 132 | class _Pred, |
| 133 | class _Iter1, |
| 134 | class _Sent1, |
| 135 | class _Iter2, |
| 136 | class _Sent2, |
| 137 | class _Proj1, |
| 138 | class _Proj2> |
| 139 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter1, _Iter1> __find_end_impl( |
| 140 | _Iter1 __first1, |
| 141 | _Sent1 __sent1, |
| 142 | _Iter2 __first2, |
| 143 | _Sent2 __sent2, |
| 144 | _Pred& __pred, |
| 145 | _Proj1& __proj1, |
| 146 | _Proj2& __proj2, |
| 147 | random_access_iterator_tag, |
| 148 | random_access_iterator_tag) { |
| 149 | typedef typename iterator_traits<_Iter1>::difference_type _D1; |
| 150 | auto __last1 = _IterOps<_AlgPolicy>::next(__first1, __sent1); |
| 151 | auto __last2 = _IterOps<_AlgPolicy>::next(__first2, __sent2); |
| 152 | // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern |
| 153 | auto __len2 = __last2 - __first2; |
| 154 | if (__len2 == 0) |
| 155 | return std::make_pair(__last1, __last1); |
| 156 | auto __len1 = __last1 - __first1; |
| 157 | if (__len1 < __len2) |
| 158 | return std::make_pair(__last1, __last1); |
| 159 | const _Iter1 __s = __first1 + _D1(__len2 - 1); // End of pattern match can't go before here |
| 160 | _Iter1 __l1 = __last1; |
| 161 | _Iter2 __l2 = __last2; |
| 162 | --__l2; |
| 163 | while (true) { |
| 164 | while (true) { |
| 165 | if (__s == __l1) |
| 166 | return std::make_pair(__last1, __last1); |
| 167 | if (std::__invoke(__pred, std::__invoke(__proj1, *--__l1), std::__invoke(__proj2, *__l2))) |
| 168 | break; |
| 169 | } |
| 170 | _Iter1 __last_match = __l1; |
| 171 | _Iter1 __m1 = __l1; |
| 172 | _Iter2 __m2 = __l2; |
| 173 | while (true) { |
| 174 | if (__m2 == __first2) |
| 175 | return std::make_pair(__m1, ++__last_match); |
| 176 | // no need to check range on __m1 because __s guarantees we have enough source |
| 177 | if (!std::__invoke(__pred, std::__invoke(__proj1, *--__m1), std::__invoke(__proj2, *--__m2))) { |
| 178 | break; |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> |
| 185 | [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _ForwardIterator1 __find_end_classic( |
| 186 | _ForwardIterator1 __first1, |
| 187 | _ForwardIterator1 __last1, |
| 188 | _ForwardIterator2 __first2, |
| 189 | _ForwardIterator2 __last2, |
| 190 | _BinaryPredicate& __pred) { |
| 191 | auto __proj = __identity(); |
| 192 | return std::__find_end_impl<_ClassicAlgPolicy>( |
| 193 | __first1, |
| 194 | __last1, |
| 195 | __first2, |
| 196 | __last2, |
| 197 | __pred, |
| 198 | __proj, |
| 199 | __proj, |
| 200 | typename iterator_traits<_ForwardIterator1>::iterator_category(), |
| 201 | typename iterator_traits<_ForwardIterator2>::iterator_category()) |
| 202 | .first; |
| 203 | } |
| 204 | |
| 205 | template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> |
| 206 | [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator1 find_end( |
| 207 | _ForwardIterator1 __first1, |
| 208 | _ForwardIterator1 __last1, |
| 209 | _ForwardIterator2 __first2, |
| 210 | _ForwardIterator2 __last2, |
| 211 | _BinaryPredicate __pred) { |
| 212 | return std::__find_end_classic(__first1, __last1, __first2, __last2, __pred); |
| 213 | } |
| 214 | |
| 215 | template <class _ForwardIterator1, class _ForwardIterator2> |
| 216 | [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator1 |
| 217 | find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, _ForwardIterator2 __last2) { |
| 218 | return std::find_end(__first1, __last1, __first2, __last2, __equal_to()); |
| 219 | } |
| 220 | |
| 221 | _LIBCPP_END_NAMESPACE_STD |
| 222 | |
| 223 | #endif // _LIBCPP___ALGORITHM_FIND_END_OF_H |
| 224 | |