public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH 1/2] libstdc++: Implement LWG 3523 changes to ranges::iota_view
@ 2021-10-19 12:17 Patrick Palka
  2021-10-19 12:17 ` [PATCH 2/2] libstdc++: Implement P1739R4 changes to views::take/drop/counted Patrick Palka
  2021-10-19 12:37 ` [PATCH 1/2] libstdc++: Implement LWG 3523 changes to ranges::iota_view Jonathan Wakely
  0 siblings, 2 replies; 4+ messages in thread
From: Patrick Palka @ 2021-10-19 12:17 UTC (permalink / raw)
  To: gcc-patches; +Cc: libstdc++, Patrick Palka

libstdc++-v3/ChangeLog:

	* include/std/ranges (iota_view::_Iterator): Befriend iota_view.
	(iota_view::_Sentinel): Likewise.
	(iota_view::iota_view): Add three overloads each taking an
	iterator/sentinel pair as per LWG 3523.
	* testsuite/std/ranges/iota/iota_view.cc (test06): New test.
---
 libstdc++-v3/include/std/ranges               | 21 +++++++++++++++++++
 .../testsuite/std/ranges/iota/iota_view.cc    | 21 +++++++++++++++++++
 2 files changed, 42 insertions(+)

diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges
index b8de400dfbb..85f232d8fb9 100644
--- a/libstdc++-v3/include/std/ranges
+++ b/libstdc++-v3/include/std/ranges
@@ -532,6 +532,7 @@ namespace ranges
       private:
 	_Winc _M_value = _Winc();
 
+	friend iota_view;
         friend _Sentinel;
       };
 
@@ -568,6 +569,8 @@ namespace ranges
 	operator-(const _Sentinel& __x, const _Iterator& __y)
 	  requires sized_sentinel_for<_Bound, _Winc>
 	{ return __x._M_distance_from(__y); }
+
+	friend iota_view;
       };
 
       _Winc _M_value = _Winc();
@@ -590,6 +593,24 @@ namespace ranges
 	  __glibcxx_assert( bool(__value <= __bound) );
       }
 
+      constexpr
+      iota_view(_Iterator __first, _Iterator __last)
+	requires same_as<_Winc, _Bound>
+	: iota_view(__first._M_value, __last._M_value)
+      { }
+
+      constexpr
+      iota_view(_Iterator __first, unreachable_sentinel_t __last)
+	requires same_as<_Bound, unreachable_sentinel_t>
+	: iota_view(__first._M_value, __last)
+      { }
+
+      constexpr
+      iota_view(_Iterator __first, _Sentinel __last)
+	requires (!same_as<_Winc, _Bound>) && (!same_as<_Bound, unreachable_sentinel_t>)
+	: iota_view(__first._M_value, __last._M_bound)
+      { }
+
       constexpr _Iterator
       begin() const { return _Iterator{_M_value}; }
 
diff --git a/libstdc++-v3/testsuite/std/ranges/iota/iota_view.cc b/libstdc++-v3/testsuite/std/ranges/iota/iota_view.cc
index 362ef1f7f78..5bebe4be693 100644
--- a/libstdc++-v3/testsuite/std/ranges/iota/iota_view.cc
+++ b/libstdc++-v3/testsuite/std/ranges/iota/iota_view.cc
@@ -18,6 +18,7 @@
 // { dg-options "-std=gnu++2a" }
 // { dg-do run { target c++2a } }
 
+#include <algorithm>
 #include <ranges>
 #include <testsuite_hooks.h>
 
@@ -90,6 +91,25 @@ test05()
   VERIFY( r.begin() - r.end() == -3 );
 }
 
+void
+test06()
+{
+  // Verify LWG 3523 changes.
+  auto v1 = std::views::iota(0, 5);
+  auto w1 = decltype(v1)(v1.begin(), v1.end());
+  VERIFY( std::ranges::equal(v1, w1) );
+
+  auto v2 = std::views::iota(0);
+  auto w2 = decltype(v2)(v2.begin(), v2.end());
+  static_assert(std::same_as<decltype(w2.end()), std::unreachable_sentinel_t>);
+  VERIFY( *w2.begin() == 0 );
+
+  auto v3 = std::views::iota(0, 5l);
+  auto w3 = decltype(v3)(v3.begin(), v3.end());
+  static_assert(!std::ranges::common_range<decltype(w3)>);
+  VERIFY( std::ranges::equal(v3, w3) );
+}
+
 int
 main()
 {
@@ -98,4 +118,5 @@ main()
   test03();
   test04();
   test05();
+  test06();
 }
-- 
2.33.1.637.gf443b226ca


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 2/2] libstdc++: Implement P1739R4 changes to views::take/drop/counted
  2021-10-19 12:17 [PATCH 1/2] libstdc++: Implement LWG 3523 changes to ranges::iota_view Patrick Palka
