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