Message ID | 20221019164645.301739-1-ppalka@redhat.com |
---|---|
State | New |
Headers | show |
Series | libstdc++: Implement P2474R2 changes to views::take/drop | expand |
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 > >
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()); }