xref: /openbsd-src/gnu/llvm/libcxx/include/__algorithm/ranges_search_n.h (revision 4bdff4bed0e3d54e55670334c7d0077db4170f86)
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 > 17
35 
36 _LIBCPP_BEGIN_NAMESPACE_STD
37 
38 namespace ranges {
39 namespace __search_n {
40 struct __fn {
41 
42   template <class _Iter1, class _Sent1, class _SizeT, class _Type, class _Pred, class _Proj>
__ranges_search_n_impl__fn43   _LIBCPP_HIDE_FROM_ABI static constexpr subrange<_Iter1> __ranges_search_n_impl(
44       _Iter1 __first, _Sent1 __last, _SizeT __count, const _Type& __value, _Pred& __pred, _Proj& __proj) {
45     if (__count == 0)
46       return {__first, __first};
47 
48     if constexpr (sized_sentinel_for<_Sent1, _Iter1>) {
49       auto __size = ranges::distance(__first, __last);
50       if (__size < __count) {
51         ranges::advance(__first, __last);
52         return {__first, __first};
53       }
54 
55       if constexpr (random_access_iterator<_Iter1>) {
56         auto __ret = std::__search_n_random_access_impl<_RangeAlgPolicy>(
57             __first, __last, __count, __value, __pred, __proj, __size);
58         return {std::move(__ret.first), std::move(__ret.second)};
59       }
60     }
61 
62     auto __ret = std::__search_n_forward_impl<_RangeAlgPolicy>(__first, __last,
63                                                                __count,
64                                                                __value,
65                                                                __pred,
66                                                                __proj);
67     return {std::move(__ret.first), std::move(__ret.second)};
68   }
69 
70   template <forward_iterator _Iter, sentinel_for<_Iter> _Sent,
71             class _Type,
72             class _Pred = ranges::equal_to,
73             class _Proj = identity>
74     requires indirectly_comparable<_Iter, const _Type*, _Pred, _Proj>
75   _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr
operator__fn76   subrange<_Iter> operator()(_Iter __first, _Sent __last,
77                              iter_difference_t<_Iter> __count,
78                              const _Type& __value,
79                              _Pred __pred = {},
80                              _Proj __proj = _Proj{}) const {
81     return __ranges_search_n_impl(__first, __last, __count, __value, __pred, __proj);
82   }
83 
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   _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr
operator__fn87   borrowed_subrange_t<_Range> operator()(_Range&& __range,
88                                          range_difference_t<_Range> __count,
89                                          const _Type& __value,
90                                          _Pred __pred = {},
91                                          _Proj __proj = {}) const {
92     auto __first = ranges::begin(__range);
93     if (__count <= 0)
94       return {__first, __first};
95     if constexpr (sized_range<_Range>) {
96       auto __size1 = ranges::size(__range);
97       if (__size1 < static_cast<range_size_t<_Range>>(__count)) {
98         ranges::advance(__first, ranges::end(__range));
99         return {__first, __first};
100       }
101     }
102 
103     return __ranges_search_n_impl(ranges::begin(__range), ranges::end(__range), __count, __value, __pred, __proj);
104   }
105 };
106 } // namespace __search_n
107 
108 inline namespace __cpo {
109   inline constexpr auto search_n = __search_n::__fn{};
110 } // namespace __cpo
111 } // namespace ranges
112 
113 _LIBCPP_END_NAMESPACE_STD
114 
115 #endif // _LIBCPP_STD_VER > 17
116 
117 #endif // _LIBCPP___ALGORITHM_RANGES_SEARCH_N_H
118