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 struct __search_n { 43 template <class _Iter1, class _Sent1, class _SizeT, class _Type, class _Pred, class _Proj> 44 _LIBCPP_HIDE_FROM_ABI static constexpr subrange<_Iter1> __ranges_search_n_impl( 45 _Iter1 __first, _Sent1 __last, _SizeT __count, const _Type& __value, _Pred& __pred, _Proj& __proj) { 46 if (__count == 0) 47 return {__first, __first}; 48 49 if constexpr (sized_sentinel_for<_Sent1, _Iter1>) { 50 auto __size = ranges::distance(__first, __last); 51 if (__size < __count) { 52 ranges::advance(__first, __last); 53 return {__first, __first}; 54 } 55 56 if constexpr (random_access_iterator<_Iter1>) { 57 auto __ret = std::__search_n_random_access_impl<_RangeAlgPolicy>( 58 __first, __last, __count, __value, __pred, __proj, __size); 59 return {std::move(__ret.first), std::move(__ret.second)}; 60 } 61 } 62 63 auto __ret = std::__search_n_forward_impl<_RangeAlgPolicy>(__first, __last, __count, __value, __pred, __proj); 64 return {std::move(__ret.first), std::move(__ret.second)}; 65 } 66 67 template <forward_iterator _Iter, 68 sentinel_for<_Iter> _Sent, 69 class _Type, 70 class _Pred = ranges::equal_to, 71 class _Proj = identity> 72 requires indirectly_comparable<_Iter, const _Type*, _Pred, _Proj> 73 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr subrange<_Iter> 74 operator()(_Iter __first, 75 _Sent __last, 76 iter_difference_t<_Iter> __count, 77 const _Type& __value, 78 _Pred __pred = {}, 79 _Proj __proj = _Proj{}) const { 80 return __ranges_search_n_impl(__first, __last, __count, __value, __pred, __proj); 81 } 82 83 template <forward_range _Range, class _Type, class _Pred = ranges::equal_to, class _Proj = identity> 84 requires indirectly_comparable<iterator_t<_Range>, const _Type*, _Pred, _Proj> 85 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr borrowed_subrange_t<_Range> operator()( 86 _Range&& __range, range_difference_t<_Range> __count, const _Type& __value, _Pred __pred = {}, _Proj __proj = {}) 87 const { 88 auto __first = ranges::begin(__range); 89 if (__count <= 0) 90 return {__first, __first}; 91 if constexpr (sized_range<_Range>) { 92 auto __size1 = ranges::size(__range); 93 if (__size1 < static_cast<range_size_t<_Range>>(__count)) { 94 ranges::advance(__first, ranges::end(__range)); 95 return {__first, __first}; 96 } 97 } 98 99 return __ranges_search_n_impl(ranges::begin(__range), ranges::end(__range), __count, __value, __pred, __proj); 100 } 101 }; 102 103 inline namespace __cpo { 104 inline constexpr auto search_n = __search_n{}; 105 } // namespace __cpo 106 } // namespace ranges 107 108 _LIBCPP_END_NAMESPACE_STD 109 110 #endif // _LIBCPP_STD_VER >= 20 111 112 _LIBCPP_POP_MACROS 113 114 #endif // _LIBCPP___ALGORITHM_RANGES_SEARCH_N_H 115