* [PATCH] libstdc++: Implement ranges::repeat_view from P2474R2
@ 2022-10-11 2:51 Patrick Palka
2022-10-11 19:38 ` Jonathan Wakely
2022-10-19 16:46 ` [PATCH] libstdc++: Implement P2474R2 changes to views::take/drop Patrick Palka
0 siblings, 2 replies; 4+ messages in thread
From: Patrick Palka @ 2022-10-11 2:51 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Patrick Palka
Tested on x86_64-pc-linux-gnu, does this look OK for trunk? (The paper
also makes changes to views::take and views::drop, which will be
implemented separately.)
libstdc++-v3/ChangeLog:
* include/std/ranges (repeat_view): Define.
(repeat_view::_Iterator): Define.
(views::__detail::__can_repeat_view): Define.
(views::__detail::__can_bounded_repeat_view): Define.
(views::_Repeat, views::repeat): Define.
* testsuite/std/ranges/repeat/1.cc: New test.
---
libstdc++-v3/include/std/ranges | 210 ++++++++++++++++++
libstdc++-v3/testsuite/std/ranges/repeat/1.cc | 93 ++++++++
2 files changed, 303 insertions(+)
create mode 100644 libstdc++-v3/testsuite/std/ranges/repeat/1.cc
diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges
index 1f821128d2d..5857d426a66 100644
--- a/libstdc++-v3/include/std/ranges
+++ b/libstdc++-v3/include/std/ranges
@@ -7356,6 +7356,216 @@ namespace views::__adaptor
inline constexpr _JoinWith join_with;
} // namespace views
+
+ template<copy_constructible _Tp, semiregular _Bound = unreachable_sentinel_t>
+ requires (is_object_v<_Tp> && same_as<_Tp, remove_cv_t<_Tp>>
+ && (__detail::__is_integer_like<_Bound> || same_as<_Bound, unreachable_sentinel_t>))
+ class repeat_view : public view_interface<repeat_view<_Tp, _Bound>>
+ {
+ __detail::__box<_Tp> _M_value = _Tp();
+ [[no_unique_address]] _Bound _M_bound = _Bound();
+
+ struct _Iterator;
+
+ public:
+ repeat_view() requires default_initializable<_Tp> = default;
+
+ constexpr explicit
+ repeat_view(const _Tp& __value, _Bound __bound = _Bound())
+ : _M_value(__value), _M_bound(__bound)
+ {
+ if constexpr (!same_as<_Bound, unreachable_sentinel_t>)
+ __glibcxx_assert(__bound >= 0);
+ }
+
+ constexpr explicit
+ repeat_view(_Tp&& __value, _Bound __bound = _Bound())
+ : _M_value(std::move(__value)), _M_bound(__bound)
+ { }
+
+ template<typename... _Args, typename... _BoundArgs>
+ requires constructible_from<_Tp, _Args...>
+ && constructible_from<_Bound, _BoundArgs...>
+ constexpr explicit
+ repeat_view(piecewise_construct_t,
+ tuple<_Args...> __args,
+ tuple<_BoundArgs...> __bound_args = tuple<>{})
+ : _M_value(std::make_from_tuple<_Tp>(std::move(__args))),
+ _M_bound(std::make_from_tuple<_Bound>(std::move(__bound_args)))
+ { }
+
+ constexpr _Iterator
+ begin() const
+ { return _Iterator(std::__addressof(*_M_value)); }
+
+ constexpr _Iterator
+ end() const requires (!same_as<_Bound, unreachable_sentinel_t>)
+ { return _Iterator(std::__addressof(*_M_value), _M_bound); }
+
+ constexpr unreachable_sentinel_t
+ end() const noexcept
+ { return unreachable_sentinel; }
+
+ constexpr auto
+ size() const requires (!same_as<_Bound, unreachable_sentinel_t>)
+ { return __detail::__to_unsigned_like(_M_bound); }
+ };
+
+ template<typename _Tp, typename _Bound>
+ repeat_view(_Tp, _Bound) -> repeat_view<_Tp, _Bound>;
+
+ template<copy_constructible _Tp, semiregular _Bound>
+ requires __detail::__is_integer_like<_Bound> || same_as<_Bound, unreachable_sentinel_t>
+ class repeat_view<_Tp, _Bound>::_Iterator
+ {
+ using __index_type
+ = __conditional_t<same_as<_Bound, unreachable_sentinel_t>, ptrdiff_t, _Bound>;
+
+ const _Tp* _M_value = nullptr;
+ __index_type _M_current = __index_type();
+
+ constexpr explicit
+ _Iterator(const _Tp* __value, __index_type __bound = __index_type())
+ : _M_value(__value), _M_current(__bound)
+ {
+ if constexpr (!same_as<_Bound, unreachable_sentinel_t>)
+ __glibcxx_assert(__bound >= 0);
+ }
+
+ friend repeat_view;
+
+ public:
+ using iterator_concept = random_access_iterator_tag;
+ using iterator_category = random_access_iterator_tag;
+ using value_type = _Tp;
+ using difference_type = __conditional_t<__detail::__is_signed_integer_like<__index_type>,
+ __index_type,
+ __detail::__iota_diff_t<__index_type>>;
+
+ _Iterator() = default;
+
+ constexpr const _Tp&
+ operator*() const noexcept
+ { return *_M_value; }
+
+ constexpr _Iterator&
+ operator++()
+ {
+ ++_M_current;
+ return *this;
+ }
+
+ constexpr _Iterator
+ operator++(int)
+ {
+ auto __tmp = *this;
+ ++*this;
+ return __tmp;
+ }
+
+ constexpr _Iterator&
+ operator--()
+ {
+ if constexpr (!same_as<_Bound, unreachable_sentinel_t>)
+ __glibcxx_assert(_M_current > 0);
+ --_M_current;
+ return *this;
+ }
+
+ constexpr _Iterator
+ operator--(int)
+ {
+ auto __tmp = *this;
+ --*this;
+ return __tmp;
+ }
+
+ constexpr _Iterator&
+ operator+=(difference_type __n)
+ {
+ if constexpr (!same_as<_Bound, unreachable_sentinel_t>)
+ __glibcxx_assert(_M_current + __n >= 0);
+ _M_current += __n;
+ return *this;
+ }
+
+ constexpr _Iterator&
+ operator-=(difference_type __n)
+ {
+ if constexpr (!same_as<_Bound, unreachable_sentinel_t>)
+ __glibcxx_assert(_M_current - __n >= 0);
+ _M_current -= __n;
+ return *this;
+ }
+
+ constexpr const _Tp&
+ operator[](difference_type __n) const noexcept
+ { return *(*this + __n); }
+
+ friend constexpr bool
+ operator==(const _Iterator& __x, const _Iterator& __y)
+ { return __x._M_current == __y._M_current; }
+
+ friend constexpr auto
+ operator<=>(const _Iterator& __x, const _Iterator& __y)
+ { return __x._M_current <=> __y._M_current; }
+
+ friend constexpr _Iterator
+ operator+(_Iterator __i, difference_type __n)
+ {
+ __i += __n;
+ return __i;
+ }
+
+ friend constexpr _Iterator
+ operator+(difference_type __n, _Iterator __i)
+ { return __i + __n; }
+
+ friend constexpr _Iterator
+ operator-(_Iterator __i, difference_type __n)
+ {
+ __i -= __n;
+ return __i;
+ }
+
+ friend constexpr difference_type
+ operator-(const _Iterator& __x, const _Iterator& __y)
+ {
+ return (static_cast<difference_type>(__x._M_current)
+ - static_cast<difference_type>(__y._M_current));
+ }
+ };
+
+ namespace views
+ {
+ namespace __detail
+ {
+ template<typename _Tp>
+ concept __can_repeat_view
+ = requires { repeat_view(std::declval<_Tp>()); };
+
+ template<typename _Tp, typename _Bound>
+ concept __can_bounded_repeat_view
+ = requires { repeat_view(std::declval<_Tp>(), std::declval<_Bound>()); };
+ }
+
+ struct _Repeat
+ {
+ template<typename _Tp>
+ requires __detail::__can_repeat_view<_Tp>
+ constexpr auto
+ operator() [[nodiscard]] (_Tp&& __value) const
+ { return repeat_view(std::forward<_Tp>(__value)); }
+
+ template<typename _Tp, typename _Bound>
+ requires __detail::__can_bounded_repeat_view<_Tp, _Bound>
+ constexpr auto
+ operator() [[nodiscard]] (_Tp&& __value, _Bound __bound) const
+ { return repeat_view(std::forward<_Tp>(__value), __bound); }
+ };
+
+ inline constexpr _Repeat repeat;
+ }
#endif // C++23
} // namespace ranges
diff --git a/libstdc++-v3/testsuite/std/ranges/repeat/1.cc b/libstdc++-v3/testsuite/std/ranges/repeat/1.cc
new file mode 100644
index 00000000000..3698ed12c14
--- /dev/null
+++ b/libstdc++-v3/testsuite/std/ranges/repeat/1.cc
@@ -0,0 +1,93 @@
+// { dg-options "-std=gnu++23" }
+// { dg-do run { target c++23 } }
+
+#include <ranges>
+#include <algorithm>
+#include <testsuite_hooks.h>
+
+namespace ranges = std::ranges;
+namespace views = std::views;
+
+constexpr bool
+test01()
+{
+ auto v = views::repeat(42);
+ static_assert(ranges::random_access_range<decltype(v)>
+ && !ranges::sized_range<decltype(v)>);
+ auto i = ranges::begin(v);
+ auto s = ranges::end(v);
+ VERIFY( *i == 42 );
+ VERIFY( i[0] == 42 );
+ VERIFY( &i[0] == &i[1] );
+ VERIFY( &*i == &*(i+1) );
+ VERIFY( i != s );
+ auto j = i + 5, k = i + 12;
+ VERIFY( k - i == 12 );
+ VERIFY( k - j == 7 );
+ VERIFY( i - j == -5 );
+ VERIFY( k > j );
+ VERIFY( j < k );
+ VERIFY( i + 5 == j );
+ VERIFY( i != j );
+ VERIFY( i + 5 <= j );
+ VERIFY( j - 5 >= i );
+
+ return true;
+}
+
+constexpr bool
+test02()
+{
+ constexpr int bound = 20;
+ auto v = views::repeat(42, bound);
+ static_assert(ranges::random_access_range<decltype(v)>
+ && ranges::sized_range<decltype(v)>);
+ VERIFY( ranges::equal(v, views::repeat(42) | views::take(bound)) );
+ auto i = ranges::begin(v);
+ auto s = ranges::end(v);
+ VERIFY( *i == 42 );
+ VERIFY( i[0] == 42 );
+ VERIFY( &i[0] == &i[1] );
+ VERIFY( &*i == &*(i+1) );
+ VERIFY( i != s );
+ auto j = i + 5, k = i + 12;
+ VERIFY( k - i == 12 );
+ VERIFY( k - j == 7 );
+ VERIFY( i - j == -5 );
+ VERIFY( k > j );
+ VERIFY( j < k );
+ VERIFY( i + 5 == j );
+ VERIFY( i != j );
+ VERIFY( i + 5 <= j );
+ VERIFY( j - 5 >= i );
+
+ VERIFY( ranges::size(v) == bound );
+ VERIFY( s - i == bound );
+ VERIFY( s - j == bound - (j - i) );
+ VERIFY( i + bound == s );
+ VERIFY( bound + i == s );
+
+ return true;
+}
+
+constexpr bool
+test03()
+{
+ struct A { int n, m; };
+ auto v = ranges::repeat_view<A, unsigned>(std::piecewise_construct,
+ std::tuple{1, 2},
+ std::tuple{3});
+ VERIFY( v[0].n == 1 );
+ VERIFY( v[0].m == 2 );
+ VERIFY( ranges::size(v) == 3 );
+
+ return true;
+}
+
+int
+main()
+{
+ static_assert(test01());
+ static_assert(test02());
+ static_assert(test03());
+}
--
2.38.0.15.gbbe21b64a0
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] libstdc++: Implement ranges::repeat_view from P2474R2
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 ` [PATCH] libstdc++: Implement P2474R2 changes to views::take/drop Patrick Palka
1 sibling, 0 replies; 4+ messages in thread
From: Jonathan Wakely @ 2022-10-11 19:38 UTC (permalink / raw)
To: Patrick Palka; +Cc: gcc-patches, libstdc++
On Tue, 11 Oct 2022 at 03:51, Patrick Palka via Libstdc++
<libstdc++@gcc.gnu.org> wrote:
>
> Tested on x86_64-pc-linux-gnu, does this look OK for trunk? (The paper
> also makes changes to views::take and views::drop, which will be
> implemented separately.)
OK, thanks.
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH] libstdc++: Implement P2474R2 changes to views::take/drop
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
2022-10-19 18:28 ` Jonathan Wakely
1 sibling, 1 reply; 4+ messages in thread
From: Patrick Palka @ 2022-10-19 16:46 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Patrick Palka
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
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] libstdc++: Implement P2474R2 changes to views::take/drop
2022-10-19 16:46 ` [PATCH] libstdc++: Implement P2474R2 changes to views::take/drop Patrick Palka
@ 2022-10-19 18:28 ` Jonathan Wakely
0 siblings, 0 replies; 4+ messages in thread
From: Jonathan Wakely @ 2022-10-19 18:28 UTC (permalink / raw)
To: Patrick Palka; +Cc: gcc-patches, libstdc++
[-- Attachment #1: Type: text/plain, Size: 6398 bytes --]
On Wed, 19 Oct 2022, 17:47 Patrick Palka via Libstdc++, <
libstdc++@gcc.gnu.org> wrote:
> Tested on x86_64-pc-linux-gnu, does this look OK for trunk?
>
OK
> 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
>
>
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2022-10-19 18:28 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [PATCH] libstdc++: Implement P2474R2 changes to views::take/drop Patrick Palka
2022-10-19 18:28 ` 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).