public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
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++: Fix chunk_by_view when value_type& and reference differ [PR108291]
Date: Wed, 12 Apr 2023 10:41:24 -0400	[thread overview]
Message-ID: <20230412144124.3356890-1-ppalka@redhat.com> (raw)

Tested on x86_64-pc-linux-gnu, does this look OK for trunk?

	PR libstdc++/108291

libstdc++-v3/ChangeLog:

	* include/std/ranges (chunk_by_view::_M_find_next): Generalize
	parameter types of the predicate passed to adjacent_find.
	(chunk_by_view::_M_find_prev): Likewise.
	* testsuite/std/ranges/adaptors/chunk_by/1.cc (test04, test05):
	New tests.
---
 libstdc++-v3/include/std/ranges               |  8 ++---
 .../std/ranges/adaptors/chunk_by/1.cc         | 35 +++++++++++++++++++
 2 files changed, 39 insertions(+), 4 deletions(-)

diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges
index be71c370eb7..dc37a8afe51 100644
--- a/libstdc++-v3/include/std/ranges
+++ b/libstdc++-v3/include/std/ranges
@@ -6743,8 +6743,8 @@ namespace views::__adaptor
     _M_find_next(iterator_t<_Vp> __current)
     {
       __glibcxx_assert(_M_pred.has_value());
-      auto __pred = [this]<typename _Tp>(_Tp&& __x, _Tp&& __y) {
-	return !bool((*_M_pred)(std::forward<_Tp>(__x), std::forward<_Tp>(__y)));
+      auto __pred = [this]<typename _Tp, typename _Up>(_Tp&& __x, _Up&& __y) {
+	return !bool((*_M_pred)(std::forward<_Tp>(__x), std::forward<_Up>(__y)));
       };
       auto __it = ranges::adjacent_find(__current, ranges::end(_M_base), __pred);
       return ranges::next(__it, 1, ranges::end(_M_base));
@@ -6754,8 +6754,8 @@ namespace views::__adaptor
     _M_find_prev(iterator_t<_Vp> __current) requires bidirectional_range<_Vp>
     {
       __glibcxx_assert(_M_pred.has_value());
-      auto __pred = [this]<typename _Tp>(_Tp&& __x, _Tp&& __y) {
-	return !bool((*_M_pred)(std::forward<_Tp>(__y), std::forward<_Tp>(__x)));
+      auto __pred = [this]<typename _Tp, typename _Up>(_Tp&& __x, _Up&& __y) {
+	return !bool((*_M_pred)(std::forward<_Up>(__y), std::forward<_Tp>(__x)));
       };
       auto __rbegin = std::make_reverse_iterator(__current);
       auto __rend = std::make_reverse_iterator(ranges::begin(_M_base));
diff --git a/libstdc++-v3/testsuite/std/ranges/adaptors/chunk_by/1.cc b/libstdc++-v3/testsuite/std/ranges/adaptors/chunk_by/1.cc
index f165c7d9a95..a8fceb105e0 100644
--- a/libstdc++-v3/testsuite/std/ranges/adaptors/chunk_by/1.cc
+++ b/libstdc++-v3/testsuite/std/ranges/adaptors/chunk_by/1.cc
@@ -61,10 +61,45 @@ test03()
   ranges::chunk_by_view<ranges::empty_view<int>, ranges::equal_to> r;
 }
 
+constexpr bool
+test04()
+{
+  // PR libstdc++/108291
+  using namespace std::literals;
+  std::string_view s = "hello";
+  auto r = s | views::chunk_by(std::less{});
+  VERIFY( ranges::equal(r,
+			(std::string_view[]){"h"sv, "el"sv, "lo"sv},
+			ranges::equal) );
+  VERIFY( ranges::equal(r | views::reverse,
+			(std::string_view[]){"lo"sv, "el"sv, "h"sv},
+			ranges::equal) );
+
+  return true;
+}
+
+void
+test05()
+{
+  // PR libstdc++/109474
+  std::vector<bool> v = {true, false, true, true, false, false};
+  auto r = v | views::chunk_by(std::equal_to{});
+  VERIFY( ranges::equal(r,
+			(std::initializer_list<bool>[])
+			  {{true}, {false}, {true, true}, {false, false}},
+			ranges::equal) );
+  VERIFY( ranges::equal(r | views::reverse,
+			(std::initializer_list<bool>[])
+			  {{false, false}, {true, true}, {false}, {true}},
+			ranges::equal) );
+}
+
 int
 main()
 {
   static_assert(test01());
   test02();
   test03();
+  static_assert(test04());
+  test05();
 }
-- 
2.40.0.335.g9857273be0


             reply	other threads:[~2023-04-12 14:41 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-12 14:41 Patrick Palka [this message]
2023-04-12 15:04 ` 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=20230412144124.3356890-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).