Skip to content

[libc++][ranges] P2542R8: Implement views::concat #120920

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 119 commits into
base: main
Choose a base branch
from

Conversation

changkhothuychung
Copy link
Contributor

@changkhothuychung changkhothuychung commented Dec 22, 2024

Closes #105419
Closes #105348
Closes #105349

@changkhothuychung changkhothuychung requested a review from a team as a code owner December 22, 2024 21:29
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Dec 22, 2024
@changkhothuychung changkhothuychung marked this pull request as draft December 22, 2024 21:29
@llvmbot
Copy link
Member

llvmbot commented Dec 22, 2024

@llvm/pr-subscribers-libcxx

Author: Nhat Nguyen (changkhothuychung)

Changes

Patch is 52.01 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/120920.diff

14 Files Affected:

  • (added) libcxx/include/__ranges/concat_view.h (+624)
  • (added) libcxx/test/std/ranges/range.adaptors/range.concat/adaptor.pass.cpp (+71)
  • (added) libcxx/test/std/ranges/range.adaptors/range.concat/begin.pass.cpp (+38)
  • (added) libcxx/test/std/ranges/range.adaptors/range.concat/ctad.pass.cpp (+54)
  • (added) libcxx/test/std/ranges/range.adaptors/range.concat/ctor.default.pass.cpp (+81)
  • (added) libcxx/test/std/ranges/range.adaptors/range.concat/ctor.view.pass.cpp (+70)
  • (added) libcxx/test/std/ranges/range.adaptors/range.concat/end.pass.cpp (+99)
  • (added) libcxx/test/std/ranges/range.adaptors/range.concat/iterator/compare.pass.cpp (+76)
  • (added) libcxx/test/std/ranges/range.adaptors/range.concat/iterator/decrement.pass.cpp (+89)
  • (added) libcxx/test/std/ranges/range.adaptors/range.concat/iterator/deref.pass.cpp (+56)
  • (added) libcxx/test/std/ranges/range.adaptors/range.concat/iterator/increment.pass.cpp (+84)
  • (added) libcxx/test/std/ranges/range.adaptors/range.concat/iterator/iter_move.pass.cpp (+74)
  • (added) libcxx/test/std/ranges/range.adaptors/range.concat/iterator/iter_swap.pass.cpp (+79)
  • (added) libcxx/test/std/ranges/range.adaptors/range.concat/types.h (+44)
