154 lines · c
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-exception7//8//===----------------------------------------------------------------------===//9 10#ifndef _LIBCPP___CXX03___ALGORITHM_SEARCH_N_H11#define _LIBCPP___CXX03___ALGORITHM_SEARCH_N_H12 13#include <__cxx03/__algorithm/comp.h>14#include <__cxx03/__algorithm/iterator_operations.h>15#include <__cxx03/__config>16#include <__cxx03/__functional/identity.h>17#include <__cxx03/__iterator/advance.h>18#include <__cxx03/__iterator/distance.h>19#include <__cxx03/__iterator/iterator_traits.h>20#include <__cxx03/__type_traits/invoke.h>21#include <__cxx03/__type_traits/is_callable.h>22#include <__cxx03/__utility/convert_to_integral.h>23#include <__cxx03/__utility/pair.h>24 25#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)26# pragma GCC system_header27#endif28 29_LIBCPP_BEGIN_NAMESPACE_STD30 31template <class _AlgPolicy, class _Pred, class _Iter, class _Sent, class _SizeT, class _Type, class _Proj>32_LIBCPP_HIDE_FROM_ABI pair<_Iter, _Iter> __search_n_forward_impl(33 _Iter __first, _Sent __last, _SizeT __count, const _Type& __value, _Pred& __pred, _Proj& __proj) {34 if (__count <= 0)35 return std::make_pair(__first, __first);36 while (true) {37 // Find first element in sequence that matchs __value, with a mininum of loop checks38 while (true) {39 if (__first == __last) { // return __last if no element matches __value40 _IterOps<_AlgPolicy>::__advance_to(__first, __last);41 return std::make_pair(__first, __first);42 }43 if (std::__invoke(__pred, std::__invoke(__proj, *__first), __value))44 break;45 ++__first;46 }47 // *__first matches __value, now match elements after here48 _Iter __m = __first;49 _SizeT __c(0);50 while (true) {51 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)52 return std::make_pair(__first, ++__m);53 if (++__m == __last) { // Otherwise if source exhaused, pattern not found54 _IterOps<_AlgPolicy>::__advance_to(__first, __last);55 return std::make_pair(__first, __first);56 }57 58 // if there is a mismatch, restart with a new __first59 if (!std::__invoke(__pred, std::__invoke(__proj, *__m), __value)) {60 __first = __m;61 ++__first;62 break;63 } // else there is a match, check next elements64 }65 }66}67 68template <class _AlgPolicy, class _Pred, class _Iter, class _Sent, class _SizeT, class _Type, class _Proj, class _DiffT>69_LIBCPP_HIDE_FROM_ABI std::pair<_Iter, _Iter> __search_n_random_access_impl(70 _Iter __first, _Sent __last, _SizeT __count, const _Type& __value, _Pred& __pred, _Proj& __proj, _DiffT __size1) {71 using difference_type = typename iterator_traits<_Iter>::difference_type;72 if (__count == 0)73 return std::make_pair(__first, __first);74 if (__size1 < static_cast<_DiffT>(__count)) {75 _IterOps<_AlgPolicy>::__advance_to(__first, __last);76 return std::make_pair(__first, __first);77 }78 79 const auto __s = __first + __size1 - difference_type(__count - 1); // Start of pattern match can't go beyond here80 while (true) {81 // Find first element in sequence that matchs __value, with a mininum of loop checks82 while (true) {83 if (__first >= __s) { // return __last if no element matches __value84 _IterOps<_AlgPolicy>::__advance_to(__first, __last);85 return std::make_pair(__first, __first);86 }87 if (std::__invoke(__pred, std::__invoke(__proj, *__first), __value))88 break;89 ++__first;90 }91 // *__first matches __value_, now match elements after here92 auto __m = __first;93 _SizeT __c(0);94 while (true) {95 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)96 return std::make_pair(__first, __first + _DiffT(__count));97 ++__m; // no need to check range on __m because __s guarantees we have enough source98 99 // if there is a mismatch, restart with a new __first100 if (!std::__invoke(__pred, std::__invoke(__proj, *__m), __value)) {101 __first = __m;102 ++__first;103 break;104 } // else there is a match, check next elements105 }106 }107}108 109template <class _Iter,110 class _Sent,111 class _DiffT,112 class _Type,113 class _Pred,114 class _Proj,115 __enable_if_t<__has_random_access_iterator_category<_Iter>::value, int> = 0>116_LIBCPP_HIDE_FROM_ABI pair<_Iter, _Iter>117__search_n_impl(_Iter __first, _Sent __last, _DiffT __count, const _Type& __value, _Pred& __pred, _Proj& __proj) {118 return std::__search_n_random_access_impl<_ClassicAlgPolicy>(119 __first, __last, __count, __value, __pred, __proj, __last - __first);120}121 122template <class _Iter1,123 class _Sent1,124 class _DiffT,125 class _Type,126 class _Pred,127 class _Proj,128 __enable_if_t<__has_forward_iterator_category<_Iter1>::value &&129 !__has_random_access_iterator_category<_Iter1>::value,130 int> = 0>131_LIBCPP_HIDE_FROM_ABI pair<_Iter1, _Iter1>132__search_n_impl(_Iter1 __first, _Sent1 __last, _DiffT __count, const _Type& __value, _Pred& __pred, _Proj& __proj) {133 return std::__search_n_forward_impl<_ClassicAlgPolicy>(__first, __last, __count, __value, __pred, __proj);134}135 136template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>137_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _ForwardIterator search_n(138 _ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value, _BinaryPredicate __pred) {139 static_assert(140 __is_callable<_BinaryPredicate, decltype(*__first), const _Tp&>::value, "BinaryPredicate has to be callable");141 auto __proj = __identity();142 return std::__search_n_impl(__first, __last, std::__convert_to_integral(__count), __value, __pred, __proj).first;143}144 145template <class _ForwardIterator, class _Size, class _Tp>146_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _ForwardIterator147search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value) {148 return std::search_n(__first, __last, std::__convert_to_integral(__count), __value, __equal_to());149}150 151_LIBCPP_END_NAMESPACE_STD152 153#endif // _LIBCPP___CXX03___ALGORITHM_SEARCH_N_H154