123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516 |
- // -*- 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_ZIP_VIEW_H
- #define _LIBCPP___RANGES_ZIP_VIEW_H
- #include <__config>
- #include <__algorithm/ranges_min.h>
- #include <__compare/three_way_comparable.h>
- #include <__concepts/convertible_to.h>
- #include <__concepts/equality_comparable.h>
- #include <__functional/invoke.h>
- #include <__functional/operations.h>
- #include <__iterator/concepts.h>
- #include <__iterator/incrementable_traits.h>
- #include <__iterator/iter_move.h>
- #include <__iterator/iter_swap.h>
- #include <__iterator/iterator_traits.h>
- #include <__ranges/access.h>
- #include <__ranges/all.h>
- #include <__ranges/concepts.h>
- #include <__ranges/empty_view.h>
- #include <__ranges/enable_borrowed_range.h>
- #include <__ranges/size.h>
- #include <__ranges/view_interface.h>
- #include <__type_traits/is_nothrow_move_constructible.h>
- #include <__type_traits/make_unsigned.h>
- #include <__utility/declval.h>
- #include <__utility/forward.h>
- #include <__utility/integer_sequence.h>
- #include <__utility/move.h>
- #include <tuple>
- #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 >= 23
- namespace ranges {
- template <class... _Ranges>
- concept __zip_is_common = (sizeof...(_Ranges) == 1 && (common_range<_Ranges> && ...)) ||
- (!(bidirectional_range<_Ranges> && ...) && (common_range<_Ranges> && ...)) ||
- ((random_access_range<_Ranges> && ...) && (sized_range<_Ranges> && ...));
- template <typename _Tp, typename _Up>
- auto __tuple_or_pair_test() -> pair<_Tp, _Up>;
- template <typename... _Types>
- requires(sizeof...(_Types) != 2)
- auto __tuple_or_pair_test() -> tuple<_Types...>;
- template <class... _Types>
- using __tuple_or_pair = decltype(__tuple_or_pair_test<_Types...>());
- template <class _Fun, class _Tuple>
- _LIBCPP_HIDE_FROM_ABI constexpr auto __tuple_transform(_Fun&& __f, _Tuple&& __tuple) {
- return std::apply(
- [&]<class... _Types>(_Types&&... __elements) {
- return __tuple_or_pair<invoke_result_t<_Fun&, _Types>...>(
- std::invoke(__f, std::forward<_Types>(__elements))...);
- },
- std::forward<_Tuple>(__tuple));
- }
- template <class _Fun, class _Tuple>
- _LIBCPP_HIDE_FROM_ABI constexpr void __tuple_for_each(_Fun&& __f, _Tuple&& __tuple) {
- std::apply(
- [&]<class... _Types>(_Types&&... __elements) {
- (static_cast<void>(std::invoke(__f, std::forward<_Types>(__elements))), ...);
- },
- std::forward<_Tuple>(__tuple));
- }
- template <class _Fun, class _Tuple1, class _Tuple2, size_t... _Indices>
- _LIBCPP_HIDE_FROM_ABI constexpr __tuple_or_pair<
- invoke_result_t<_Fun&, typename tuple_element<_Indices, remove_cvref_t<_Tuple1>>::type,
- typename tuple_element<_Indices, remove_cvref_t<_Tuple2>>::type>...>
- __tuple_zip_transform(_Fun&& __f, _Tuple1&& __tuple1, _Tuple2&& __tuple2, index_sequence<_Indices...>) {
- return {std::invoke(__f, std::get<_Indices>(std::forward<_Tuple1>(__tuple1)),
- std::get<_Indices>(std::forward<_Tuple2>(__tuple2)))...};
- }
- template <class _Fun, class _Tuple1, class _Tuple2>
- _LIBCPP_HIDE_FROM_ABI constexpr auto __tuple_zip_transform(_Fun&& __f, _Tuple1&& __tuple1, _Tuple2&& __tuple2) {
- return ranges::__tuple_zip_transform(__f, std::forward<_Tuple1>(__tuple1), std::forward<_Tuple2>(__tuple2),
- std::make_index_sequence<tuple_size<remove_cvref_t<_Tuple1>>::value>());
- }
- template <class _Fun, class _Tuple1, class _Tuple2, size_t... _Indices>
- _LIBCPP_HIDE_FROM_ABI constexpr void __tuple_zip_for_each(_Fun&& __f, _Tuple1&& __tuple1, _Tuple2&& __tuple2,
- index_sequence<_Indices...>) {
- (std::invoke(__f, std::get<_Indices>(std::forward<_Tuple1>(__tuple1)),
- std::get<_Indices>(std::forward<_Tuple2>(__tuple2))),
- ...);
- }
- template <class _Fun, class _Tuple1, class _Tuple2>
- _LIBCPP_HIDE_FROM_ABI constexpr auto __tuple_zip_for_each(_Fun&& __f, _Tuple1&& __tuple1, _Tuple2&& __tuple2) {
- return ranges::__tuple_zip_for_each(__f, std::forward<_Tuple1>(__tuple1), std::forward<_Tuple2>(__tuple2),
- std::make_index_sequence<tuple_size<remove_cvref_t<_Tuple1>>::value>());
- }
- template <class _Tuple1, class _Tuple2>
- _LIBCPP_HIDE_FROM_ABI constexpr bool __tuple_any_equals(const _Tuple1& __tuple1, const _Tuple2& __tuple2) {
- const auto __equals = ranges::__tuple_zip_transform(std::equal_to<>(), __tuple1, __tuple2);
- return std::apply([](auto... __bools) { return (__bools || ...); }, __equals);
- }
- // abs in cstdlib is not constexpr
- // TODO : remove __abs once P0533R9 is implemented.
- template <class _Tp>
- _LIBCPP_HIDE_FROM_ABI constexpr _Tp __abs(_Tp __t) {
- return __t < 0 ? -__t : __t;
- }
- template <input_range... _Views>
- requires(view<_Views> && ...) && (sizeof...(_Views) > 0)
- class zip_view : public view_interface<zip_view<_Views...>> {
- _LIBCPP_NO_UNIQUE_ADDRESS tuple<_Views...> __views_;
- template <bool>
- class __iterator;
- template <bool>
- class __sentinel;
- public:
- _LIBCPP_HIDE_FROM_ABI
- zip_view() = default;
- _LIBCPP_HIDE_FROM_ABI
- constexpr explicit zip_view(_Views... __views) : __views_(std::move(__views)...) {}
- _LIBCPP_HIDE_FROM_ABI
- constexpr auto begin()
- requires(!(__simple_view<_Views> && ...)) {
- return __iterator<false>(ranges::__tuple_transform(ranges::begin, __views_));
- }
- _LIBCPP_HIDE_FROM_ABI
- constexpr auto begin() const
- requires(range<const _Views> && ...) {
- return __iterator<true>(ranges::__tuple_transform(ranges::begin, __views_));
- }
- _LIBCPP_HIDE_FROM_ABI
- constexpr auto end()
- requires(!(__simple_view<_Views> && ...)) {
- if constexpr (!__zip_is_common<_Views...>) {
- return __sentinel<false>(ranges::__tuple_transform(ranges::end, __views_));
- } else if constexpr ((random_access_range<_Views> && ...)) {
- return begin() + iter_difference_t<__iterator<false>>(size());
- } else {
- return __iterator<false>(ranges::__tuple_transform(ranges::end, __views_));
- }
- }
- _LIBCPP_HIDE_FROM_ABI
- constexpr auto end() const
- requires(range<const _Views> && ...) {
- if constexpr (!__zip_is_common<const _Views...>) {
- return __sentinel<true>(ranges::__tuple_transform(ranges::end, __views_));
- } else if constexpr ((random_access_range<const _Views> && ...)) {
- return begin() + iter_difference_t<__iterator<true>>(size());
- } else {
- return __iterator<true>(ranges::__tuple_transform(ranges::end, __views_));
- }
- }
- _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 ranges::min({_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 ranges::min({_CT(__sizes)...});
- },
- ranges::__tuple_transform(ranges::size, __views_));
- }
- };
- template <class... _Ranges>
- zip_view(_Ranges&&...) -> zip_view<views::all_t<_Ranges>...>;
- template <bool _Const, class... _Views>
- concept __zip_all_random_access = (random_access_range<__maybe_const<_Const, _Views>> && ...);
- template <bool _Const, class... _Views>
- concept __zip_all_bidirectional = (bidirectional_range<__maybe_const<_Const, _Views>> && ...);
- template <bool _Const, class... _Views>
- concept __zip_all_forward = (forward_range<__maybe_const<_Const, _Views>> && ...);
- template <bool _Const, class... _Views>
- consteval auto __get_zip_view_iterator_tag() {
- if constexpr (__zip_all_random_access<_Const, _Views...>) {
- return random_access_iterator_tag();
- } else if constexpr (__zip_all_bidirectional<_Const, _Views...>) {
- return bidirectional_iterator_tag();
- } else if constexpr (__zip_all_forward<_Const, _Views...>) {
- return forward_iterator_tag();
- } else {
- return input_iterator_tag();
- }
- }
- template <bool _Const, class... _Views>
- struct __zip_view_iterator_category_base {};
- template <bool _Const, class... _Views>
- requires __zip_all_forward<_Const, _Views...>
- struct __zip_view_iterator_category_base<_Const, _Views...> {
- using iterator_category = input_iterator_tag;
- };
- template <input_range... _Views>
- requires(view<_Views> && ...) && (sizeof...(_Views) > 0)
- template <bool _Const>
- class zip_view<_Views...>::__iterator : public __zip_view_iterator_category_base<_Const, _Views...> {
- __tuple_or_pair<iterator_t<__maybe_const<_Const, _Views>>...> __current_;
- _LIBCPP_HIDE_FROM_ABI
- constexpr explicit __iterator(__tuple_or_pair<iterator_t<__maybe_const<_Const, _Views>>...> __current)
- : __current_(std::move(__current)) {}
- template <bool>
- friend class zip_view<_Views...>::__iterator;
- template <bool>
- friend class zip_view<_Views...>::__sentinel;
- friend class zip_view<_Views...>;
- public:
- using iterator_concept = decltype(__get_zip_view_iterator_tag<_Const, _Views...>());
- using value_type = __tuple_or_pair<range_value_t<__maybe_const<_Const, _Views>>...>;
- using difference_type = common_type_t<range_difference_t<__maybe_const<_Const, _Views>>...>;
- _LIBCPP_HIDE_FROM_ABI
- __iterator() = default;
- _LIBCPP_HIDE_FROM_ABI
- constexpr __iterator(__iterator<!_Const> __i)
- requires _Const && (convertible_to<iterator_t<_Views>, iterator_t<__maybe_const<_Const, _Views>>> && ...)
- : __current_(std::move(__i.__current_)) {}
- _LIBCPP_HIDE_FROM_ABI
- constexpr auto operator*() const {
- return ranges::__tuple_transform([](auto& __i) -> decltype(auto) { return *__i; }, __current_);
- }
- _LIBCPP_HIDE_FROM_ABI
- constexpr __iterator& operator++() {
- ranges::__tuple_for_each([](auto& __i) { ++__i; }, __current_);
- return *this;
- }
- _LIBCPP_HIDE_FROM_ABI
- constexpr void operator++(int) { ++*this; }
- _LIBCPP_HIDE_FROM_ABI
- constexpr __iterator operator++(int)
- requires __zip_all_forward<_Const, _Views...> {
- auto __tmp = *this;
- ++*this;
- return __tmp;
- }
- _LIBCPP_HIDE_FROM_ABI
- constexpr __iterator& operator--()
- requires __zip_all_bidirectional<_Const, _Views...> {
- ranges::__tuple_for_each([](auto& __i) { --__i; }, __current_);
- return *this;
- }
- _LIBCPP_HIDE_FROM_ABI
- constexpr __iterator operator--(int)
- requires __zip_all_bidirectional<_Const, _Views...> {
- auto __tmp = *this;
- --*this;
- return __tmp;
- }
- _LIBCPP_HIDE_FROM_ABI
- constexpr __iterator& operator+=(difference_type __x)
- requires __zip_all_random_access<_Const, _Views...> {
- ranges::__tuple_for_each([&]<class _Iter>(_Iter& __i) { __i += iter_difference_t<_Iter>(__x); }, __current_);
- return *this;
- }
- _LIBCPP_HIDE_FROM_ABI
- constexpr __iterator& operator-=(difference_type __x)
- requires __zip_all_random_access<_Const, _Views...> {
- ranges::__tuple_for_each([&]<class _Iter>(_Iter& __i) { __i -= iter_difference_t<_Iter>(__x); }, __current_);
- return *this;
- }
- _LIBCPP_HIDE_FROM_ABI
- constexpr auto operator[](difference_type __n) const
- requires __zip_all_random_access<_Const, _Views...> {
- return ranges::__tuple_transform(
- [&]<class _Iter>(_Iter& __i) -> decltype(auto) { return __i[iter_difference_t<_Iter>(__n)]; }, __current_);
- }
- _LIBCPP_HIDE_FROM_ABI
- friend constexpr bool operator==(const __iterator& __x, const __iterator& __y)
- requires(equality_comparable<iterator_t<__maybe_const<_Const, _Views>>> && ...) {
- if constexpr (__zip_all_bidirectional<_Const, _Views...>) {
- return __x.__current_ == __y.__current_;
- } else {
- return ranges::__tuple_any_equals(__x.__current_, __y.__current_);
- }
- }
- _LIBCPP_HIDE_FROM_ABI
- friend constexpr bool operator<(const __iterator& __x, const __iterator& __y)
- requires __zip_all_random_access<_Const, _Views...> {
- return __x.__current_ < __y.__current_;
- }
- _LIBCPP_HIDE_FROM_ABI
- friend constexpr bool operator>(const __iterator& __x, const __iterator& __y)
- requires __zip_all_random_access<_Const, _Views...> {
- return __y < __x;
- }
- _LIBCPP_HIDE_FROM_ABI
- friend constexpr bool operator<=(const __iterator& __x, const __iterator& __y)
- requires __zip_all_random_access<_Const, _Views...> {
- return !(__y < __x);
- }
- _LIBCPP_HIDE_FROM_ABI
- friend constexpr bool operator>=(const __iterator& __x, const __iterator& __y)
- requires __zip_all_random_access<_Const, _Views...> {
- return !(__x < __y);
- }
- _LIBCPP_HIDE_FROM_ABI
- friend constexpr auto operator<=>(const __iterator& __x, const __iterator& __y)
- requires __zip_all_random_access<_Const, _Views...> &&
- (three_way_comparable<iterator_t<__maybe_const<_Const, _Views>>> && ...) {
- return __x.__current_ <=> __y.__current_;
- }
- _LIBCPP_HIDE_FROM_ABI
- friend constexpr __iterator operator+(const __iterator& __i, difference_type __n)
- requires __zip_all_random_access<_Const, _Views...> {
- auto __r = __i;
- __r += __n;
- return __r;
- }
- _LIBCPP_HIDE_FROM_ABI
- friend constexpr __iterator operator+(difference_type __n, const __iterator& __i)
- requires __zip_all_random_access<_Const, _Views...> {
- return __i + __n;
- }
- _LIBCPP_HIDE_FROM_ABI
- friend constexpr __iterator operator-(const __iterator& __i, difference_type __n)
- requires __zip_all_random_access<_Const, _Views...> {
- auto __r = __i;
- __r -= __n;
- return __r;
- }
- _LIBCPP_HIDE_FROM_ABI
- friend constexpr difference_type operator-(const __iterator& __x, const __iterator& __y)
- requires(sized_sentinel_for<iterator_t<__maybe_const<_Const, _Views>>, iterator_t<__maybe_const<_Const, _Views>>> &&
- ...) {
- const auto __diffs = ranges::__tuple_zip_transform(minus<>(), __x.__current_, __y.__current_);
- return std::apply(
- [](auto... __ds) {
- return ranges::min({difference_type(__ds)...},
- [](auto __a, auto __b) { return ranges::__abs(__a) < ranges::__abs(__b); });
- },
- __diffs);
- }
- _LIBCPP_HIDE_FROM_ABI
- friend constexpr auto iter_move(const __iterator& __i) noexcept(
- (noexcept(ranges::iter_move(std::declval<const iterator_t<__maybe_const<_Const, _Views>>&>())) && ...) &&
- (is_nothrow_move_constructible_v<range_rvalue_reference_t<__maybe_const<_Const, _Views>>> && ...)) {
- return ranges::__tuple_transform(ranges::iter_move, __i.__current_);
- }
- _LIBCPP_HIDE_FROM_ABI
- friend constexpr void iter_swap(const __iterator& __l, const __iterator& __r) noexcept(
- (noexcept(ranges::iter_swap(std::declval<const iterator_t<__maybe_const<_Const, _Views>>&>(),
- std::declval<const iterator_t<__maybe_const<_Const, _Views>>&>())) &&
- ...))
- requires(indirectly_swappable<iterator_t<__maybe_const<_Const, _Views>>> && ...) {
- ranges::__tuple_zip_for_each(ranges::iter_swap, __l.__current_, __r.__current_);
- }
- };
- template <input_range... _Views>
- requires(view<_Views> && ...) && (sizeof...(_Views) > 0)
- template <bool _Const>
- class zip_view<_Views...>::__sentinel {
- __tuple_or_pair<sentinel_t<__maybe_const<_Const, _Views>>...> __end_;
- _LIBCPP_HIDE_FROM_ABI
- constexpr explicit __sentinel(__tuple_or_pair<sentinel_t<__maybe_const<_Const, _Views>>...> __end) : __end_(__end) {}
- friend class zip_view<_Views...>;
- // hidden friend cannot access private member of iterator because they are friends of friends
- template <bool _OtherConst>
- _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto)
- __iter_current(zip_view<_Views...>::__iterator<_OtherConst> const& __it) {
- return (__it.__current_);
- }
- public:
- _LIBCPP_HIDE_FROM_ABI
- __sentinel() = default;
- _LIBCPP_HIDE_FROM_ABI
- constexpr __sentinel(__sentinel<!_Const> __i)
- requires _Const && (convertible_to<sentinel_t<_Views>, sentinel_t<__maybe_const<_Const, _Views>>> && ...)
- : __end_(std::move(__i.__end_)) {}
- template <bool _OtherConst>
- requires(sentinel_for<sentinel_t<__maybe_const<_Const, _Views>>, iterator_t<__maybe_const<_OtherConst, _Views>>> &&
- ...)
- _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __iterator<_OtherConst>& __x, const __sentinel& __y) {
- return ranges::__tuple_any_equals(__iter_current(__x), __y.__end_);
- }
- template <bool _OtherConst>
- requires(
- sized_sentinel_for<sentinel_t<__maybe_const<_Const, _Views>>, iterator_t<__maybe_const<_OtherConst, _Views>>> &&
- ...)
- _LIBCPP_HIDE_FROM_ABI friend constexpr common_type_t<range_difference_t<__maybe_const<_OtherConst, _Views>>...>
- operator-(const __iterator<_OtherConst>& __x, const __sentinel& __y) {
- const auto __diffs = ranges::__tuple_zip_transform(minus<>(), __iter_current(__x), __y.__end_);
- return std::apply(
- [](auto... __ds) {
- using _Diff = common_type_t<range_difference_t<__maybe_const<_OtherConst, _Views>>...>;
- return ranges::min({_Diff(__ds)...},
- [](auto __a, auto __b) { return ranges::__abs(__a) < ranges::__abs(__b); });
- },
- __diffs);
- }
- template <bool _OtherConst>
- requires(
- sized_sentinel_for<sentinel_t<__maybe_const<_Const, _Views>>, iterator_t<__maybe_const<_OtherConst, _Views>>> &&
- ...)
- _LIBCPP_HIDE_FROM_ABI friend constexpr common_type_t<range_difference_t<__maybe_const<_OtherConst, _Views>>...>
- operator-(const __sentinel& __y, const __iterator<_OtherConst>& __x) {
- return -(__x - __y);
- }
- };
- template <class... _Views>
- inline constexpr bool enable_borrowed_range<zip_view<_Views...>> = (enable_borrowed_range<_Views> && ...);
- namespace views {
- namespace __zip {
- struct __fn {
- _LIBCPP_HIDE_FROM_ABI constexpr auto operator()() const noexcept { return empty_view<tuple<>>{}; }
- template <class... _Ranges>
- _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Ranges&&... __rs) const
- noexcept(noexcept(zip_view<all_t<_Ranges&&>...>(std::forward<_Ranges>(__rs)...)))
- -> decltype(zip_view<all_t<_Ranges&&>...>(std::forward<_Ranges>(__rs)...)) {
- return zip_view<all_t<_Ranges>...>(std::forward<_Ranges>(__rs)...);
- }
- };
- } // namespace __zip
- inline namespace __cpo {
- inline constexpr auto zip = __zip::__fn{};
- } // namespace __cpo
- } // namespace views
- } // namespace ranges
- #endif // _LIBCPP_STD_VER >= 23
- _LIBCPP_END_NAMESPACE_STD
- _LIBCPP_POP_MACROS
- #endif // _LIBCPP___RANGES_ZIP_VIEW_H
|