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_SEARCH_H 11 #define _LIBCPP___ALGORITHM_SEARCH_H 12 13 #include <__algorithm/comp.h> 14 #include <__config> 15 #include <__iterator/iterator_traits.h> 16 #include <utility> 17 18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 19 #pragma GCC system_header 20 #endif 21 22 _LIBCPP_BEGIN_NAMESPACE_STD 23 24 template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2> 25 pair<_ForwardIterator1, _ForwardIterator1> 26 _LIBCPP_CONSTEXPR_AFTER_CXX11 __search(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 27 _ForwardIterator2 __first2, _ForwardIterator2 __last2, 28 _BinaryPredicate __pred, forward_iterator_tag, forward_iterator_tag) { 29 if (__first2 == __last2) 30 return _VSTD::make_pair(__first1, __first1); // Everything matches an empty sequence 31 while (true) { 32 // Find first element in sequence 1 that matchs *__first2, with a mininum of loop checks 33 while (true) { 34 if (__first1 == __last1) // return __last1 if no element matches *__first2 35 return _VSTD::make_pair(__last1, __last1); 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) // If pattern exhausted, __first1 is the answer (works for 1 element pattern) 45 return _VSTD::make_pair(__first1, __m1); 46 if (++__m1 == __last1) // Otherwise if source exhaused, pattern not found 47 return _VSTD::make_pair(__last1, __last1); 48 if (!__pred(*__m1, *__m2)) // if there is a mismatch, restart with a new __first1 49 { 50 ++__first1; 51 break; 52 } // else there is a match, check next elements 53 } 54 } 55 } 56 57 template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2> 58 _LIBCPP_CONSTEXPR_AFTER_CXX11 pair<_RandomAccessIterator1, _RandomAccessIterator1> 59 __search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, _RandomAccessIterator2 __first2, 60 _RandomAccessIterator2 __last2, _BinaryPredicate __pred, random_access_iterator_tag, 61 random_access_iterator_tag) { 62 typedef typename iterator_traits<_RandomAccessIterator1>::difference_type _D1; 63 typedef typename iterator_traits<_RandomAccessIterator2>::difference_type _D2; 64 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern 65 const _D2 __len2 = __last2 - __first2; 66 if (__len2 == 0) 67 return _VSTD::make_pair(__first1, __first1); 68 const _D1 __len1 = __last1 - __first1; 69 if (__len1 < __len2) 70 return _VSTD::make_pair(__last1, __last1); 71 const _RandomAccessIterator1 __s = __last1 - _D1(__len2 - 1); // Start of pattern match can't go beyond here 72 73 while (true) { 74 while (true) { 75 if (__first1 == __s) 76 return _VSTD::make_pair(__last1, __last1); 77 if (__pred(*__first1, *__first2)) 78 break; 79 ++__first1; 80 } 81 82 _RandomAccessIterator1 __m1 = __first1; 83 _RandomAccessIterator2 __m2 = __first2; 84 while (true) { 85 if (++__m2 == __last2) 86 return _VSTD::make_pair(__first1, __first1 + _D1(__len2)); 87 ++__m1; // no need to check range on __m1 because __s guarantees we have enough source 88 if (!__pred(*__m1, *__m2)) { 89 ++__first1; 90 break; 91 } 92 } 93 } 94 } 95 96 template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> 97 _LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator1 98 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, _ForwardIterator2 __last2, 99 _BinaryPredicate __pred) { 100 return _VSTD::__search<_BinaryPredicate&>( 101 __first1, __last1, __first2, __last2, __pred, 102 typename iterator_traits<_ForwardIterator1>::iterator_category(), 103 typename iterator_traits<_ForwardIterator2>::iterator_category()).first; 104 } 105 106 template <class _ForwardIterator1, class _ForwardIterator2> 107 _LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator1 108 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, _ForwardIterator2 __last2) { 109 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1; 110 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2; 111 return _VSTD::search(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>()); 112 } 113 114 #if _LIBCPP_STD_VER > 14 115 template <class _ForwardIterator, class _Searcher> 116 _LIBCPP_NODISCARD_EXT _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator 117 search(_ForwardIterator __f, _ForwardIterator __l, const _Searcher& __s) { 118 return __s(__f, __l).first; 119 } 120 121 #endif 122 123 _LIBCPP_END_NAMESPACE_STD 124 125 #endif // _LIBCPP___ALGORITHM_SEARCH_H 126