1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___ALGORITHM_RANGES_SEARCH_N_H
10 #define _LIBCPP___ALGORITHM_RANGES_SEARCH_N_H
12 #include <__cxx03/__algorithm/iterator_operations.h>
13 #include <__cxx03/__algorithm/search_n.h>
14 #include <__cxx03/__config>
15 #include <__cxx03/__functional/identity.h>
16 #include <__cxx03/__functional/ranges_operations.h>
17 #include <__cxx03/__iterator/advance.h>
18 #include <__cxx03/__iterator/concepts.h>
19 #include <__cxx03/__iterator/distance.h>
20 #include <__cxx03/__iterator/incrementable_traits.h>
21 #include <__cxx03/__iterator/indirectly_comparable.h>
22 #include <__cxx03/__iterator/iterator_traits.h>
23 #include <__cxx03/__ranges/access.h>
24 #include <__cxx03/__ranges/concepts.h>
25 #include <__cxx03/__ranges/size.h>
26 #include <__cxx03/__ranges/subrange.h>
27 #include <__cxx03/__utility/move.h>
28 #include <__cxx03/__utility/pair.h>
30 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
31 # pragma GCC system_header
35 #include <__cxx03/__undef_macros>
37 #if _LIBCPP_STD_VER >= 20
39 _LIBCPP_BEGIN_NAMESPACE_STD
42 namespace __search_n
{
44 template <class _Iter1
, class _Sent1
, class _SizeT
, class _Type
, class _Pred
, class _Proj
>
45 _LIBCPP_HIDE_FROM_ABI
static constexpr subrange
<_Iter1
> __ranges_search_n_impl(
46 _Iter1 __first
, _Sent1 __last
, _SizeT __count
, const _Type
& __value
, _Pred
& __pred
, _Proj
& __proj
) {
48 return {__first
, __first
};
50 if constexpr (sized_sentinel_for
<_Sent1
, _Iter1
>) {
51 auto __size
= ranges::distance(__first
, __last
);
52 if (__size
< __count
) {
53 ranges::advance(__first
, __last
);
54 return {__first
, __first
};
57 if constexpr (random_access_iterator
<_Iter1
>) {
58 auto __ret
= std::__search_n_random_access_impl
<_RangeAlgPolicy
>(
59 __first
, __last
, __count
, __value
, __pred
, __proj
, __size
);
60 return {std::move(__ret
.first
), std::move(__ret
.second
)};
64 auto __ret
= std::__search_n_forward_impl
<_RangeAlgPolicy
>(__first
, __last
, __count
, __value
, __pred
, __proj
);
65 return {std::move(__ret
.first
), std::move(__ret
.second
)};
68 template <forward_iterator _Iter
,
69 sentinel_for
<_Iter
> _Sent
,
71 class _Pred
= ranges::equal_to
,
72 class _Proj
= identity
>
73 requires indirectly_comparable
<_Iter
, const _Type
*, _Pred
, _Proj
>
74 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI
constexpr subrange
<_Iter
>
75 operator()(_Iter __first
,
77 iter_difference_t
<_Iter
> __count
,
80 _Proj __proj
= _Proj
{}) const {
81 return __ranges_search_n_impl(__first
, __last
, __count
, __value
, __pred
, __proj
);
84 template <forward_range _Range
, class _Type
, class _Pred
= ranges::equal_to
, class _Proj
= identity
>
85 requires indirectly_comparable
<iterator_t
<_Range
>, const _Type
*, _Pred
, _Proj
>
86 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI
constexpr borrowed_subrange_t
<_Range
> operator()(
87 _Range
&& __range
, range_difference_t
<_Range
> __count
, const _Type
& __value
, _Pred __pred
= {}, _Proj __proj
= {})
89 auto __first
= ranges::begin(__range
);
91 return {__first
, __first
};
92 if constexpr (sized_range
<_Range
>) {
93 auto __size1
= ranges::size(__range
);
94 if (__size1
< static_cast<range_size_t
<_Range
>>(__count
)) {
95 ranges::advance(__first
, ranges::end(__range
));
96 return {__first
, __first
};
100 return __ranges_search_n_impl(ranges::begin(__range
), ranges::end(__range
), __count
, __value
, __pred
, __proj
);
103 } // namespace __search_n
105 inline namespace __cpo
{
106 inline constexpr auto search_n
= __search_n::__fn
{};
108 } // namespace ranges
110 _LIBCPP_END_NAMESPACE_STD
112 #endif // _LIBCPP_STD_VER >= 20
116 #endif // _LIBCPP___ALGORITHM_RANGES_SEARCH_N_H