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 29 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 30 # pragma GCC system_header 31 #endif 32 33 #if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) 34 35 _LIBCPP_BEGIN_NAMESPACE_STD 36 37 namespace ranges { 38 namespace __search_n { 39 struct __fn { 40 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 = __search_n_random_access_impl<_RangeAlgPolicy>(__first, __last, 56 __count, 57 __value, 58 __pred, 59 __proj, 60 __size); 61 return {std::move(__ret.first), std::move(__ret.second)}; 62 } 63 } 64 65 auto __ret = std::__search_n_forward_impl<_RangeAlgPolicy>(__first, __last, 66 __count, 67 __value, 68 __pred, 69 __proj); 70 return {std::move(__ret.first), std::move(__ret.second)}; 71 } 72 73 template <forward_iterator _Iter, sentinel_for<_Iter> _Sent, 74 class _Type, 75 class _Pred = ranges::equal_to, 76 class _Proj = identity> 77 requires indirectly_comparable<_Iter, const _Type*, _Pred, _Proj> 78 _LIBCPP_HIDE_FROM_ABI constexpr 79 subrange<_Iter> operator()(_Iter __first, _Sent __last, 80 iter_difference_t<_Iter> __count, 81 const _Type& __value, 82 _Pred __pred = {}, 83 _Proj __proj = _Proj{}) const { 84 return __ranges_search_n_impl(__first, __last, __count, __value, __pred, __proj); 85 } 86 87 template <forward_range _Range, class _Type, class _Pred = ranges::equal_to, class _Proj = identity> 88 requires indirectly_comparable<iterator_t<_Range>, const _Type*, _Pred, _Proj> 89 _LIBCPP_HIDE_FROM_ABI constexpr 90 borrowed_subrange_t<_Range> operator()(_Range&& __range, 91 range_difference_t<_Range> __count, 92 const _Type& __value, 93 _Pred __pred = {}, 94 _Proj __proj = {}) const { 95 auto __first = ranges::begin(__range); 96 if (__count <= 0) 97 return {__first, __first}; 98 if constexpr (sized_range<_Range>) { 99 auto __size1 = ranges::size(__range); 100 if (__size1 < static_cast<range_size_t<_Range>>(__count)) { 101 ranges::advance(__first, ranges::end(__range)); 102 return {__first, __first}; 103 } 104 } 105 106 return __ranges_search_n_impl(ranges::begin(__range), ranges::end(__range), __count, __value, __pred, __proj); 107 } 108 }; 109 } // namespace __search_n 110 111 inline namespace __cpo { 112 inline constexpr auto search_n = __search_n::__fn{}; 113 } // namespace __cpo 114 } // namespace ranges 115 116 _LIBCPP_END_NAMESPACE_STD 117 118 #endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) 119 120 #endif // _LIBCPP___ALGORITHM_RANGES_SEARCH_N_H 121