|
| 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_IF_H |
| 10 | +#define _LIBCPP___ALGORITHM_RANGES_FIND_LAST_IF_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/next.h> |
| 18 | +#include <__iterator/prev.h> |
| 19 | +#include <__iterator/projected.h> |
| 20 | +#include <__ranges/access.h> |
| 21 | +#include <__ranges/concepts.h> |
| 22 | +#include <__ranges/subrange.h> |
| 23 | +#include <__utility/move.h> |
| 24 | + |
| 25 | +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 26 | +# pragma GCC system_header |
| 27 | +#endif |
| 28 | + |
| 29 | +_LIBCPP_PUSH_MACROS |
| 30 | +#include <__undef_macros> |
| 31 | + |
| 32 | +#if _LIBCPP_STD_VER >= 23 |
| 33 | + |
| 34 | +_LIBCPP_BEGIN_NAMESPACE_STD |
| 35 | + |
| 36 | +namespace ranges { |
| 37 | + |
| 38 | +template <class _Iter, class _Sent, class _Pred, class _Proj> |
| 39 | +_LIBCPP_HIDE_FROM_ABI constexpr static subrange<_Iter> |
| 40 | +__find_last_if_impl(_Iter __first, _Sent __last, _Pred& __pred, _Proj& __proj) { |
| 41 | + if (__first == __last) { |
| 42 | + return subrange<_Iter>(__first, __first); |
| 43 | + } |
| 44 | + |
| 45 | + if constexpr (bidirectional_iterator<_Iter>) { |
| 46 | + auto __last_it = ranges::next(__first, __last); |
| 47 | + for (auto __it = ranges::prev(__last_it); __it != __first; --__it) { |
| 48 | + if (std::invoke(__pred, std::invoke(__proj, *__it))) { |
| 49 | + return subrange<_Iter>(std::move(__it), std::move(__last_it)); |
| 50 | + } |
| 51 | + } |
| 52 | + if (std::invoke(__pred, std::invoke(__proj, *__first))) { |
| 53 | + return subrange<_Iter>(std::move(__first), std::move(__last_it)); |
| 54 | + } |
| 55 | + return subrange<_Iter>(__last_it, __last_it); |
| 56 | + } else { |
| 57 | + bool __found = false; |
| 58 | + _Iter __found_it; |
| 59 | + for (; __first != __last; ++__first) { |
| 60 | + if (std::invoke(__pred, std::invoke(__proj, *__first))) { |
| 61 | + __found = true; |
| 62 | + __found_it = __first; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + if (__found) { |
| 67 | + return subrange<_Iter>(std::move(__found_it), std::move(__first)); |
| 68 | + } else { |
| 69 | + return subrange<_Iter>(__first, __first); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +namespace __find_last_if { |
| 75 | +struct __fn { |
| 76 | + template <forward_iterator _Iter, |
| 77 | + sentinel_for<_Iter> _Sent, |
| 78 | + class _Proj = identity, |
| 79 | + indirect_unary_predicate<projected<_Iter, _Proj>> _Pred> |
| 80 | + [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr subrange<_Iter> |
| 81 | + operator()(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {}) const { |
| 82 | + return __find_last_if_impl(std::move(__first), std::move(__last), __pred, __proj); |
| 83 | + } |
| 84 | + |
| 85 | + template <forward_range _Range, |
| 86 | + class _Proj = identity, |
| 87 | + indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred> |
| 88 | + [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr borrowed_subrange_t<_Range> |
| 89 | + operator()(_Range&& __range, _Pred __pred, _Proj __proj = {}) const { |
| 90 | + return __find_last_if_impl(ranges::begin(__range), ranges::end(__range), __pred, __proj); |
| 91 | + } |
| 92 | +}; |
| 93 | +} // namespace __find_last_if |
| 94 | + |
| 95 | +inline namespace __cpo { |
| 96 | +inline constexpr auto find_last_if = __find_last_if::__fn{}; |
| 97 | +} // namespace __cpo |
| 98 | +} // namespace ranges |
| 99 | + |
| 100 | +_LIBCPP_END_NAMESPACE_STD |
| 101 | + |
| 102 | +#endif // _LIBCPP_STD_VER >= 23 |
| 103 | + |
| 104 | +_LIBCPP_POP_MACROS |
| 105 | + |
| 106 | +#endif // _LIBCPP___ALGORITHM_RANGES_FIND_LAST_IF_H |
0 commit comments