@ 2021-10-19 12:17 ` Patrick Palka
  2021-10-21 15:22   ` Jonathan Wakely
  2021-10-19 12:37 ` [PATCH 1/2] libstdc++: Implement LWG 3523 changes to ranges::iota_view Jonathan Wakely
  1 sibling, 1 reply; 4+ messages in thread
From: Patrick Palka @ 2021-10-19 12:17 UTC (permalink / raw)
  To: gcc-patches; +Cc: libstdc++, Patrick Palka

This implements P1739R4 along with the resolution for LWG 3407 which
corrects the paper's wording.

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

libstdc++-v3/ChangeLog:

	* include/bits/ranges_util.h (views::_Drop): Forward declare.
	(subrange): Befriend views::_Drop.
	(subrange::_S_store_size): Declare constexpr instead of just
	const, remove obsolete comment.
	* include/std/ranges (views::__detail::__is_empty_view): Define.
	(views::__detail::__is_basic_string_view): Likewise.
	(views::__detail::__is_subrange): Likewise.
	(views::__detail::__is_iota_view): Likewise.
	(views::__detail::__can_take_view): Rename template parm _Tp to _Dp.
	(views::_Take): Rename template parm _Tp to _Dp, make it non-deducible
	and fix its type to range_difference_t<_Range>.  Implement P1739R4 and
	LWG 3407 changes.
	(views::__detail::__can_drop_view): Rename template parm _Tp to _Dp.
	(views::_Drop): As with views::_Take.
	(views::_Counted): Implement P1739R4 changes.
	* include/std/span (__detail::__is_std_span): Rename to ...
	(__detail::__is_span): ... this and turn it into a variable
	template.
	(__detail::__is_std_array): Turn it into a variable template.
	(span::span): Adjust uses of __is_std_span and __is_std_array
	accordingly.
	* testsuite/std/ranges/adaptors/p1739.cc: New test.
---
 libstdc++-v3/include/bits/ranges_util.h       |   7 +-
 libstdc++-v3/include/std/ranges               | 106 +++++++++++++++---
 libstdc++-v3/include/std/span                 |  12 +-
 .../testsuite/std/ranges/adaptors/p1739.cc    |  88 +++++++++++++++
 4 files changed, 192 insertions(+), 21 deletions(-)
 create mode 100644 libstdc++-v3/testsuite/std/ranges/adaptors/p1739.cc

diff --git a/libstdc++-v3/include/bits/ranges_util.h b/libstdc++-v3/include/bits/ranges_util.h
index 7e7b958d274..1afa66d298c 100644
--- a/libstdc++-v3/include/bits/ranges_util.h
+++ b/libstdc++-v3/include/bits/ranges_util.h
@@ -211,6 +211,8 @@ namespace ranges
 
   } // namespace __detail
 
+  namespace views { struct _Drop; }
+
   enum class subrange_kind : bool { unsized, sized };
 
   /// The ranges::subrange class template
@@ -221,8 +223,9 @@ namespace ranges
     class subrange : public view_interface<subrange<_It, _Sent, _Kind>>
     {
     private:
-      // XXX: gcc complains when using constexpr here
-      static const bool _S_store_size
+      friend struct views::_Drop; // Needs to inspect _S_store_size.
+
+      static constexpr bool _S_store_size
 	= _Kind == subrange_kind::sized && !sized_sentinel_for<_Sent, _It>;
 
       _It _M_begin = _It();
diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges
index 85f232d8fb9..64396027c1b 100644
--- a/libstdc++-v3/include/std/ranges
+++ b/libstdc++-v3/include/std/ranges
@@ -42,6 +42,7 @@
 #include <initializer_list>
 #include <iterator>
 #include <optional>
+#include <span>
 #include <tuple>
 #include <bits/ranges_util.h>
 #include <bits/refwrap.h>
@@ -2147,19 +2148,66 @@ namespace views::__adaptor
   {
     namespace __detail
     {
-      template<typename _Range, typename _Tp>
+      template<typename _Range>
+	constexpr bool __is_empty_view = false;
+
+      template<typename _Tp>
+	constexpr bool __is_empty_view<empty_view<_Tp>> = true;
+
+      template<typename _Range>
+	constexpr bool __is_basic_string_view = false;
+
+      template<typename _CharT, typename _Traits>
+	constexpr bool __is_basic_string_view<basic_string_view<_CharT, _Traits>> = true;
+
+      template<typename _Range>
+	constexpr bool __is_subrange = false;
+
+      template<typename _Iter, typename _Sent, subrange_kind _Kind>
+	constexpr bool __is_subrange<subrange<_Iter, _Sent, _Kind>> = true;
+
+      template<typename _Range>
+	constexpr bool __is_iota_view = false;
+
+      template<typename _Winc, typename _Bound>
+	constexpr bool __is_iota_view<iota_view<_Winc, _Bound>> = true;
+
+      template<typename _Range, typename _Dp>
 	concept __can_take_view
-	  = requires { take_view(std::declval<_Range>(), std::declval<_Tp>()); };
+	  = requires { take_view(std::declval<_Range>(), std::declval<_Dp>()); };
     } // namespace __detail
 
     struct _Take : __adaptor::_RangeAdaptor<_Take>
     {
-      template<viewable_range _Range, typename _Tp>
-	requires __detail::__can_take_view<_Range, _Tp>
+      template<viewable_range _Range, typename _Dp = range_difference_t<_Range>>
+	requires __detail::__can_take_view<_Range, _Dp>
 	constexpr auto
-	operator() [[nodiscard]] (_Range&& __r, _Tp&& __n) const
+	operator() [[nodiscard]] (_Range&& __r, type_identity_t<_Dp> __n) const
 	{
-	  return take_view(std::forward<_Range>(__r), std::forward<_Tp>(__n));
+	  using _Tp = remove_cvref_t<_Range>;
+	  if constexpr (__detail::__is_empty_view<_Tp>)
+	    return _Tp();
+	  else if constexpr (random_access_range<_Tp>
+			     && sized_range<_Tp>
+			     && (std::__detail::__is_span<_Tp>
+				 || __detail::__is_basic_string_view<_Tp>
+				 || __detail::__is_subrange<_Tp>
+				 || __detail::__is_iota_view<_Tp>))
+	    {
+	      __n = std::min<_Dp>(ranges::distance(__r), __n);
+	      auto __begin = ranges::begin(__r);
+	      auto __end = __begin + __n;
+	      if constexpr (std::__detail::__is_span<_Tp>)
+		return span<typename _Tp::element_type>(__begin, __end);
+	      else if constexpr (__detail::__is_basic_string_view<_Tp>)
+		return _Tp(__begin, __end);
+	      else if constexpr (__detail::__is_subrange<_Tp>)
+		return subrange<iterator_t<_Tp>>(__begin, __end);
+	      else
+		return iota_view(*__begin, *__end);
+	    }
+	  else
+	    return take_view(std::forward<_Range>(__r), __n);
 	}
 
       using _RangeAdaptor<_Take>::operator();
@@ -2394,19 +2442,49 @@ namespace views::__adaptor
   {
     namespace __detail
     {
-      template<typename _Range, typename _Tp>
+      template<typename _Range, typename _Dp>
 	concept __can_drop_view
-	  = requires { drop_view(std::declval<_Range>(), std::declval<_Tp>()); };
+	  = requires { drop_view(std::declval<_Range>(), std::declval<_Dp>()); };
     } // namespace __detail
 
     struct _Drop : __adaptor::_RangeAdaptor<_Drop>
     {
-      template<viewable_range _Range, typename _Tp>
-	requires __detail::__can_drop_view<_Range, _Tp>
+      template<viewable_range _Range, typename _Dp = range_difference_t<_Range>>
+	requires __detail::__can_drop_view<_Range, _Dp>
 	constexpr auto
-	operator() [[nodiscard]] (_Range&& __r, _Tp&& __n) const
+	operator() [[nodiscard]] (_Range&& __r, type_identity_t<_Dp> __n) const
 	{
-	  return drop_view(std::forward<_Range>(__r), std::forward<_Tp>(__n));
+	  using _Tp = remove_cvref_t<_Range>;
+	  if constexpr (__detail::__is_empty_view<_Tp>)
+	    return _Tp();
+	  else if constexpr (random_access_range<_Tp>
+			     && sized_range<_Tp>
+			     && (std::__detail::__is_span<_Tp>
+				 || __detail::__is_basic_string_view<_Tp>
+				 || __detail::__is_iota_view<_Tp>
+				 || __detail::__is_subrange<_Tp>))
+	    {
+	      __n = std::min<_Dp>(ranges::distance(__r), __n);
+	      auto __begin = ranges::begin(__r) + __n;
+	      auto __end = ranges::end(__r);
+	      if constexpr (std::__detail::__is_span<_Tp>)
+		return span<typename _Tp::element_type>(__begin, __end);
+	      else if constexpr (__detail::__is_subrange<_Tp>)
+		{
+		  if constexpr (_Tp::_S_store_size)
+		    {
+		      using ranges::__detail::__to_unsigned_like;
+		      auto __m = ranges::distance(__r) - __n;
+		      return _Tp(__begin, __end, __to_unsigned_like(__m));
+		    }
+		  else
+		    return _Tp(__begin, __end);
+		}
+	      else
+		return _Tp(__begin, __end);
+	    }
+	  else
+	    return drop_view(std::forward<_Range>(__r), __n);
 	}
 
       using _RangeAdaptor<_Drop>::operator();
@@ -3536,7 +3614,9 @@ namespace views::__adaptor
       constexpr auto
       operator() [[nodiscard]] (_Iter __i, iter_difference_t<_Iter> __n) const
       {
-	if constexpr (random_access_iterator<_Iter>)
+	if constexpr (contiguous_iterator<_Iter>)
+	  return span(std::__to_address(__i), __n);
+	else if constexpr (random_access_iterator<_Iter>)
 	  return subrange(__i, __i + __n);
 	else
 	  return subrange(counted_iterator(std::move(__i), __n),
diff --git a/libstdc++-v3/include/std/span b/libstdc++-v3/include/std/span
index af0d24b29f2..ba75a497310 100644
--- a/libstdc++-v3/include/std/span
+++ b/libstdc++-v3/include/std/span
@@ -58,16 +58,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   namespace __detail
   {
     template<typename _Tp>
-      struct __is_std_span : false_type { };
+      constexpr bool __is_span = false;
 
     template<typename _Tp, size_t _Num>
-      struct __is_std_span<span<_Tp, _Num>> : true_type { };
+      constexpr bool __is_span<span<_Tp, _Num>> = true;
 
     template<typename _Tp>
-      struct __is_std_array : false_type { };
+      constexpr bool __is_std_array = false;
 
     template<typename _Tp, size_t _Num>
-      struct __is_std_array<std::array<_Tp, _Num>> : true_type { };
+      constexpr bool __is_std_array<std::array<_Tp, _Num>> = true;
 
     template<size_t _Extent>
       class __extent_storage
@@ -203,8 +203,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       template<typename _Range>
 	requires ranges::contiguous_range<_Range> && ranges::sized_range<_Range>
 	  && (ranges::borrowed_range<_Range> || is_const_v<element_type>)
-	  && (!__detail::__is_std_span<remove_cvref_t<_Range>>::value)
-	  && (!__detail::__is_std_array<remove_cvref_t<_Range>>::value)
+	  && (!__detail::__is_span<remove_cvref_t<_Range>>)
+	  && (!__detail::__is_std_array<remove_cvref_t<_Range>>)
 	  && (!is_array_v<remove_cvref_t<_Range>>)
 	  && __is_compatible_ref<ranges::range_reference_t<_Range>>::value
 	constexpr explicit(extent != dynamic_extent)
diff --git a/libstdc++-v3/testsuite/std/ranges/adaptors/p1739.cc b/libstdc++-v3/testsuite/std/ranges/adaptors/p1739.cc
new file mode 100644
index 00000000000..243b8a03583
--- /dev/null
+++ b/libstdc++-v3/testsuite/std/ranges/adaptors/p1739.cc
@@ -0,0 +1,88 @@
+// { dg-options "-std=gnu++20" }
+// { dg-do run { target c++20 } }
+
+#include <algorithm>
+#include <ranges>
+#include <testsuite_hooks.h>
+
+// Verify P1739R4 (and LWG 3407) changes views::take/drop/counted.
+
+namespace ranges = std::ranges;
+namespace views = ranges::views;
+
+void
+test01()
+{
+  ranges::empty_view<int> v;
+  using ty = decltype(v);
+  using ty = decltype(views::take(v, 42));
+  using ty = decltype(views::drop(v, 42));
+}
+
+void
+test02()
+{
+  int x[] = {1,2,3,4,5};
+  std::span r(x, x+5);
+  using ty = decltype(r);
+
+  std::same_as<ty> auto v1 = views::take(r, 2);
+  VERIFY( ranges::equal(v1, (int[]){1,2}) );
+
+  std::same_as<ty> auto v2 = views::drop(r, 3);
+  VERIFY( ranges::equal(v2, (int[]){4,5}) );
+
+  std::same_as<ty> auto v3 = views::counted(r.begin(), 4);
+  VERIFY( ranges::equal(v3, (int[]){1,2,3,4}) );
+}
+
+void
+test03()
+{
+  using namespace std::literals;
+  std::string_view r = "hello world"sv;
+  using ty = decltype(r);
+
+  std::same_as<ty> auto v1 = views::take(r, 5);
+  VERIFY( ranges::equal(v1, "hello"sv) );
+
+  std::same_as<ty> auto v2 = views::drop(r, 6);
+  VERIFY( ranges::equal(v2, "world"sv) );
+}
+
+void
+test04()
+{
+  int x[] = {1,2,3,4,5};
+  ranges::subrange r(x, x+5);
+  using ty = decltype(r);
+
+  std::same_as<ty> auto v1 = views::take(r, 2);
+  VERIFY( ranges::equal(v1, (int[]){1,2}) );
+
+  std::same_as<ty> auto v2 = views::drop(r, 1);
+  VERIFY( ranges::equal(v2, (int[]){2,3,4,5}) );
+}
+
+void
+test05()
+{
+  auto r1 = views::iota(1, 6);
+  using ty1 = decltype(r1);
+
+  std::same_as<ty1> auto v1 = views::take(r1, 2);
+  VERIFY( ranges::equal(v1, (int[]){1,2}) );
+
+  std::same_as<ty1> auto v2 = views::drop(r1, 3);
+  VERIFY( ranges::equal(v2, (int[]){4,5}) );
+}
+
+int
+main()
+{
+  test01();
+  test02();
+  test03();
+  test04();
+  test05();
+}
-- 
2.33.1.637.gf443b226ca


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/2] libstdc++: Implement LWG 3523 changes to ranges::iota_view
  2021-10-19 12:17 [PATCH 1/2] libstdc++: Implement LWG 3523 changes to ranges::iota_view Patrick Palka
  2021-10-19 12:17 ` [PATCH 2/2] libstdc++: Implement P1739R4 changes to views::take/drop/counted Patrick Palka
