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++: Implement P2474R2 changes to views::take/drop
Date: Wed, 19 Oct 2022 12:46:45 -0400	[thread overview]
Message-ID: <20221019164645.301739-1-ppalka@redhat.com> (raw)
In-Reply-To: <20221011025113.624107-1-ppalka@redhat.com>

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

libstdc++-v3/ChangeLog:

	* include/std/ranges (views::__detail::__is_repeat_view): Define
	and later define a partial specialization.
	(views::__detail::__take_of_repeat_view): Declare and later define.
	(views::__detail::__drop_of_repeat_view): Likewise.
	(views::_Take::operator()): Return a repeat_view if the argument
	is a repeat_view as per P2474R2.
	(views::_Drop::operator()): Likewise.
	(repeat_view): Befriend __take/drop_of_repeat_view.
	* testsuite/std/ranges/repeat/1.cc (test04): New test.
---
 libstdc++-v3/include/std/ranges               | 56 +++++++++++++++++++
 libstdc++-v3/testsuite/std/ranges/repeat/1.cc | 33 +++++++++++
 2 files changed, 89 insertions(+)

diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges
index b29264931cc..6de08244bd1 100644
--- a/libstdc++-v3/include/std/ranges
+++ b/libstdc++-v3/include/std/ranges
@@ -2257,6 +2257,13 @@ namespace views::__adaptor
       template<typename _Winc, typename _Bound>
 	inline constexpr bool __is_iota_view<iota_view<_Winc, _Bound>> = true;
 
+      template<typename _Range>
+	inline constexpr bool __is_repeat_view = false;
+
+      template<typename _Range>
+	constexpr auto
+	__take_of_repeat_view(_Range&&, range_difference_t<_Range>); // defined later
+
       template<typename _Range, typename _Dp>
 	concept __can_take_view
 	  = requires { take_view(std::declval<_Range>(), std::declval<_Dp>()); };
@@ -2291,6 +2298,8 @@ namespace views::__adaptor
 	      else
 		return iota_view(*__begin, *__end);
 	    }
+	  else if constexpr (__detail::__is_repeat_view<_Tp>)
+	    return __detail::__take_of_repeat_view(std::forward<_Range>(__r), __n);
 	  else
 	    return take_view(std::forward<_Range>(__r), __n);
 	}
@@ -2527,6 +2536,10 @@ namespace views::__adaptor
   {
     namespace __detail
     {
+      template<typename _Range>
+	constexpr auto
+	__drop_of_repeat_view(_Range&&, range_difference_t<_Range>); // defined later
+
       template<typename _Range, typename _Dp>
 	concept __can_drop_view
 	  = requires { drop_view(std::declval<_Range>(), std::declval<_Dp>()); };
@@ -2568,6 +2581,8 @@ namespace views::__adaptor
 	      else
 		return _Tp(__begin, __end);
 	    }
+	  else if constexpr (__detail::__is_repeat_view<_Tp>)
+	    return __detail::__drop_of_repeat_view(std::forward<_Range>(__r), __n);
 	  else
 	    return drop_view(std::forward<_Range>(__r), __n);
 	}
@@ -7367,6 +7382,14 @@ namespace views::__adaptor
 
     struct _Iterator;
 
+    template<typename _Range>
+      friend constexpr auto
+      views::__detail::__take_of_repeat_view(_Range&&, range_difference_t<_Range>);
+
+    template<typename _Range>
+      friend constexpr auto
+      views::__detail::__drop_of_repeat_view(_Range&&, range_difference_t<_Range>);
+
   public:
     repeat_view() requires default_initializable<_Tp> = default;
 
@@ -7540,6 +7563,9 @@ namespace views::__adaptor
   {
     namespace __detail
     {
+      template<typename _Tp, typename _Bound>
+	inline constexpr bool __is_repeat_view<repeat_view<_Tp, _Bound>> = true;
+
       template<typename _Tp>
 	concept __can_repeat_view
 	  = requires { repeat_view(std::declval<_Tp>()); };
@@ -7565,6 +7591,36 @@ namespace views::__adaptor
     };
 
     inline constexpr _Repeat repeat;
+
+    namespace __detail
+    {
+      template<typename _Range>
+	constexpr auto
+	__take_of_repeat_view(_Range&& __r, range_difference_t<_Range> __n)
+	{
+	  using _Tp = remove_cvref_t<_Range>;
+	  static_assert(__is_repeat_view<_Tp>);
+	  if constexpr (sized_range<_Tp>)
+	    return views::repeat(*__r._M_value, std::min(ranges::distance(__r), __n));
+	  else
+	    return views::repeat(*__r._M_value, __n);
+	}
+
+      template<typename _Range>
+	constexpr auto
+	__drop_of_repeat_view(_Range&& __r, range_difference_t<_Range> __n)
+	{
+	  using _Tp = remove_cvref_t<_Range>;
+	  static_assert(__is_repeat_view<_Tp>);
+	  if constexpr (sized_range<_Tp>)
+	    {
+	      auto __sz = ranges::distance(__r);
+	      return views::repeat(*__r._M_value, __sz - std::min(__sz, __n));
+	    }
+	  else
+	    return __r;
+	}
+    }
   }
 
   template<input_range _Vp>
diff --git a/libstdc++-v3/testsuite/std/ranges/repeat/1.cc b/libstdc++-v3/testsuite/std/ranges/repeat/1.cc
index 3698ed12c14..542c8614f88 100644
--- a/libstdc++-v3/testsuite/std/ranges/repeat/1.cc
+++ b/libstdc++-v3/testsuite/std/ranges/repeat/1.cc
@@ -84,10 +84,43 @@ test03()
   return true;
 }
 
+constexpr bool
+test04()
+{
+  // Verify P2472R2 changes to views::take/drop.
+  auto r = views::repeat(42);
+
+  auto rt = r | views::take(10);
+  static_assert(views::__detail::__is_repeat_view<decltype(rt)>);
+  VERIFY( ranges::equal(rt, views::repeat(42, 10)) );
+
+  auto rd = r | views::drop(10);
+  static_assert(std::same_as<decltype(rd), decltype(r)>);
+
+  auto br = views::repeat(42, 37);
+
+  auto brt = br | views::take(10);
+  static_assert(std::same_as<decltype(brt), decltype(br)>);
+  VERIFY( ranges::equal(brt, views::repeat(42, 10)) );
+
+  auto brt100 = br | views::take(100);
+  VERIFY( ranges::equal(brt100, br) );
+
+  auto brd = br | views::drop(10);
+  static_assert(std::same_as<decltype(brd), decltype(br)>);
+  VERIFY( ranges::equal(brd, views::repeat(42, 27)) );
+
+  auto brd100 = br | views::drop(100);
+  VERIFY( ranges::empty(brd100) );
+
+  return true;
+}
+
 int
 main()
 {
   static_assert(test01());
   static_assert(test02());
   static_assert(test03());
+  static_assert(test04());
 }
-- 
2.38.1.119.g9c32cfb49c


  parent reply	other threads:[~2022-10-19 16:46 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-11  2:51 [PATCH] libstdc++: Implement ranges::repeat_view from P2474R2 Patrick Palka
2022-10-11 19:38 ` Jonathan Wakely
2022-10-19 16:46 ` Patrick Palka [this message]
2022-10-19 18:28   ` [PATCH] libstdc++: Implement P2474R2 changes to views::take/drop 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=20221019164645.301739-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).