| 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_FIND_LAST_H |
| 10 | #define _LIBCPP___ALGORITHM_RANGES_FIND_LAST_H |
| 11 | |
| 12 | #include <__config> |
| 13 | #include <__functional/identity.h> |
| 14 | #include <__functional/invoke.h> |
| 15 | #include <__functional/ranges_operations.h> |
| 16 | #include <__iterator/concepts.h> |
| 17 | #include <__iterator/indirectly_comparable.h> |
| 18 | #include <__iterator/next.h> |
| 19 | #include <__iterator/prev.h> |
| 20 | #include <__iterator/projected.h> |
| 21 | #include <__ranges/access.h> |
| 22 | #include <__ranges/concepts.h> |
| 23 | #include <__ranges/subrange.h> |
| 24 | #include <__utility/forward.h> |
| 25 | #include <__utility/move.h> |
| 26 | |
| 27 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 28 | # pragma GCC system_header |
| 29 | #endif |
| 30 | |
| 31 | _LIBCPP_PUSH_MACROS |
| 32 | #include <__undef_macros> |
| 33 | |
| 34 | #if _LIBCPP_STD_VER >= 23 |
| 35 | |
| 36 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 37 | |
| 38 | namespace ranges { |
| 39 | |
| 40 | template <class _Iter, class _Sent, class _Pred, class _Proj> |
| 41 | constexpr subrange<_Iter> __find_last_impl(_Iter __first, _Sent __last, _Pred __pred, _Proj& __proj) { |
| 42 | if (__first == __last) { |
| 43 | return subrange<_Iter>(__first, __first); |
| 44 | } |
| 45 | |
| 46 | if constexpr (bidirectional_iterator<_Iter>) { |
| 47 | auto __last_it = ranges::next(__first, __last); |
| 48 | for (auto __it = ranges::prev(__last_it); __it != __first; --__it) { |
| 49 | if (__pred(std::invoke(__proj, *__it))) { |
| 50 | return subrange<_Iter>(std::move(__it), std::move(__last_it)); |
| 51 | } |
| 52 | } |
| 53 | if (__pred(std::invoke(__proj, *__first))) { |
| 54 | return subrange<_Iter>(std::move(__first), std::move(__last_it)); |
| 55 | } |
| 56 | return subrange<_Iter>(__last_it, __last_it); |
| 57 | } else { |
| 58 | bool __found = false; |
| 59 | _Iter __found_it; |
| 60 | for (; __first != __last; ++__first) { |
| 61 | if (__pred(std::invoke(__proj, *__first))) { |
| 62 | __found = true; |
| 63 | __found_it = __first; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | if (__found) { |
| 68 | return subrange<_Iter>(std::move(__found_it), std::move(__first)); |
| 69 | } else { |
| 70 | return subrange<_Iter>(__first, __first); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | struct __find_last { |
| 76 | template <class _Type> |
| 77 | struct __op { |
| 78 | const _Type& __value; |
| 79 | template <class _Elem> |
| 80 | constexpr decltype(auto) operator()(_Elem&& __elem) const { |
| 81 | return std::forward<_Elem>(__elem) == __value; |
| 82 | } |
| 83 | }; |
| 84 | |
| 85 | template <forward_iterator _Iter, sentinel_for<_Iter> _Sent, class _Type, class _Proj = identity> |
| 86 | requires indirect_binary_predicate<ranges::equal_to, projected<_Iter, _Proj>, const _Type*> |
| 87 | [[nodiscard]] constexpr static subrange<_Iter> |
| 88 | operator()(_Iter __first, _Sent __last, const _Type& __value, _Proj __proj = {}) { |
| 89 | return ranges::__find_last_impl(std::move(__first), std::move(__last), __op<_Type>{__value}, __proj); |
| 90 | } |
| 91 | |
| 92 | template <forward_range _Range, class _Type, class _Proj = identity> |
| 93 | requires indirect_binary_predicate<ranges::equal_to, projected<iterator_t<_Range>, _Proj>, const _Type*> |
| 94 | [[nodiscard]] constexpr static borrowed_subrange_t<_Range> |
| 95 | operator()(_Range&& __range, const _Type& __value, _Proj __proj = {}) { |
| 96 | return ranges::__find_last_impl(ranges::begin(__range), ranges::end(__range), __op<_Type>{__value}, __proj); |
| 97 | } |
| 98 | }; |
| 99 | |
| 100 | struct __find_last_if { |
| 101 | template <class _Pred> |
| 102 | struct __op { |
| 103 | _Pred& __pred; |
| 104 | template <class _Elem> |
| 105 | constexpr decltype(auto) operator()(_Elem&& __elem) const { |
| 106 | return std::invoke(__pred, std::forward<_Elem>(__elem)); |
| 107 | } |
| 108 | }; |
| 109 | |
| 110 | template <forward_iterator _Iter, |
| 111 | sentinel_for<_Iter> _Sent, |
| 112 | class _Proj = identity, |
| 113 | indirect_unary_predicate<projected<_Iter, _Proj>> _Pred> |
| 114 | [[nodiscard]] constexpr static subrange<_Iter> |
| 115 | operator()(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {}) { |
| 116 | return ranges::__find_last_impl(std::move(__first), std::move(__last), __op<_Pred>{__pred}, __proj); |
| 117 | } |
| 118 | |
| 119 | template <forward_range _Range, |
| 120 | class _Proj = identity, |
| 121 | indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred> |
| 122 | [[nodiscard]] constexpr static borrowed_subrange_t<_Range> |
| 123 | operator()(_Range&& __range, _Pred __pred, _Proj __proj = {}) { |
| 124 | return ranges::__find_last_impl(ranges::begin(__range), ranges::end(__range), __op<_Pred>{__pred}, __proj); |
| 125 | } |
| 126 | }; |
| 127 | |
| 128 | struct __find_last_if_not { |
| 129 | template <class _Pred> |
| 130 | struct __op { |
| 131 | _Pred& __pred; |
| 132 | template <class _Elem> |
| 133 | constexpr decltype(auto) operator()(_Elem&& __elem) const { |
| 134 | return !std::invoke(__pred, std::forward<_Elem>(__elem)); |
| 135 | } |
| 136 | }; |
| 137 | |
| 138 | template <forward_iterator _Iter, |
| 139 | sentinel_for<_Iter> _Sent, |
| 140 | class _Proj = identity, |
| 141 | indirect_unary_predicate<projected<_Iter, _Proj>> _Pred> |
| 142 | [[nodiscard]] constexpr static subrange<_Iter> |
| 143 | operator()(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {}) { |
| 144 | return ranges::__find_last_impl(std::move(__first), std::move(__last), __op<_Pred>{__pred}, __proj); |
| 145 | } |
| 146 | |
| 147 | template <forward_range _Range, |
| 148 | class _Proj = identity, |
| 149 | indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred> |
| 150 | [[nodiscard]] constexpr static borrowed_subrange_t<_Range> |
| 151 | operator()(_Range&& __range, _Pred __pred, _Proj __proj = {}) { |
| 152 | return ranges::__find_last_impl(ranges::begin(__range), ranges::end(__range), __op<_Pred>{__pred}, __proj); |
| 153 | } |
| 154 | }; |
| 155 | |
| 156 | inline namespace __cpo { |
| 157 | inline constexpr auto find_last = __find_last{}; |
| 158 | inline constexpr auto find_last_if = __find_last_if{}; |
| 159 | inline constexpr auto find_last_if_not = __find_last_if_not{}; |
| 160 | } // namespace __cpo |
| 161 | } // namespace ranges |
| 162 | |
| 163 | _LIBCPP_END_NAMESPACE_STD |
| 164 | |
| 165 | #endif // _LIBCPP_STD_VER >= 23 |
| 166 | |
| 167 | _LIBCPP_POP_MACROS |
| 168 | |
| 169 | #endif // _LIBCPP___ALGORITHM_RANGES_FIND_LAST_H |
| 170 | |