From patchwork Wed Oct 19 16:46:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 1692113 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=eXhgYgEw; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4MsxVX08sZz23jk for ; Thu, 20 Oct 2022 03:47:46 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 131BD3858291 for ; Wed, 19 Oct 2022 16:47:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 131BD3858291 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1666198064; bh=qUEHmf8oU++fw6/Uz8TBMewnWvt4cswXudtqEM5nEDo=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=eXhgYgEwnhABXayEx5jfhcP99isg1+b1rTbdQZaKIrF07OIzdA+cTjygA4UAOHxBq JrZdKnxWFEqX9v+BMOkLma1CXV062srFpyKZJoekJ6KyUw/o6LA9GgEjVMYpTAAZhY JUQaLorWgdKIXpehWq0Jh+ZHNby+byjuEL7kggJ0= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 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) To: gcc-patches@gcc.gnu.org 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 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Patrick Palka via Gcc-patches From: Patrick Palka Reply-To: Patrick Palka Cc: libstdc++@gcc.gnu.org Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" 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()); }