public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r13-3389] libstdc++: Implement remaining P2474R2 changes to views::take/drop
@ 2022-10-19 19:43 Patrick Palka
  0 siblings, 0 replies; only message in thread
From: Patrick Palka @ 2022-10-19 19:43 UTC (permalink / raw)
  To: gcc-cvs, libstdc++-cvs

https://gcc.gnu.org/g:fb4db9bc1c567eaf850e997853d084d0832dd4c1

commit r13-3389-gfb4db9bc1c567eaf850e997853d084d0832dd4c1
Author: Patrick Palka <ppalka@redhat.com>
Date:   Wed Oct 19 15:43:13 2022 -0400

    libstdc++: Implement remaining P2474R2 changes to views::take/drop
    
    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.

Diff:
---
 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..a55e9e7f760 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..f9817c99600 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 P2474R2 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());
 }

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-10-19 19:43 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-19 19:43 [gcc r13-3389] libstdc++: Implement remaining P2474R2 changes to views::take/drop Patrick Palka

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).