From: Patrick Palka <ppalka@redhat.com>
To: gcc-patches@gcc.gnu.org
Cc: libstdc++@gcc.gnu.org, Patrick Palka <ppalka@redhat.com>
Subject: [PATCH] libstdc++: extraneous begin in cartesian_product_view::end [PR107572]
Date: Tue, 7 Mar 2023 15:13:35 -0500 [thread overview]
Message-ID: <20230307201335.14969-1-ppalka@redhat.com> (raw)
ranges::begin() isn't guaranteed to be equality-preserving for
non-forward ranges, so in cartesian_product_view::end we need to be
careful about calling begin() on the first range (which could be
non-forward) in the (non-degenerate) case where __empty_tail is false.
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 doing it via __tuple_transform.
Tested on x86_64-pc-linux-gnu, does this look OK for trunk?
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 | 36 +++++++++++++------
.../std/ranges/cartesian_product/1.cc | 22 ++++++++++++
2 files changed, 48 insertions(+), 10 deletions(-)
diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges
index e0cac15a64f..0de7bdef504 100644
--- a/libstdc++-v3/include/std/ranges
+++ b/libstdc++-v3/include/std/ranges
@@ -8078,26 +8078,42 @@ 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 __it = [this]<size_t... _Is>(index_sequence<_Is...>) {
+ bool __empty_tail = (ranges::empty(std::get<1 + _Is>(_M_bases)) || ...);
+ auto& __first = std::get<0>(_M_bases);
+ auto __first_it = __empty_tail
+ ? ranges::begin(__first)
+ : __detail::__cartesian_common_arg_end(__first);
+ // N.B. When implementing P2165R4 this should be changed to always return tuple.
+ if constexpr (sizeof...(_Is) == 1)
+ return std::make_pair(std::move(__first_it),
+ ranges::begin(std::get<1 + _Is>(_M_bases))...);
+ else
+ return std::make_tuple(std::move(__first_it),
+ 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)};
}
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 __it = [this]<size_t... _Is>(index_sequence<_Is...>) {
+ bool __empty_tail = (ranges::empty(std::get<1 + _Is>(_M_bases)) || ...);
+ auto& __first = std::get<0>(_M_bases);
+ auto __first_it = __empty_tail
+ ? ranges::begin(__first)
+ : __detail::__cartesian_common_arg_end(__first);
+ // N.B. When implementing P2165R4 this should be changed to always return tuple.
+ if constexpr (sizeof...(_Is) == 1)
+ return std::make_pair(std::move(__first_it),
+ ranges::begin(std::get<1 + _Is>(_M_bases))...);
+ else
+ return std::make_tuple(std::move(__first_it),
+ 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)};
}
diff --git a/libstdc++-v3/testsuite/std/ranges/cartesian_product/1.cc b/libstdc++-v3/testsuite/std/ranges/cartesian_product/1.cc
index 1ec4422e6f3..34639f514aa 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,26 @@ 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>{}; }
+ };
+ istream_range r;
+ int i = 0;
+ for (auto [v] : views::cartesian_product(r))
+ {
+ VERIFY( v == i );
+ ++i;
+ };
+ VERIFY( i == 5 );
+}
+
int
main()
{
@@ -187,4 +208,5 @@ main()
test04();
test05();
static_assert(test06());
+ test07();
}
--
2.40.0.rc0.57.g454dfcbddf
next reply other threads:[~2023-03-07 20:13 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-07 20:13 Patrick Palka [this message]
2023-03-07 20:49 ` Patrick Palka
2023-03-09 17:59 ` Jonathan Wakely
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230307201335.14969-1-ppalka@redhat.com \
--to=ppalka@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=libstdc++@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).