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 0739C38357A3 for ; Mon, 14 Nov 2022 15:11:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 0739C38357A3 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=1668438713; 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: in-reply-to:in-reply-to:references:references; bh=zrPqVelt8tTA39VSIzjYEBqFwWFSljFYo7teu/K+Ego=; b=MTnY1yOWQla+7Ka230JfCP578Vb6lSXKyn5VrdlgijjLQfcPOpGBGEgk394mXj+jH7Y2CH 9fD3EeiSNlY2U4Ye9Igp18WkWsH1clzVw+ez4M5tI9OAy6+M+JuOX0jGH8/gtTo3fCJWwH ZQv3KNhLuD9zzaNigHyFiUOhj4OXMO0= Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-353-KxoBT4Q7NtmfP7JGm4Wi5Q-1; Mon, 14 Nov 2022 10:11:52 -0500 X-MC-Unique: KxoBT4Q7NtmfP7JGm4Wi5Q-1 Received: by mail-qv1-f69.google.com with SMTP id w12-20020a056214012c00b004c6257ca968so5189082qvs.16 for ; Mon, 14 Nov 2022 07:11:52 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=mime-version:references:message-id:in-reply-to:subject:cc:to:date :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=AC+CNoR2SQSwdEG4LMlrPwdPvFbA9DH0rgnPCSadc0c=; b=ZJ7cRZhkEgMIYiHjkxbJYAI+tAvewnRl6REw2riRJkZ9yt6UCw+MYom0Gid5ZCf4NE MUt9vChhL8H2EWstSTpduy51tOY2zBYkfwtYRcD9mI0Cx7fUsAhK8SQvmj68tbEufCvx RDzBIJen2jlgbTk7b2iWCGHHF3S2gWKj8Bzaf2wxZWvkUHWppyLCvICRkeXERXea6xn5 KcmnGcNZRhg7sn3Uqbbzmu4IPg1vJ5TnfD60Ceqpcf6cIgO00B7EpgYEBzTLtrSVFUUi qHeadW4xlxF5NlONPaMB3dKwNuEVse5BccpAs44ELurg82JaavaVqVtCvqMzTbp0DCAq LPHw== X-Gm-Message-State: ANoB5pl40eh6zMO2Pej2O9b7xT+MJaQylmhwxYK1Tbs+i1F7rWYTX29r Kn0OzsqfiH2FJy/eqfPuOdUReW94Yw2Z2t8H+9dJ6afxh42TQnWKEdnAouW8lyTfldYR0zFggKh mW7QdvtuxLqOtaFE= X-Received: by 2002:a05:620a:2714:b0:6fa:2210:5734 with SMTP id b20-20020a05620a271400b006fa22105734mr11750250qkp.630.1668438710609; Mon, 14 Nov 2022 07:11:50 -0800 (PST) X-Google-Smtp-Source: AA0mqf7fErm05vZ2G4y4cuHeNNexSFhgQsCT8ZvS1SyycMEZD/4MeJOLeoUruWEiOuLiS+SZZamnRA== X-Received: by 2002:a05:620a:2714:b0:6fa:2210:5734 with SMTP id b20-20020a05620a271400b006fa22105734mr11750225qkp.630.1668438710321; Mon, 14 Nov 2022 07:11:50 -0800 (PST) Received: from [192.168.1.130] (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id q184-20020a378ec1000000b006ecb9dfdd15sm6555050qkd.92.2022.11.14.07.11.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Nov 2022 07:11:49 -0800 (PST) From: Patrick Palka X-Google-Original-From: Patrick Palka Date: Mon, 14 Nov 2022 10:11:48 -0500 (EST) To: Jonathan Wakely cc: =?ISO-8859-15?Q?Daniel_Kr=FCgler?= , Patrick Palka , gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org Subject: Re: [PATCH 2/3] libstdc++: Implement ranges::iota from P2440R1 In-Reply-To: Message-ID: <0695bdea-897c-7c35-261b-5c426b594144@idea> References: <20221114045047.362745-1-ppalka@redhat.com> <20221114045047.362745-2-ppalka@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: multipart/mixed; boundary="8323329-983860816-1668438709=:5757" X-Spam-Status: No, score=-13.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_NUMSUBJECT,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: This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --8323329-983860816-1668438709=:5757 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8BIT On Mon, 14 Nov 2022, Jonathan Wakely wrote: > On Mon, 14 Nov 2022 at 10:17, Daniel Krügler wrote: > > > > Am Mo., 14. Nov. 2022 um 11:09 Uhr schrieb Jonathan Wakely via > > Libstdc++ : > > > > > > On Mon, 14 Nov 2022 at 04:52, Patrick Palka via Libstdc++ > > > wrote: > > > > > > > > Tested on x86_64-pc-linux-gnu, does this look OK for trunk? > > > > > > > > libstdc++-v3/ChangeLog: > > > > > > > > * include/bits/ranges_algo.h (out_value_result): Define. > > > > (iota_result): Define. > > > > (__iota_fn, iota): Define. > > > > * testsuite/25_algorithms/iota/1.cc: New test. > > > > --- > > > > libstdc++-v3/include/bits/ranges_algo.h | 48 +++++++++++++++++++ > > > > .../testsuite/25_algorithms/iota/1.cc | 29 +++++++++++ > > > > 2 files changed, 77 insertions(+) > > > > create mode 100644 libstdc++-v3/testsuite/25_algorithms/iota/1.cc > > > > > > > > diff --git a/libstdc++-v3/include/bits/ranges_algo.h b/libstdc++-v3/include/bits/ranges_algo.h > > > > index da0ca981dc3..f003117c569 100644 > > > > --- a/libstdc++-v3/include/bits/ranges_algo.h > > > > +++ b/libstdc++-v3/include/bits/ranges_algo.h > > > > @@ -3517,6 +3517,54 @@ namespace ranges > > > > }; > > > > > > > > inline constexpr __contains_subrange_fn contains_subrange{}; > > > > + > > > > + template > > > > + struct out_value_result > > > > + { > > > > + [[no_unique_address]] _Out out; > > > > + [[no_unique_address]] _Tp value; > > > > + > > > > + template > > > > + requires convertible_to > > > > + && convertible_to > > > > + constexpr > > > > + operator out_value_result<_Out2, _Tp2>() const & > > > > + { return {out, value}; } > > > > + > > > > + template > > > > + requires convertible_to<_Out, _Out2> > > > > + && convertible_to<_Tp, _Tp2> > > > > + constexpr > > > > + operator out_value_result<_Out2, _Tp2>() && > > > > + { return {std::move(out), std::move(value)}; } > > > > + }; > > > > + > > > > + template > > > > + using iota_result = out_value_result<_Out, _Tp>; > > > > + > > > > + struct __iota_fn > > > > + { > > > > + template _Sent, weakly_incrementable _Tp> > > > > + requires indirectly_writable<_Out, const _Tp&> > > > > + constexpr iota_result<_Out, _Tp> > > > > + operator()(_Out __first, _Sent __last, _Tp __value) const > > > > + { > > > > + while (__first != __last) > > > > + { > > > > + *__first = static_cast&>(__value); > > > > > > Is this any different to const_cast(__value) ? > > > > I think it is. const_cast can potentially mean the removal > > of volatile, > > True. > > > so I would always look with suspicion on const_cast > _Tp&>, while static_cast is clearer. Alternatively, as_const could be > > used, which does add_const_t. > > Which means evaluating the add_const trait *and* overload resolution > for as_const* *and* a runtime function call. > > Let's go with static_cast. Sounds good, like so? -- >8 -- Subject: [PATCH 2/3] libstdc++: Implement ranges::iota from P2440R1 libstdc++-v3/ChangeLog: * include/bits/ranges_algo.h (out_value_result): Define. (iota_result): Define. (__iota_fn, iota): Define. * testsuite/25_algorithms/iota/1.cc: New test. --- libstdc++-v3/include/bits/ranges_algo.h | 48 +++++++++++++++++++ .../testsuite/25_algorithms/iota/1.cc | 29 +++++++++++ 2 files changed, 77 insertions(+) create mode 100644 libstdc++-v3/testsuite/25_algorithms/iota/1.cc diff --git a/libstdc++-v3/include/bits/ranges_algo.h b/libstdc++-v3/include/bits/ranges_algo.h index 11206bdbcaa..f75735f02cb 100644 --- a/libstdc++-v3/include/bits/ranges_algo.h +++ b/libstdc++-v3/include/bits/ranges_algo.h @@ -3517,6 +3517,54 @@ namespace ranges }; inline constexpr __contains_subrange_fn contains_subrange{}; + + template + struct out_value_result + { + [[no_unique_address]] _Out out; + [[no_unique_address]] _Tp value; + + template + requires convertible_to + && convertible_to + constexpr + operator out_value_result<_Out2, _Tp2>() const & + { return {out, value}; } + + template + requires convertible_to<_Out, _Out2> + && convertible_to<_Tp, _Tp2> + constexpr + operator out_value_result<_Out2, _Tp2>() && + { return {std::move(out), std::move(value)}; } + }; + + template + using iota_result = out_value_result<_Out, _Tp>; + + struct __iota_fn + { + template _Sent, weakly_incrementable _Tp> + requires indirectly_writable<_Out, const _Tp&> + constexpr iota_result<_Out, _Tp> + operator()(_Out __first, _Sent __last, _Tp __value) const + { + while (__first != __last) + { + *__first = static_cast(__value); + ++__first; + ++__value; + } + return {std::move(__first), std::move(__value)}; + } + + template _Range> + constexpr iota_result, _Tp> + operator()(_Range&& __r, _Tp __value) const + { return (*this)(ranges::begin(__r), ranges::end(__r), std::move(__value)); } + }; + + inline constexpr __iota_fn iota{}; #endif // C++23 } // namespace ranges diff --git a/libstdc++-v3/testsuite/25_algorithms/iota/1.cc b/libstdc++-v3/testsuite/25_algorithms/iota/1.cc new file mode 100644 index 00000000000..ad2bf08adf5 --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/iota/1.cc @@ -0,0 +1,29 @@ +// { dg-options "-std=gnu++23" } +// { dg-do run { target c++23 } } + +#include +#include +#include + +namespace ranges = std::ranges; + +void +test01() +{ + int x[3] = {}; + __gnu_test::test_output_range rx(x); + auto r0 = ranges::iota(rx, 0); + VERIFY( r0.out.ptr == x+3 ); + VERIFY( r0.value == 3 ); + VERIFY( ranges::equal(x, (int[]){0,1,2}) ); + auto r1 = ranges::iota(x, x+2, 5); + VERIFY( r1.out == x+2 ); + VERIFY( r1.value == 7 ); + VERIFY( ranges::equal(x, (int[]){5,6,2}) ); +} + +int +main() +{ + test01(); +} -- 2.38.1.420.g319605f8f0 --8323329-983860816-1668438709=:5757--