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