diff --git a/libcxx/include/__ranges/concat_view.h b/libcxx/include/__ranges/concat_view.h
new file mode 100644
index 00000000000000..9d17aae65ecaf5
--- /dev/null
+++ b/libcxx/include/__ranges/concat_view.h
@@ -0,0 +1,624 @@
+// -*- 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/constructible.h>
+#include <__concepts/copyable.h>
+#include <__concepts/derived_from.h>
+#include <__concepts/equality_comparable.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/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/view_interface.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/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_BEGIN_NAMESPACE_STD
+
+#if _LIBCPP_STD_VER >= 20
+
+namespace ranges {
+
+template <class T, class... Ts>
+struct extract_last : extract_last<Ts...> {};
+
+template <class T>
+struct extract_last<T> {
+  using type = T;
+};
+
+template <class _T, class... _Ts>
+struct derived_from_pack {
+  constexpr static bool value =
+      derived_from_pack<_T, typename extract_last<_Ts...>::type>::value && derived_from_pack<_Ts...>::value;
+};
+
+template <class _T, class _IterCategory>
+struct derived_from_pack<_T, _IterCategory> {
+  constexpr static bool value = derived_from<_T, _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 { // exposition only
+  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 <class R>
+concept constant_time_reversible = // exposition only
+    (bidirectional_range<R> && common_range<R>) || (sized_range<R> && random_access_range<R>);
+
+template <bool Const, class... Rs>
+concept concat_is_bidirectional =
+    ((bidirectional_range<__maybe_const<Const, Rs>> && ...) &&
+     (constant_time_reversible<__maybe_const<Const, Rs>> && ...));
+
+template <bool Const, class... Views>
+concept all_forward = // exposition only
+    (forward_range<__maybe_const<Const, Views>> && ...);
+
+template <bool Const, class... Ts>
+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;
+  class sentinel;
+
+public:
+  _LIBCPP_HIDE_FROM_ABI constexpr concat_view() = default;
+
+  _LIBCPP_HIDE_FROM_ABI constexpr explicit concat_view(Views... views) : views_(std::move(views)...) {}
+
+  constexpr iterator<false> begin()
+    requires(!(__simple_view<Views> && ...))
+  {
+    iterator<false> it(this, in_place_index<0>, ranges::begin(get<0>(views_)));
+    it.template satisfy<0>();
+    return it;
+  }
+
+  constexpr iterator<true> begin() const
+    requires((range<const Views> && ...) && concatable<const Views...>)
+  {
+    iterator<true> it(this, in_place_index<0>, ranges::begin(get<0>(views_)));
+    it.template satisfy<0>();
+    return it;
+  }
+
+  constexpr auto end()
+    requires(!(__simple_view<Views> && ...))
+  {
+    if constexpr (common_range<typename last_view<Views...>::type>) {
+      // last_view to be implemented
+      constexpr auto N = sizeof...(Views);
+      return iterator<false>(this, in_place_index<N - 1>, ranges::end(get<N - 1>(views_)));
+    } else {
+      return default_sentinel;
+    }
+  }
+
+  constexpr auto end() const
+    requires(range<const Views> && ...)
+  {
+    if constexpr (common_range<typename last_view<Views...>::type>) {
+      // last_view to be implemented
+      constexpr auto N = sizeof...(Views);
+      return iterator<true>(this, in_place_index<N - 1>, ranges::end(get<N - 1>(views_)));
+    } else {
+      return default_sentinel;
+    }
+  }
+
+  constexpr auto size()
+    requires(sized_range<Views> && ...)
+  {
+    return apply(
+        [](auto... sizes) {
+          using CT = make_unsigned_t<common_type_t<decltype(sizes)...>>;
+          return (CT(sizes) + ...);
+        },
+        tuple_transform(ranges::size, views_));
+  }
+
+  constexpr auto size() const
+    requires(sized_range<const Views> && ...)
+  {
+    return apply(
+        [](auto... sizes) {
+          using CT = make_unsigned_t<common_type_t<decltype(sizes)...>>;
+          return (CT(sizes) + ...);
+        },
+        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>::value;
+  constexpr static bool derive_pack_bidirectional_iterator =
+      derived_from_pack<typename iterator_traits<iterator_t<__maybe_const<Const, Views>>>::iterator_category...,
+                        bidirectional_iterator_tag>::value;
+  constexpr static bool derive_pack_forward_iterator =
+      derived_from_pack<typename iterator_traits< iterator_t<__maybe_const<Const, Views>>>::iterator_category...,
+                        forward_iterator_tag>::value;
+  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_;                                        // exposition only
+  __maybe_const<Const, concat_view>* parent_ = nullptr; // exposition only
+
+  template <std::size_t N>
+  constexpr void satisfy() {
+    if constexpr (N < (sizeof...(Views) - 1)) {
+      if (get<N>(it_) == ranges::end(get<N>(parent_->views_))) {
+        it_.template emplace<N + 1>(ranges::begin(get<N + 1>(parent_->views_)));
+        satisfy<N + 1>();
+      }
+    }
+  }
+
+  template <std::size_t N>
+  constexpr void prev() {
+    if constexpr (N == 0) {
+      --get<0>(it_);
+    } else {
+      if (get<N>(it_) == ranges::begin(get<N>(parent_->views_))) {
+        using prev_view = __maybe_const<Const, tuple_element_t<N - 1, tuple<Views...>>>;
+        if constexpr (common_range<prev_view>) {
+          it_.emplace<N - 1>(ranges::end(get<N - 1>(parent_->views_)));
+        } else {
+          it_.emplace<N - 1>(
+              ranges::__next(ranges::begin(get<N - 1>(parent_->views_)), ranges::size(get<N - 1>(parent_->views_))));
+        }
+        prev<N - 1>();
+      } else {
+        --get<N>(it_);
+      }
+    }
+  }
+
+  template <std::size_t N>
+  constexpr void advance_fwd(difference_type offset, difference_type steps) {
+    using underlying_diff_type = iter_difference_t<variant_alternative_t<N, base_iter>>;
+    if constexpr (N == sizeof...(Views) - 1) {
+      get<N>(it_) += static_cast<underlying_diff_type>(steps);
+    } else {
+      difference_type n_size = ranges::size(get<N>(parent_->views_));
+      if (offset + steps < n_size) {
+        get<N>(it_) += static_cast<underlying_diff_type>(steps);
+      } else {
+        it_.template emplace<N + 1>(ranges::begin(get<N + 1>(parent_->views_)));
+        advance_fwd<N + 1>(0, offset + steps - n_size);
+      }
+    }
+  }
+
+  template <std::size_t N>
+  constexpr void advance_bwd(difference_type offset, difference_type steps) {
+    using underlying_diff_type = iter_difference_t<variant_alternative_t<N, base_iter>>;
+    if constexpr (N == 0) {
+      get<N>(it_) -= static_cast<underlying_diff_type>(steps);
+    } else {
+      if (offset >= steps) {
+        get<N>(it_) -= static_cast<underlying_diff_type>(steps);
+      } else {
+        auto prev_size = ranges::__distance(get<N - 1>(parent_->views_));
+        it_.emplace<N - 1>(ranges::begin(get<N - 1>(parent_->views_)) + prev_size);
+        advance_bwd<N - 1>(prev_size, steps - offset);
+      }
+    }
+  }
+
+  template <size_t... Is, typename Func>
+  constexpr void apply_fn_with_const_index(size_t index, Func&& func, std::index_sequence<Is...>) {
+    ((index == Is ? (func(std::integral_constant<size_t, Is>{}), 0) : 0), ...);
+  }
+
+  template <size_t N, typename Func>
+  constexpr void apply_fn_with_const_index(size_t index, Func&& func) {
+    apply_fn_with_const_index(index, std::forward<Func>(func), std::make_index_sequence<N>{});
+  }
+
+  template <class... Args>
+  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:
+  iterator() = default;
+
+  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_) {}
+
+  constexpr decltype(auto) operator*() const {
+    using reference = concat_reference_t<__maybe_const<Const, Views>...>;
+    return std::visit([](auto&& it) -> reference { return *it; }, it_);
+  }
+
+  constexpr iterator& operator++() {
+    size_t active_index = it_.index();
+    apply_fn_with_const_index<std::variant_size_v<decltype(it_)>>(active_index, [&](auto index_constant) {
+      constexpr size_t i = index_constant.value;
+      ++get<i>(it_);
+      satisfy<i>();
+    });
+    return *this;
+  }
+
+  constexpr void operator++(int) { ++*this; }
+
+  constexpr iterator operator++(int)
+    requires(forward_range<__maybe_const<Const, Views>> && ...)
+  {
+    auto tmp = *this;
+    ++*this;
+    return tmp;
+  }
+
+  constexpr iterator& operator--()
+    requires concat_is_bidirectional<Const, Views...>
+  {
+    size_t active_index = it_.index();
+    apply_fn_with_const_index<std::variant_size_v<decltype(it_)>>(active_index, [&](auto index_constant) {
+      constexpr size_t i = index_constant.value;
+      prev<i>();
+    });
+    return *this;
+  }
+
+  constexpr iterator operator--(int)
+    requires concat_is_bidirectional<Const, Views...>
+  {
+    auto __tmp = *this;
+    --*this;
+    return __tmp;
+  }
+
+  friend constexpr bool operator==(const iterator& x, const iterator& y)
+    requires(equality_comparable<iterator_t<__maybe_const<Const, Views>>> && ...)
+  {
+    return x.it_ == y.it_;
+  }
+
+  constexpr decltype(auto) operator[](difference_type n) const
+    requires concat_is_random_access<Const, Views...>
+  {
+    return *((*this) + n);
+  }
+
+  friend constexpr iterator operator+(const iterator& it, difference_type n)
+    requires concat_is_random_access<Const, Views...>
+  {
+    auto temp = it;
+    temp += n;
+    return temp;
+  }
+
+  friend constexpr iterator operator+(difference_type n, const iterator& it)
+    requires concat_is_random_access<Const, Views...>
+  {
+    return it + n;
+  }
+
+  constexpr iterator& operator+=(difference_type n)
+    requires concat_is_random_access<Const, Views...>
+  {
+    size_t active_index = it_.index();
+    if (n > 0) {
+      std::visit(
+          [&](auto& active_it) {
+            apply_fn_with_const_index<std::tuple_size_v<decltype(parent_->views_)>>(
+                active_index, [&](auto index_constant) {
+                  constexpr size_t I  = index_constant.value;
+                  auto& active_view   = std::get<I>(parent_->views_);
+                  difference_type idx = active_it - ranges::begin(active_view);
+                  advance_fwd<I>(idx, n);
+                });
+          },
+          it_);
+    }
+
+    else if (n < 0) {
+      std::visit(
+          [&](auto& active_it) {
+            apply_fn_with_const_index<std::tuple_size_v<decltype(parent_->views_)>>(
+                active_index, [&](auto index_constant) {
+                  constexpr size_t I  = index_constant.value;
+                  auto& active_view   = std::get<I>(parent_->views_);
+                  difference_type idx = active_it - ranges::begin(active_view);
+                  advance_bwd<I>(idx, -n);
+                });
+          },
+          it_);
+    }
+
+    return *this;
+  }
+
+  constexpr iterator& operator-=(difference_type n)
+    requires concat_is_random_access<Const, Views...>
+  {
+    *this += -n;
+    return *this;
+  }
+
+  friend constexpr bool operator==(const iterator& it, default_sentinel_t) {
+    constexpr auto last_idx = sizeof...(Views) - 1;
+    return it.it_.index() == last_idx &&
+           std::get<last_idx>(it.it_) == ranges::end(std::get<last_idx>(it.parent_->views_));
+  }
+
+  friend constexpr bool operator<(const iterator& x, const iterator& y)
+    requires(random_access_range<__maybe_const<Const, Views>> && ...)
+  {
+    return x.it_ < y.it_;
+  }
+
+  friend constexpr bool operator>(const iterator& x, const iterator& y)
+    requires(random_access_range<__maybe_const<Const, Views>> && ...)
+  {
+    return x.it_ > y.it_;
+  }
+
+  friend constexpr bool operator<=(const iterator& x, const iterator& y)
+    requires(random_access_range<__maybe_const<Const, Views>> && ...)
+  {
+    return x.it_ <= y.it_;
+  }
+
+  friend constexpr bool operator>=(const iterator& x, const iterator& y)
+    requires(random_access_range<__maybe_const<Const, Views>> && ...)
+  {
+    return x.it_ >= y.it_;
+  }
+
+  friend constexpr auto operator<=>(const iterator& x, const iterator& y)
+    requires((random_access_range<__maybe_const<Const, Views>> && ...) &&
+             (three_way_comparable<__maybe_const<Const, Views>> && ...))
+  {
+    return x.it_ <=> y.it_;
+  }
+
+  friend constexpr decltype(auto) iter_move(const iterator& it) noexcept(
+
+      ((is_nothrow_invocable_v< decltype(ranges::iter_move), const iterator_t<__maybe_const<Const, Views>>& >) &&
+       ...) &&
+      ((is_nothrow_convertible_v< range_rvalue_reference_t<__maybe_const<Const, Views>>,
+                                  concat_rvalue_reference_t<__maybe_const<Const, Views>...> >) &&
+       ...))
+
+  {
+    return std::visit(
+        [](const auto& i) -> concat_rvalue_reference_t<__maybe_const<Const, Views>...> { return ranges::iter_move(i); },
+        it.it_);
+  }
+
+  friend constexpr void iter_swap(const iterator& x, const iterator& y)
+
+      noexcept((noexcept(ranges::swap(*x, *y))) &&
+               (noexcept(ranges::iter_swap(std::declval<const iterator_t<__maybe_const<Const, Views>>>(),
+                                           std::declval<const iterator_t<__maybe_const<Const, Views>>>())) &&
+                ...))
+
+    requires swappable_with<iter_reference_t<iterator>, iter_reference_t<iterator>> &&
+             (... && indirectly_swappable<iterator_t<__maybe_const<Const, Views>>>)
+  {
+    std::visit(
+        [&](const auto& it1, const auto& it2) {
+          if constexpr (is_same_v<decltype(it1), decltype(it2)>) {
+            ranges::iter_swap(it1, it2);
+          } else {
+            ranges::swap(*x, *y);
+          }
+        },
+        x.it_,
+        y.it_);
+  }
+
+  friend constexpr difference_type operator-(const iterator& x, const iterator& y)
+    requires concat_is_random_access<Const, Views...>
+  {
+    size_t ix = x.it_.index();
+    size_t iy = y.it_.index();
+
+    if (ix > iy) {
+      std::visit(
+          [&](auto& it_x, auto& it_y) {
+            it_x.apply_fn_with_const_index<std::tuple_size_v<decltype(x.parent_->views_)>>(
+                ix, [&](auto index_constant) {
+                  constexpr size_t index_x = index_constant.value;
+                  auto dx = ranges::__distance(ranges::begin(std::get<index_x>(x.parent_->views_)), it_x);
+
+                  it_y.apply_fn_with_const_index<std::tuple_size_v<decltype(y.parent_->views_)>>(
+                      iy, [&](auto index_constant) {
+                        constexpr size_t index_y = index_constant.value;
+                        auto dy = ranges::__distance(ranges::begin(std::get<index_y>(y.parent_->views_)), it_y);
+                        difference_type s = 0;
+                        for (size_t idx = index_y + 1; idx < index_x; idx++) {
+                          s += ranges::size(std::get<idx>(x.parent_->views_));
+                        }
+                        return dy + s + dx;
+                      });
+                });
+          },
+          x.it_,
+          y.it_);
+    } else if (ix < iy) {
+      return -(y - x);
+    } else {
+      std::visit([&](const auto& it1, const auto& it2) { return it1 - it2; }, x.it_, y.it_);
+    }
+  }
+
+  friend constexpr iterator operator-(const iterator& it, difference_type n)
+    requires concat_is_random_access<Const, Views...>
+  {
+    auto temp = it;
+    temp -= n;
+    re...
[truncated]

Copy link

github-actions bot commented Dec 22, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@changkhothuychung changkhothuychung marked this pull request as ready for review December 23, 2024 05:45
@changkhothuychung
Copy link
Contributor Author

I have no idea why I am getting error: no type named 'concat_view' in namespace 'std::ranges' in the buildkite. My test runs successfully locally. Does anyone know what is wrong?

@changkhothuychung changkhothuychung changed the title [libcxx] implement views::concat [libc++] implement views::concat Dec 23, 2024
Copy link
Contributor

@philnik777 philnik777 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make sure you're following the libc++ Coding Guidelines.

@changkhothuychung
Copy link
Contributor Author

@frederick-vs-ja , while creating the tests for valueless by exception, I ran into an issue with ranges::__distance, this is the error log. However, if I change it do ranges::distance, the tests pass. So looks like ranges::__distance is not working correctly? Can you help me take a look?

# | /Users/nguyennhat/Desktop/llvm-project/build/runtimes/runtimes-bins/libcxx/test-suite-install/include/c++/v1/__ranges/concat_view.h:312:28: error: no matching conversion for functional-style cast from 'const typename tuple_element<0UL, tuple<Range<0>, Range<1>>>::type' (aka 'const Range<0>') to 'ranges::__distance'
# |   312 |         auto __prev_size = ranges::__distance(std::get<_Idx - 1>(__parent_->__views_));
# |       |                            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | /Users/nguyennhat/Desktop/llvm-project/build/runtimes/runtimes-bins/libcxx/test-suite-install/include/c++/v1/__ranges/concat_view.h:455:15: note: in instantiation of function template specialization 'std::ranges::concat_view<Range<0>, Range<1>>::__iterator<true>::__advance_bwd<1UL>' requested here
# |   455 |               __advance_bwd<__i>(__idx, -__n);
# |       |               ^
# | /Users/nguyennhat/Desktop/llvm-project/build/runtimes/runtimes-bins/libcxx/test-suite-install/include/c++/v1/__ranges/concat_view.h:321:25: note: in instantiation of function template specialization 'std::ranges::concat_view<Range<0>, Range<1>>::__iterator<true>::operator+=(difference_type)::(anonymous class)::operator()(Iter<0> &)::(anonymous class)::operator()<std::integral_constant<unsigned long, 1>>' requested here
# |   321 |     ((__index == _Is ? (__func(integral_constant<size_t, _Is>{}), 0) : 0), ...);
# |       |                         ^
# | /Users/nguyennhat/Desktop/llvm-project/build/runtimes/runtimes-bins/libcxx/test-suite-install/include/c++/v1/__ranges/concat_view.h:326:5: note: in instantiation of function template specialization 'std::ranges::concat_view<Range<0>, Range<1>>::__iterator<true>::__apply_at_index<0UL, 1UL, (lambda at /Users/nguyennhat/Desktop/llvm-project/build/runtimes/runtimes-bins/libcxx/test-suite-install/include/c++/v1/__ranges/concat_view.h:451:91)>' requested here
# |   326 |     __apply_at_index(__index, std::forward<_Func>(__func), make_index_sequence<_Idx>{});
# |       |     ^
# | /Users/nguyennhat/Desktop/llvm-project/build/runtimes/runtimes-bins/libcxx/test-suite-install/include/c++/v1/__ranges/concat_view.h:451:13: note: in instantiation of function template specialization 'std::ranges::concat_view<Range<0>, Range<1>>::__iterator<true>::__apply_at_index<2UL, (lambda at /Users/nguyennhat/Desktop/llvm-project/build/runtimes/runtimes-bins/libcxx/test-suite-install/include/c++/v1/__ranges/concat_view.h:451:91)>' requested here
# |   451 |             __apply_at_index<tuple_size_v<decltype(__parent_->__views_)>>(__active_index, [&](auto __index_constant) {
# |       |             ^
# | /Users/nguyennhat/Desktop/llvm-project/build/runtimes/runtimes-bins/libcxx/test-suite-install/include/c++/v1/__type_traits/invoke.h:176:10: note: in instantiation of function template specialization 'std::ranges::concat_view<Range<0>, Range<1>>::__iterator<true>::operator+=(difference_type)::(anonymous class)::operator()<Iter<0>>' requested here
# |   176 | decltype(std::declval<_Fp>()(std::declval<_Args>()...))
# |       |          ^
# | /Users/nguyennhat/Desktop/llvm-project/build/runtimes/runtimes-bins/libcxx/test-suite-install/include/c++/v1/__type_traits/invoke.h:186:19: note: (skipping 19 contexts in backtrace; use -ftemplate-backtrace-limit=0 to see all)
# |   186 |   static decltype(std::__invoke(std::declval<_XFp>(), std::declval<_XArgs>()...)) __try_call(int);
# |       |                   ^
# | /Users/nguyennhat/Desktop/llvm-project/build/runtimes/runtimes-bins/libcxx/test-suite-install/include/c++/v1/__iterator/concepts.h:189:9: note: in instantiation of requirement here
# |   189 |       { __j[__n] } -> same_as<iter_reference_t<_Ip>>;
# |       |         ^~~~~~~~
# | /Users/nguyennhat/Desktop/llvm-project/build/runtimes/runtimes-bins/libcxx/test-suite-install/include/c++/v1/__iterator/concepts.h:183:5: note: while substituting template arguments into constraint expression here
# |   183 |     requires(_Ip __i, const _Ip __j, const iter_difference_t<_Ip> __n) {
# |       |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |   184 |       { __i += __n } -> same_as<_Ip&>;
# |       |       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |   185 |       { __j + __n } -> same_as<_Ip>;
# |       |       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |   186 |       { __n + __j } -> same_as<_Ip>;
# |       |       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |   187 |       { __i -= __n } -> same_as<_Ip&>;
# |       |       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |   188 |       { __j - __n } -> same_as<_Ip>;
# |       |       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |   189 |       { __j[__n] } -> same_as<iter_reference_t<_Ip>>;
# |       |       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |   190 |     };
# |       |     ~
# | /Users/nguyennhat/Desktop/llvm-project/build/runtimes/runtimes-bins/libcxx/test-suite-install/include/c++/v1/__iterator/advance.h:106:19: note: while checking the satisfaction of concept 'random_access_iterator<std::ranges::concat_view<Range<0>, Range<1>>::__iterator<true>>' requested here
# |   106 |     if constexpr (random_access_iterator<_Ip>) {
# |       |                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | /Users/nguyennhat/Desktop/llvm-project/build/runtimes/runtimes-bins/libcxx/test-suite-install/include/c++/v1/__iterator/next.h:53:5: note: in instantiation of function template specialization 'std::ranges::__advance::operator()<std::ranges::concat_view<Range<0>, Range<1>>::__iterator<true>>' requested here
# |    53 |     ranges::advance(__x, __n);
# |       |     ^
# | /Users/nguyennhat/Desktop/llvm-project/libcxx/test/libcxx/ranges/range.adaptors/range.concat/iterator.valueless_by_exception.pass.cpp:140:35: note: in instantiation of function template specialization 'std::ranges::__next::operator()<std::ranges::concat_view<Range<0>, Range<1>>::__iterator<true>>' requested here
# |   140 |     auto iter2 = std::ranges::next(cv.begin(), 4);
# |       |                                   ^
# | /Users/nguyennhat/Desktop/llvm-project/build/runtimes/runtimes-bins/libcxx/test-suite-install/include/c++/v1/__iterator/distance.h:55:8: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'const typename tuple_element<0UL, tuple<Range<0>, Range<1>>>::type' (aka 'const Range<0>') to 'const __distance' for 1st argument
# |    55 | struct __distance {
# |       |        ^~~~~~~~~~
# | /Users/nguyennhat/Desktop/llvm-project/build/runtimes/runtimes-bins/libcxx/test-suite-install/include/c++/v1/__iterator/distance.h:55:8: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'const typename tuple_element<0UL, tuple<Range<0>, Range<1>>>::type' (aka 'const Range<0>') to '__distance' for 1st argument
# |    55 | struct __distance {
# |       |        ^~~~~~~~~~
# | /Users/nguyennhat/Desktop/llvm-project/build/runtimes/runtimes-bins/libcxx/test-suite-install/include/c++/v1/__iterator/distance.h:55:8: note: candidate constructor (the implicit default constructor) not viable: requires 0 arguments, but 1 was provided
# | In file included from /Users/nguyennhat/Desktop/llvm-project/libcxx/test/libcxx/ranges/range.adaptors/range.concat/iterator.valueless_by_exception.pass.cpp:13:
# | In file included from /Users/nguyennhat/Desktop/llvm-project/build/runtimes/runtimes-bins/libcxx/test-suite-install/include/c++/v1/ranges:446:
# | /Users/nguyennhat/Desktop/llvm-project/build/runtimes/runtimes-bins/libcxx/test-suite-install/include/c++/v1/__ranges/concat_view.h:455:15: error: no matching member function for call to '__advance_bwd'
# |   455 |               __advance_bwd<__i>(__idx, -__n);
# |       |               ^~~~~~~~~~~~~~~~~~
# | /Users/nguyennhat/Desktop/llvm-project/build/runtimes/runtimes-bins/libcxx/test-suite-install/include/c++/v1/__ranges/concat_view.h:321:25: note: in instantiation of function template specialization 'std::ranges::concat_view<Range<0>, Range<1>>::__iterator<true>::operator+=(difference_type)::(anonymous class)::operator()(Iter<1> &)::(anonymous class)::operator()<std::integral_constant<unsigned long, 1>>' requested here
# |   321 |     ((__index == _Is ? (__func(integral_constant<size_t, _Is>{}), 0) : 0), ...);
# |       |                         ^
# | /Users/nguyennhat/Desktop/llvm-project/build/runtimes/runtimes-bins/libcxx/test-suite-install/include/c++/v1/__ranges/concat_view.h:326:5: note: in instantiation of function template specialization 'std::ranges::concat_view<Range<0>, Range<1>>::__iterator<true>::__apply_at_index<0UL, 1UL, (lambda at /Users/nguyennhat/Desktop/llvm-project/build/runtimes/runtimes-bins/libcxx/test-suite-install/include/c++/v1/__ranges/concat_view.h:451:91)>' requested here
# |   326 |     __apply_at_index(__index, std::forward<_Func>(__func), make_index_sequence<_Idx>{});
# |       |     ^
# | /Users/nguyennhat/Desktop/llvm-project/build/runtimes/runtimes-bins/libcxx/test-suite-install/include/c++/v1/__ranges/concat_view.h:451:13: note: in instantiation of function template specialization 'std::ranges::concat_view<Range<0>, Range<1>>::__iterator<true>::__apply_at_index<2UL, (lambda at /Users/nguyennhat/Desktop/llvm-project/build/runtimes/runtimes-bins/libcxx/test-suite-install/include/c++/v1/__ranges/concat_view.h:451:91)>' requested here
# |   451 |             __apply_at_index<tuple_size_v<decltype(__parent_->__views_)>>(__active_index, [&](auto __index_constant) {
# |       |             ^
# | /Users/nguyennhat/Desktop/llvm-project/build/runtimes/runtimes-bins/libcxx/test-suite-install/include/c++/v1/__type_traits/invoke.h:176:10: note: in instantiation of function template specialization 'std::ranges::concat_view<Range<0>, Range<1>>::__iterator<true>::operator+=(difference_type)::(anonymous class)::operator()<Iter<1>>' requested here
# |   176 | decltype(std::declval<_Fp>()(std::declval<_Args>()...))
# |       |          ^
# | /Users/nguyennhat/Desktop/llvm-project/build/runtimes/runtimes-bins/libcxx/test-suite-install/include/c++/v1/__type_traits/invoke.h:186:19: note: while substituting deduced template arguments into function template '__invoke' [with _Fp = (lambda at /Users/nguyennhat/Desktop/llvm-project/build/runtimes/runtimes-bins/libcxx/test-suite-install/include/c++/v1/__ranges/concat_view.h:450:11), _Args = <Iter<1> &>]
# |   186 |   static decltype(std::__invoke(std::declval<_XFp>(), std::declval<_XArgs>()...)) __try_call(int);
# |       |                   ^
# | /Users/nguyennhat/Desktop/llvm-project/build/runtimes/runtimes-bins/libcxx/test-suite-install/include/c++/v1/__type_traits/invoke.h:192:44: note: (skipping 18 contexts in backtrace; use -ftemplate-backtrace-limit=0 to see all)
# |   192 |   using _Result _LIBCPP_NODEBUG = decltype(__try_call<_Fp, _Args...>(0));
# |       |                                            ^
# | /Users/nguyennhat/Desktop/llvm-project/build/runtimes/runtimes-bins/libcxx/test-suite-install/include/c++/v1/__iterator/concepts.h:189:9: note: in instantiation of requirement here
# |   189 |       { __j[__n] } -> same_as<iter_reference_t<_Ip>>;
# |       |         ^~~~~~~~
# | /Users/nguyennhat/Desktop/llvm-project/build/runtimes/runtimes-bins/libcxx/test-suite-install/include/c++/v1/__iterator/concepts.h:183:5: note: while substituting template arguments into constraint expression here
# |   183 |     requires(_Ip __i, const _Ip __j, const iter_difference_t<_Ip> __n) {
# |       |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |   184 |       { __i += __n } -> same_as<_Ip&>;
# |       |       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |   185 |       { __j + __n } -> same_as<_Ip>;
# |       |       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |   186 |       { __n + __j } -> same_as<_Ip>;
# |       |       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |   187 |       { __i -= __n } -> same_as<_Ip&>;
# |       |       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |   188 |       { __j - __n } -> same_as<_Ip>;
# |       |       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |   189 |       { __j[__n] } -> same_as<iter_reference_t<_Ip>>;
# |       |       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |   190 |     };
# |       |     ~
# | /Users/nguyennhat/Desktop/llvm-project/build/runtimes/runtimes-bins/libcxx/test-suite-install/include/c++/v1/__iterator/advance.h:106:19: note: while checking the satisfaction of concept 'random_access_iterator<std::ranges::concat_view<Range<0>, Range<1>>::__iterator<true>>' requested here
# |   106 |     if constexpr (random_access_iterator<_Ip>) {
# |       |                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | /Users/nguyennhat/Desktop/llvm-project/build/runtimes/runtimes-bins/libcxx/test-suite-install/include/c++/v1/__iterator/next.h:53:5: note: in instantiation of function template specialization 'std::ranges::__advance::operator()<std::ranges::concat_view<Range<0>, Range<1>>::__iterator<true>>' requested here
# |    53 |     ranges::advance(__x, __n);
# |       |     ^
# | /Users/nguyennhat/Desktop/llvm-project/libcxx/test/libcxx/ranges/range.adaptors/range.concat/iterator.valueless_by_exception.pass.cpp:140:35: note: in instantiation of function template specialization 'std::ranges::__next::operator()<std::ranges::concat_view<Range<0>, Range<1>>::__iterator<true>>' requested here
# |   140 |     auto iter2 = std::ranges::next(cv.begin(), 4);
# |       |                                   ^
# | /Users/nguyennhat/Desktop/llvm-project/build/runtimes/runtimes-bins/libcxx/test-suite-install/include/c++/v1/__ranges/concat_view.h:304:40: note: candidate template ignored: substitution failure [with _Idx = 1]
# |   304 |   _LIBCPP_HIDE_FROM_ABI constexpr void __advance_bwd(difference_type __offset, difference_type __steps) {
# |       |                                        ^
# | 2 errors generated.
# `-----------------------------
# error: command failed with exit status: 1

--

@frederick-vs-ja

This comment was marked as resolved.

….valueless_by_exception.pass.cpp

Co-authored-by: A. Jiang <[email protected]>
….valueless_by_exception.pass.cpp

Co-authored-by: A. Jiang <[email protected]>
Copy link
Contributor

@frederick-vs-ja frederick-vs-ja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM modulo styles.

@changkhothuychung
Copy link
Contributor Author

@frederick-vs-ja since you approved, do you have any other concern? If not, i'll merge the PR after 1-2 days once CI is green.

@frederick-vs-ja
Copy link
Contributor

@frederick-vs-ja since you approved, do you have any other concern? If not, i'll merge the PR after 1-2 days once CI is green.

I'd ask @huixie90 for definitive (IMO) approval.

@huixie90
Copy link
Contributor

@frederick-vs-ja since you approved, do you have any other concern? If not, i'll merge the PR after 1-2 days once CI is green.

I'd ask @huixie90 for definitive (IMO) approval.

will take another pass this weekend

@huixie90 huixie90 self-assigned this May 16, 2025
Copy link
Contributor

@huixie90 huixie90 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a first pass on the implementation source code and left some comments.
I did not look at the tests yet but by looking at the file names i think the tests for the random-access functions are missing

#ifndef _LIBCPP___RANGES_CONCAT_VIEW_H
#define _LIBCPP___RANGES_CONCAT_VIEW_H

#include <__algorithm/ranges_find_if.h>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this used?

#include <__concepts/equality_comparable.h>
#include <__concepts/swappable.h>
#include <__config>
#include <__functional/bind_back.h>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this used?

I would suggest to go through all the includes here to see if they are actually used or is it just something left over from the early iterations?

Comment on lines +99 to +129
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 _LIBCPP_NODEBUG = common_reference_t<range_reference_t<_Rs>...>;

template <class... _Rs>
using __concat_value_t _LIBCPP_NODEBUG = common_type_t<range_value_t<_Rs>...>;

template <class... _Rs>
using __concat_rvalue_reference_t _LIBCPP_NODEBUG = 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...>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These exposition only concepts are being implemented in this PR
https://github.com/llvm/llvm-project/pull/65536/files#diff-35741011d906a81be46c7f783dccd5ff46e32f319da2267b8e37b00e4e77bc6e

Depending on which is landed first, you two need to rebase on top of each other. Since these concepts are reused in two views, I would suggest you to follow the other patch's approach: put them into a common header to be shared with the two views

Comment on lines +71 to +72
"`LWG4079 <https://wg21.link/LWG4079>`__","Missing Preconditions in ``concat_view::iterator``\`s conversion constructor","2024-06 (St. Louis)","|Complete|","21",""
"`LWG4082 <https://wg21.link/LWG4082>`__","``views::concat(r)`` is well-formed when ``r`` is an ``output_range``","2024-06 (St. Louis)","|Complete|","21",""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have tests for these two LWG issues?

_LIBCPP_HIDE_FROM_ABI constexpr auto end()
requires(!(__simple_view<_Views> && ...))
{
if constexpr (common_range<typename __last_view<_Views...>::type>) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing maybe-const.

And potentially missing tests to cover the case

Comment on lines +565 to +586
__variant_detail::__visitation::__variant::__visit_value(
[&](auto& __it_x, auto& __it_y) {
__it_x.template __apply_at_index<tuple_size_v<decltype(__x.__parent_->__views_)>>(
__ix, [&](auto __index_constant_x) {
constexpr size_t __index_x = __index_constant_x.value;
auto __dx = ranges::distance(ranges::begin(std::get<__index_x>(__x.__parent_->__views_)), __it_x);

__it_y.template __apply_at_index<tuple_size_v<decltype(__y.__parent_->__views_)>>(
__iy, [&](auto __index_constant_y) {
constexpr size_t __index_y = __index_constant_y.value;
auto __dy =
ranges::distance(ranges::begin(std::get<__index_y>(__y.__parent_->__views_)), __it_y);
difference_type __s = 0;
for (size_t __idx = __index_y + 1; __idx < __index_x; __idx++) {
__s += ranges::size(std::get<__idx>(__x.__parent_->__views_));
}
return __dy + __s + __dx;
});
});
},
__x.__it_,
__y.__it_);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it correct? or tested? __it_x seem to be of type the concrete underlying indicator , but then you used as a variant : __it_x.template __apply_at_index .

Comment on lines +613 to +627
[&](auto& __it_x) {
__it_x.template __apply_at_index<tuple_size_v<decltype(__x.__parent_->__views_)>>(
__ix, [&](auto __index_constant) {
constexpr size_t __index_x = __index_constant.value;
auto __dx = ranges::distance(ranges::begin(std::get<__index_x>(__x.__parent_->__views_)), __it_x);

difference_type __s = 0;
for (size_t __idx = 0; __idx < __index_x; __idx++) {
__s += ranges::size(std::get<__idx>(__x.__parent_->__views_));
}

return -(__dx + __s);
});
},
__x.__it_);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same question as above

_LIBCPP_HIDE_FROM_ABI friend constexpr decltype(auto) iter_move(const __iterator& __it) noexcept(

((is_nothrow_invocable_v< decltype(ranges::iter_move), const iterator_t<__maybe_const<_Const, _Views>>& >) &&
...) &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the spec does not expand on this line

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This spec ([range.concat.iterator]/42) is simpler. But I think such implementation strategy is equivalent, as all is_nothrow_invocable_v<...> and is_nothrow_convertible_v<...> need to be instantiated.

Comment on lines +564 to +584
if (__ix > __iy) {
__variant_detail::__visitation::__variant::__visit_value(
[&](auto& __it_x, auto& __it_y) {
__it_x.template __apply_at_index<tuple_size_v<decltype(__x.__parent_->__views_)>>(
__ix, [&](auto __index_constant_x) {
constexpr size_t __index_x = __index_constant_x.value;
auto __dx = ranges::distance(ranges::begin(std::get<__index_x>(__x.__parent_->__views_)), __it_x);

__it_y.template __apply_at_index<tuple_size_v<decltype(__y.__parent_->__views_)>>(
__iy, [&](auto __index_constant_y) {
constexpr size_t __index_y = __index_constant_y.value;
auto __dy =
ranges::distance(ranges::begin(std::get<__index_y>(__y.__parent_->__views_)), __it_y);
difference_type __s = 0;
for (size_t __idx = __index_y + 1; __idx < __index_x; __idx++) {
__s += ranges::size(std::get<__idx>(__x.__parent_->__views_));
}
return __dy + __s + __dx;
});
});
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again. i have doubts in the correctness of this function and whether it is tested at all. __it_x should have the underlying iterator type but you are using it as a concat iterator

namespace views {
namespace __concat {
struct __fn {
template <input_range _Range>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would avoid putting extra contraints here and just rely on the return type SFIANE

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would avoid putting extra contraints here and just rely on the return type SFIANE

This seems more complicated to me. Does views::all already reject non-input ranges?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++26 libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
9 participants