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