Message ID | 20221114045047.362745-2-ppalka@redhat.com |
---|---|
State | Committed |
Headers |
Return-Path: <gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org> X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9D6A7382F11E for <patchwork@sourceware.org>; Mon, 14 Nov 2022 04:52:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9D6A7382F11E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1668401573; bh=drlh8yvJH9WnyQMIDFpSFVEc1bXUUGazvfNFD920NwU=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=tUGxInVYpJUEbGgY/l1TOESkJ4eXeYkh/le4qM+gl3lEssYTR4kHmUQmllI5e8pqg asnbZeiSHaSK09UJk/RpZ5aOOfauCi1vyVv6WEcKllPol2zCP5zltxYshViMkCcJMi edIC9BBixAMy/XhYrm9OYPbXvuE3VrMxZBO82K5Q= 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 1D29C38518BC for <gcc-patches@gcc.gnu.org>; Mon, 14 Nov 2022 04:51:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 1D29C38518BC Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-173-jacyX9IDN-eJrIExSMqFkA-1; Sun, 13 Nov 2022 23:51:20 -0500 X-MC-Unique: jacyX9IDN-eJrIExSMqFkA-1 Received: by mail-qt1-f199.google.com with SMTP id cj6-20020a05622a258600b003a519d02f59so7371398qtb.5 for <gcc-patches@gcc.gnu.org>; Sun, 13 Nov 2022 20:51:20 -0800 (PST) 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=drlh8yvJH9WnyQMIDFpSFVEc1bXUUGazvfNFD920NwU=; b=kwg7eZJ+Nxz517lccmjPHd9nb7D4XWsWXmxLAr9zDlqvfyxzkzyI8gkvgr65i6dfYY nFb/5GEPKU9Ka2osdkH6oc8YRr9OVdMCJm122id2CwONBV8IWSpSng4cQhrIlW/WO/jk /GOFDpX9gxrAdrtlT1/sd27uetBBx3oOy/UkylZO5fDO0jHQrgjvUI0CVMFRzmyLT7PX 9AyXgm31jnwsoWOSPP6O7jLPqvDgNAT6Tt1SKNCLKCm7QsXh4nEiOIEIrz5yHWzJPUCL Br574GWPP/tLNuz/SyIMIPtJW9IIMQY79nSImVBVV3AmWYlGsDTQ2VA2n7f96A+vw5Pq VORQ== X-Gm-Message-State: ANoB5pmE5TDKX0nCxNl8aIvQJfxg2AvJvk1G5p2ydqnddJgphzMdL0zl 5Khf1a2iNS05v5BpcVALt1ndKOD4ymz9GyrM+dB1NmLVVsDqa951gv5bRv1YVXPku6HebWjOxeM RFzltzycfsUnzINZhna50/D8wL8Sy8i7gLrNmxWrGIWEQGGs+M/wn5h6FLXn45Q1wLyw= X-Received: by 2002:a05:622a:248c:b0:3a5:6005:7db6 with SMTP id cn12-20020a05622a248c00b003a560057db6mr10827105qtb.131.1668401479128; Sun, 13 Nov 2022 20:51:19 -0800 (PST) X-Google-Smtp-Source: AA0mqf5lvkfMS5EmBuv1eWuU+QTyANkNnLLEbqol5m88GkatISzY2LOJTIr9CpDniQassBX0jx+/Sw== X-Received: by 2002:a05:622a:248c:b0:3a5:6005:7db6 with SMTP id cn12-20020a05622a248c00b003a560057db6mr10827098qtb.131.1668401478847; Sun, 13 Nov 2022 20:51:18 -0800 (PST) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id x7-20020ac84a07000000b0035badb499c7sm5180567qtq.21.2022.11.13.20.51.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 13 Nov 2022 20:51:18 -0800 (PST) To: gcc-patches@gcc.gnu.org Cc: libstdc++@gcc.gnu.org, Patrick Palka <ppalka@redhat.com> Subject: [PATCH 2/3] libstdc++: Implement ranges::iota from P2440R1 Date: Sun, 13 Nov 2022 23:50:46 -0500 Message-Id: <20221114045047.362745-2-ppalka@redhat.com> X-Mailer: git-send-email 2.38.1.420.g319605f8f0 In-Reply-To: <20221114045047.362745-1-ppalka@redhat.com> References: <20221114045047.362745-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=-13.5 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list <gcc-patches.gcc.gnu.org> List-Unsubscribe: <https://gcc.gnu.org/mailman/options/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe> List-Archive: <https://gcc.gnu.org/pipermail/gcc-patches/> List-Post: <mailto:gcc-patches@gcc.gnu.org> List-Help: <mailto:gcc-patches-request@gcc.gnu.org?subject=help> List-Subscribe: <https://gcc.gnu.org/mailman/listinfo/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe> From: Patrick Palka via Gcc-patches <gcc-patches@gcc.gnu.org> Reply-To: Patrick Palka <ppalka@redhat.com> Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" <gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org> |
Series |
[1/3] libstdc++: Implement ranges::contains/contains_subrange from P2302R4
|
|
Commit Message
Patrick Palka
Nov. 14, 2022, 4:50 a.m. UTC
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
Comments
On Mon, 14 Nov 2022 at 04:52, Patrick Palka via Libstdc++ <libstdc++@gcc.gnu.org> 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<typename _Out, typename _Tp> > + struct out_value_result > + { > + [[no_unique_address]] _Out out; > + [[no_unique_address]] _Tp value; > + > + template<typename _Out2, typename _Tp2> > + requires convertible_to<const _Out&, _Out2> > + && convertible_to<const _Tp&, _Tp2> > + constexpr > + operator out_value_result<_Out2, _Tp2>() const & > + { return {out, value}; } > + > + template<typename _Out2, typename _Tp2> > + requires convertible_to<_Out, _Out2> > + && convertible_to<_Tp, _Tp2> > + constexpr > + operator out_value_result<_Out2, _Tp2>() && > + { return {std::move(out), std::move(value)}; } > + }; > + > + template<typename _Out, typename _Tp> > + using iota_result = out_value_result<_Out, _Tp>; > + > + struct __iota_fn > + { > + template<input_or_output_iterator _Out, sentinel_for<_Out> _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<add_const_t<_Tp>&>(__value); Is this any different to const_cast<const _Tp&>(__value) ? We know _Tp must be the same as decay_t<_Tp> and non-void, because it's passed by value, and therefore I think is_same_v<const _Tp, add_const_t<_Tp>> is always true, isn't it? We don't need to care about people saying ranges::iota.operator()<O,S,T&>(o,s,t), those people are animals. We would just change the function parameter to const _Tp which would mean that *_first = __value; always uses a const lvalue, but maybe that's a bit too subtle. The cast makes it more explicit what's happening, especially the const_cast version. > + ++__first; > + ++__value; > + } > + return {std::move(__first), std::move(__value)}; > + } > + > + template<weakly_incrementable _Tp, output_range<const _Tp&> _Range> > + constexpr iota_result<borrowed_iterator_t<_Range>, _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 <algorithm> > +#include <testsuite_hooks.h> > +#include <testsuite_iterators.h> > + > +namespace ranges = std::ranges; > + > +void > +test01() > +{ > + int x[3] = {}; > + __gnu_test::test_output_range<int> 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 >
Am Mo., 14. Nov. 2022 um 11:09 Uhr schrieb Jonathan Wakely via Libstdc++ <libstdc++@gcc.gnu.org>: > > On Mon, 14 Nov 2022 at 04:52, Patrick Palka via Libstdc++ > <libstdc++@gcc.gnu.org> 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<typename _Out, typename _Tp> > > + struct out_value_result > > + { > > + [[no_unique_address]] _Out out; > > + [[no_unique_address]] _Tp value; > > + > > + template<typename _Out2, typename _Tp2> > > + requires convertible_to<const _Out&, _Out2> > > + && convertible_to<const _Tp&, _Tp2> > > + constexpr > > + operator out_value_result<_Out2, _Tp2>() const & > > + { return {out, value}; } > > + > > + template<typename _Out2, typename _Tp2> > > + requires convertible_to<_Out, _Out2> > > + && convertible_to<_Tp, _Tp2> > > + constexpr > > + operator out_value_result<_Out2, _Tp2>() && > > + { return {std::move(out), std::move(value)}; } > > + }; > > + > > + template<typename _Out, typename _Tp> > > + using iota_result = out_value_result<_Out, _Tp>; > > + > > + struct __iota_fn > > + { > > + template<input_or_output_iterator _Out, sentinel_for<_Out> _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<add_const_t<_Tp>&>(__value); > > Is this any different to const_cast<const _Tp&>(__value) ? I think it is. const_cast<const _Tp&> can potentially mean the removal of volatile, so I would always look with suspicion on const_cast<const _Tp&>, while static_cast is clearer. Alternatively, as_const could be used, which does add_const_t. - Daniel
On Mon, 14 Nov 2022 at 10:17, Daniel Krügler <daniel.kruegler@gmail.com> wrote: > > Am Mo., 14. Nov. 2022 um 11:09 Uhr schrieb Jonathan Wakely via > Libstdc++ <libstdc++@gcc.gnu.org>: > > > > On Mon, 14 Nov 2022 at 04:52, Patrick Palka via Libstdc++ > > <libstdc++@gcc.gnu.org> 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<typename _Out, typename _Tp> > > > + struct out_value_result > > > + { > > > + [[no_unique_address]] _Out out; > > > + [[no_unique_address]] _Tp value; > > > + > > > + template<typename _Out2, typename _Tp2> > > > + requires convertible_to<const _Out&, _Out2> > > > + && convertible_to<const _Tp&, _Tp2> > > > + constexpr > > > + operator out_value_result<_Out2, _Tp2>() const & > > > + { return {out, value}; } > > > + > > > + template<typename _Out2, typename _Tp2> > > > + requires convertible_to<_Out, _Out2> > > > + && convertible_to<_Tp, _Tp2> > > > + constexpr > > > + operator out_value_result<_Out2, _Tp2>() && > > > + { return {std::move(out), std::move(value)}; } > > > + }; > > > + > > > + template<typename _Out, typename _Tp> > > > + using iota_result = out_value_result<_Out, _Tp>; > > > + > > > + struct __iota_fn > > > + { > > > + template<input_or_output_iterator _Out, sentinel_for<_Out> _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<add_const_t<_Tp>&>(__value); > > > > Is this any different to const_cast<const _Tp&>(__value) ? > > I think it is. const_cast<const _Tp&> can potentially mean the removal > of volatile, True. > so I would always look with suspicion on const_cast<const > _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<const _Tp&>.
On Mon, 14 Nov 2022, Jonathan Wakely wrote: > On Mon, 14 Nov 2022 at 10:17, Daniel Krügler <daniel.kruegler@gmail.com> wrote: > > > > Am Mo., 14. Nov. 2022 um 11:09 Uhr schrieb Jonathan Wakely via > > Libstdc++ <libstdc++@gcc.gnu.org>: > > > > > > On Mon, 14 Nov 2022 at 04:52, Patrick Palka via Libstdc++ > > > <libstdc++@gcc.gnu.org> 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<typename _Out, typename _Tp> > > > > + struct out_value_result > > > > + { > > > > + [[no_unique_address]] _Out out; > > > > + [[no_unique_address]] _Tp value; > > > > + > > > > + template<typename _Out2, typename _Tp2> > > > > + requires convertible_to<const _Out&, _Out2> > > > > + && convertible_to<const _Tp&, _Tp2> > > > > + constexpr > > > > + operator out_value_result<_Out2, _Tp2>() const & > > > > + { return {out, value}; } > > > > + > > > > + template<typename _Out2, typename _Tp2> > > > > + requires convertible_to<_Out, _Out2> > > > > + && convertible_to<_Tp, _Tp2> > > > > + constexpr > > > > + operator out_value_result<_Out2, _Tp2>() && > > > > + { return {std::move(out), std::move(value)}; } > > > > + }; > > > > + > > > > + template<typename _Out, typename _Tp> > > > > + using iota_result = out_value_result<_Out, _Tp>; > > > > + > > > > + struct __iota_fn > > > > + { > > > > + template<input_or_output_iterator _Out, sentinel_for<_Out> _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<add_const_t<_Tp>&>(__value); > > > > > > Is this any different to const_cast<const _Tp&>(__value) ? > > > > I think it is. const_cast<const _Tp&> can potentially mean the removal > > of volatile, > > True. > > > so I would always look with suspicion on const_cast<const > > _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<const _Tp&>. 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<typename _Out, typename _Tp> + struct out_value_result + { + [[no_unique_address]] _Out out; + [[no_unique_address]] _Tp value; + + template<typename _Out2, typename _Tp2> + requires convertible_to<const _Out&, _Out2> + && convertible_to<const _Tp&, _Tp2> + constexpr + operator out_value_result<_Out2, _Tp2>() const & + { return {out, value}; } + + template<typename _Out2, typename _Tp2> + requires convertible_to<_Out, _Out2> + && convertible_to<_Tp, _Tp2> + constexpr + operator out_value_result<_Out2, _Tp2>() && + { return {std::move(out), std::move(value)}; } + }; + + template<typename _Out, typename _Tp> + using iota_result = out_value_result<_Out, _Tp>; + + struct __iota_fn + { + template<input_or_output_iterator _Out, sentinel_for<_Out> _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<const _Tp&>(__value); + ++__first; + ++__value; + } + return {std::move(__first), std::move(__value)}; + } + + template<weakly_incrementable _Tp, output_range<const _Tp&> _Range> + constexpr iota_result<borrowed_iterator_t<_Range>, _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 <algorithm> +#include <testsuite_hooks.h> +#include <testsuite_iterators.h> + +namespace ranges = std::ranges; + +void +test01() +{ + int x[3] = {}; + __gnu_test::test_output_range<int> 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(); +}
On Mon, 14 Nov 2022 at 15:11, Patrick Palka <ppalka@redhat.com> wrote: > > On Mon, 14 Nov 2022, Jonathan Wakely wrote: > > > On Mon, 14 Nov 2022 at 10:17, Daniel Krügler <daniel.kruegler@gmail.com> wrote: > > > > > > Am Mo., 14. Nov. 2022 um 11:09 Uhr schrieb Jonathan Wakely via > > > Libstdc++ <libstdc++@gcc.gnu.org>: > > > > > > > > On Mon, 14 Nov 2022 at 04:52, Patrick Palka via Libstdc++ > > > > <libstdc++@gcc.gnu.org> 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<typename _Out, typename _Tp> > > > > > + struct out_value_result > > > > > + { > > > > > + [[no_unique_address]] _Out out; > > > > > + [[no_unique_address]] _Tp value; > > > > > + > > > > > + template<typename _Out2, typename _Tp2> > > > > > + requires convertible_to<const _Out&, _Out2> > > > > > + && convertible_to<const _Tp&, _Tp2> > > > > > + constexpr > > > > > + operator out_value_result<_Out2, _Tp2>() const & > > > > > + { return {out, value}; } > > > > > + > > > > > + template<typename _Out2, typename _Tp2> > > > > > + requires convertible_to<_Out, _Out2> > > > > > + && convertible_to<_Tp, _Tp2> > > > > > + constexpr > > > > > + operator out_value_result<_Out2, _Tp2>() && > > > > > + { return {std::move(out), std::move(value)}; } > > > > > + }; > > > > > + > > > > > + template<typename _Out, typename _Tp> > > > > > + using iota_result = out_value_result<_Out, _Tp>; > > > > > + > > > > > + struct __iota_fn > > > > > + { > > > > > + template<input_or_output_iterator _Out, sentinel_for<_Out> _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<add_const_t<_Tp>&>(__value); > > > > > > > > Is this any different to const_cast<const _Tp&>(__value) ? > > > > > > I think it is. const_cast<const _Tp&> can potentially mean the removal > > > of volatile, > > > > True. > > > > > so I would always look with suspicion on const_cast<const > > > _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<const _Tp&>. > > Sounds good, like so? OK for trunk, thanks. > > -- >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<typename _Out, typename _Tp> > + struct out_value_result > + { > + [[no_unique_address]] _Out out; > + [[no_unique_address]] _Tp value; > + > + template<typename _Out2, typename _Tp2> > + requires convertible_to<const _Out&, _Out2> > + && convertible_to<const _Tp&, _Tp2> > + constexpr > + operator out_value_result<_Out2, _Tp2>() const & > + { return {out, value}; } > + > + template<typename _Out2, typename _Tp2> > + requires convertible_to<_Out, _Out2> > + && convertible_to<_Tp, _Tp2> > + constexpr > + operator out_value_result<_Out2, _Tp2>() && > + { return {std::move(out), std::move(value)}; } > + }; > + > + template<typename _Out, typename _Tp> > + using iota_result = out_value_result<_Out, _Tp>; > + > + struct __iota_fn > + { > + template<input_or_output_iterator _Out, sentinel_for<_Out> _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<const _Tp&>(__value); > + ++__first; > + ++__value; > + } > + return {std::move(__first), std::move(__value)}; > + } > + > + template<weakly_incrementable _Tp, output_range<const _Tp&> _Range> > + constexpr iota_result<borrowed_iterator_t<_Range>, _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 <algorithm> > +#include <testsuite_hooks.h> > +#include <testsuite_iterators.h> > + > +namespace ranges = std::ranges; > + > +void > +test01() > +{ > + int x[3] = {}; > + __gnu_test::test_output_range<int> 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
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<typename _Out, typename _Tp> + struct out_value_result + { + [[no_unique_address]] _Out out; + [[no_unique_address]] _Tp value; + + template<typename _Out2, typename _Tp2> + requires convertible_to<const _Out&, _Out2> + && convertible_to<const _Tp&, _Tp2> + constexpr + operator out_value_result<_Out2, _Tp2>() const & + { return {out, value}; } + + template<typename _Out2, typename _Tp2> + requires convertible_to<_Out, _Out2> + && convertible_to<_Tp, _Tp2> + constexpr + operator out_value_result<_Out2, _Tp2>() && + { return {std::move(out), std::move(value)}; } + }; + + template<typename _Out, typename _Tp> + using iota_result = out_value_result<_Out, _Tp>; + + struct __iota_fn + { + template<input_or_output_iterator _Out, sentinel_for<_Out> _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<add_const_t<_Tp>&>(__value); + ++__first; + ++__value; + } + return {std::move(__first), std::move(__value)}; + } + + template<weakly_incrementable _Tp, output_range<const _Tp&> _Range> + constexpr iota_result<borrowed_iterator_t<_Range>, _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 <algorithm> +#include <testsuite_hooks.h> +#include <testsuite_iterators.h> + +namespace ranges = std::ranges; + +void +test01() +{ + int x[3] = {}; + __gnu_test::test_output_range<int> 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(); +}