1 //===----------------------------------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef _LIBCPP___ALGORITHM_RANGES_SEARCH_N_H
10 #define _LIBCPP___ALGORITHM_RANGES_SEARCH_N_H
11 
12 #include <__algorithm/iterator_operations.h>
13 #include <__algorithm/search_n.h>
14 #include <__config>
15 #include <__functional/identity.h>
16 #include <__functional/ranges_operations.h>
17 #include <__iterator/advance.h>
18 #include <__iterator/concepts.h>
19 #include <__iterator/distance.h>
20 #include <__iterator/incrementable_traits.h>
21 #include <__iterator/indirectly_comparable.h>
22 #include <__iterator/iterator_traits.h>
23 #include <__ranges/access.h>
24 #include <__ranges/concepts.h>
25 #include <__ranges/size.h>
26 #include <__ranges/subrange.h>
27 #include <__utility/move.h>
28 #include <__utility/pair.h>
29 
30 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
31 #  pragma GCC system_header
32 #endif
33 
34 #if _LIBCPP_STD_VER >= 20
35 
36 _LIBCPP_BEGIN_NAMESPACE_STD
37 
38 namespace ranges {
39 namespace __search_n {
40 struct __fn {
41   template <class _Iter1, class _Sent1, class _SizeT, class _Type, class _Pred, class _Proj>
42   _LIBCPP_HIDE_FROM_ABI static constexpr subrange<_Iter1> __ranges_search_n_impl(
43       _Iter1 __first, _Sent1 __last, _SizeT __count, const _Type& __value, _Pred& __pred, _Proj& __proj) {
44     if (__count == 0)
45       return {__first, __first};
46 
47     if constexpr (sized_sentinel_for<_Sent1, _Iter1>) {
48       auto __size = ranges::distance(__first, __last);
49       if (__size < __count) {
50         ranges::advance(__first, __last);
51         return {__first, __first};
52       }
53 
54       if constexpr (random_access_iterator<_Iter1>) {
55         auto __ret = std::__search_n_random_access_impl<_RangeAlgPolicy>(
56             __first, __last, __count, __value, __pred, __proj, __size);
57         return {std::move(__ret.first), std::move(__ret.second)};
58       }
59     }
60 
61     auto __ret = std::__search_n_forward_impl<_RangeAlgPolicy>(__first, __last, __count, __value, __pred, __proj);
62     return {std::move(__ret.first), std::move(__ret.second)};
63   }
64 
65   template <forward_iterator _Iter,
66             sentinel_for<_Iter> _Sent,
67             class _Type,
68             class _Pred = ranges::equal_to,
69             class _Proj = identity>
70     requires indirectly_comparable<_Iter, const _Type*, _Pred, _Proj>
71   _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr subrange<_Iter>
72   operator()(_Iter __first,
73              _Sent __last,
74              iter_difference_t<_Iter> __count,
75              const _Type& __value,
76              _Pred __pred = {},
77              _Proj __proj = _Proj{}) const {
78     return __ranges_search_n_impl(__first, __last, __count, __value, __pred, __proj);
79   }
80 
81   template <forward_range _Range, class _Type, class _Pred = ranges::equal_to, class _Proj = identity>
82     requires indirectly_comparable<iterator_t<_Range>, const _Type*, _Pred, _Proj>
83   _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr borrowed_subrange_t<_Range> operator()(
84       _Range&& __range, range_difference_t<_Range> __count, const _Type& __value, _Pred __pred = {}, _Proj __proj = {})
85       const {
86     auto __first = ranges::begin(__range);
87     if (__count <= 0)
88       return {__first, __first};
89     if constexpr (sized_range<_Range>) {
90       auto __size1 = ranges::size(__range);
91       if (__size1 < static_cast<range_size_t<_Range>>(__count)) {
92         ranges::advance(__first, ranges::end(__range));
93         return {__first, __first};
94       }
95     }
96 
97     return __ranges_search_n_impl(ranges::begin(__range), ranges::end(__range), __count, __value, __pred, __proj);
98   }
99 };
100 } // namespace __search_n
101 
102 inline namespace __cpo {
103 inline constexpr auto search_n = __search_n::__fn{};
104 } // namespace __cpo
105 } // namespace ranges
106 
107 _LIBCPP_END_NAMESPACE_STD
108 
109 #endif // _LIBCPP_STD_VER >= 20
110 
111 #endif // _LIBCPP___ALGORITHM_RANGES_SEARCH_N_H
112