Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:11

0001 //===----------------------------------------------------------------------===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef _LIBCPP___ALGORITHM_RANGES_SEARCH_H
0010 #define _LIBCPP___ALGORITHM_RANGES_SEARCH_H
0011 
0012 #include <__algorithm/iterator_operations.h>
0013 #include <__algorithm/search.h>
0014 #include <__config>
0015 #include <__functional/identity.h>
0016 #include <__functional/ranges_operations.h>
0017 #include <__iterator/advance.h>
0018 #include <__iterator/concepts.h>
0019 #include <__iterator/distance.h>
0020 #include <__iterator/indirectly_comparable.h>
0021 #include <__ranges/access.h>
0022 #include <__ranges/concepts.h>
0023 #include <__ranges/size.h>
0024 #include <__ranges/subrange.h>
0025 #include <__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 struct __search {
0037   template <class _Iter1, class _Sent1, class _Iter2, class _Sent2, class _Pred, class _Proj1, class _Proj2>
0038   _LIBCPP_HIDE_FROM_ABI static constexpr subrange<_Iter1> __ranges_search_impl(
0039       _Iter1 __first1,
0040       _Sent1 __last1,
0041       _Iter2 __first2,
0042       _Sent2 __last2,
0043       _Pred& __pred,
0044       _Proj1& __proj1,
0045       _Proj2& __proj2) {
0046     if constexpr (sized_sentinel_for<_Sent2, _Iter2>) {
0047       auto __size2 = ranges::distance(__first2, __last2);
0048       if (__size2 == 0)
0049         return {__first1, __first1};
0050 
0051       if constexpr (sized_sentinel_for<_Sent1, _Iter1>) {
0052         auto __size1 = ranges::distance(__first1, __last1);
0053         if (__size1 < __size2) {
0054           ranges::advance(__first1, __last1);
0055           return {__first1, __first1};
0056         }
0057 
0058         if constexpr (random_access_iterator<_Iter1> && random_access_iterator<_Iter2>) {
0059           auto __ret = std::__search_random_access_impl<_RangeAlgPolicy>(
0060               __first1, __last1, __first2, __last2, __pred, __proj1, __proj2, __size1, __size2);
0061           return {__ret.first, __ret.second};
0062         }
0063       }
0064     }
0065 
0066     auto __ret =
0067         std::__search_forward_impl<_RangeAlgPolicy>(__first1, __last1, __first2, __last2, __pred, __proj1, __proj2);
0068     return {__ret.first, __ret.second};
0069   }
0070 
0071   template <forward_iterator _Iter1,
0072             sentinel_for<_Iter1> _Sent1,
0073             forward_iterator _Iter2,
0074             sentinel_for<_Iter2> _Sent2,
0075             class _Pred  = ranges::equal_to,
0076             class _Proj1 = identity,
0077             class _Proj2 = identity>
0078     requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
0079   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr subrange<_Iter1> operator()(
0080       _Iter1 __first1,
0081       _Sent1 __last1,
0082       _Iter2 __first2,
0083       _Sent2 __last2,
0084       _Pred __pred   = {},
0085       _Proj1 __proj1 = {},
0086       _Proj2 __proj2 = {}) const {
0087     return __ranges_search_impl(__first1, __last1, __first2, __last2, __pred, __proj1, __proj2);
0088   }
0089 
0090   template <forward_range _Range1,
0091             forward_range _Range2,
0092             class _Pred  = ranges::equal_to,
0093             class _Proj1 = identity,
0094             class _Proj2 = identity>
0095     requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>, _Pred, _Proj1, _Proj2>
0096   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr borrowed_subrange_t<_Range1> operator()(
0097       _Range1&& __range1, _Range2&& __range2, _Pred __pred = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const {
0098     auto __first1 = ranges::begin(__range1);
0099     if constexpr (sized_range<_Range2>) {
0100       auto __size2 = ranges::size(__range2);
0101       if (__size2 == 0)
0102         return {__first1, __first1};
0103       if constexpr (sized_range<_Range1>) {
0104         auto __size1 = ranges::size(__range1);
0105         if (__size1 < __size2) {
0106           ranges::advance(__first1, ranges::end(__range1));
0107           return {__first1, __first1};
0108         }
0109       }
0110     }
0111 
0112     return __ranges_search_impl(
0113         ranges::begin(__range1),
0114         ranges::end(__range1),
0115         ranges::begin(__range2),
0116         ranges::end(__range2),
0117         __pred,
0118         __proj1,
0119         __proj2);
0120   }
0121 };
0122 
0123 inline namespace __cpo {
0124 inline constexpr auto search = __search{};
0125 } // namespace __cpo
0126 } // namespace ranges
0127 
0128 _LIBCPP_END_NAMESPACE_STD
0129 
0130 #endif // _LIBCPP_STD_VER >= 20
0131 
0132 #endif // _LIBCPP___ALGORITHM_RANGES_SEARCH_H