From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ed1-x530.google.com (mail-ed1-x530.google.com [IPv6:2a00:1450:4864:20::530]) by sourceware.org (Postfix) with ESMTPS id 7D98F3858D3C; Wed, 19 Oct 2022 18:28:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7D98F3858D3C Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-ed1-x530.google.com with SMTP id r14so26539451edc.7; Wed, 19 Oct 2022 11:28:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=MaDnSmr1TUDWulhdATc4Uv2Qziaqm6KENjNYtlRycVw=; b=M8xfxeS+phpXdlwaz36tlCIYeEB+DMv/wx98yYuK61H7y1pg11RBEyYdFo+YcQ4a/G C2N4CuNl5HdyY6ErJFMcLW83FXvmZHmBVwxkH8rdrFQH2+Ctcwo2LYHFbiHF1/T8jMxx YVuw9WUmZdiNZ6DRY51BopOgVJWMCHNjYTlSxTyFYGLSIFvK1N591DdgfiwkioaFes1M 2icJ0scF42bkeilmdlAZJphjwTQ2neNCfVfUcIBRh0hCYd1TtovFSGiopUHjgqgdy4qg gYRAkaLVYJFCxcvNR0YY3kMPV0oy6MiVLkAgAo1gP14mmY/hTxCf28/gYDc6LBR+HI93 AtcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=MaDnSmr1TUDWulhdATc4Uv2Qziaqm6KENjNYtlRycVw=; b=d+kICn4bH2LxyhhrpzzPXHfWuzNfVgS7N3PFrfONN46ZjZtKn2hrW/e3oAb5rkGoXh hsTY5Sut7oMRhRl/Lz08MjkXctKXvRKEQ1uMrEnHZeav7oUNQwDg4c2NIbhCcmWbqDDD vCvAFQ00peaQQ+ioqGO4MjnvCgIXS0OH3kZjljbqMzeZCNi7jyk2Wm1Eu2EQUvg19SgL bVsQVLNgZo3Z5U/OmCOWs2mMHeTx9TgmcNQ8b6ezNzkDbhCZXN79DP2+hW56+QCOzYWf OsOujn/fyNwnDdQ/ecK+XGwpizknf9o3WO+kkCE+BfG37UXOEPBITLzsE68AnkojWoFw wCUw== X-Gm-Message-State: ACrzQf3MWkzGUvT0S8ieAb0+kxAlhoO1kf6UG0p6Nrk+CXKySLHcVwtY 7VE6C30oI5UcwCZvW4pYAGQ0FWViyH1rvcIvMVI= X-Google-Smtp-Source: AMsMyM5k4rc/olgoNMhFysl6YFYPKrYYMNmjHLWNGgIbcj+evweS+1WYK+g4N6GpTPaSQQMcd4kR7QAdWng8UOqbS6E= X-Received: by 2002:a05:6402:84d:b0:454:f41d:6ccf with SMTP id b13-20020a056402084d00b00454f41d6ccfmr8773803edz.129.1666204095152; Wed, 19 Oct 2022 11:28:15 -0700 (PDT) MIME-Version: 1.0 References: <20221011025113.624107-1-ppalka@redhat.com> <20221019164645.301739-1-ppalka@redhat.com> In-Reply-To: <20221019164645.301739-1-ppalka@redhat.com> From: Jonathan Wakely Date: Wed, 19 Oct 2022 19:28:02 +0100 Message-ID: Subject: Re: [PATCH] libstdc++: Implement P2474R2 changes to views::take/drop To: Patrick Palka Cc: gcc-patches , "libstdc++" Content-Type: multipart/alternative; boundary="0000000000003707a305eb675f3e" X-Spam-Status: No, score=-6.5 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,HTML_MESSAGE,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: --0000000000003707a305eb675f3e Content-Type: text/plain; charset="UTF-8" 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 > inline constexpr bool __is_iota_view> = > true; > > + template > + inline constexpr bool __is_repeat_view = false; > + > + template > + constexpr auto > + __take_of_repeat_view(_Range&&, range_difference_t<_Range>); // > defined later > + > template > 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 > + constexpr auto > + __drop_of_repeat_view(_Range&&, range_difference_t<_Range>); // > defined later > + > template > 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 > + friend constexpr auto > + views::__detail::__take_of_repeat_view(_Range&&, > range_difference_t<_Range>); > + > + template > + 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 > + inline constexpr bool __is_repeat_view> = > true; > + > template > concept __can_repeat_view > = requires { repeat_view(std::declval<_Tp>()); }; > @@ -7565,6 +7591,36 @@ namespace views::__adaptor > }; > > inline constexpr _Repeat repeat; > + > + namespace __detail > + { > + template > + 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 > + 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 > 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); > + VERIFY( ranges::equal(rt, views::repeat(42, 10)) ); > + > + auto rd = r | views::drop(10); > + static_assert(std::same_as); > + > + auto br = views::repeat(42, 37); > + > + auto brt = br | views::take(10); > + static_assert(std::same_as); > + 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); > + 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 > > --0000000000003707a305eb675f3e--