From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1888) id E42E23858C3A; Fri, 3 Feb 2023 15:54:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E42E23858C3A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1675439660; bh=KOo+ayTqWPcEKnuzH6nPFpB4Sp18m2fOBw2dHmLZmqw=; h=From:To:Subject:Date:From; b=RXCWLKmnKORwKstrmi5RVnrDGQUpprWVMxlrnxxjxhbptBk6YfFNYx4+FOCnJfLef yi7pXzBDlgIa67thW0Ch0YjAbsheD5AEEUlGa2z0u5PBA7I8JvnrcXWL3uj1coJzIy sOMkTIlVuBpQ6s+0gpfiGTRSztm1K6RlQ5egwG5M= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Patrick Palka To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r13-5688] libstdc++: Implement ranges::iota from P2440R1 X-Act-Checkin: gcc X-Git-Author: Patrick Palka X-Git-Refname: refs/heads/master X-Git-Oldrev: 6716822c541bfd5e4bb91a39c0cb2c85b70f83de X-Git-Newrev: 28752bcbbfb2efa1b57db725d7a3e352eabcc336 Message-Id: <20230203155420.E42E23858C3A@sourceware.org> Date: Fri, 3 Feb 2023 15:54:20 +0000 (GMT) List-Id: https://gcc.gnu.org/g:28752bcbbfb2efa1b57db725d7a3e352eabcc336 commit r13-5688-g28752bcbbfb2efa1b57db725d7a3e352eabcc336 Author: Patrick Palka Date: Fri Feb 3 10:52:15 2023 -0500 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. Diff: --- libstdc++-v3/include/bits/ranges_algo.h | 48 ++++++++++++++++++++++++++ libstdc++-v3/testsuite/25_algorithms/iota/1.cc | 29 ++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/libstdc++-v3/include/bits/ranges_algo.h b/libstdc++-v3/include/bits/ranges_algo.h index b2c99028916..8f9e1f8bcc3 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(); +}