From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 950F23858C83 for ; Wed, 19 Oct 2022 16:46:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 950F23858C83 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1666198010; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=qUEHmf8oU++fw6/Uz8TBMewnWvt4cswXudtqEM5nEDo=; b=fVtVl63joi2f6pa6gpyoKUVex/CFsbCmelWBe30YlRQc/U9RnL5qaJaJs06Pc2DVd41xgn QiG26o4dq0JIYW27sU8BeDo2Q5aYDVw2MKbKZEF85tuzoJqslvxg23XZpLE301dGpHERPV L+LlKeI0BC3IadE5WS6FqbF/0Hz8s8Y= Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-623-idPCYhgEPO6ex6HipPQOfw-1; Wed, 19 Oct 2022 12:46:49 -0400 X-MC-Unique: idPCYhgEPO6ex6HipPQOfw-1 Received: by mail-qt1-f200.google.com with SMTP id k12-20020ac8474c000000b0039cdd680ccaso9400864qtp.1 for ; Wed, 19 Oct 2022 09:46:49 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qUEHmf8oU++fw6/Uz8TBMewnWvt4cswXudtqEM5nEDo=; b=MdqJa0Fgldquw9/ZOE7LpDVLkqToBe7jIwNaUKcsjTXuVjWv3sOlIwhkSVJ7boMnrd whNXfW5wuTVC6vndJOEUQdXpUp5aV/U9rd4ui0+kVSsrPaRwGXtsWUG2Tydo2dg7bTv8 Crx+WCG7gCa5bA/KTSYRXSnBcfGlafZ6XLs3QhcKEqnxbPfu3tiqOp/gDCjXr9clZEgq qrX/7LyhV9HmfWyc6HsCZlWNFDuBd+RWADRQ5yytIKbQXIje93sO6MZrZ2uIfXEzRORv O4KZPhseDfmqpQh0kaemw+JLnslpR5hw1t+llPi1u5gufFquSgoJEYHv0o7+AM8Y7QLz BDmw== X-Gm-Message-State: ACrzQf3mhg7VHokFMhv4qh5pMc1mIjQuNu53GTZTniib4qPn62jijm9p tPnFrfmt3GuJCvQApmk+VQkruWpFVTSQ+vIVurZFAlAkNRY83mpeOHR5r3UtESgDgJ/AKwat486 qIpdqxwDu0lBEhFTSzbWIz6oQAmUECC1/CILgOdTIIj8JViWuHd4S+uVmI7MopF9ImPU= X-Received: by 2002:a05:6214:508c:b0:4b4:3b0e:5fee with SMTP id kk12-20020a056214508c00b004b43b0e5feemr7370184qvb.23.1666198007990; Wed, 19 Oct 2022 09:46:47 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6huaYlCPWP/h2MlnVHl8xsrYvLd3lwnjTNeH8xdGCyzFMapaBMWfkYrkoTY3dZ5uUINgHwnA== X-Received: by 2002:a05:6214:508c:b0:4b4:3b0e:5fee with SMTP id kk12-20020a056214508c00b004b43b0e5feemr7370165qvb.23.1666198007659; Wed, 19 Oct 2022 09:46:47 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id a10-20020ac85b8a000000b00397b1c60780sm4436759qta.61.2022.10.19.09.46.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Oct 2022 09:46:47 -0700 (PDT) From: Patrick Palka To: gcc-patches@gcc.gnu.org Cc: libstdc++@gcc.gnu.org, Patrick Palka Subject: [PATCH] libstdc++: Implement P2474R2 changes to views::take/drop Date: Wed, 19 Oct 2022 12:46:45 -0400 Message-Id: <20221019164645.301739-1-ppalka@redhat.com> X-Mailer: git-send-email 2.38.1.119.g9c32cfb49c In-Reply-To: <20221011025113.624107-1-ppalka@redhat.com> References: <20221011025113.624107-1-ppalka@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-14.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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 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