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_TRANSFORM_VIEW_H 11 #define _LIBCPP___RANGES_TRANSFORM_VIEW_H 12 13 #include <__compare/three_way_comparable.h> 14 #include <__concepts/constructible.h> 15 #include <__concepts/convertible_to.h> 16 #include <__concepts/copyable.h> 17 #include <__concepts/derived_from.h> 18 #include <__concepts/equality_comparable.h> 19 #include <__concepts/invocable.h> 20 #include <__config> 21 #include <__functional/bind_back.h> 22 #include <__functional/invoke.h> 23 #include <__functional/perfect_forward.h> 24 #include <__iterator/concepts.h> 25 #include <__iterator/iterator_traits.h> 26 #include <__memory/addressof.h> 27 #include <__ranges/access.h> 28 #include <__ranges/all.h> 29 #include <__ranges/concepts.h> 30 #include <__ranges/empty.h> 31 #include <__ranges/movable_box.h> 32 #include <__ranges/range_adaptor.h> 33 #include <__ranges/size.h> 34 #include <__ranges/view_interface.h> 35 #include <__type_traits/conditional.h> 36 #include <__type_traits/decay.h> 37 #include <__type_traits/is_nothrow_constructible.h> 38 #include <__type_traits/is_object.h> 39 #include <__type_traits/is_reference.h> 40 #include <__type_traits/maybe_const.h> 41 #include <__type_traits/remove_cvref.h> 42 #include <__utility/forward.h> 43 #include <__utility/in_place.h> 44 #include <__utility/move.h> 45 46 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 47 # pragma GCC system_header 48 #endif 49 50 _LIBCPP_BEGIN_NAMESPACE_STD 51 52 #if _LIBCPP_STD_VER >= 20 53 54 namespace ranges { 55 56 template<class _Fn, class _View> 57 concept __regular_invocable_with_range_ref = 58 regular_invocable<_Fn, range_reference_t<_View>>; 59 60 template<class _View, class _Fn> 61 concept __transform_view_constraints = 62 view<_View> && is_object_v<_Fn> && 63 regular_invocable<_Fn&, range_reference_t<_View>> && 64 __can_reference<invoke_result_t<_Fn&, range_reference_t<_View>>>; 65 66 # if _LIBCPP_STD_VER >= 23 67 template <input_range _View, move_constructible _Fn> 68 # else 69 template <input_range _View, copy_constructible _Fn> 70 # endif 71 requires __transform_view_constraints<_View, _Fn> 72 class transform_view : public view_interface<transform_view<_View, _Fn>> { 73 template<bool> class __iterator; 74 template<bool> class __sentinel; 75 76 _LIBCPP_NO_UNIQUE_ADDRESS __movable_box<_Fn> __func_; 77 _LIBCPP_NO_UNIQUE_ADDRESS _View __base_ = _View(); 78 79 public: 80 _LIBCPP_HIDE_FROM_ABI 81 transform_view() 82 requires default_initializable<_View> && default_initializable<_Fn> = default; 83 84 _LIBCPP_HIDE_FROM_ABI 85 constexpr _LIBCPP_EXPLICIT_SINCE_CXX23 transform_view(_View __base, _Fn __func) 86 : __func_(std::in_place, std::move(__func)), __base_(std::move(__base)) {} 87 88 _LIBCPP_HIDE_FROM_ABI 89 constexpr _View base() const& requires copy_constructible<_View> { return __base_; } 90 _LIBCPP_HIDE_FROM_ABI 91 constexpr _View base() && { return std::move(__base_); } 92 93 _LIBCPP_HIDE_FROM_ABI 94 constexpr __iterator<false> begin() { 95 return __iterator<false>{*this, ranges::begin(__base_)}; 96 } 97 _LIBCPP_HIDE_FROM_ABI 98 constexpr __iterator<true> begin() const 99 requires range<const _View> && 100 __regular_invocable_with_range_ref<const _Fn&, const _View> 101 { 102 return __iterator<true>(*this, ranges::begin(__base_)); 103 } 104 105 _LIBCPP_HIDE_FROM_ABI 106 constexpr __sentinel<false> end() { 107 return __sentinel<false>(ranges::end(__base_)); 108 } 109 _LIBCPP_HIDE_FROM_ABI 110 constexpr __iterator<false> end() 111 requires common_range<_View> 112 { 113 return __iterator<false>(*this, ranges::end(__base_)); 114 } 115 _LIBCPP_HIDE_FROM_ABI 116 constexpr __sentinel<true> end() const 117 requires range<const _View> && 118 __regular_invocable_with_range_ref<const _Fn&, const _View> 119 { 120 return __sentinel<true>(ranges::end(__base_)); 121 } 122 _LIBCPP_HIDE_FROM_ABI 123 constexpr __iterator<true> end() const 124 requires common_range<const _View> && 125 __regular_invocable_with_range_ref<const _Fn&, const _View> 126 { 127 return __iterator<true>(*this, ranges::end(__base_)); 128 } 129 130 _LIBCPP_HIDE_FROM_ABI 131 constexpr auto size() requires sized_range<_View> { return ranges::size(__base_); } 132 _LIBCPP_HIDE_FROM_ABI 133 constexpr auto size() const requires sized_range<const _View> { return ranges::size(__base_); } 134 }; 135 136 template<class _Range, class _Fn> 137 transform_view(_Range&&, _Fn) -> transform_view<views::all_t<_Range>, _Fn>; 138 139 template<class _View> 140 struct __transform_view_iterator_concept { using type = input_iterator_tag; }; 141 142 template<random_access_range _View> 143 struct __transform_view_iterator_concept<_View> { using type = random_access_iterator_tag; }; 144 145 template<bidirectional_range _View> 146 struct __transform_view_iterator_concept<_View> { using type = bidirectional_iterator_tag; }; 147 148 template<forward_range _View> 149 struct __transform_view_iterator_concept<_View> { using type = forward_iterator_tag; }; 150 151 template<class, class> 152 struct __transform_view_iterator_category_base {}; 153 154 template<forward_range _View, class _Fn> 155 struct __transform_view_iterator_category_base<_View, _Fn> { 156 using _Cat = typename iterator_traits<iterator_t<_View>>::iterator_category; 157 158 using iterator_category = conditional_t< 159 is_reference_v<invoke_result_t<_Fn&, range_reference_t<_View>>>, 160 conditional_t< 161 derived_from<_Cat, contiguous_iterator_tag>, 162 random_access_iterator_tag, 163 _Cat 164 >, 165 input_iterator_tag 166 >; 167 }; 168 169 # if _LIBCPP_STD_VER >= 23 170 template <input_range _View, move_constructible _Fn> 171 # else 172 template <input_range _View, copy_constructible _Fn> 173 # endif 174 requires __transform_view_constraints<_View, _Fn> 175 template <bool _Const> 176 class transform_view<_View, _Fn>::__iterator : public __transform_view_iterator_category_base<_View, _Fn> { 177 178 using _Parent = __maybe_const<_Const, transform_view>; 179 using _Base = __maybe_const<_Const, _View>; 180 181 _Parent *__parent_ = nullptr; 182 183 template<bool> 184 friend class transform_view<_View, _Fn>::__iterator; 185 186 template<bool> 187 friend class transform_view<_View, _Fn>::__sentinel; 188 189 public: 190 iterator_t<_Base> __current_ = iterator_t<_Base>(); 191 192 using iterator_concept = typename __transform_view_iterator_concept<_View>::type; 193 using value_type = remove_cvref_t<invoke_result_t<_Fn&, range_reference_t<_Base>>>; 194 using difference_type = range_difference_t<_Base>; 195 196 _LIBCPP_HIDE_FROM_ABI 197 __iterator() requires default_initializable<iterator_t<_Base>> = default; 198 199 _LIBCPP_HIDE_FROM_ABI 200 constexpr __iterator(_Parent& __parent, iterator_t<_Base> __current) 201 : __parent_(std::addressof(__parent)), __current_(std::move(__current)) {} 202 203 // Note: `__i` should always be `__iterator<false>`, but directly using 204 // `__iterator<false>` is ill-formed when `_Const` is false 205 // (see http://wg21.link/class.copy.ctor#5). 206 _LIBCPP_HIDE_FROM_ABI 207 constexpr __iterator(__iterator<!_Const> __i) 208 requires _Const && convertible_to<iterator_t<_View>, iterator_t<_Base>> 209 : __parent_(__i.__parent_), __current_(std::move(__i.__current_)) {} 210 211 _LIBCPP_HIDE_FROM_ABI 212 constexpr const iterator_t<_Base>& base() const& noexcept { 213 return __current_; 214 } 215 216 _LIBCPP_HIDE_FROM_ABI 217 constexpr iterator_t<_Base> base() && { 218 return std::move(__current_); 219 } 220 221 _LIBCPP_HIDE_FROM_ABI 222 constexpr decltype(auto) operator*() const 223 noexcept(noexcept(std::invoke(*__parent_->__func_, *__current_))) 224 { 225 return std::invoke(*__parent_->__func_, *__current_); 226 } 227 228 _LIBCPP_HIDE_FROM_ABI 229 constexpr __iterator& operator++() { 230 ++__current_; 231 return *this; 232 } 233 234 _LIBCPP_HIDE_FROM_ABI 235 constexpr void operator++(int) { ++__current_; } 236 237 _LIBCPP_HIDE_FROM_ABI 238 constexpr __iterator operator++(int) 239 requires forward_range<_Base> 240 { 241 auto __tmp = *this; 242 ++*this; 243 return __tmp; 244 } 245 246 _LIBCPP_HIDE_FROM_ABI 247 constexpr __iterator& operator--() 248 requires bidirectional_range<_Base> 249 { 250 --__current_; 251 return *this; 252 } 253 254 _LIBCPP_HIDE_FROM_ABI 255 constexpr __iterator operator--(int) 256 requires bidirectional_range<_Base> 257 { 258 auto __tmp = *this; 259 --*this; 260 return __tmp; 261 } 262 263 _LIBCPP_HIDE_FROM_ABI 264 constexpr __iterator& operator+=(difference_type __n) 265 requires random_access_range<_Base> 266 { 267 __current_ += __n; 268 return *this; 269 } 270 271 _LIBCPP_HIDE_FROM_ABI 272 constexpr __iterator& operator-=(difference_type __n) 273 requires random_access_range<_Base> 274 { 275 __current_ -= __n; 276 return *this; 277 } 278 279 _LIBCPP_HIDE_FROM_ABI 280 constexpr decltype(auto) operator[](difference_type __n) const 281 noexcept(noexcept(std::invoke(*__parent_->__func_, __current_[__n]))) 282 requires random_access_range<_Base> 283 { 284 return std::invoke(*__parent_->__func_, __current_[__n]); 285 } 286 287 _LIBCPP_HIDE_FROM_ABI 288 friend constexpr bool operator==(const __iterator& __x, const __iterator& __y) 289 requires equality_comparable<iterator_t<_Base>> 290 { 291 return __x.__current_ == __y.__current_; 292 } 293 294 _LIBCPP_HIDE_FROM_ABI 295 friend constexpr bool operator<(const __iterator& __x, const __iterator& __y) 296 requires random_access_range<_Base> 297 { 298 return __x.__current_ < __y.__current_; 299 } 300 301 _LIBCPP_HIDE_FROM_ABI 302 friend constexpr bool operator>(const __iterator& __x, const __iterator& __y) 303 requires random_access_range<_Base> 304 { 305 return __x.__current_ > __y.__current_; 306 } 307 308 _LIBCPP_HIDE_FROM_ABI 309 friend constexpr bool operator<=(const __iterator& __x, const __iterator& __y) 310 requires random_access_range<_Base> 311 { 312 return __x.__current_ <= __y.__current_; 313 } 314 315 _LIBCPP_HIDE_FROM_ABI 316 friend constexpr bool operator>=(const __iterator& __x, const __iterator& __y) 317 requires random_access_range<_Base> 318 { 319 return __x.__current_ >= __y.__current_; 320 } 321 322 _LIBCPP_HIDE_FROM_ABI 323 friend constexpr auto operator<=>(const __iterator& __x, const __iterator& __y) 324 requires random_access_range<_Base> && three_way_comparable<iterator_t<_Base>> 325 { 326 return __x.__current_ <=> __y.__current_; 327 } 328 329 _LIBCPP_HIDE_FROM_ABI 330 friend constexpr __iterator operator+(__iterator __i, difference_type __n) 331 requires random_access_range<_Base> 332 { 333 return __iterator{*__i.__parent_, __i.__current_ + __n}; 334 } 335 336 _LIBCPP_HIDE_FROM_ABI 337 friend constexpr __iterator operator+(difference_type __n, __iterator __i) 338 requires random_access_range<_Base> 339 { 340 return __iterator{*__i.__parent_, __i.__current_ + __n}; 341 } 342 343 _LIBCPP_HIDE_FROM_ABI 344 friend constexpr __iterator operator-(__iterator __i, difference_type __n) 345 requires random_access_range<_Base> 346 { 347 return __iterator{*__i.__parent_, __i.__current_ - __n}; 348 } 349 350 _LIBCPP_HIDE_FROM_ABI 351 friend constexpr difference_type operator-(const __iterator& __x, const __iterator& __y) 352 requires sized_sentinel_for<iterator_t<_Base>, iterator_t<_Base>> 353 { 354 return __x.__current_ - __y.__current_; 355 } 356 357 _LIBCPP_HIDE_FROM_ABI 358 friend constexpr decltype(auto) iter_move(const __iterator& __i) 359 noexcept(noexcept(*__i)) 360 { 361 if constexpr (is_lvalue_reference_v<decltype(*__i)>) 362 return std::move(*__i); 363 else 364 return *__i; 365 } 366 }; 367 368 # if _LIBCPP_STD_VER >= 23 369 template <input_range _View, move_constructible _Fn> 370 # else 371 template <input_range _View, copy_constructible _Fn> 372 # endif 373 requires __transform_view_constraints<_View, _Fn> 374 template <bool _Const> 375 class transform_view<_View, _Fn>::__sentinel { 376 using _Parent = __maybe_const<_Const, transform_view>; 377 using _Base = __maybe_const<_Const, _View>; 378 379 sentinel_t<_Base> __end_ = sentinel_t<_Base>(); 380 381 template<bool> 382 friend class transform_view<_View, _Fn>::__iterator; 383 384 template<bool> 385 friend class transform_view<_View, _Fn>::__sentinel; 386 387 public: 388 _LIBCPP_HIDE_FROM_ABI 389 __sentinel() = default; 390 391 _LIBCPP_HIDE_FROM_ABI 392 constexpr explicit __sentinel(sentinel_t<_Base> __end) : __end_(__end) {} 393 394 // Note: `__i` should always be `__sentinel<false>`, but directly using 395 // `__sentinel<false>` is ill-formed when `_Const` is false 396 // (see http://wg21.link/class.copy.ctor#5). 397 _LIBCPP_HIDE_FROM_ABI 398 constexpr __sentinel(__sentinel<!_Const> __i) 399 requires _Const && convertible_to<sentinel_t<_View>, sentinel_t<_Base>> 400 : __end_(std::move(__i.__end_)) {} 401 402 _LIBCPP_HIDE_FROM_ABI 403 constexpr sentinel_t<_Base> base() const { return __end_; } 404 405 template<bool _OtherConst> 406 requires sentinel_for<sentinel_t<_Base>, iterator_t<__maybe_const<_OtherConst, _View>>> 407 _LIBCPP_HIDE_FROM_ABI 408 friend constexpr bool operator==(const __iterator<_OtherConst>& __x, const __sentinel& __y) { 409 return __x.__current_ == __y.__end_; 410 } 411 412 template<bool _OtherConst> 413 requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<__maybe_const<_OtherConst, _View>>> 414 _LIBCPP_HIDE_FROM_ABI 415 friend constexpr range_difference_t<__maybe_const<_OtherConst, _View>> 416 operator-(const __iterator<_OtherConst>& __x, const __sentinel& __y) { 417 return __x.__current_ - __y.__end_; 418 } 419 420 template<bool _OtherConst> 421 requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<__maybe_const<_OtherConst, _View>>> 422 _LIBCPP_HIDE_FROM_ABI 423 friend constexpr range_difference_t<__maybe_const<_OtherConst, _View>> 424 operator-(const __sentinel& __x, const __iterator<_OtherConst>& __y) { 425 return __x.__end_ - __y.__current_; 426 } 427 }; 428 429 namespace views { 430 namespace __transform { 431 struct __fn { 432 template<class _Range, class _Fn> 433 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI 434 constexpr auto operator()(_Range&& __range, _Fn&& __f) const 435 noexcept(noexcept(transform_view(std::forward<_Range>(__range), std::forward<_Fn>(__f)))) 436 -> decltype( transform_view(std::forward<_Range>(__range), std::forward<_Fn>(__f))) 437 { return transform_view(std::forward<_Range>(__range), std::forward<_Fn>(__f)); } 438 439 template<class _Fn> 440 requires constructible_from<decay_t<_Fn>, _Fn> 441 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI 442 constexpr auto operator()(_Fn&& __f) const 443 noexcept(is_nothrow_constructible_v<decay_t<_Fn>, _Fn>) 444 { return __range_adaptor_closure_t(std::__bind_back(*this, std::forward<_Fn>(__f))); } 445 }; 446 } // namespace __transform 447 448 inline namespace __cpo { 449 inline constexpr auto transform = __transform::__fn{}; 450 } // namespace __cpo 451 } // namespace views 452 453 } // namespace ranges 454 455 #endif // _LIBCPP_STD_VER >= 20 456 457 _LIBCPP_END_NAMESPACE_STD 458 459 #endif // _LIBCPP___RANGES_TRANSFORM_VIEW_H 460