================ @@ -0,0 +1,637 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP___RANGES_CONCAT_VIEW_H +#define _LIBCPP___RANGES_CONCAT_VIEW_H + +#include <__algorithm/ranges_find_if.h> +#include <__assert> +#include <__concepts/common_reference_with.h> +#include <__concepts/constructible.h> +#include <__concepts/convertible_to.h> +#include <__concepts/copyable.h> +#include <__concepts/derived_from.h> +#include <__concepts/equality_comparable.h> +#include <__concepts/swappable.h> +#include <__config> +#include <__functional/bind_back.h> +#include <__functional/invoke.h> +#include <__functional/reference_wrapper.h> +#include <__iterator/concepts.h> +#include <__iterator/default_sentinel.h> +#include <__iterator/distance.h> +#include <__iterator/incrementable_traits.h> +#include <__iterator/iter_move.h> +#include <__iterator/iter_swap.h> +#include <__iterator/iterator_traits.h> +#include <__iterator/next.h> +#include <__memory/addressof.h> +#include <__ranges/access.h> +#include <__ranges/all.h> +#include <__ranges/concepts.h> +#include <__ranges/movable_box.h> +#include <__ranges/non_propagating_cache.h> +#include <__ranges/range_adaptor.h> +#include <__ranges/size.h> +#include <__ranges/view_interface.h> +#include <__ranges/zip_view.h> +#include <__type_traits/conditional.h> +#include <__type_traits/decay.h> +#include <__type_traits/is_nothrow_constructible.h> +#include <__type_traits/is_nothrow_convertible.h> +#include <__type_traits/is_object.h> +#include <__type_traits/make_unsigned.h> +#include <__type_traits/maybe_const.h> +#include <__utility/forward.h> +#include <__utility/in_place.h> +#include <__utility/move.h> +#include <tuple> +#include <variant> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +#endif + +_LIBCPP_PUSH_MACROS +#include <__undef_macros> + +_LIBCPP_BEGIN_NAMESPACE_STD + +#if _LIBCPP_STD_VER >= 26 + +namespace ranges { + +# ifdef __cpp_pack_indexing +template <class... _Tp> +using __extract_last = _Tp...[sizeof...(_Tp) - 1]; +# else +template <class _Tp, class... _Tail> +struct __extract_last_impl : __extract_last_impl<_Tail...> {}; +template <class _Tp> +struct __extract_last_impl<_Tp> { + using type = _Tp; +}; + +template <class... _Tp> +using __extract_last = __extract_last_impl<_Tp...>::type; +# endif + +template <class _Tp, class... _Tail> +constexpr bool __derived_from_pack = + __derived_from_pack<_Tp, __extract_last<_Tail...>> && __derived_from_pack<_Tail...>; + +template <class _Tp, class _IterCategory> +constexpr bool __derived_from_pack<_Tp, _IterCategory> = derived_from<_Tp, _IterCategory>; + +template <class _View, class... _Views> +struct __last_view : __last_view<_Views...> {}; + +template <class _View> +struct __last_view<_View> { + using type = _View; +}; + +template <class _Ref, class _RRef, class _It> +concept __concat_indirectly_readable_impl = requires(const _It __it) { + { *__it } -> convertible_to<_Ref>; + { ranges::iter_move(__it) } -> convertible_to<_RRef>; +}; + +template <class... _Rs> +using __concat_reference_t = common_reference_t<range_reference_t<_Rs>...>; + +template <class... _Rs> +using __concat_value_t = common_type_t<range_value_t<_Rs>...>; + +template <class... _Rs> +using __concat_rvalue_reference_t = common_reference_t<range_rvalue_reference_t<_Rs>...>; + +template <class... _Rs> +concept __concat_indirectly_readable = + common_reference_with<__concat_reference_t<_Rs...>&&, __concat_value_t<_Rs...>&> && + common_reference_with<__concat_reference_t<_Rs...>&&, __concat_rvalue_reference_t<_Rs...>&&> && + common_reference_with<__concat_rvalue_reference_t<_Rs...>&&, __concat_value_t<_Rs...> const&> && + (__concat_indirectly_readable_impl<__concat_reference_t<_Rs...>, + __concat_rvalue_reference_t<_Rs...>, + iterator_t<_Rs>> && + ...); + +template <class... _Rs> +concept __concatable = requires { + typename __concat_reference_t<_Rs...>; + typename __concat_value_t<_Rs...>; + typename __concat_rvalue_reference_t<_Rs...>; +} && __concat_indirectly_readable<_Rs...>; + +template <bool _Const, class... _Rs> +concept __concat_is_random_access = + (random_access_range<__maybe_const<_Const, _Rs>> && ...) && (sized_range<__maybe_const<_Const, _Rs>> && ...); + +template <bool _Const, class... _Rs> +concept __concat_is_bidirectional = + ((bidirectional_range<__maybe_const<_Const, _Rs>> && ...) && (common_range<__maybe_const<_Const, _Rs>> && ...)); + +template <bool _Const, class... _Views> +concept __all_forward = (forward_range<__maybe_const<_Const, _Views>> && ...); + +template <bool _Const, class... _Tp> +struct __apply_drop_first; + +template <bool _Const, class _Head, class... _Tail> +struct __apply_drop_first<_Const, _Head, _Tail...> { + static constexpr bool value = (sized_range<__maybe_const<_Const, _Tail>> && ...); +}; + +template <input_range... _Views> + requires(view<_Views> && ...) && (sizeof...(_Views) > 0) && __concatable<_Views...> +class concat_view : public view_interface<concat_view<_Views...>> { + tuple<_Views...> __views_; + + template <bool _Const> + class __iterator; + +public: + _LIBCPP_HIDE_FROM_ABI constexpr concat_view() = default; + + _LIBCPP_HIDE_FROM_ABI constexpr explicit concat_view(_Views... __views) : __views_(std::move(__views)...) {} + + _LIBCPP_HIDE_FROM_ABI constexpr __iterator<false> begin() + requires(!(__simple_view<_Views> && ...)) + { + __iterator<false> __it(this, in_place_index<0>, ranges::begin(std::get<0>(__views_))); + __it.template __satisfy<0>(); + return __it; + } + + _LIBCPP_HIDE_FROM_ABI constexpr __iterator<true> begin() const + requires((range<const _Views> && ...) && __concatable<const _Views...>) + { + __iterator<true> __it(this, in_place_index<0>, ranges::begin(std::get<0>(__views_))); + __it.template __satisfy<0>(); + return __it; + } + + _LIBCPP_HIDE_FROM_ABI constexpr auto end() + requires(!(__simple_view<_Views> && ...)) + { + if constexpr (common_range<typename __last_view<_Views...>::type>) { + constexpr auto __n = sizeof...(_Views); + return __iterator<false>(this, in_place_index<__n - 1>, ranges::end(std::get<__n - 1>(__views_))); + } else { + return default_sentinel; + } + } + + _LIBCPP_HIDE_FROM_ABI constexpr auto end() const + requires(range<const _Views> && ...) + { + if constexpr (common_range<typename __last_view<_Views...>::type>) { + constexpr auto __n = sizeof...(_Views); + return __iterator<true>(this, in_place_index<__n - 1>, ranges::end(std::get<__n - 1>(__views_))); + } else { + return default_sentinel; + } + } + + _LIBCPP_HIDE_FROM_ABI constexpr auto size() + requires(sized_range<_Views> && ...) + { + return std::apply( + [](auto... __sizes) { + using CT = make_unsigned_t<common_type_t<decltype(__sizes)...>>; + return (CT(__sizes) + ...); + }, + ranges::__tuple_transform(ranges::size, __views_)); + } + + _LIBCPP_HIDE_FROM_ABI constexpr auto size() const + requires(sized_range<const _Views> && ...) + { + return std::apply( + [](auto... __sizes) { + using CT = make_unsigned_t<common_type_t<decltype(__sizes)...>>; + return (CT(__sizes) + ...); + }, + ranges::__tuple_transform(ranges::size, __views_)); + } +}; + +template <class... _Views> +concat_view(_Views&&...) -> concat_view<views::all_t<_Views>...>; + +template <input_range... _Views> + requires(view<_Views> && ...) && (sizeof...(_Views) > 0) && __concatable<_Views...> +template <bool _Const> +class concat_view<_Views...>::__iterator { +public: + constexpr static bool derive_pack_random_iterator = + __derived_from_pack<typename iterator_traits<iterator_t<__maybe_const<_Const, _Views>>>::iterator_category..., + random_access_iterator_tag>; + constexpr static bool derive_pack_bidirectional_iterator = + __derived_from_pack<typename iterator_traits<iterator_t<__maybe_const<_Const, _Views>>>::iterator_category..., + bidirectional_iterator_tag>; + constexpr static bool derive_pack_forward_iterator = + __derived_from_pack<typename iterator_traits< iterator_t<__maybe_const<_Const, _Views>>>::iterator_category..., + forward_iterator_tag>; + using iterator_category = + _If<!is_reference_v<__concat_reference_t<__maybe_const<_Const, _Views>...>>, + input_iterator_tag, + _If<derive_pack_random_iterator, + random_access_iterator_tag, + _If<derive_pack_bidirectional_iterator, + bidirectional_iterator_tag, + _If<derive_pack_forward_iterator, forward_iterator_tag, input_iterator_tag > > > >; + using iterator_concept = + _If<__concat_is_random_access<_Const, _Views...>, + random_access_iterator_tag, + _If<__concat_is_bidirectional<_Const, _Views...>, + bidirectional_iterator_tag, + _If< __all_forward<_Const, _Views...>, forward_iterator_tag, input_iterator_tag > > >; + using value_type = __concat_value_t<__maybe_const<_Const, _Views>...>; + using difference_type = common_type_t<range_difference_t<__maybe_const<_Const, _Views>>...>; + using __base_iter = variant<iterator_t<__maybe_const<_Const, _Views>>...>; + + __base_iter it_; + __maybe_const<_Const, concat_view>* parent_ = nullptr; + + template <std::size_t _Idx> + _LIBCPP_HIDE_FROM_ABI constexpr void __satisfy() { + if constexpr (_Idx < (sizeof...(_Views) - 1)) { + if (std::get<_Idx>(it_) == ranges::end(std::get<_Idx>(parent_->__views_))) { + it_.template emplace<_Idx + 1>(ranges::begin(std::get<_Idx + 1>(parent_->__views_))); + __satisfy<_Idx + 1>(); + } + } + } + + template <std::size_t _Idx> + _LIBCPP_HIDE_FROM_ABI constexpr void __prev() { + if constexpr (_Idx == 0) { + --std::get<0>(it_); + } else { + if (std::get<_Idx>(it_) == ranges::begin(std::get<_Idx>(parent_->__views_))) { + using __prev_view = __maybe_const<_Const, tuple_element_t<_Idx - 1, tuple<_Views...>>>; + if constexpr (common_range<__prev_view>) { + it_.template emplace<_Idx - 1>(ranges::end(std::get<_Idx - 1>(parent_->__views_))); + } else { + it_.template emplace<_Idx - 1>(ranges::__next(ranges::begin(std::get<_Idx - 1>(parent_->__views_)), + ranges::size(std::get<_Idx - 1>(parent_->__views_)))); + } + __prev<_Idx - 1>(); + } else { + --std::get<_Idx>(it_); + } + } + } + + template <std::size_t _Idx> + _LIBCPP_HIDE_FROM_ABI constexpr void __advance_fwd(difference_type __offset, difference_type __steps) { + using underlying_diff_type = iter_difference_t<variant_alternative_t<_Idx, __base_iter>>; + if constexpr (_Idx == sizeof...(_Views) - 1) { + std::get<_Idx>(it_) += static_cast<underlying_diff_type>(__steps); + } else { + difference_type __n_size = ranges::size(std::get<_Idx>(parent_->__views_)); + if (__offset + __steps < __n_size) { + std::get<_Idx>(it_) += static_cast<underlying_diff_type>(__steps); + } else { + it_.template emplace<_Idx + 1>(ranges::begin(std::get<_Idx + 1>(parent_->__views_))); + __advance_fwd<_Idx + 1>(0, __offset + __steps - __n_size); + } + } + } + + template <std::size_t _Idx> + _LIBCPP_HIDE_FROM_ABI constexpr void __advance_bwd(difference_type __offset, difference_type __steps) { + using underlying_diff_type = iter_difference_t<variant_alternative_t<_Idx, __base_iter>>; + if constexpr (_Idx == 0) { + std::get<_Idx>(it_) -= static_cast<underlying_diff_type>(__steps); + } else { + if (__offset >= __steps) { + std::get<_Idx>(it_) -= static_cast<underlying_diff_type>(__steps); + } else { + auto __prev_size = ranges::__distance(std::get<_Idx - 1>(parent_->__views_)); + it_.template emplace<_Idx - 1>(ranges::begin(std::get<_Idx - 1>(parent_->__views_)) + __prev_size); + __advance_bwd<_Idx - 1>(__prev_size, __steps - __offset); + } + } + } + + template <size_t... _Is, typename _Func> + _LIBCPP_HIDE_FROM_ABI constexpr void __apply_at_index(size_t __index, _Func&& __func, index_sequence<_Is...>) { + ((__index == _Is ? (__func(integral_constant<size_t, _Is>{}), 0) : 0), ...); + } + + template <size_t _Idx, typename _Func> + _LIBCPP_HIDE_FROM_ABI constexpr void __apply_at_index(size_t __index, _Func&& __func) { + __apply_at_index(__index, std::forward<_Func>(__func), make_index_sequence<_Idx>{}); + } + + template <class... _Args> + _LIBCPP_HIDE_FROM_ABI explicit constexpr __iterator(__maybe_const<_Const, concat_view>* __parent, _Args&&... __args) + requires constructible_from<__base_iter, _Args&&...> + : it_(std::forward<_Args>(__args)...), parent_(__parent) {} + +public: + _LIBCPP_HIDE_FROM_ABI __iterator() = default; + + _LIBCPP_HIDE_FROM_ABI constexpr __iterator(__iterator<!_Const> __i) + requires _Const && (convertible_to<iterator_t<_Views>, iterator_t<const _Views>> && ...) + : it_(__base_iter(in_place_index<__i.index()>, std::get<__i.index()>(std::move(__i.it_)))), + parent_(__i.parent_) {} ---------------- frederick-vs-ja wrote:
I guess we can use something like this ```C++ [&__src = __i.it_]<size_t... _Indices>(index_sequence<_Indices...>) -> __base_iter { using __src_lref = decltype((src)); using __construction_fptr = __base_iter(*)(__src_lref); static constexpr __construction_fptr __vtable[]{ [](__src_lref __src_var) -> __base_iter { return __base_iter(in_place_type<_Indices>, std::__get<_Indices>(std::move(__src_lref))); }... }; return __vtbl[__i.index()](__src); }(make_index_sequence<variant_size_v<__base_iter>>{}) ``` https://github.com/llvm/llvm-project/pull/120920 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits