|
| 1 | +// -*- C++ -*- |
| 2 | +//===----------------------------------------------------------------------===// |
| 3 | +// |
| 4 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | +// See https://llvm.org/LICENSE.txt for license information. |
| 6 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | +// |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | + |
| 10 | +#ifndef _LIBCPP___RANGES_TO_CACHE_LATEST_VIEW_H |
| 11 | +#define _LIBCPP___RANGES_TO_CACHE_LATEST_VIEW_H |
| 12 | + |
| 13 | +#include <__concepts/constructible.h> |
| 14 | +#include <__config> |
| 15 | +#include <__iterator/concepts.h> |
| 16 | +#include <__iterator/iter_move.h> |
| 17 | +#include <__iterator/iter_swap.h> |
| 18 | +#include <__iterator/iterator_traits.h> |
| 19 | +#include <__memory/addressof.h> |
| 20 | +#include <__ranges/access.h> |
| 21 | +#include <__ranges/all.h> |
| 22 | +#include <__ranges/concepts.h> |
| 23 | +#include <__ranges/non_propagating_cache.h> |
| 24 | +#include <__ranges/range_adaptor.h> |
| 25 | +#include <__ranges/size.h> |
| 26 | +#include <__ranges/view_interface.h> |
| 27 | +#include <__type_traits/add_pointer.h> |
| 28 | +#include <__type_traits/conditional.h> |
| 29 | +#include <__type_traits/is_reference.h> |
| 30 | +#include <__utility/as_lvalue.h> |
| 31 | +#include <__utility/forward.h> |
| 32 | +#include <__utility/move.h> |
| 33 | + |
| 34 | +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 35 | +# pragma GCC system_header |
| 36 | +#endif |
| 37 | + |
| 38 | +_LIBCPP_PUSH_MACROS |
| 39 | +#include <__undef_macros> |
| 40 | + |
| 41 | +_LIBCPP_BEGIN_NAMESPACE_STD |
| 42 | + |
| 43 | +#if _LIBCPP_STD_VER >= 26 |
| 44 | + |
| 45 | +namespace ranges { |
| 46 | + |
| 47 | +// [range.cache.latest.view] |
| 48 | + |
| 49 | +template <input_range _View> |
| 50 | + requires view<_View> |
| 51 | +class cache_latest_view : public view_interface<cache_latest_view<_View>> { |
| 52 | + _View __base_ = _View(); // exposition only |
| 53 | + using __cache_t _LIBCPP_NODEBUG = |
| 54 | + conditional_t<is_reference_v<range_reference_t<_View>>, // exposition only |
| 55 | + add_pointer_t<range_reference_t<_View>>, |
| 56 | + range_reference_t<_View>>; |
| 57 | + |
| 58 | + __non_propagating_cache<__cache_t> __cache_; // exposition only |
| 59 | + |
| 60 | + // [range.cache.latest.iterator], class cache_latest_view::iterator |
| 61 | + class iterator; // exposition only |
| 62 | + // [range.cache.latest.sentinel], class cache_latest_view::sentinel |
| 63 | + class sentinel; // exposition only |
| 64 | + |
| 65 | +public: |
| 66 | + _LIBCPP_HIDE_FROM_ABI cache_latest_view() |
| 67 | + requires default_initializable<_View> |
| 68 | + = default; |
| 69 | + _LIBCPP_HIDE_FROM_ABI constexpr explicit cache_latest_view(_View __base) : __base_{std::move(__base)} {} |
| 70 | + |
| 71 | + _LIBCPP_HIDE_FROM_ABI constexpr _View base() const& |
| 72 | + requires copy_constructible<_View> |
| 73 | + { |
| 74 | + return __base_; |
| 75 | + } |
| 76 | + _LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); } |
| 77 | + |
| 78 | + _LIBCPP_HIDE_FROM_ABI constexpr auto begin() { return iterator(*this); } |
| 79 | + _LIBCPP_HIDE_FROM_ABI constexpr auto end() { return sentinel{*this}; } |
| 80 | + |
| 81 | + _LIBCPP_HIDE_FROM_ABI constexpr auto size() |
| 82 | + requires sized_range<_View> |
| 83 | + { |
| 84 | + return ranges::size(__base_); |
| 85 | + } |
| 86 | + _LIBCPP_HIDE_FROM_ABI constexpr auto size() const |
| 87 | + requires sized_range<const _View> |
| 88 | + { |
| 89 | + return ranges::size(__base_); |
| 90 | + } |
| 91 | + |
| 92 | + // TODO: Implement when P2846R6 is available. |
| 93 | + // constexpr auto reserve_hint() |
| 94 | + // requires approximately_sized_range<_View> |
| 95 | + // { |
| 96 | + // return ranges::reserve_hint(__base_); |
| 97 | + // } |
| 98 | + // constexpr auto reserve_hint() const |
| 99 | + // requires approximately_sized_range<const _View> |
| 100 | + // { |
| 101 | + // return ranges::reserve_hint(__base_); |
| 102 | + // } |
| 103 | +}; |
| 104 | + |
| 105 | +template <class _Range> |
| 106 | +cache_latest_view(_Range&&) -> cache_latest_view<views::all_t<_Range>>; |
| 107 | + |
| 108 | +// [range.cache.latest.iterator] |
| 109 | + |
| 110 | +template <input_range _View> |
| 111 | + requires view<_View> |
| 112 | +class cache_latest_view<_View>::iterator { |
| 113 | + cache_latest_view* __parent_; // exposition only |
| 114 | + iterator_t<_View> __current_; // exposition only |
| 115 | + |
| 116 | + _LIBCPP_HIDE_FROM_ABI constexpr explicit iterator(cache_latest_view& __parent) // exposition only |
| 117 | + : __parent_{std::addressof(__parent)}, __current_{ranges::begin(__parent.__base_)} {} |
| 118 | + |
| 119 | + friend class cache_latest_view<_View>; |
| 120 | + |
| 121 | +public: |
| 122 | + using difference_type = range_difference_t<_View>; |
| 123 | + using value_type = range_value_t<_View>; |
| 124 | + using iterator_concept = input_iterator_tag; |
| 125 | + |
| 126 | + _LIBCPP_HIDE_FROM_ABI iterator(iterator&&) = default; |
| 127 | + _LIBCPP_HIDE_FROM_ABI iterator& operator=(iterator&&) = default; |
| 128 | + |
| 129 | + _LIBCPP_HIDE_FROM_ABI constexpr iterator_t<_View> base() && { return std::move(__current_); } |
| 130 | + _LIBCPP_HIDE_FROM_ABI constexpr const iterator_t<_View>& base() const& noexcept { return __current_; } |
| 131 | + |
| 132 | + _LIBCPP_HIDE_FROM_ABI constexpr range_reference_t<_View>& operator*() const { |
| 133 | + if constexpr (is_reference_v<range_reference_t<_View>>) { |
| 134 | + if (!__parent_->__cache_.__has_value()) { |
| 135 | + __parent_->__cache_.__emplace(std::addressof(std::__as_lvalue(*__current_))); |
| 136 | + } |
| 137 | + return **__parent_->__cache_; |
| 138 | + } else { |
| 139 | + if (!__parent_->__cache_.__has_value()) { |
| 140 | + __parent_->__cache_.__emplace_from([&]() -> decltype(auto) { return *__current_; }); |
| 141 | + } |
| 142 | + return *__parent_->__cache_; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + _LIBCPP_HIDE_FROM_ABI constexpr iterator& operator++() { |
| 147 | + __parent_->__cache_.__reset(); |
| 148 | + ++__current_; |
| 149 | + return *this; |
| 150 | + } |
| 151 | + _LIBCPP_HIDE_FROM_ABI constexpr void operator++(int) { ++*this; } |
| 152 | + |
| 153 | + _LIBCPP_HIDE_FROM_ABI friend constexpr range_rvalue_reference_t<_View> |
| 154 | + iter_move(const iterator& __i) noexcept(noexcept(ranges::iter_move(__i.__current_))) { |
| 155 | + return ranges::iter_move(__i.__current_); |
| 156 | + } |
| 157 | + |
| 158 | + _LIBCPP_HIDE_FROM_ABI friend constexpr void |
| 159 | + iter_swap(const iterator& __x, |
| 160 | + const iterator& __y) noexcept(noexcept(ranges::iter_swap(__x.__current_, __y.__current_))) |
| 161 | + requires indirectly_swappable<iterator_t<_View>> |
| 162 | + { |
| 163 | + ranges::iter_swap(__x.__current_, __y.__current_); |
| 164 | + } |
| 165 | +}; |
| 166 | + |
| 167 | +// [range.cache.latest.sentinel] |
| 168 | + |
| 169 | +template <input_range _View> |
| 170 | + requires view<_View> |
| 171 | +class cache_latest_view<_View>::sentinel { |
| 172 | + sentinel_t<_View> __end_ = sentinel_t<_View>(); // exposition only |
| 173 | + |
| 174 | + _LIBCPP_HIDE_FROM_ABI constexpr explicit sentinel(cache_latest_view& __parent) // exposition only |
| 175 | + : __end_{ranges::end(__parent.__base_)} {} |
| 176 | + |
| 177 | + friend class cache_latest_view<_View>; |
| 178 | + |
| 179 | +public: |
| 180 | + _LIBCPP_HIDE_FROM_ABI sentinel() = default; |
| 181 | + |
| 182 | + _LIBCPP_HIDE_FROM_ABI constexpr sentinel_t<_View> base() const { return __end_; } |
| 183 | + |
| 184 | + _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const iterator& __x, const sentinel& __y) { |
| 185 | + return __x.__current_ == __y.__end_; |
| 186 | + } |
| 187 | + |
| 188 | + _LIBCPP_HIDE_FROM_ABI friend constexpr range_difference_t<_View> operator-(const iterator& __x, const sentinel& __y) |
| 189 | + requires sized_sentinel_for<sentinel_t<_View>, iterator_t<_View>> |
| 190 | + { |
| 191 | + return __x.__current_ - __y.__end_; |
| 192 | + } |
| 193 | + _LIBCPP_HIDE_FROM_ABI friend constexpr range_difference_t<_View> operator-(const sentinel& __x, const iterator& __y) |
| 194 | + requires sized_sentinel_for<sentinel_t<_View>, iterator_t<_View>> |
| 195 | + { |
| 196 | + return __x.__end_ - __y.__current_; |
| 197 | + } |
| 198 | +}; |
| 199 | + |
| 200 | +namespace views { |
| 201 | +namespace __cache_latest_view { |
| 202 | + |
| 203 | +struct __fn : __range_adaptor_closure<__fn> { |
| 204 | + template <class _Range> |
| 205 | + [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr auto |
| 206 | + operator()(_Range&& __range) noexcept(noexcept(/**/ cache_latest_view(std::forward<_Range>(__range)))) |
| 207 | + -> decltype(/*-------------------------------*/ cache_latest_view(std::forward<_Range>(__range))) { |
| 208 | + return /*--------------------------------------*/ cache_latest_view(std::forward<_Range>(__range)); |
| 209 | + } |
| 210 | +}; |
| 211 | + |
| 212 | +} // namespace __cache_latest_view |
| 213 | + |
| 214 | +inline namespace __cpo { |
| 215 | +inline constexpr auto cache_latest = __cache_latest_view::__fn{}; |
| 216 | +} // namespace __cpo |
| 217 | +} // namespace views |
| 218 | +} // namespace ranges |
| 219 | + |
| 220 | +#endif // _LIBCPP_STD_VER >= 26 |
| 221 | + |
| 222 | +_LIBCPP_END_NAMESPACE_STD |
| 223 | + |
| 224 | +_LIBCPP_POP_MACROS |
| 225 | + |
| 226 | +#endif // _LIBCPP___RANGES_TO_CACHE_LATEST_VIEW_H |
0 commit comments