@ 2021-10-19 12:37 ` Jonathan Wakely
  1 sibling, 0 replies; 4+ messages in thread
From: Jonathan Wakely @ 2021-10-19 12:37 UTC (permalink / raw)
  To: Patrick Palka; +Cc: gcc Patches, libstdc++

On Tue, 19 Oct 2021 at 13:18, Patrick Palka via Libstdc++
<libstdc++@gcc.gnu.org> wrote:
>
> libstdc++-v3/ChangeLog:
>
>         * include/std/ranges (iota_view::_Iterator): Befriend iota_view.
>         (iota_view::_Sentinel): Likewise.
>         (iota_view::iota_view): Add three overloads each taking an
>         iterator/sentinel pair as per LWG 3523.
>         * testsuite/std/ranges/iota/iota_view.cc (test06): New test.

OK for trunk and gcc-11 (after some soak time), thanks.


> ---
>  libstdc++-v3/include/std/ranges               | 21 +++++++++++++++++++
>  .../testsuite/std/ranges/iota/iota_view.cc    | 21 +++++++++++++++++++
>  2 files changed, 42 insertions(+)
>
> diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges
> index b8de400dfbb..85f232d8fb9 100644
> --- a/libstdc++-v3/include/std/ranges
> +++ b/libstdc++-v3/include/std/ranges
> @@ -532,6 +532,7 @@ namespace ranges
>        private:
>         _Winc _M_value = _Winc();
>
> +       friend iota_view;
>          friend _Sentinel;
>        };
>
> @@ -568,6 +569,8 @@ namespace ranges
>         operator-(const _Sentinel& __x, const _Iterator& __y)
>           requires sized_sentinel_for<_Bound, _Winc>
>         { return __x._M_distance_from(__y); }
> +
> +       friend iota_view;
>        };
>
>        _Winc _M_value = _Winc();
> @@ -590,6 +593,24 @@ namespace ranges
>           __glibcxx_assert( bool(__value <= __bound) );
>        }
>
> +      constexpr
> +      iota_view(_Iterator __first, _Iterator __last)
> +       requires same_as<_Winc, _Bound>
> +       : iota_view(__first._M_value, __last._M_value)
> +      { }
> +
> +      constexpr
> +      iota_view(_Iterator __first, unreachable_sentinel_t __last)
> +       requires same_as<_Bound, unreachable_sentinel_t>
> +       : iota_view(__first._M_value, __last)
> +      { }
> +
> +      constexpr
> +      iota_view(_Iterator __first, _Sentinel __last)
> +       requires (!same_as<_Winc, _Bound>) && (!same_as<_Bound, unreachable_sentinel_t>)
> +       : iota_view(__first._M_value, __last._M_bound)
> +      { }
> +
>        constexpr _Iterator
>        begin() const { return _Iterator{_M_value}; }
>
> diff --git a/libstdc++-v3/testsuite/std/ranges/iota/iota_view.cc b/libstdc++-v3/testsuite/std/ranges/iota/iota_view.cc
> index 362ef1f7f78..5bebe4be693 100644
> --- a/libstdc++-v3/testsuite/std/ranges/iota/iota_view.cc
> +++ b/libstdc++-v3/testsuite/std/ranges/iota/iota_view.cc
> @@ -18,6 +18,7 @@
>  // { dg-options "-std=gnu++2a" }
>  // { dg-do run { target c++2a } }
>
> +#include <algorithm>
>  #include <ranges>
>  #include <testsuite_hooks.h>
>
> @@ -90,6 +91,25 @@ test05()
>    VERIFY( r.begin() - r.end() == -3 );
>  }
>
> +void
> +test06()
> +{
> +  // Verify LWG 3523 changes.
> +  auto v1 = std::views::iota(0, 5);
> +  auto w1 = decltype(v1)(v1.begin(), v1.end());
> +  VERIFY( std::ranges::equal(v1, w1) );
> +
> +  auto v2 = std::views::iota(0);
> +  auto w2 = decltype(v2)(v2.begin(), v2.end());
> +  static_assert(std::same_as<decltype(w2.end()), std::unreachable_sentinel_t>);
> +  VERIFY( *w2.begin() == 0 );
> +
> +  auto v3 = std::views::iota(0, 5l);
> +  auto w3 = decltype(v3)(v3.begin(), v3.end());
> +  static_assert(!std::ranges::common_range<decltype(w3)>);
> +  VERIFY( std::ranges::equal(v3, w3) );
> +}
> +
>  int
>  main()
>  {
> @@ -98,4 +118,5 @@ main()
>    test03();
>    test04();
>    test05();
> +  test06();
>  }
> --
> 2.33.1.637.gf443b226ca
>


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 2/2] libstdc++: Implement P1739R4 changes to views::take/drop/counted
  2021-10-19 12:17 ` [PATCH 2/2] libstdc++: Implement P1739R4 changes to views::take/drop/counted Patrick Palka
