File indexing completed on 2026-05-03 08:13:21
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef _LIBCPP___CXX03___ALGORITHM_SEARCH_N_H
0011 #define _LIBCPP___CXX03___ALGORITHM_SEARCH_N_H
0012
0013 #include <__cxx03/__algorithm/comp.h>
0014 #include <__cxx03/__algorithm/iterator_operations.h>
0015 #include <__cxx03/__config>
0016 #include <__cxx03/__functional/identity.h>
0017 #include <__cxx03/__functional/invoke.h>
0018 #include <__cxx03/__iterator/advance.h>
0019 #include <__cxx03/__iterator/concepts.h>
0020 #include <__cxx03/__iterator/distance.h>
0021 #include <__cxx03/__iterator/iterator_traits.h>
0022 #include <__cxx03/__ranges/concepts.h>
0023 #include <__cxx03/__type_traits/is_callable.h>
0024 #include <__cxx03/__utility/convert_to_integral.h>
0025 #include <__cxx03/__utility/pair.h>
0026
0027 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0028 # pragma GCC system_header
0029 #endif
0030
0031 _LIBCPP_BEGIN_NAMESPACE_STD
0032
0033 template <class _AlgPolicy, class _Pred, class _Iter, class _Sent, class _SizeT, class _Type, class _Proj>
0034 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter, _Iter> __search_n_forward_impl(
0035 _Iter __first, _Sent __last, _SizeT __count, const _Type& __value, _Pred& __pred, _Proj& __proj) {
0036 if (__count <= 0)
0037 return std::make_pair(__first, __first);
0038 while (true) {
0039
0040 while (true) {
0041 if (__first == __last) {
0042 _IterOps<_AlgPolicy>::__advance_to(__first, __last);
0043 return std::make_pair(__first, __first);
0044 }
0045 if (std::__invoke(__pred, std::__invoke(__proj, *__first), __value))
0046 break;
0047 ++__first;
0048 }
0049
0050 _Iter __m = __first;
0051 _SizeT __c(0);
0052 while (true) {
0053 if (++__c == __count)
0054 return std::make_pair(__first, ++__m);
0055 if (++__m == __last) {
0056 _IterOps<_AlgPolicy>::__advance_to(__first, __last);
0057 return std::make_pair(__first, __first);
0058 }
0059
0060
0061 if (!std::__invoke(__pred, std::__invoke(__proj, *__m), __value)) {
0062 __first = __m;
0063 ++__first;
0064 break;
0065 }
0066 }
0067 }
0068 }
0069
0070 template <class _AlgPolicy, class _Pred, class _Iter, class _Sent, class _SizeT, class _Type, class _Proj, class _DiffT>
0071 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 std::pair<_Iter, _Iter> __search_n_random_access_impl(
0072 _Iter __first, _Sent __last, _SizeT __count, const _Type& __value, _Pred& __pred, _Proj& __proj, _DiffT __size1) {
0073 using difference_type = typename iterator_traits<_Iter>::difference_type;
0074 if (__count == 0)
0075 return std::make_pair(__first, __first);
0076 if (__size1 < static_cast<_DiffT>(__count)) {
0077 _IterOps<_AlgPolicy>::__advance_to(__first, __last);
0078 return std::make_pair(__first, __first);
0079 }
0080
0081 const auto __s = __first + __size1 - difference_type(__count - 1);
0082 while (true) {
0083
0084 while (true) {
0085 if (__first >= __s) {
0086 _IterOps<_AlgPolicy>::__advance_to(__first, __last);
0087 return std::make_pair(__first, __first);
0088 }
0089 if (std::__invoke(__pred, std::__invoke(__proj, *__first), __value))
0090 break;
0091 ++__first;
0092 }
0093
0094 auto __m = __first;
0095 _SizeT __c(0);
0096 while (true) {
0097 if (++__c == __count)
0098 return std::make_pair(__first, __first + _DiffT(__count));
0099 ++__m;
0100
0101
0102 if (!std::__invoke(__pred, std::__invoke(__proj, *__m), __value)) {
0103 __first = __m;
0104 ++__first;
0105 break;
0106 }
0107 }
0108 }
0109 }
0110
0111 template <class _Iter,
0112 class _Sent,
0113 class _DiffT,
0114 class _Type,
0115 class _Pred,
0116 class _Proj,
0117 __enable_if_t<__has_random_access_iterator_category<_Iter>::value, int> = 0>
0118 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter, _Iter>
0119 __search_n_impl(_Iter __first, _Sent __last, _DiffT __count, const _Type& __value, _Pred& __pred, _Proj& __proj) {
0120 return std::__search_n_random_access_impl<_ClassicAlgPolicy>(
0121 __first, __last, __count, __value, __pred, __proj, __last - __first);
0122 }
0123
0124 template <class _Iter1,
0125 class _Sent1,
0126 class _DiffT,
0127 class _Type,
0128 class _Pred,
0129 class _Proj,
0130 __enable_if_t<__has_forward_iterator_category<_Iter1>::value &&
0131 !__has_random_access_iterator_category<_Iter1>::value,
0132 int> = 0>
0133 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter1, _Iter1>
0134 __search_n_impl(_Iter1 __first, _Sent1 __last, _DiffT __count, const _Type& __value, _Pred& __pred, _Proj& __proj) {
0135 return std::__search_n_forward_impl<_ClassicAlgPolicy>(__first, __last, __count, __value, __pred, __proj);
0136 }
0137
0138 template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
0139 _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator search_n(
0140 _ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value, _BinaryPredicate __pred) {
0141 static_assert(
0142 __is_callable<_BinaryPredicate, decltype(*__first), const _Tp&>::value, "BinaryPredicate has to be callable");
0143 auto __proj = __identity();
0144 return std::__search_n_impl(__first, __last, std::__convert_to_integral(__count), __value, __pred, __proj).first;
0145 }
0146
0147 template <class _ForwardIterator, class _Size, class _Tp>
0148 _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
0149 search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value) {
0150 return std::search_n(__first, __last, std::__convert_to_integral(__count), __value, __equal_to());
0151 }
0152
0153 _LIBCPP_END_NAMESPACE_STD
0154
0155 #endif