diff mbox series

libstdc++: Fix chunk_by_view when value_type& and reference differ [PR108291]

Message ID 20230412144124.3356890-1-ppalka@redhat.com
State New
Headers show
Series libstdc++: Fix chunk_by_view when value_type& and reference differ [PR108291] | expand

Commit Message

Patrick Palka April 12, 2023, 2:41 p.m. UTC
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(-)

Comments

Jonathan Wakely April 12, 2023, 3:04 p.m. UTC | #1
On Wed, 12 Apr 2023 at 15:41, Patrick Palka via Libstdc++
<libstdc++@gcc.gnu.org> wrote:
>
> Tested on x86_64-pc-linux-gnu, does this look OK for trunk?

OK, thanks.


>
>         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
>
diff mbox series

Patch

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();
 }