1753f127fSDimitry Andric //===----------------------------------------------------------------------===// 2753f127fSDimitry Andric // 3753f127fSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4753f127fSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5753f127fSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6753f127fSDimitry Andric // 7753f127fSDimitry Andric //===----------------------------------------------------------------------===// 8753f127fSDimitry Andric 9753f127fSDimitry Andric #ifndef _LIBCPP___ALGORITHM_RANGES_SEARCH_H 10753f127fSDimitry Andric #define _LIBCPP___ALGORITHM_RANGES_SEARCH_H 11753f127fSDimitry Andric 12753f127fSDimitry Andric #include <__algorithm/iterator_operations.h> 13753f127fSDimitry Andric #include <__algorithm/search.h> 14753f127fSDimitry Andric #include <__config> 15753f127fSDimitry Andric #include <__functional/identity.h> 16753f127fSDimitry Andric #include <__functional/ranges_operations.h> 17753f127fSDimitry Andric #include <__iterator/advance.h> 18753f127fSDimitry Andric #include <__iterator/concepts.h> 19753f127fSDimitry Andric #include <__iterator/distance.h> 20753f127fSDimitry Andric #include <__iterator/indirectly_comparable.h> 21753f127fSDimitry Andric #include <__ranges/access.h> 22753f127fSDimitry Andric #include <__ranges/concepts.h> 23753f127fSDimitry Andric #include <__ranges/size.h> 24753f127fSDimitry Andric #include <__ranges/subrange.h> 25bdd1243dSDimitry Andric #include <__utility/pair.h> 26753f127fSDimitry Andric 27753f127fSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 28753f127fSDimitry Andric # pragma GCC system_header 29753f127fSDimitry Andric #endif 30753f127fSDimitry Andric 3106c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 20 32753f127fSDimitry Andric 33753f127fSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD 34753f127fSDimitry Andric 35753f127fSDimitry Andric namespace ranges { 36753f127fSDimitry Andric namespace __search { 37753f127fSDimitry Andric struct __fn { 38753f127fSDimitry Andric template <class _Iter1, class _Sent1, class _Iter2, class _Sent2, class _Pred, class _Proj1, class _Proj2> __ranges_search_impl__fn39753f127fSDimitry Andric _LIBCPP_HIDE_FROM_ABI static constexpr subrange<_Iter1> __ranges_search_impl( 40753f127fSDimitry Andric _Iter1 __first1, 41753f127fSDimitry Andric _Sent1 __last1, 42753f127fSDimitry Andric _Iter2 __first2, 43753f127fSDimitry Andric _Sent2 __last2, 44753f127fSDimitry Andric _Pred& __pred, 45753f127fSDimitry Andric _Proj1& __proj1, 46753f127fSDimitry Andric _Proj2& __proj2) { 47753f127fSDimitry Andric if constexpr (sized_sentinel_for<_Sent2, _Iter2>) { 48753f127fSDimitry Andric auto __size2 = ranges::distance(__first2, __last2); 49753f127fSDimitry Andric if (__size2 == 0) 50753f127fSDimitry Andric return {__first1, __first1}; 51753f127fSDimitry Andric 52753f127fSDimitry Andric if constexpr (sized_sentinel_for<_Sent1, _Iter1>) { 53753f127fSDimitry Andric auto __size1 = ranges::distance(__first1, __last1); 54753f127fSDimitry Andric if (__size1 < __size2) { 55753f127fSDimitry Andric ranges::advance(__first1, __last1); 56753f127fSDimitry Andric return {__first1, __first1}; 57753f127fSDimitry Andric } 58753f127fSDimitry Andric 59753f127fSDimitry Andric if constexpr (random_access_iterator<_Iter1> && random_access_iterator<_Iter2>) { 60753f127fSDimitry Andric auto __ret = std::__search_random_access_impl<_RangeAlgPolicy>( 61753f127fSDimitry Andric __first1, __last1, __first2, __last2, __pred, __proj1, __proj2, __size1, __size2); 62753f127fSDimitry Andric return {__ret.first, __ret.second}; 63753f127fSDimitry Andric } 64753f127fSDimitry Andric } 65753f127fSDimitry Andric } 66753f127fSDimitry Andric 67753f127fSDimitry Andric auto __ret = 68753f127fSDimitry Andric std::__search_forward_impl<_RangeAlgPolicy>(__first1, __last1, __first2, __last2, __pred, __proj1, __proj2); 69753f127fSDimitry Andric return {__ret.first, __ret.second}; 70753f127fSDimitry Andric } 71753f127fSDimitry Andric 7206c3fb27SDimitry Andric template <forward_iterator _Iter1, 7306c3fb27SDimitry Andric sentinel_for<_Iter1> _Sent1, 7406c3fb27SDimitry Andric forward_iterator _Iter2, 7506c3fb27SDimitry Andric sentinel_for<_Iter2> _Sent2, 76753f127fSDimitry Andric class _Pred = ranges::equal_to, 77753f127fSDimitry Andric class _Proj1 = identity, 78753f127fSDimitry Andric class _Proj2 = identity> 79753f127fSDimitry Andric requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2> operator__fn80*0fca6ea1SDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr subrange<_Iter1> operator()( 8106c3fb27SDimitry Andric _Iter1 __first1, 8206c3fb27SDimitry Andric _Sent1 __last1, 8306c3fb27SDimitry Andric _Iter2 __first2, 8406c3fb27SDimitry Andric _Sent2 __last2, 85753f127fSDimitry Andric _Pred __pred = {}, 86753f127fSDimitry Andric _Proj1 __proj1 = {}, 87753f127fSDimitry Andric _Proj2 __proj2 = {}) const { 88753f127fSDimitry Andric return __ranges_search_impl(__first1, __last1, __first2, __last2, __pred, __proj1, __proj2); 89753f127fSDimitry Andric } 90753f127fSDimitry Andric 91753f127fSDimitry Andric template <forward_range _Range1, 92753f127fSDimitry Andric forward_range _Range2, 93753f127fSDimitry Andric class _Pred = ranges::equal_to, 94753f127fSDimitry Andric class _Proj1 = identity, 95753f127fSDimitry Andric class _Proj2 = identity> 96753f127fSDimitry Andric requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>, _Pred, _Proj1, _Proj2> operator__fn97*0fca6ea1SDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr borrowed_subrange_t<_Range1> operator()( 9806c3fb27SDimitry Andric _Range1&& __range1, _Range2&& __range2, _Pred __pred = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const { 99753f127fSDimitry Andric auto __first1 = ranges::begin(__range1); 100753f127fSDimitry Andric if constexpr (sized_range<_Range2>) { 101753f127fSDimitry Andric auto __size2 = ranges::size(__range2); 102753f127fSDimitry Andric if (__size2 == 0) 103753f127fSDimitry Andric return {__first1, __first1}; 104753f127fSDimitry Andric if constexpr (sized_range<_Range1>) { 105753f127fSDimitry Andric auto __size1 = ranges::size(__range1); 106753f127fSDimitry Andric if (__size1 < __size2) { 107753f127fSDimitry Andric ranges::advance(__first1, ranges::end(__range1)); 108753f127fSDimitry Andric return {__first1, __first1}; 109753f127fSDimitry Andric } 110753f127fSDimitry Andric } 111753f127fSDimitry Andric } 112753f127fSDimitry Andric 113753f127fSDimitry Andric return __ranges_search_impl( 114753f127fSDimitry Andric ranges::begin(__range1), 115753f127fSDimitry Andric ranges::end(__range1), 116753f127fSDimitry Andric ranges::begin(__range2), 117753f127fSDimitry Andric ranges::end(__range2), 118753f127fSDimitry Andric __pred, 119753f127fSDimitry Andric __proj1, 120753f127fSDimitry Andric __proj2); 121753f127fSDimitry Andric } 122753f127fSDimitry Andric }; 123753f127fSDimitry Andric } // namespace __search 124753f127fSDimitry Andric 125753f127fSDimitry Andric inline namespace __cpo { 126753f127fSDimitry Andric inline constexpr auto search = __search::__fn{}; 127753f127fSDimitry Andric } // namespace __cpo 128753f127fSDimitry Andric } // namespace ranges 129753f127fSDimitry Andric 130753f127fSDimitry Andric _LIBCPP_END_NAMESPACE_STD 131753f127fSDimitry Andric 13206c3fb27SDimitry Andric #endif // _LIBCPP_STD_VER >= 20 133753f127fSDimitry Andric 134753f127fSDimitry Andric #endif // _LIBCPP___ALGORITHM_RANGES_SEARCH_H 135