| 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 |
_LIBCPP_PUSH_MACROS |
| 35 |
#include <__undef_macros> |
| 36 |
|
| 37 |
#if _LIBCPP_STD_VER >= 20 |
| 38 |
|
| 39 |
_LIBCPP_BEGIN_NAMESPACE_STD |
| 40 |
|
| 41 |
namespace ranges { |
| 42 |
namespace __search_n { |
| 43 |
struct __fn { |
| 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) { |
| 47 |
if (__count == 0) |
| 48 |
return {__first, __first}; |
| 49 |
|
| 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}; |
| 55 |
} |
| 56 |
|
| 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)}; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 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)}; |
| 66 |
} |
| 67 |
|
| 68 |
template <forward_iterator _Iter, |
| 69 |
sentinel_for<_Iter> _Sent, |
| 70 |
class _Type, |
| 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, |
| 76 |
_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 |
[[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 = {}) |
| 88 |
const { |
| 89 |
auto __first = ranges::begin(__range); |
| 90 |
if (__count <= 0) |
| 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}; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
return __ranges_search_n_impl(ranges::begin(__range), ranges::end(__range), __count, __value, __pred, __proj); |
| 101 |
} |
| 102 |
}; |
| 103 |
} // namespace __search_n |
| 104 |
|
| 105 |
inline namespace __cpo { |
| 106 |
inline constexpr auto search_n = __search_n::__fn{}; |
| 107 |
} // namespace __cpo |
| 108 |
} // namespace ranges |
| 109 |
|
| 110 |
_LIBCPP_END_NAMESPACE_STD |
| 111 |
|
| 112 |
#endif // _LIBCPP_STD_VER >= 20 |
| 113 |
|
| 114 |
_LIBCPP_POP_MACROS |
| 115 |
|
| 116 |
#endif // _LIBCPP___ALGORITHM_RANGES_SEARCH_N_H |