public inbox for gcc-cvs@sourceware.org help / color / mirror / Atom feed
From: Patrick Palka <ppalka@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r13-6558] libstdc++: extraneous begin in cartesian_product_view::end [PR107572] Date: Thu, 9 Mar 2023 18:25:52 +0000 (GMT) [thread overview] Message-ID: <20230309182552.59A1A3858D20@sourceware.org> (raw) 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(); }
reply other threads:[~2023-03-09 18:25 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20230309182552.59A1A3858D20@sourceware.org \ --to=ppalka@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ --cc=libstdc++-cvs@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: linkBe 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).