From patchwork Fri Apr 30 14:38:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 1472354 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: 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@gcc.gnu.org; receiver=) Authentication-Results: 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=d0h579po; 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 RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4FWw3k1Mj0z9sW4 for ; Sat, 1 May 2021 00:38:57 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 154D83A62C28; Fri, 30 Apr 2021 14:38:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 154D83A62C28 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1619793527; bh=ki5ErtpEzDjlwXrUNabG5oGu/ZXiNdWQ42ToPsXvlxk=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=d0h579po9RRb1WAv4j3mA/oNGEzYCBTwT2UoEUBsx+sPXZQ3gOngd76by3OHpWo1+ S0oAVPj2vDQjUUal0qpUPadiIbe6yqkgVUDUqmkH36D3K7v+rxC6EbNCxKmGAxGtcC e5PVymWApqFNpias9DvaPpWAQry5Ng2PNuH5LNeY= 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 [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id 40BDF3A62C28 for ; Fri, 30 Apr 2021 14:38:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 40BDF3A62C28 Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-385-RV4Sf0hAOkWyAyR0NbqfCw-1; Fri, 30 Apr 2021 10:38:39 -0400 X-MC-Unique: RV4Sf0hAOkWyAyR0NbqfCw-1 Received: by mail-qk1-f197.google.com with SMTP id s10-20020a05620a030ab02902e061a1661fso30560390qkm.12 for ; Fri, 30 Apr 2021 07:38:39 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=ki5ErtpEzDjlwXrUNabG5oGu/ZXiNdWQ42ToPsXvlxk=; b=Bn5T59/pQebl3P+dzDEERmmE171T4MgbqFck5S0Va7IJg5nIT5pijCPhcgDbWovzib S6d2J1mT6Bn/F63M3u6oz1S+7ls9nvd/L0MPVn8s8XeaUi5xw8a+ZDWLFyINoprxS7NF J2yyqlmj1pVztX7aqGjCGDuomipQOAM2y8nhBeVcYtRRVlzGN01dI8DlsuCd7qz4rJFq QoM6l0UJXFlnDH6WfAgS6msZ6MuqAeVDle1PEKyaaSttrEbXZTOM5ilCA8DkDa2wJvaB EfJ6Sj2Gaphz9YHiDhH+PC3bfXZvtzAzd6pqmtmeeiCrjHFsQUzOorlYM/tGgaZOAWT/ aoQQ== X-Gm-Message-State: AOAM532w9VYhJGYokXw2yoDB4CC/0lc+S99UMegNtwOi9Ei7b++TFXpP in6UK1Lz6OmCJztKjW9mfe3MeaRo7oKmnPWnVGyNbElwX9Y0VVtJEl1ABzanKw0tTkXZL66Lh7z bIaNEmwT4i0o0mWDmSgS3jIqeFEX/fSsOgENdnuh0VKzbSccKRCjmMiQcqfEqRcjzqsU= X-Received: by 2002:a37:a597:: with SMTP id o145mr5710988qke.265.1619793518546; Fri, 30 Apr 2021 07:38:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzFhT3sFXVR1dAIbT7pUwydYr7mwyCn1QWMbgXt5bhvqfCbcGqOPozxBsA27lSF5gdet5jeAA== X-Received: by 2002:a37:a597:: with SMTP id o145mr5710954qke.265.1619793518217; Fri, 30 Apr 2021 07:38:38 -0700 (PDT) Received: from localhost.localdomain (ool-457d493a.dyn.optonline.net. [69.125.73.58]) by smtp.gmail.com with ESMTPSA id z25sm1675620qtm.41.2021.04.30.07.38.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Apr 2021 07:38:37 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH] libstdc++: Implement P2328 changes to join_view Date: Fri, 30 Apr 2021 10:38:35 -0400 Message-Id: <20210430143835.893089-1-ppalka@redhat.com> X-Mailer: git-send-email 2.31.1.362.g311531c9de MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-16.2 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_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=unavailable autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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@gcc.gnu.org Sender: "Gcc-patches" This implements the wording changes of "join_view should join all views of ranges". Tested on x86_64-pc-linux-gnu, does this look OK for trunk? libstdc++-v3/ChangeLog: * include/std/ranges (__detail::__non_propating_cache): Define as per P2328. (join_view): Remove constraints on the value and reference types of the wrapped iterator type as per P2328. (join_view::_Iterator::_M_satisfy): Adjust as per P2328. (join_view::_Iterator::operator++): Likewise. (join_view::_M_inner): Use __non_propating_cache as per P2328. Remove now-redundant use of __maybe_present_t. * testsuite/std/ranges/adaptors/join.cc (test10): New test. --- libstdc++-v3/include/std/ranges | 70 ++++++++++++++++--- .../testsuite/std/ranges/adaptors/join.cc | 12 ++++ 2 files changed, 71 insertions(+), 11 deletions(-) diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges index 4be643baeaa..b43f9b337a4 100644 --- a/libstdc++-v3/include/std/ranges +++ b/libstdc++-v3/include/std/ranges @@ -2258,10 +2258,61 @@ namespace views::__adaptor inline constexpr _DropWhile drop_while; } // namespace views + namespace __detail + { + template + struct __non_propagating_cache + { + // When _Tp is not an object type (e.g. is a reference type), we make + // __non_propagating_cache<_Tp> empty rather than an invalid type so + // that users can easily conditionally declare data members with this + // type (such as join_view::_M_inner). + }; + + template + requires is_object_v<_Tp> + struct __non_propagating_cache<_Tp> : private optional<_Tp> + { + __non_propagating_cache() = default; + + constexpr + __non_propagating_cache(const __non_propagating_cache&) noexcept + { } + + constexpr + __non_propagating_cache(__non_propagating_cache&& __other) noexcept + { __other.reset(); } + + constexpr __non_propagating_cache& + operator=(const __non_propagating_cache& __other) noexcept + { + if (std::__addressof(__other) != this) + this->reset(); + return *this; + } + + constexpr __non_propagating_cache& + operator=(__non_propagating_cache&& __other) noexcept + { + this->reset(); + __other.reset(); + return *this; + } + + using optional<_Tp>::operator*; + + template + _Tp& + _M_emplace_deref(const _Iter& __i) + { + this->reset(); + return this->emplace(*__i); + } + }; + } + template requires view<_Vp> && input_range> - && (is_reference_v> - || view>) class join_view : public view_interface> { private: @@ -2327,17 +2378,16 @@ namespace views::__adaptor constexpr void _M_satisfy() { - auto __update_inner = [this] (range_reference_t<_Base> __x) -> auto& - { + auto __update_inner = [this] (const iterator_t<_Base>& __x) -> auto&& { if constexpr (_S_ref_is_glvalue) - return __x; + return *__x; else - return (_M_parent->_M_inner = views::all(std::move(__x))); + return _M_parent->_M_inner._M_emplace_deref(__x); }; for (; _M_outer != ranges::end(_M_parent->_M_base); ++_M_outer) { - auto& __inner = __update_inner(*_M_outer); + auto&& __inner = __update_inner(_M_outer); _M_inner = ranges::begin(__inner); if (_M_inner != ranges::end(__inner)) return; @@ -2413,7 +2463,7 @@ namespace views::__adaptor if constexpr (_S_ref_is_glvalue) return *_M_outer; else - return _M_parent->_M_inner; + return *_M_parent->_M_inner; }(); if (++_M_inner == ranges::end(__inner_range)) { @@ -2524,10 +2574,8 @@ namespace views::__adaptor friend _Sentinel; }; - // XXX: _M_inner is "present only when !is_reference_v<_InnerRange>" [[no_unique_address]] - __detail::__maybe_present_t, - views::all_t<_InnerRange>> _M_inner; + __detail::__non_propagating_cache> _M_inner; _Vp _M_base = _Vp(); public: diff --git a/libstdc++-v3/testsuite/std/ranges/adaptors/join.cc b/libstdc++-v3/testsuite/std/ranges/adaptors/join.cc index fb06a7698af..6890b105eab 100644 --- a/libstdc++-v3/testsuite/std/ranges/adaptors/join.cc +++ b/libstdc++-v3/testsuite/std/ranges/adaptors/join.cc @@ -160,6 +160,17 @@ test09() static_assert(!requires { 0 | join; }); } +void +test10() +{ +// Verify P2328 changes. + int r[] = {1, 2, 3}; + auto v = r + | views::transform([] (int n) { return std::vector{{n, -n}}; }) + | views::join; + VERIFY( ranges::equal(v, (int[]){1, -1, 2, -2, 3, -3}) ); +} + int main() { @@ -172,4 +183,5 @@ main() test07(); test08(); test09(); + test10(); }