|
| 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 | +#ifndef _LIBCPP___RANGES_TRANSFORM_VIEW_H |
| 10 | +#define _LIBCPP___RANGES_TRANSFORM_VIEW_H |
| 11 | + |
| 12 | +#include <__config> |
| 13 | +#include <__iterator/iterator_traits.h> |
| 14 | +#include <__iterator/concepts.h> |
| 15 | +#include <__iterator/iter_swap.h> |
| 16 | +#include <__ranges/access.h> |
| 17 | +#include <__ranges/concepts.h> |
| 18 | +#include <__ranges/copyable_box.h> |
| 19 | +#include <__ranges/empty.h> |
| 20 | +#include <__ranges/view_interface.h> |
| 21 | +#include <type_traits> |
| 22 | + |
| 23 | +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 24 | +#pragma GCC system_header |
| 25 | +#endif |
| 26 | + |
| 27 | +_LIBCPP_PUSH_MACROS |
| 28 | +#include <__undef_macros> |
| 29 | + |
| 30 | +_LIBCPP_BEGIN_NAMESPACE_STD |
| 31 | + |
| 32 | +#if !defined(_LIBCPP_HAS_NO_RANGES) |
| 33 | + |
| 34 | +namespace ranges { |
| 35 | + |
| 36 | +template<class _View, class _Fn> |
| 37 | +concept __transform_view_constraints = |
| 38 | + view<_View> && is_object_v<_Fn> && |
| 39 | + regular_invocable<_Fn&, range_reference_t<_View>> && |
| 40 | + __referenceable<invoke_result_t<_Fn&, range_reference_t<_View>>>; |
| 41 | + |
| 42 | +template<input_range _View, copy_constructible _Fn> |
| 43 | + requires __transform_view_constraints<_View, _Fn> |
| 44 | +class transform_view : public view_interface<transform_view<_View, _Fn>> { |
| 45 | + template<bool> class __iterator; |
| 46 | + template<bool> class __sentinel; |
| 47 | + |
| 48 | + [[no_unique_address]] __copyable_box<_Fn> __func_; |
| 49 | + [[no_unique_address]] _View __base_ = _View(); |
| 50 | + |
| 51 | +public: |
| 52 | + transform_view() |
| 53 | + requires default_initializable<_View> && default_initializable<_Fn> = default; |
| 54 | + |
| 55 | + constexpr transform_view(_View __base, _Fn __func) |
| 56 | + : __func_(_VSTD::in_place, _VSTD::move(__func)), __base_(_VSTD::move(__base)) {} |
| 57 | + |
| 58 | + constexpr _View base() const& requires copy_constructible<_View> { return __base_; } |
| 59 | + constexpr _View base() && { return _VSTD::move(__base_); } |
| 60 | + |
| 61 | + constexpr __iterator<false> begin() { |
| 62 | + return __iterator<false>{*this, ranges::begin(__base_)}; |
| 63 | + } |
| 64 | + constexpr __iterator<true> begin() const |
| 65 | + requires range<const _View> && |
| 66 | + regular_invocable<const _Fn&, range_reference_t<const _View>> |
| 67 | + { |
| 68 | + return __iterator<true>(*this, ranges::begin(__base_)); |
| 69 | + } |
| 70 | + |
| 71 | + constexpr __sentinel<false> end() { |
| 72 | + return __sentinel<false>(ranges::end(__base_)); |
| 73 | + } |
| 74 | + constexpr __iterator<false> end() |
| 75 | + requires common_range<_View> |
| 76 | + { |
| 77 | + return __iterator<false>(*this, ranges::end(__base_)); |
| 78 | + } |
| 79 | + constexpr __sentinel<true> end() const |
| 80 | + requires range<const _View> && |
| 81 | + regular_invocable<const _Fn&, range_reference_t<const _View>> |
| 82 | + { |
| 83 | + return __sentinel<true>(ranges::end(__base_)); |
| 84 | + } |
| 85 | + constexpr __iterator<true> end() const |
| 86 | + requires common_range<const _View> && |
| 87 | + regular_invocable<const _Fn&, range_reference_t<const _View>> |
| 88 | + { |
| 89 | + return __iterator<true>(*this, ranges::end(__base_)); |
| 90 | + } |
| 91 | + |
| 92 | + constexpr auto size() requires sized_range<_View> { return ranges::size(__base_); } |
| 93 | + constexpr auto size() const requires sized_range<const _View> { return ranges::size(__base_); } |
| 94 | +}; |
| 95 | + |
| 96 | +// TODO: replace the decltype with all_t when that's implemented. |
| 97 | +template<class _Range, class _Fn> |
| 98 | +transform_view(_Range&&, _Fn) |
| 99 | + -> transform_view<decltype(views::all(std::declval<_Range>())), _Fn>; |
| 100 | + |
| 101 | +template<class _View> |
| 102 | +struct __transform_view_iterator_concept { using type = input_iterator_tag; }; |
| 103 | + |
| 104 | +template<random_access_range _View> |
| 105 | +struct __transform_view_iterator_concept<_View> { using type = random_access_iterator_tag; }; |
| 106 | + |
| 107 | +template<bidirectional_range _View> |
| 108 | +struct __transform_view_iterator_concept<_View> { using type = bidirectional_iterator_tag; }; |
| 109 | + |
| 110 | +template<forward_range _View> |
| 111 | +struct __transform_view_iterator_concept<_View> { using type = forward_iterator_tag; }; |
| 112 | + |
| 113 | +template<class, class> |
| 114 | +struct __transform_view_iterator_category_base {}; |
| 115 | + |
| 116 | +template<forward_range _View, class _Fn> |
| 117 | +struct __transform_view_iterator_category_base<_View, _Fn> { |
| 118 | + using _Cat = typename iterator_traits<iterator_t<_View>>::iterator_category; |
| 119 | + |
| 120 | + using iterator_category = conditional_t< |
| 121 | + is_lvalue_reference_v<invoke_result_t<_Fn&, range_reference_t<_View>>>, |
| 122 | + conditional_t< |
| 123 | + derived_from<_Cat, contiguous_iterator_tag>, |
| 124 | + random_access_iterator_tag, |
| 125 | + _Cat |
| 126 | + >, |
| 127 | + input_iterator_tag |
| 128 | + >; |
| 129 | +}; |
| 130 | + |
| 131 | +template<input_range _View, copy_constructible _Fn> |
| 132 | + requires __transform_view_constraints<_View, _Fn> |
| 133 | +template<bool _Const> |
| 134 | +class transform_view<_View, _Fn>::__iterator |
| 135 | + : public __transform_view_iterator_category_base<_View, _Fn> { |
| 136 | + |
| 137 | + using _Parent = __maybe_const<_Const, transform_view>; |
| 138 | + using _Base = __maybe_const<_Const, _View>; |
| 139 | + |
| 140 | + _Parent *__parent_ = nullptr; |
| 141 | + |
| 142 | + template<bool> |
| 143 | + friend class transform_view<_View, _Fn>::__iterator; |
| 144 | + |
| 145 | + template<bool> |
| 146 | + friend class transform_view<_View, _Fn>::__sentinel; |
| 147 | + |
| 148 | +public: |
| 149 | + iterator_t<_Base> __current_ = iterator_t<_Base>(); |
| 150 | + |
| 151 | + using iterator_concept = typename __transform_view_iterator_concept<_View>::type; |
| 152 | + using value_type = remove_cvref_t<invoke_result_t<_Fn&, range_reference_t<_Base>>>; |
| 153 | + using difference_type = range_difference_t<_Base>; |
| 154 | + |
| 155 | + __iterator() requires default_initializable<iterator_t<_Base>> = default; |
| 156 | + |
| 157 | + constexpr __iterator(_Parent& __parent, iterator_t<_Base> __current) |
| 158 | + : __parent_(_VSTD::addressof(__parent)), __current_(_VSTD::move(__current)) {} |
| 159 | + |
| 160 | + // Note: `__i` should always be `__iterator<false>`, but directly using |
| 161 | + // `__iterator<false>` is ill-formed when `_Const` is false |
| 162 | + // (see http://wg21.link/class.copy.ctor#5). |
| 163 | + constexpr __iterator(__iterator<!_Const> __i) |
| 164 | + requires _Const && convertible_to<iterator_t<_View>, iterator_t<_Base>> |
| 165 | + : __parent_(__i.__parent_), __current_(_VSTD::move(__i.__current_)) {} |
| 166 | + |
| 167 | + constexpr iterator_t<_Base> base() const& |
| 168 | + requires copyable<iterator_t<_Base>> |
| 169 | + { |
| 170 | + return __current_; |
| 171 | + } |
| 172 | + |
| 173 | + constexpr iterator_t<_Base> base() && { |
| 174 | + return _VSTD::move(__current_); |
| 175 | + } |
| 176 | + |
| 177 | + constexpr decltype(auto) operator*() const |
| 178 | + noexcept(noexcept(_VSTD::invoke(*__parent_->__func_, *__current_))) |
| 179 | + { |
| 180 | + return _VSTD::invoke(*__parent_->__func_, *__current_); |
| 181 | + } |
| 182 | + |
| 183 | + constexpr __iterator& operator++() { |
| 184 | + ++__current_; |
| 185 | + return *this; |
| 186 | + } |
| 187 | + |
| 188 | + constexpr void operator++(int) { ++__current_; } |
| 189 | + |
| 190 | + constexpr __iterator operator++(int) |
| 191 | + requires forward_range<_Base> |
| 192 | + { |
| 193 | + auto __tmp = *this; |
| 194 | + ++*this; |
| 195 | + return __tmp; |
| 196 | + } |
| 197 | + |
| 198 | + constexpr __iterator& operator--() |
| 199 | + requires bidirectional_range<_Base> |
| 200 | + { |
| 201 | + --__current_; |
| 202 | + return *this; |
| 203 | + } |
| 204 | + |
| 205 | + constexpr __iterator operator--(int) |
| 206 | + requires bidirectional_range<_Base> |
| 207 | + { |
| 208 | + auto __tmp = *this; |
| 209 | + --*this; |
| 210 | + return __tmp; |
| 211 | + } |
| 212 | + |
| 213 | + constexpr __iterator& operator+=(difference_type __n) |
| 214 | + requires random_access_range<_Base> |
| 215 | + { |
| 216 | + __current_ += __n; |
| 217 | + return *this; |
| 218 | + } |
| 219 | + |
| 220 | + constexpr __iterator& operator-=(difference_type __n) |
| 221 | + requires random_access_range<_Base> |
| 222 | + { |
| 223 | + __current_ -= __n; |
| 224 | + return *this; |
| 225 | + } |
| 226 | + |
| 227 | + constexpr decltype(auto) operator[](difference_type __n) const |
| 228 | + noexcept(noexcept(_VSTD::invoke(*__parent_->__func_, __current_[__n]))) |
| 229 | + requires random_access_range<_Base> |
| 230 | + { |
| 231 | + return _VSTD::invoke(*__parent_->__func_, __current_[__n]); |
| 232 | + } |
| 233 | + |
| 234 | + friend constexpr bool operator==(const __iterator& __x, const __iterator& __y) |
| 235 | + requires equality_comparable<iterator_t<_Base>> |
| 236 | + { |
| 237 | + return __x.__current_ == __y.__current_; |
| 238 | + } |
| 239 | + |
| 240 | + friend constexpr bool operator<(const __iterator& __x, const __iterator& __y) |
| 241 | + requires random_access_range<_Base> |
| 242 | + { |
| 243 | + return __x.__current_ < __y.__current_; |
| 244 | + } |
| 245 | + |
| 246 | + friend constexpr bool operator>(const __iterator& __x, const __iterator& __y) |
| 247 | + requires random_access_range<_Base> |
| 248 | + { |
| 249 | + return __x.__current_ > __y.__current_; |
| 250 | + } |
| 251 | + |
| 252 | + friend constexpr bool operator<=(const __iterator& __x, const __iterator& __y) |
| 253 | + requires random_access_range<_Base> |
| 254 | + { |
| 255 | + return __x.__current_ <= __y.__current_; |
| 256 | + } |
| 257 | + |
| 258 | + friend constexpr bool operator>=(const __iterator& __x, const __iterator& __y) |
| 259 | + requires random_access_range<_Base> |
| 260 | + { |
| 261 | + return __x.__current_ >= __y.__current_; |
| 262 | + } |
| 263 | + |
| 264 | +// TODO: Fix this as soon as soon as three_way_comparable is implemented. |
| 265 | +// friend constexpr auto operator<=>(const __iterator& __x, const __iterator& __y) |
| 266 | +// requires random_access_range<_Base> && three_way_comparable<iterator_t<_Base>> |
| 267 | +// { |
| 268 | +// return __x.__current_ <=> __y.__current_; |
| 269 | +// } |
| 270 | + |
| 271 | + friend constexpr __iterator operator+(__iterator __i, difference_type __n) |
| 272 | + requires random_access_range<_Base> |
| 273 | + { |
| 274 | + return __iterator{*__i.__parent_, __i.__current_ + __n}; |
| 275 | + } |
| 276 | + |
| 277 | + friend constexpr __iterator operator+(difference_type __n, __iterator __i) |
| 278 | + requires random_access_range<_Base> |
| 279 | + { |
| 280 | + return __iterator{*__i.__parent_, __i.__current_ + __n}; |
| 281 | + } |
| 282 | + |
| 283 | + friend constexpr __iterator operator-(__iterator __i, difference_type __n) |
| 284 | + requires random_access_range<_Base> |
| 285 | + { |
| 286 | + return __iterator{*__i.__parent_, __i.__current_ - __n}; |
| 287 | + } |
| 288 | + |
| 289 | + friend constexpr difference_type operator-(const __iterator& __x, const __iterator& __y) |
| 290 | + requires sized_sentinel_for<iterator_t<_Base>, iterator_t<_Base>> |
| 291 | + { |
| 292 | + return __x.__current_ - __y.__current_; |
| 293 | + } |
| 294 | + |
| 295 | + friend constexpr decltype(auto) iter_move(const __iterator& __i) |
| 296 | + noexcept(noexcept(*__i)) |
| 297 | + { |
| 298 | + if constexpr (is_lvalue_reference_v<decltype(*__i)>) |
| 299 | + return _VSTD::move(*__i); |
| 300 | + else |
| 301 | + return *__i; |
| 302 | + } |
| 303 | +}; |
| 304 | + |
| 305 | +template<input_range _View, copy_constructible _Fn> |
| 306 | + requires __transform_view_constraints<_View, _Fn> |
| 307 | +template<bool _Const> |
| 308 | +class transform_view<_View, _Fn>::__sentinel { |
| 309 | + using _Parent = __maybe_const<_Const, transform_view>; |
| 310 | + using _Base = __maybe_const<_Const, _View>; |
| 311 | + |
| 312 | + sentinel_t<_Base> __end_ = sentinel_t<_Base>(); |
| 313 | + |
| 314 | + template<bool> |
| 315 | + friend class transform_view<_View, _Fn>::__iterator; |
| 316 | + |
| 317 | + template<bool> |
| 318 | + friend class transform_view<_View, _Fn>::__sentinel; |
| 319 | + |
| 320 | +public: |
| 321 | + __sentinel() = default; |
| 322 | + |
| 323 | + constexpr explicit __sentinel(sentinel_t<_Base> __end_) : __end_(__end_) {} |
| 324 | + |
| 325 | + // Note: `__i` should always be `__sentinel<false>`, but directly using |
| 326 | + // `__sentinel<false>` is ill-formed when `_Const` is false |
| 327 | + // (see http://wg21.link/class.copy.ctor#5). |
| 328 | + constexpr __sentinel(__sentinel<!_Const> __i) |
| 329 | + requires _Const && convertible_to<sentinel_t<_View>, sentinel_t<_Base>> |
| 330 | + : __end_(_VSTD::move(__i.__end_)) {} |
| 331 | + |
| 332 | + constexpr sentinel_t<_Base> base() const { return __end_; } |
| 333 | + |
| 334 | + template<bool _OtherConst> |
| 335 | + requires sentinel_for<sentinel_t<_Base>, iterator_t<__maybe_const<_OtherConst, _View>>> |
| 336 | + friend constexpr bool operator==(const __iterator<_OtherConst>& __x, const __sentinel& __y) { |
| 337 | + return __x.__current_ == __y.__end_; |
| 338 | + } |
| 339 | + |
| 340 | + template<bool _OtherConst> |
| 341 | + requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<__maybe_const<_OtherConst, _View>>> |
| 342 | + friend constexpr range_difference_t<__maybe_const<_OtherConst, _View>> |
| 343 | + operator-(const __iterator<_OtherConst>& __x, const __sentinel& __y) { |
| 344 | + return __x.__current_ - __y.__end_; |
| 345 | + } |
| 346 | + |
| 347 | + template<bool _OtherConst> |
| 348 | + requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<__maybe_const<_OtherConst, _View>>> |
| 349 | + friend constexpr range_difference_t<__maybe_const<_OtherConst, _View>> |
| 350 | + operator-(const __sentinel& __x, const __iterator<_OtherConst>& __y) { |
| 351 | + return __x.__end_ - __y.__current_; |
| 352 | + } |
| 353 | +}; |
| 354 | + |
| 355 | +} // namespace ranges |
| 356 | + |
| 357 | +#endif // !defined(_LIBCPP_HAS_NO_RANGES) |
| 358 | + |
| 359 | +_LIBCPP_END_NAMESPACE_STD |
| 360 | + |
| 361 | +_LIBCPP_POP_MACROS |
| 362 | + |
| 363 | +#endif // _LIBCPP___RANGES_TRANSFORM_VIEW_H |
0 commit comments