public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r13-6558] libstdc++: extraneous begin in cartesian_product_view::end [PR107572]
@ 2023-03-09 18:25 Patrick Palka
  0 siblings, 0 replies; only message in thread
From: Patrick Palka @ 2023-03-09 18:25 UTC (permalink / raw)
  To: gcc-cvs, libstdc++-cvs

https://gcc.gnu.org/g:3df9760d56662bdf38dd45f7398f003bbd64fdfe

commit r13-6558-g3df9760d56662bdf38dd45f7398f003bbd64fdfe
Author: Patrick Palka <ppalka@redhat.com>
Date:   Thu Mar 9 13:25:44 2023 -0500

    libstdc++: extraneous begin in cartesian_product_view::end [PR107572]
    
    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.

Diff:
---
 libstdc++-v3/include/std/ranges                    | 32 ++++++++++++++--------
 .../testsuite/std/ranges/cartesian_product/1.cc    | 24 ++++++++++++++++
 2 files changed, 44 insertions(+), 12 deletions(-)

diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges
index e0cac15a64f..0a65d74bb5b 100644
--- a/libstdc++-v3/include/std/ranges
+++ b/libstdc++-v3/include/std/ranges
@@ -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
diff --git a/libstdc++-v3/testsuite/std/ranges/cartesian_product/1.cc b/libstdc++-v3/testsuite/std/ranges/cartesian_product/1.cc
index 1ec4422e6f3..f52c2b96d58 100644
--- a/libstdc++-v3/testsuite/std/ranges/cartesian_product/1.cc
+++ b/libstdc++-v3/testsuite/std/ranges/cartesian_product/1.cc
@@ -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();
 }

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-03-09 18:25 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-09 18:25 [gcc r13-6558] libstdc++: extraneous begin in cartesian_product_view::end [PR107572] Patrick Palka

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).