| 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_ZIP_TRANSFORM_VIEW_H |
| 11 | #define _LIBCPP___RANGES_ZIP_TRANSFORM_VIEW_H |
| 12 | |
| 13 | #include <__config> |
| 14 | |
| 15 | #include <__concepts/constructible.h> |
| 16 | #include <__concepts/convertible_to.h> |
| 17 | #include <__concepts/derived_from.h> |
| 18 | #include <__concepts/equality_comparable.h> |
| 19 | #include <__concepts/invocable.h> |
| 20 | #include <__functional/invoke.h> |
| 21 | #include <__iterator/concepts.h> |
| 22 | #include <__iterator/incrementable_traits.h> |
| 23 | #include <__iterator/iterator_traits.h> |
| 24 | #include <__memory/addressof.h> |
| 25 | #include <__ranges/access.h> |
| 26 | #include <__ranges/all.h> |
| 27 | #include <__ranges/concepts.h> |
| 28 | #include <__ranges/empty_view.h> |
| 29 | #include <__ranges/movable_box.h> |
| 30 | #include <__ranges/view_interface.h> |
| 31 | #include <__ranges/zip_view.h> |
| 32 | #include <__type_traits/decay.h> |
| 33 | #include <__type_traits/invoke.h> |
| 34 | #include <__type_traits/is_object.h> |
| 35 | #include <__type_traits/is_reference.h> |
| 36 | #include <__type_traits/is_referenceable.h> |
| 37 | #include <__type_traits/maybe_const.h> |
| 38 | #include <__type_traits/remove_cvref.h> |
| 39 | #include <__utility/forward.h> |
| 40 | #include <__utility/in_place.h> |
| 41 | #include <__utility/move.h> |
| 42 | #include <tuple> // for std::apply |
| 43 | |
| 44 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 45 | # pragma GCC system_header |
| 46 | #endif |
| 47 | |
| 48 | _LIBCPP_PUSH_MACROS |
| 49 | #include <__undef_macros> |
| 50 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 51 | |
| 52 | #if _LIBCPP_STD_VER >= 23 |
| 53 | |
| 54 | namespace ranges { |
| 55 | |
| 56 | template <move_constructible _Fn, input_range... _Views> |
| 57 | requires(view<_Views> && ...) && |
| 58 | (sizeof...(_Views) > 0) && is_object_v<_Fn> && regular_invocable<_Fn&, range_reference_t<_Views>...> && |
| 59 | __referenceable<invoke_result_t<_Fn&, range_reference_t<_Views>...>> |
| 60 | class zip_transform_view : public view_interface<zip_transform_view<_Fn, _Views...>> { |
| 61 | _LIBCPP_NO_UNIQUE_ADDRESS zip_view<_Views...> __zip_; |
| 62 | _LIBCPP_NO_UNIQUE_ADDRESS __movable_box<_Fn> __fun_; |
| 63 | |
| 64 | using _InnerView _LIBCPP_NODEBUG = zip_view<_Views...>; |
| 65 | template <bool _Const> |
| 66 | using __ziperator _LIBCPP_NODEBUG = iterator_t<__maybe_const<_Const, _InnerView>>; |
| 67 | template <bool _Const> |
| 68 | using __zentinel _LIBCPP_NODEBUG = sentinel_t<__maybe_const<_Const, _InnerView>>; |
| 69 | |
| 70 | template <bool> |
| 71 | class __iterator; |
| 72 | |
| 73 | template <bool> |
| 74 | class __sentinel; |
| 75 | |
| 76 | public: |
| 77 | _LIBCPP_HIDE_FROM_ABI zip_transform_view() = default; |
| 78 | |
| 79 | _LIBCPP_HIDE_FROM_ABI constexpr explicit zip_transform_view(_Fn __fun, _Views... __views) |
| 80 | : __zip_(std::move(__views)...), __fun_(in_place, std::move(__fun)) {} |
| 81 | |
| 82 | _LIBCPP_HIDE_FROM_ABI constexpr auto begin() { return __iterator<false>(*this, __zip_.begin()); } |
| 83 | |
| 84 | _LIBCPP_HIDE_FROM_ABI constexpr auto begin() const |
| 85 | requires range<const _InnerView> && regular_invocable<const _Fn&, range_reference_t<const _Views>...> |
| 86 | { |
| 87 | return __iterator<true>(*this, __zip_.begin()); |
| 88 | } |
| 89 | |
| 90 | _LIBCPP_HIDE_FROM_ABI constexpr auto end() { |
| 91 | if constexpr (common_range<_InnerView>) { |
| 92 | return __iterator<false>(*this, __zip_.end()); |
| 93 | } else { |
| 94 | return __sentinel<false>(__zip_.end()); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | _LIBCPP_HIDE_FROM_ABI constexpr auto end() const |
| 99 | requires range<const _InnerView> && regular_invocable<const _Fn&, range_reference_t<const _Views>...> |
| 100 | { |
| 101 | if constexpr (common_range<const _InnerView>) { |
| 102 | return __iterator<true>(*this, __zip_.end()); |
| 103 | } else { |
| 104 | return __sentinel<true>(__zip_.end()); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | _LIBCPP_HIDE_FROM_ABI constexpr auto size() |
| 109 | requires sized_range<_InnerView> |
| 110 | { |
| 111 | return __zip_.size(); |
| 112 | } |
| 113 | |
| 114 | _LIBCPP_HIDE_FROM_ABI constexpr auto size() const |
| 115 | requires sized_range<const _InnerView> |
| 116 | { |
| 117 | return __zip_.size(); |
| 118 | } |
| 119 | }; |
| 120 | |
| 121 | template <class _Fn, class... _Ranges> |
| 122 | zip_transform_view(_Fn, _Ranges&&...) -> zip_transform_view<_Fn, views::all_t<_Ranges>...>; |
| 123 | |
| 124 | template <bool _Const, class _Fn, class... _Views> |
| 125 | struct __zip_transform_iterator_category_base {}; |
| 126 | |
| 127 | template <bool _Const, class _Fn, class... _Views> |
| 128 | requires forward_range<__maybe_const<_Const, zip_view<_Views...>>> |
| 129 | struct __zip_transform_iterator_category_base<_Const, _Fn, _Views...> { |
| 130 | private: |
| 131 | template <class _View> |
| 132 | using __tag _LIBCPP_NODEBUG = typename iterator_traits<iterator_t<__maybe_const<_Const, _View>>>::iterator_category; |
| 133 | |
| 134 | static consteval auto __get_iterator_category() { |
| 135 | if constexpr (!is_reference_v<invoke_result_t<__maybe_const<_Const, _Fn>&, |
| 136 | range_reference_t<__maybe_const<_Const, _Views>>...>>) { |
| 137 | return input_iterator_tag(); |
| 138 | } else if constexpr ((derived_from<__tag<_Views>, random_access_iterator_tag> && ...)) { |
| 139 | return random_access_iterator_tag(); |
| 140 | } else if constexpr ((derived_from<__tag<_Views>, bidirectional_iterator_tag> && ...)) { |
| 141 | return bidirectional_iterator_tag(); |
| 142 | } else if constexpr ((derived_from<__tag<_Views>, forward_iterator_tag> && ...)) { |
| 143 | return forward_iterator_tag(); |
| 144 | } else { |
| 145 | return input_iterator_tag(); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | public: |
| 150 | using iterator_category = decltype(__get_iterator_category()); |
| 151 | }; |
| 152 | |
| 153 | template <move_constructible _Fn, input_range... _Views> |
| 154 | requires(view<_Views> && ...) && |
| 155 | (sizeof...(_Views) > 0) && is_object_v<_Fn> && regular_invocable<_Fn&, range_reference_t<_Views>...> && |
| 156 | __referenceable<invoke_result_t<_Fn&, range_reference_t<_Views>...>> |
| 157 | template <bool _Const> |
| 158 | class zip_transform_view<_Fn, _Views...>::__iterator |
| 159 | : public __zip_transform_iterator_category_base<_Const, _Fn, _Views...> { |
| 160 | using _Parent _LIBCPP_NODEBUG = __maybe_const<_Const, zip_transform_view>; |
| 161 | using _Base _LIBCPP_NODEBUG = __maybe_const<_Const, _InnerView>; |
| 162 | |
| 163 | friend zip_transform_view<_Fn, _Views...>; |
| 164 | |
| 165 | _Parent* __parent_ = nullptr; |
| 166 | __ziperator<_Const> __inner_; |
| 167 | |
| 168 | _LIBCPP_HIDE_FROM_ABI constexpr __iterator(_Parent& __parent, __ziperator<_Const> __inner) |
| 169 | : __parent_(std::addressof(__parent)), __inner_(std::move(__inner)) {} |
| 170 | |
| 171 | _LIBCPP_HIDE_FROM_ABI constexpr auto __get_deref_and_invoke() const noexcept { |
| 172 | return [&__fun = *__parent_->__fun_](const auto&... __iters) noexcept(noexcept(std::invoke( |
| 173 | *__parent_->__fun_, *__iters...))) -> decltype(auto) { return std::invoke(__fun, *__iters...); }; |
| 174 | } |
| 175 | |
| 176 | public: |
| 177 | using iterator_concept = typename __ziperator<_Const>::iterator_concept; |
| 178 | using value_type = |
| 179 | remove_cvref_t<invoke_result_t<__maybe_const<_Const, _Fn>&, range_reference_t<__maybe_const<_Const, _Views>>...>>; |
| 180 | using difference_type = range_difference_t<_Base>; |
| 181 | |
| 182 | _LIBCPP_HIDE_FROM_ABI __iterator() = default; |
| 183 | _LIBCPP_HIDE_FROM_ABI constexpr __iterator(__iterator<!_Const> __i) |
| 184 | requires _Const && convertible_to<__ziperator<false>, __ziperator<_Const>> |
| 185 | : __parent_(__i.__parent_), __inner_(std::move(__i.__inner_)) {} |
| 186 | |
| 187 | _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator*() const |
| 188 | noexcept(noexcept(std::apply(__get_deref_and_invoke(), __zip_view_iterator_access::__get_underlying(__inner_)))) { |
| 189 | return std::apply(__get_deref_and_invoke(), __zip_view_iterator_access::__get_underlying(__inner_)); |
| 190 | } |
| 191 | |
| 192 | _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator++() { |
| 193 | ++__inner_; |
| 194 | return *this; |
| 195 | } |
| 196 | |
| 197 | _LIBCPP_HIDE_FROM_ABI constexpr void operator++(int) { ++*this; } |
| 198 | |
| 199 | _LIBCPP_HIDE_FROM_ABI constexpr __iterator operator++(int) |
| 200 | requires forward_range<_Base> |
| 201 | { |
| 202 | auto __tmp = *this; |
| 203 | ++*this; |
| 204 | return __tmp; |
| 205 | } |
| 206 | |
| 207 | _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator--() |
| 208 | requires bidirectional_range<_Base> |
| 209 | { |
| 210 | --__inner_; |
| 211 | return *this; |
| 212 | } |
| 213 | |
| 214 | _LIBCPP_HIDE_FROM_ABI constexpr __iterator operator--(int) |
| 215 | requires bidirectional_range<_Base> |
| 216 | { |
| 217 | auto __tmp = *this; |
| 218 | --*this; |
| 219 | return __tmp; |
| 220 | } |
| 221 | |
| 222 | _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator+=(difference_type __x) |
| 223 | requires random_access_range<_Base> |
| 224 | { |
| 225 | __inner_ += __x; |
| 226 | return *this; |
| 227 | } |
| 228 | |
| 229 | _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator-=(difference_type __x) |
| 230 | requires random_access_range<_Base> |
| 231 | { |
| 232 | __inner_ -= __x; |
| 233 | return *this; |
| 234 | } |
| 235 | |
| 236 | _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator[](difference_type __n) const |
| 237 | requires random_access_range<_Base> |
| 238 | { |
| 239 | return std::apply( |
| 240 | [&]<class... _Is>(const _Is&... __iters) -> decltype(auto) { |
| 241 | return std::invoke(*__parent_->__fun_, __iters[iter_difference_t<_Is>(__n)]...); |
| 242 | }, |
| 243 | __zip_view_iterator_access::__get_underlying(__inner_)); |
| 244 | } |
| 245 | |
| 246 | _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __iterator& __x, const __iterator& __y) |
| 247 | requires equality_comparable<__ziperator<_Const>> |
| 248 | { |
| 249 | return __x.__inner_ == __y.__inner_; |
| 250 | } |
| 251 | |
| 252 | _LIBCPP_HIDE_FROM_ABI friend constexpr auto operator<=>(const __iterator& __x, const __iterator& __y) |
| 253 | requires random_access_range<_Base> |
| 254 | { |
| 255 | return __x.__inner_ <=> __y.__inner_; |
| 256 | } |
| 257 | |
| 258 | _LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator+(const __iterator& __i, difference_type __n) |
| 259 | requires random_access_range<_Base> |
| 260 | { |
| 261 | return __iterator(*__i.__parent_, __i.__inner_ + __n); |
| 262 | } |
| 263 | |
| 264 | _LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator+(difference_type __n, const __iterator& __i) |
| 265 | requires random_access_range<_Base> |
| 266 | { |
| 267 | return __iterator(*__i.__parent_, __i.__inner_ + __n); |
| 268 | } |
| 269 | |
| 270 | _LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator-(const __iterator& __i, difference_type __n) |
| 271 | requires random_access_range<_Base> |
| 272 | { |
| 273 | return __iterator(*__i.__parent_, __i.__inner_ - __n); |
| 274 | } |
| 275 | |
| 276 | _LIBCPP_HIDE_FROM_ABI friend constexpr difference_type operator-(const __iterator& __x, const __iterator& __y) |
| 277 | requires sized_sentinel_for<__ziperator<_Const>, __ziperator<_Const>> |
| 278 | { |
| 279 | return __x.__inner_ - __y.__inner_; |
| 280 | } |
| 281 | }; |
| 282 | |
| 283 | template <move_constructible _Fn, input_range... _Views> |
| 284 | requires(view<_Views> && ...) && |
| 285 | (sizeof...(_Views) > 0) && is_object_v<_Fn> && regular_invocable<_Fn&, range_reference_t<_Views>...> && |
| 286 | __referenceable<invoke_result_t<_Fn&, range_reference_t<_Views>...>> |
| 287 | template <bool _Const> |
| 288 | class zip_transform_view<_Fn, _Views...>::__sentinel { |
| 289 | __zentinel<_Const> __inner_; |
| 290 | |
| 291 | friend zip_transform_view<_Fn, _Views...>; |
| 292 | |
| 293 | _LIBCPP_HIDE_FROM_ABI constexpr explicit __sentinel(__zentinel<_Const> __inner) : __inner_(__inner) {} |
| 294 | |
| 295 | public: |
| 296 | _LIBCPP_HIDE_FROM_ABI __sentinel() = default; |
| 297 | |
| 298 | _LIBCPP_HIDE_FROM_ABI constexpr __sentinel(__sentinel<!_Const> __i) |
| 299 | requires _Const && convertible_to<__zentinel<false>, __zentinel<_Const>> |
| 300 | : __inner_(__i.__inner_) {} |
| 301 | |
| 302 | template <bool _OtherConst> |
| 303 | requires sentinel_for<__zentinel<_Const>, __ziperator<_OtherConst>> |
| 304 | _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __iterator<_OtherConst>& __x, const __sentinel& __y) { |
| 305 | return __x.__inner_ == __y.__inner_; |
| 306 | } |
| 307 | |
| 308 | template <bool _OtherConst> |
| 309 | requires sized_sentinel_for<__zentinel<_Const>, __ziperator<_OtherConst>> |
| 310 | _LIBCPP_HIDE_FROM_ABI friend constexpr range_difference_t<__maybe_const<_OtherConst, _InnerView>> |
| 311 | operator-(const __iterator<_OtherConst>& __x, const __sentinel& __y) { |
| 312 | return __x.__inner_ - __y.__inner_; |
| 313 | } |
| 314 | |
| 315 | template <bool _OtherConst> |
| 316 | requires sized_sentinel_for<__zentinel<_Const>, __ziperator<_OtherConst>> |
| 317 | _LIBCPP_HIDE_FROM_ABI friend constexpr range_difference_t<__maybe_const<_OtherConst, _InnerView>> |
| 318 | operator-(const __sentinel& __x, const __iterator<_OtherConst>& __y) { |
| 319 | return __x.__inner_ - __y.__inner_; |
| 320 | } |
| 321 | }; |
| 322 | |
| 323 | namespace views { |
| 324 | namespace __zip_transform { |
| 325 | |
| 326 | struct __fn { |
| 327 | template <class _Fn> |
| 328 | requires(move_constructible<decay_t<_Fn>> && regular_invocable<decay_t<_Fn>&> && |
| 329 | is_object_v<invoke_result_t<decay_t<_Fn>&>>) |
| 330 | _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Fn&&) const |
| 331 | noexcept(noexcept(auto(views::empty<decay_t<invoke_result_t<decay_t<_Fn>&>>>))) { |
| 332 | return views::empty<decay_t<invoke_result_t<decay_t<_Fn>&>>>; |
| 333 | } |
| 334 | |
| 335 | template <class _Fn, class... _Ranges> |
| 336 | requires(sizeof...(_Ranges) > 0) |
| 337 | _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Fn&& __fun, _Ranges&&... __rs) const |
| 338 | noexcept(noexcept(zip_transform_view(std::forward<_Fn>(__fun), std::forward<_Ranges>(__rs)...))) |
| 339 | -> decltype(zip_transform_view(std::forward<_Fn>(__fun), std::forward<_Ranges>(__rs)...)) { |
| 340 | return zip_transform_view(std::forward<_Fn>(__fun), std::forward<_Ranges>(__rs)...); |
| 341 | } |
| 342 | }; |
| 343 | |
| 344 | } // namespace __zip_transform |
| 345 | inline namespace __cpo { |
| 346 | inline constexpr auto zip_transform = __zip_transform::__fn{}; |
| 347 | } // namespace __cpo |
| 348 | } // namespace views |
| 349 | } // namespace ranges |
| 350 | |
| 351 | #endif // _LIBCPP_STD_VER >= 23 |
| 352 | |
| 353 | _LIBCPP_END_NAMESPACE_STD |
| 354 | |
| 355 | _LIBCPP_POP_MACROS |
| 356 | |
| 357 | #endif // _LIBCPP___RANGES_ZIP_TRANSFORM_VIEW_H |
| 358 | |