File indexing completed on 2026-05-03 08:13:21
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef _LIBCPP___CXX03___ALGORITHM_RANGES_SEARCH_H
0010 #define _LIBCPP___CXX03___ALGORITHM_RANGES_SEARCH_H
0011
0012 #include <__cxx03/__algorithm/iterator_operations.h>
0013 #include <__cxx03/__algorithm/search.h>
0014 #include <__cxx03/__config>
0015 #include <__cxx03/__functional/identity.h>
0016 #include <__cxx03/__functional/ranges_operations.h>
0017 #include <__cxx03/__iterator/advance.h>
0018 #include <__cxx03/__iterator/concepts.h>
0019 #include <__cxx03/__iterator/distance.h>
0020 #include <__cxx03/__iterator/indirectly_comparable.h>
0021 #include <__cxx03/__ranges/access.h>
0022 #include <__cxx03/__ranges/concepts.h>
0023 #include <__cxx03/__ranges/size.h>
0024 #include <__cxx03/__ranges/subrange.h>
0025 #include <__cxx03/__utility/pair.h>
0026
0027 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0028 # pragma GCC system_header
0029 #endif
0030
0031 #if _LIBCPP_STD_VER >= 20
0032
0033 _LIBCPP_BEGIN_NAMESPACE_STD
0034
0035 namespace ranges {
0036 namespace __search {
0037 struct __fn {
0038 template <class _Iter1, class _Sent1, class _Iter2, class _Sent2, class _Pred, class _Proj1, class _Proj2>
0039 _LIBCPP_HIDE_FROM_ABI static constexpr subrange<_Iter1> __ranges_search_impl(
0040 _Iter1 __first1,
0041 _Sent1 __last1,
0042 _Iter2 __first2,
0043 _Sent2 __last2,
0044 _Pred& __pred,
0045 _Proj1& __proj1,
0046 _Proj2& __proj2) {
0047 if constexpr (sized_sentinel_for<_Sent2, _Iter2>) {
0048 auto __size2 = ranges::distance(__first2, __last2);
0049 if (__size2 == 0)
0050 return {__first1, __first1};
0051
0052 if constexpr (sized_sentinel_for<_Sent1, _Iter1>) {
0053 auto __size1 = ranges::distance(__first1, __last1);
0054 if (__size1 < __size2) {
0055 ranges::advance(__first1, __last1);
0056 return {__first1, __first1};
0057 }
0058
0059 if constexpr (random_access_iterator<_Iter1> && random_access_iterator<_Iter2>) {
0060 auto __ret = std::__search_random_access_impl<_RangeAlgPolicy>(
0061 __first1, __last1, __first2, __last2, __pred, __proj1, __proj2, __size1, __size2);
0062 return {__ret.first, __ret.second};
0063 }
0064 }
0065 }
0066
0067 auto __ret =
0068 std::__search_forward_impl<_RangeAlgPolicy>(__first1, __last1, __first2, __last2, __pred, __proj1, __proj2);
0069 return {__ret.first, __ret.second};
0070 }
0071
0072 template <forward_iterator _Iter1,
0073 sentinel_for<_Iter1> _Sent1,
0074 forward_iterator _Iter2,
0075 sentinel_for<_Iter2> _Sent2,
0076 class _Pred = ranges::equal_to,
0077 class _Proj1 = identity,
0078 class _Proj2 = identity>
0079 requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
0080 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr subrange<_Iter1> operator()(
0081 _Iter1 __first1,
0082 _Sent1 __last1,
0083 _Iter2 __first2,
0084 _Sent2 __last2,
0085 _Pred __pred = {},
0086 _Proj1 __proj1 = {},
0087 _Proj2 __proj2 = {}) const {
0088 return __ranges_search_impl(__first1, __last1, __first2, __last2, __pred, __proj1, __proj2);
0089 }
0090
0091 template <forward_range _Range1,
0092 forward_range _Range2,
0093 class _Pred = ranges::equal_to,
0094 class _Proj1 = identity,
0095 class _Proj2 = identity>
0096 requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>, _Pred, _Proj1, _Proj2>
0097 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr borrowed_subrange_t<_Range1> operator()(
0098 _Range1&& __range1, _Range2&& __range2, _Pred __pred = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const {
0099 auto __first1 = ranges::begin(__range1);
0100 if constexpr (sized_range<_Range2>) {
0101 auto __size2 = ranges::size(__range2);
0102 if (__size2 == 0)
0103 return {__first1, __first1};
0104 if constexpr (sized_range<_Range1>) {
0105 auto __size1 = ranges::size(__range1);
0106 if (__size1 < __size2) {
0107 ranges::advance(__first1, ranges::end(__range1));
0108 return {__first1, __first1};
0109 }
0110 }
0111 }
0112
0113 return __ranges_search_impl(
0114 ranges::begin(__range1),
0115 ranges::end(__range1),
0116 ranges::begin(__range2),
0117 ranges::end(__range2),
0118 __pred,
0119 __proj1,
0120 __proj2);
0121 }
0122 };
0123 }
0124
0125 inline namespace __cpo {
0126 inline constexpr auto search = __search::__fn{};
0127 }
0128 }
0129
0130 _LIBCPP_END_NAMESPACE_STD
0131
0132 #endif
0133
0134 #endif