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