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