]> gcc.gnu.org Git - gcc.git/commitdiff
libstdc++: extraneous begin in cartesian_product_view::end [PR107572]
authorPatrick Palka <ppalka@redhat.com>
Thu, 9 Mar 2023 18:25:44 +0000 (13:25 -0500)
committerPatrick Palka <ppalka@redhat.com>
Thu, 9 Mar 2023 18:25:44 +0000 (13:25 -0500)
ranges::begin() isn't guaranteed to be equality-preserving for non-forward
ranges, so in cartesian_product_view::end we need to avoid needlessly
calling begin() on the first range (which could be non-forward) in the
case where __empty_tail is false as per its specification.

Since we're already using a variadic lambda to compute __empty_tail, we
might as well use that same lambda to build up the tuple of iterators
instead of building it separately via e.g. std::apply or __tuple_transform.

PR libstdc++/107572

libstdc++-v3/ChangeLog:

* include/std/ranges (cartesian_product_view::end): When
building the tuple of iterators, avoid calling ranges::begin on
the first range if __empty_tail is false.
* testsuite/std/ranges/cartesian_product/1.cc (test07): New test.

libstdc++-v3/include/std/ranges
libstdc++-v3/testsuite/std/ranges/cartesian_product/1.cc

index e0cac15a64f738a07abfeff149e484c20b75010b..0a65d74bb5bd1b4800044223cb1b9d43a0f5b802 100644 (file)
@@ -8078,27 +8078,35 @@ namespace views::__adaptor
     end() requires ((!__detail::__simple_view<_First> || ... || !__detail::__simple_view<_Vs>)
                    && __detail::__cartesian_product_is_common<_First, _Vs...>)
     {
-      bool __empty_tail = [this]<size_t... _Is>(index_sequence<_Is...>) {
-       return (ranges::empty(std::get<1 + _Is>(_M_bases)) || ...);
+      auto __its = [this]<size_t... _Is>(index_sequence<_Is...>) {
+       using _Ret = __detail::__tuple_or_pair_t<iterator_t<_First>,
+                                                iterator_t<_Vs>...>;
+       bool __empty_tail = (ranges::empty(std::get<1 + _Is>(_M_bases)) || ...);
+       auto& __first = std::get<0>(_M_bases);
+       return _Ret{(__empty_tail
+                    ? ranges::begin(__first)
+                    : __detail::__cartesian_common_arg_end(__first)),
+                   ranges::begin(std::get<1 + _Is>(_M_bases))...};
       }(make_index_sequence<sizeof...(_Vs)>{});
 
-      auto __it = __detail::__tuple_transform(ranges::begin, _M_bases);
-      if (!__empty_tail)
-       std::get<0>(__it) = __detail::__cartesian_common_arg_end(std::get<0>(_M_bases));
-      return _Iterator<false>{*this, std::move(__it)};
+      return _Iterator<false>{*this, std::move(__its)};
     }
 
     constexpr _Iterator<true>
     end() const requires __detail::__cartesian_product_is_common<const _First, const _Vs...>
     {
-      bool __empty_tail = [this]<size_t... _Is>(index_sequence<_Is...>) {
-       return (ranges::empty(std::get<1 + _Is>(_M_bases)) || ...);
+      auto __its = [this]<size_t... _Is>(index_sequence<_Is...>) {
+       using _Ret = __detail::__tuple_or_pair_t<iterator_t<const _First>,
+                                                iterator_t<const _Vs>...>;
+       bool __empty_tail = (ranges::empty(std::get<1 + _Is>(_M_bases)) || ...);
+       auto& __first = std::get<0>(_M_bases);
+       return _Ret{(__empty_tail
+                    ? ranges::begin(__first)
+                    : __detail::__cartesian_common_arg_end(__first)),
+                   ranges::begin(std::get<1 + _Is>(_M_bases))...};
       }(make_index_sequence<sizeof...(_Vs)>{});
 
-      auto __it = __detail::__tuple_transform(ranges::begin, _M_bases);
-      if (!__empty_tail)
-       std::get<0>(__it) = __detail::__cartesian_common_arg_end(std::get<0>(_M_bases));
-      return _Iterator<true>{*this, std::move(__it)};
+      return _Iterator<true>{*this, std::move(__its)};
     }
 
     constexpr default_sentinel_t
index 1ec4422e6f3acf5996ee1923a83c3ed598a64248..f52c2b96d5860bc80882a68df796c6fd46bd0b2b 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <ranges>
 #include <algorithm>
+#include <sstream>
 #include <testsuite_hooks.h>
 #include <testsuite_iterators.h>
 
@@ -178,6 +179,28 @@ test06()
   return true;
 }
 
+void
+test07()
+{
+  // PR libstdc++/107572
+  static std::istringstream ints("0 1 2 3 4");
+  struct istream_range {
+    auto begin() { return std::istream_iterator<int>{ints}; }
+    auto end() { return std::istream_iterator<int>{}; }
+    using iterator_concept = std::input_iterator_tag;
+  };
+  static_assert(!ranges::forward_range<istream_range>
+               && ranges::common_range<istream_range>);
+  istream_range r;
+  int i = 0;
+  for (auto [v] : views::cartesian_product(r))
+    {
+      VERIFY( v == i );
+      ++i;
+    };
+  VERIFY( i == 5 );
+}
+
 int
 main()
 {
@@ -187,4 +210,5 @@ main()
   test04();
   test05();
   static_assert(test06());
+  test07();
 }
This page took 0.07269 seconds and 5 git commands to generate.