@ 2021-10-21 15:22   ` Jonathan Wakely
  0 siblings, 0 replies; 4+ messages in thread
From: Jonathan Wakely @ 2021-10-21 15:22 UTC (permalink / raw)
  To: Patrick Palka; +Cc: gcc Patches, libstdc++

On Tue, 19 Oct 2021 at 13:20, Patrick Palka via Libstdc++ <
libstdc++@gcc.gnu.org> wrote:

> This implements P1739R4 along with the resolution for LWG 3407 which
> corrects the paper's wording.
>
> Tested on x86_64-pc-linux-gnu, does this look OK for trunk?
>

Yes, thanks.



> libstdc++-v3/ChangeLog:
>
>         * include/bits/ranges_util.h (views::_Drop): Forward declare.
>         (subrange): Befriend views::_Drop.
>         (subrange::_S_store_size): Declare constexpr instead of just
>         const, remove obsolete comment.
>         * include/std/ranges (views::__detail::__is_empty_view): Define.
>         (views::__detail::__is_basic_string_view): Likewise.
>         (views::__detail::__is_subrange): Likewise.
>         (views::__detail::__is_iota_view): Likewise.
>         (views::__detail::__can_take_view): Rename template parm _Tp to
> _Dp.
>         (views::_Take): Rename template parm _Tp to _Dp, make it
> non-deducible
>         and fix its type to range_difference_t<_Range>.  Implement P1739R4
> and
>         LWG 3407 changes.
>         (views::__detail::__can_drop_view): Rename template parm _Tp to
> _Dp.
>         (views::_Drop): As with views::_Take.
>         (views::_Counted): Implement P1739R4 changes.
>         * include/std/span (__detail::__is_std_span): Rename to ...
>         (__detail::__is_span): ... this and turn it into a variable
>         template.
>         (__detail::__is_std_array): Turn it into a variable template.
>         (span::span): Adjust uses of __is_std_span and __is_std_array
>         accordingly.
>         * testsuite/std/ranges/adaptors/p1739.cc: New test.
> ---
>  libstdc++-v3/include/bits/ranges_util.h       |   7 +-
>  libstdc++-v3/include/std/ranges               | 106 +++++++++++++++---
>  libstdc++-v3/include/std/span                 |  12 +-
>  .../testsuite/std/ranges/adaptors/p1739.cc    |  88 +++++++++++++++
>  4 files changed, 192 insertions(+), 21 deletions(-)
>  create mode 100644 libstdc++-v3/testsuite/std/ranges/adaptors/p1739.cc
>
> diff --git a/libstdc++-v3/include/bits/ranges_util.h
> b/libstdc++-v3/include/bits/ranges_util.h
> index 7e7b958d274..1afa66d298c 100644
> --- a/libstdc++-v3/include/bits/ranges_util.h
> +++ b/libstdc++-v3/include/bits/ranges_util.h
> @@ -211,6 +211,8 @@ namespace ranges
>
>    } // namespace __detail
>
> +  namespace views { struct _Drop; }
> +
>    enum class subrange_kind : bool { unsized, sized };
>
>    /// The ranges::subrange class template
> @@ -221,8 +223,9 @@ namespace ranges
>      class subrange : public view_interface<subrange<_It, _Sent, _Kind>>
>      {
>      private:
> -      // XXX: gcc complains when using constexpr here
> -      static const bool _S_store_size
> +      friend struct views::_Drop; // Needs to inspect _S_store_size.
> +
> +      static constexpr bool _S_store_size
>         = _Kind == subrange_kind::sized && !sized_sentinel_for<_Sent, _It>;
>
>        _It _M_begin = _It();
> diff --git a/libstdc++-v3/include/std/ranges
> b/libstdc++-v3/include/std/ranges
> index 85f232d8fb9..64396027c1b 100644
> --- a/libstdc++-v3/include/std/ranges
> +++ b/libstdc++-v3/include/std/ranges
> @@ -42,6 +42,7 @@
>  #include <initializer_list>
>  #include <iterator>
>  #include <optional>
> +#include <span>
>  #include <tuple>
>  #include <bits/ranges_util.h>
>  #include <bits/refwrap.h>
> @@ -2147,19 +2148,66 @@ namespace views::__adaptor
>    {
>      namespace __detail
>      {
> -      template<typename _Range, typename _Tp>
> +      template<typename _Range>
> +       constexpr bool __is_empty_view = false;
> +
> +      template<typename _Tp>
> +       constexpr bool __is_empty_view<empty_view<_Tp>> = true;
> +
> +      template<typename _Range>
> +       constexpr bool __is_basic_string_view = false;
> +
> +      template<typename _CharT, typename _Traits>
> +       constexpr bool __is_basic_string_view<basic_string_view<_CharT,
> _Traits>> = true;
> +
> +      template<typename _Range>
> +       constexpr bool __is_subrange = false;
> +
> +      template<typename _Iter, typename _Sent, subrange_kind _Kind>
> +       constexpr bool __is_subrange<subrange<_Iter, _Sent, _Kind>> = true;
> +
> +      template<typename _Range>
> +       constexpr bool __is_iota_view = false;
> +
> +      template<typename _Winc, typename _Bound>
> +       constexpr bool __is_iota_view<iota_view<_Winc, _Bound>> = true;
> +
> +      template<typename _Range, typename _Dp>
>         concept __can_take_view
> -         = requires { take_view(std::declval<_Range>(),
> std::declval<_Tp>()); };
> +         = requires { take_view(std::declval<_Range>(),
> std::declval<_Dp>()); };
>      } // namespace __detail
>
>      struct _Take : __adaptor::_RangeAdaptor<_Take>
>      {
> -      template<viewable_range _Range, typename _Tp>
> -       requires __detail::__can_take_view<_Range, _Tp>
> +      template<viewable_range _Range, typename _Dp =
> range_difference_t<_Range>>
> +       requires __detail::__can_take_view<_Range, _Dp>
>         constexpr auto
> -       operator() [[nodiscard]] (_Range&& __r, _Tp&& __n) const
> +       operator() [[nodiscard]] (_Range&& __r, type_identity_t<_Dp> __n)
> const
>         {
> -         return take_view(std::forward<_Range>(__r),
> std::forward<_Tp>(__n));
> +         using _Tp = remove_cvref_t<_Range>;
> +         if constexpr (__detail::__is_empty_view<_Tp>)
> +           return _Tp();
> +         else if constexpr (random_access_range<_Tp>
> +                            && sized_range<_Tp>
> +                            && (std::__detail::__is_span<_Tp>
> +                                || __detail::__is_basic_string_view<_Tp>
> +                                || __detail::__is_subrange<_Tp>
> +                                || __detail::__is_iota_view<_Tp>))
> +           {
> +             __n = std::min<_Dp>(ranges::distance(__r), __n);
> +             auto __begin = ranges::begin(__r);
> +             auto __end = __begin + __n;
> +             if constexpr (std::__detail::__is_span<_Tp>)
> +               return span<typename _Tp::element_type>(__begin, __end);
> +             else if constexpr (__detail::__is_basic_string_view<_Tp>)
> +               return _Tp(__begin, __end);
> +             else if constexpr (__detail::__is_subrange<_Tp>)
> +               return subrange<iterator_t<_Tp>>(__begin, __end);
> +             else
> +               return iota_view(*__begin, *__end);
> +           }
> +         else
> +           return take_view(std::forward<_Range>(__r), __n);
>         }
>
>        using _RangeAdaptor<_Take>::operator();
> @@ -2394,19 +2442,49 @@ namespace views::__adaptor
>    {
>      namespace __detail
>      {
> -      template<typename _Range, typename _Tp>
> +      template<typename _Range, typename _Dp>
>         concept __can_drop_view
> -         = requires { drop_view(std::declval<_Range>(),
> std::declval<_Tp>()); };
> +         = requires { drop_view(std::declval<_Range>(),
> std::declval<_Dp>()); };
>      } // namespace __detail
>
>      struct _Drop : __adaptor::_RangeAdaptor<_Drop>
>      {
> -      template<viewable_range _Range, typename _Tp>
> -       requires __detail::__can_drop_view<_Range, _Tp>
> +      template<viewable_range _Range, typename _Dp =
> range_difference_t<_Range>>
> +       requires __detail::__can_drop_view<_Range, _Dp>
>         constexpr auto
> -       operator() [[nodiscard]] (_Range&& __r, _Tp&& __n) const
> +       operator() [[nodiscard]] (_Range&& __r, type_identity_t<_Dp> __n)
> const
>         {
> -         return drop_view(std::forward<_Range>(__r),
> std::forward<_Tp>(__n));
> +         using _Tp = remove_cvref_t<_Range>;
> +         if constexpr (__detail::__is_empty_view<_Tp>)
> +           return _Tp();
> +         else if constexpr (random_access_range<_Tp>
> +                            && sized_range<_Tp>
> +                            && (std::__detail::__is_span<_Tp>
> +                                || __detail::__is_basic_string_view<_Tp>
> +                                || __detail::__is_iota_view<_Tp>
> +                                || __detail::__is_subrange<_Tp>))
> +           {
> +             __n = std::min<_Dp>(ranges::distance(__r), __n);
> +             auto __begin = ranges::begin(__r) + __n;
> +             auto __end = ranges::end(__r);
> +             if constexpr (std::__detail::__is_span<_Tp>)
> +               return span<typename _Tp::element_type>(__begin, __end);
> +             else if constexpr (__detail::__is_subrange<_Tp>)
> +               {
> +                 if constexpr (_Tp::_S_store_size)
> +                   {
> +                     using ranges::__detail::__to_unsigned_like;
> +                     auto __m = ranges::distance(__r) - __n;
> +                     return _Tp(__begin, __end, __to_unsigned_like(__m));
> +                   }
> +                 else
> +                   return _Tp(__begin, __end);
> +               }
> +             else
> +               return _Tp(__begin, __end);
> +           }
> +         else
> +           return drop_view(std::forward<_Range>(__r), __n);
>         }
>
>        using _RangeAdaptor<_Drop>::operator();
> @@ -3536,7 +3614,9 @@ namespace views::__adaptor
>        constexpr auto
>        operator() [[nodiscard]] (_Iter __i, iter_difference_t<_Iter> __n)
> const
>        {
> -       if constexpr (random_access_iterator<_Iter>)
> +       if constexpr (contiguous_iterator<_Iter>)
> +         return span(std::__to_address(__i), __n);
> +       else if constexpr (random_access_iterator<_Iter>)
>           return subrange(__i, __i + __n);
>         else
>           return subrange(counted_iterator(std::move(__i), __n),
> diff --git a/libstdc++-v3/include/std/span b/libstdc++-v3/include/std/span
> index af0d24b29f2..ba75a497310 100644
> --- a/libstdc++-v3/include/std/span
> +++ b/libstdc++-v3/include/std/span
> @@ -58,16 +58,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>    namespace __detail
>    {
>      template<typename _Tp>
> -      struct __is_std_span : false_type { };
> +      constexpr bool __is_span = false;
>
>      template<typename _Tp, size_t _Num>
> -      struct __is_std_span<span<_Tp, _Num>> : true_type { };
> +      constexpr bool __is_span<span<_Tp, _Num>> = true;
>
>      template<typename _Tp>
> -      struct __is_std_array : false_type { };
> +      constexpr bool __is_std_array = false;
>
>      template<typename _Tp, size_t _Num>
> -      struct __is_std_array<std::array<_Tp, _Num>> : true_type { };
> +      constexpr bool __is_std_array<std::array<_Tp, _Num>> = true;
>
>      template<size_t _Extent>
>        class __extent_storage
> @@ -203,8 +203,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>        template<typename _Range>
>         requires ranges::contiguous_range<_Range> &&
> ranges::sized_range<_Range>
>           && (ranges::borrowed_range<_Range> || is_const_v<element_type>)
> -         && (!__detail::__is_std_span<remove_cvref_t<_Range>>::value)
> -         && (!__detail::__is_std_array<remove_cvref_t<_Range>>::value)
> +         && (!__detail::__is_span<remove_cvref_t<_Range>>)
> +         && (!__detail::__is_std_array<remove_cvref_t<_Range>>)
>           && (!is_array_v<remove_cvref_t<_Range>>)
>           && __is_compatible_ref<ranges::range_reference_t<_Range>>::value
>         constexpr explicit(extent != dynamic_extent)
> diff --git a/libstdc++-v3/testsuite/std/ranges/adaptors/p1739.cc
> b/libstdc++-v3/testsuite/std/ranges/adaptors/p1739.cc
> new file mode 100644
> index 00000000000..243b8a03583
> --- /dev/null
> +++ b/libstdc++-v3/testsuite/std/ranges/adaptors/p1739.cc
> @@ -0,0 +1,88 @@
> +// { dg-options "-std=gnu++20" }
> +// { dg-do run { target c++20 } }
> +
> +#include <algorithm>
> +#include <ranges>
> +#include <testsuite_hooks.h>
> +
> +// Verify P1739R4 (and LWG 3407) changes views::take/drop/counted.
> +
> +namespace ranges = std::ranges;
> +namespace views = ranges::views;
> +
> +void
> +test01()
> +{
> +  ranges::empty_view<int> v;
> +  using ty = decltype(v);
> +  using ty = decltype(views::take(v, 42));
> +  using ty = decltype(views::drop(v, 42));
> +}
> +
> +void
> +test02()
> +{
> +  int x[] = {1,2,3,4,5};
> +  std::span r(x, x+5);
> +  using ty = decltype(r);
> +
> +  std::same_as<ty> auto v1 = views::take(r, 2);
> +  VERIFY( ranges::equal(v1, (int[]){1,2}) );
> +
> +  std::same_as<ty> auto v2 = views::drop(r, 3);
> +  VERIFY( ranges::equal(v2, (int[]){4,5}) );
> +
> +  std::same_as<ty> auto v3 = views::counted(r.begin(), 4);
> +  VERIFY( ranges::equal(v3, (int[]){1,2,3,4}) );
> +}
> +
> +void
> +test03()
> +{
> +  using namespace std::literals;
> +  std::string_view r = "hello world"sv;
> +  using ty = decltype(r);
> +
> +  std::same_as<ty> auto v1 = views::take(r, 5);
> +  VERIFY( ranges::equal(v1, "hello"sv) );
> +
> +  std::same_as<ty> auto v2 = views::drop(r, 6);
> +  VERIFY( ranges::equal(v2, "world"sv) );
> +}
> +
> +void
> +test04()
> +{
> +  int x[] = {1,2,3,4,5};
> +  ranges::subrange r(x, x+5);
> +  using ty = decltype(r);
> +
> +  std::same_as<ty> auto v1 = views::take(r, 2);
> +  VERIFY( ranges::equal(v1, (int[]){1,2}) );
> +
> +  std::same_as<ty> auto v2 = views::drop(r, 1);
> +  VERIFY( ranges::equal(v2, (int[]){2,3,4,5}) );
> +}
> +
> +void
> +test05()
> +{
> +  auto r1 = views::iota(1, 6);
> +  using ty1 = decltype(r1);
> +
> +  std::same_as<ty1> auto v1 = views::take(r1, 2);
> +  VERIFY( ranges::equal(v1, (int[]){1,2}) );
> +
> +  std::same_as<ty1> auto v2 = views::drop(r1, 3);
> +  VERIFY( ranges::equal(v2, (int[]){4,5}) );
> +}
> +
> +int
> +main()
> +{
> +  test01();
> +  test02();
> +  test03();
> +  test04();
> +  test05();
> +}
> --
> 2.33.1.637.gf443b226ca
>
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-10-21 15:23 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-19 12:17 [PATCH 1/2] libstdc++: Implement LWG 3523 changes to ranges::iota_view Patrick Palka
2021-10-19 12:17 ` [PATCH 2/2] libstdc++: Implement P1739R4 changes to views::take/drop/counted Patrick Palka
2021-10-21 15:22   ` Jonathan Wakely
2021-10-19 12:37 ` [PATCH 1/2] libstdc++: Implement LWG 3523 changes to ranges::iota_view Jonathan Wakely

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