From patchwork Tue Aug 30 17:13:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 845 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:ecc5:0:0:0:0:0 with SMTP id s5csp1901481wro; Tue, 30 Aug 2022 10:14:54 -0700 (PDT) X-Google-Smtp-Source: AA6agR51coyK9oiSUSxxFQg7Rr2peDfJZzvnE9QQXDXm2DyGQoFri+C78wPVC5KBI/xVMfdaICSc X-Received: by 2002:a17:907:7f02:b0:73d:dffa:57b3 with SMTP id qf2-20020a1709077f0200b0073ddffa57b3mr15263612ejc.19.1661879694353; Tue, 30 Aug 2022 10:14:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661879694; cv=none; d=google.com; s=arc-20160816; b=0NshcquYc1Yb7qHToA2BT8KIXkWs9raQbApd88CVPJw4OkRvn4qSLiM8DZtM4AUjlm GDSGe/VH/51syMrB0cIx2ESTU49pvc+iux7i3uG3yR5ucwBF64yEuQBzBnUyIFHVWv4n LHEZnd7+qGLkj0f6YRoyBuv0WT2HC7QyvZuffKYc2tFm5VqqAkUYSNldfcMO+KdutvVf u0spp8MBLOis5XgGB/pW2xTRSmg9ZmHvQ2rgiaS5ybEkLiwNz3raBWiw8g84L9Nw9n1h Lqw2+40LPaloVBfe8Lv4HRc9AAYgpP23qsgEEJqKQI2uD4n6z0MpYXLZ+x/0A4qSst/a egTg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=CsIaBRq/QLDou+AKhA6xuaJY8k9MTzjX+8a/OJcjiYU=; b=uJvdZBbPKPj7KQ6IBDPMuSM/OtNgztBsdOV29FuddoVYbGGlsgFfqRiqwi0yJ43bHn RrpxaopJaAEa+OJzV0R5RM/GxnuMyqgAEz0SIrL4XHfI9ADosKxtndjl4IxNNsdi2MlO v9HRtJQ+DGJ14AR52M1N3mhCgn7Mjg7SPz1ZcMdpObNaItIdBo4IX0FwoGkF8o3N5MH8 PF+BMDeVlf+jg1fEh2j1c0x2TzlTYDC0gm5iMgO1xJti+Q/MbAPdWXB8Z/JsXB80Fgem 4P1v50DSszg0/ConcA0pNbUOAzOAskhw9Bj9Vyr22KLor5uxXL/3lfx2Cgvcjnc84tiB RcdA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=iyQCmlvR; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id go19-20020a1709070d9300b007416e100f3dsi3147974ejc.986.2022.08.30.10.14.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Aug 2022 10:14:54 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=iyQCmlvR; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1CCA73AA800D for ; Tue, 30 Aug 2022 17:14:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1CCA73AA800D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1661879668; bh=CsIaBRq/QLDou+AKhA6xuaJY8k9MTzjX+8a/OJcjiYU=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=iyQCmlvRlirMwXv0056YxcNAHTu4ZUTznvwC0N+0z2MoU4NplFMQwMXDhZ3NtSbv/ Y+GQMLq6QGjcpbxevuIC2Sp2PDYWBgrGgFK3s52Enp0U6QnIW8Za9WkTQ+fCPr6qFa LbmQ/clRcPLe8TBfKlch2Y3qZBDcbDjNO6jJ54ZM= 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.133.124]) by sourceware.org (Postfix) with ESMTPS id 4BFF3385C401 for ; Tue, 30 Aug 2022 17:13:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 4BFF3385C401 Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-440-Zn9NwhtxNT284ixmPSS-Ew-1; Tue, 30 Aug 2022 13:13:40 -0400 X-MC-Unique: Zn9NwhtxNT284ixmPSS-Ew-1 Received: by mail-qk1-f197.google.com with SMTP id j13-20020a05620a288d00b006be7b2a758fso5803425qkp.1 for ; Tue, 30 Aug 2022 10:13:40 -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:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date; bh=CsIaBRq/QLDou+AKhA6xuaJY8k9MTzjX+8a/OJcjiYU=; b=I7l1RgVRSaHn/nTcRp4PS9NBm0GYZahKIUQgPSSPD7dOXttCopeEfero/l7DxnFBbu dHo6p/iM1GCjSWDSc7SiC9/mgR/dT+T5LxJnV3jrex56S6533V9f2ljyZ7fLdmZFve7E OvOrYY0wA1NOZHmAoslQvXPSK4DPTuu0kf4ekDfNjvLXTnguFDF5Nul7vFGMmR2hUoz3 QApubdEJqGP1faNiWqegYJF2TxvWuiJOy19smgKZVrp9gd9Ad9XtQ5ECx4VtkPyGe+Of Vh8U0bVuNaDoLqgKsll/cSp+JomoG7U1h88qgilcxm//D5trEo8D8CHdutgJCNmSJGEg 89eA== X-Gm-Message-State: ACgBeo0xtLL1MI3LAldibIUsThD/ulb6GrSH25EjbrcYFJH0qYiz9Vn+ kDfBKmxETim0lkHrRKrML+PA1yyRQ7/Szg3Uj/sWlKW2B5TG9fCM2nXyBCi6NDKIZOsuEDf+u4k SV3616ZjgxYGwbbyo9fTfl4WUm07EdqNGWhBqgHWm4nAkjN9GhHeL6gge7PnEHiZUv2s= X-Received: by 2002:a05:622a:1483:b0:343:7ba3:f60f with SMTP id t3-20020a05622a148300b003437ba3f60fmr15714442qtx.94.1661879619863; Tue, 30 Aug 2022 10:13:39 -0700 (PDT) X-Received: by 2002:a05:622a:1483:b0:343:7ba3:f60f with SMTP id t3-20020a05622a148300b003437ba3f60fmr15714402qtx.94.1661879619422; Tue, 30 Aug 2022 10:13:39 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id k17-20020a05620a415100b006bad7a2964fsm8455222qko.78.2022.08.30.10.13.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Aug 2022 10:13:38 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH 1/2] libstdc++: Implement ranges::adjacent_view from P2321R2 Date: Tue, 30 Aug 2022 13:13:34 -0400 Message-Id: <20220830171335.54110-1-ppalka@redhat.com> X-Mailer: git-send-email 2.37.2.490.g6c8e4ee870 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham 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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1742607162774560888?= X-GMAIL-MSGID: =?utf-8?q?1742607162774560888?= Tested on x86_64-pc-linux-gnu, does this look OK for trunk? libstdc++-v3/ChangeLog: * include/std/ranges (adjacent_view): Define. (enable_borrowed_range): Define. (__detail::__repeated_tuple): Define. (adjacent_view::_Iterator): Define. (adjacent_view::_Sentinel): Define. (views::__detail::__can_adjacent_view): Define. (views::_Adjacent): Define. (views::adjacent): Define. (views::pairwise): Define. * testsuite/std/ranges/adaptors/adjacent/1.cc: New test. --- libstdc++-v3/include/std/ranges | 359 ++++++++++++++++++ .../std/ranges/adaptors/adjacent/1.cc | 110 ++++++ 2 files changed, 469 insertions(+) create mode 100644 libstdc++-v3/testsuite/std/ranges/adaptors/adjacent/1.cc diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges index 6e2e561ed12..4fb879a088c 100644 --- a/libstdc++-v3/include/std/ranges +++ b/libstdc++-v3/include/std/ranges @@ -5081,6 +5081,365 @@ namespace views::__adaptor inline constexpr _ZipTransform zip_transform; } + + template + requires view<_Vp> && (_Nm > 0) + class adjacent_view : public view_interface> + { + _Vp _M_base = _Vp(); + + template class _Iterator; + template class _Sentinel; + + struct __as_sentinel + { }; + + public: + adjacent_view() requires default_initializable<_Vp> = default; + + constexpr explicit + adjacent_view(_Vp __base) + : _M_base(std::move(__base)) + { } + + constexpr auto + begin() requires (!__detail::__simple_view<_Vp>) + { return _Iterator(ranges::begin(_M_base), ranges::end(_M_base)); } + + constexpr auto + begin() const requires range + { return _Iterator(ranges::begin(_M_base), ranges::end(_M_base)); } + + constexpr auto + end() requires (!__detail::__simple_view<_Vp>) + { + if constexpr (common_range<_Vp>) + return _Iterator(__as_sentinel{}, ranges::begin(_M_base), ranges::end(_M_base)); + else + return _Sentinel(ranges::end(_M_base)); + } + + constexpr auto + end() const requires range + { + if constexpr (common_range) + return _Iterator(__as_sentinel{}, ranges::begin(_M_base), ranges::end(_M_base)); + else + return _Sentinel(ranges::end(_M_base)); + } + + constexpr auto + size() requires sized_range<_Vp> + { + using _ST = decltype(ranges::size(_M_base)); + using _CT = common_type_t<_ST, size_t>; + auto __sz = static_cast<_CT>(ranges::size(_M_base)); + __sz -= std::min<_CT>(__sz, _Nm - 1); + return static_cast<_ST>(__sz); + } + + constexpr auto + size() const requires sized_range + { + using _ST = decltype(ranges::size(_M_base)); + using _CT = common_type_t<_ST, size_t>; + auto __sz = static_cast<_CT>(ranges::size(_M_base)); + __sz -= std::min<_CT>(__sz, _Nm - 1); + return static_cast<_ST>(__sz); + } + }; + + template + inline constexpr bool enable_borrowed_range> + = enable_borrowed_range<_Vp>; + + namespace __detail + { + // Yields tuple<_Tp, ..., _Tp> with _Nm elements. + template + using __repeated_tuple = decltype(std::tuple_cat(std::declval>())); + } + + template + requires view<_Vp> && (_Nm > 0) + template + class adjacent_view<_Vp, _Nm>::_Iterator + { + using _Base = __detail::__maybe_const_t<_Const, _Vp>; + array, _Nm> _M_current = array, _Nm>(); + + constexpr + _Iterator(iterator_t<_Base> __first, sentinel_t<_Base> __last) + { + for (auto& __i : _M_current) + { + __i = __first; + ranges::advance(__first, 1, __last); + } + } + + constexpr + _Iterator(__as_sentinel, iterator_t<_Base> __first, iterator_t<_Base> __last) + { + if constexpr (!bidirectional_range<_Base>) + for (auto& __it : _M_current) + __it = __last; + else + for (ssize_t __i = _Nm-1; __i >= 0; --__i) + { + _M_current[__i] = __last; + ranges::advance(__last, -1, __first); + } + } + + static auto + _S_iter_concept() + { + if constexpr (random_access_range<_Base>) + return random_access_iterator_tag{}; + else if constexpr (bidirectional_range<_Base>) + return bidirectional_iterator_tag{}; + else + return forward_iterator_tag{}; + } + + friend class adjacent_view; + + public: + using iterator_category = input_iterator_tag; + using iterator_concept = decltype(_S_iter_concept()); + using value_type = conditional_t<_Nm == 2, + pair, range_value_t<_Base>>, + __detail::__repeated_tuple, _Nm>>; + using difference_type = range_difference_t<_Base>; + + _Iterator() = default; + + constexpr + _Iterator(_Iterator __i) + requires _Const && convertible_to, iterator_t<_Base>> + { + for (size_t __j = 0; __j < _Nm; ++__j) + _M_current[__j] = std::move(__i[__j]); + } + + constexpr auto + operator*() const + { + auto __f = [](auto& __i) -> decltype(auto) { return *__i; }; + return __detail::__tuple_transform(__f, _M_current); + } + + constexpr _Iterator& + operator++() + { + for (auto& __i : _M_current) + ++__i; + return *this; + } + + constexpr _Iterator + operator++(int) + { + auto __tmp = *this; + ++*this; + return __tmp; + } + + constexpr _Iterator& + operator--() requires bidirectional_range<_Base> + { + for (auto& __i : _M_current) + --__i; + return *this; + } + + constexpr _Iterator + operator--(int) requires bidirectional_range<_Base> + { + auto __tmp = *this; + --*this; + return __tmp; + } + + constexpr _Iterator& + operator+=(difference_type __x) + requires random_access_range<_Base> + { + for (auto& __i : _M_current) + __i += __x; + return *this; + } + + constexpr _Iterator& + operator-=(difference_type __x) + requires random_access_range<_Base> + { + for (auto& __i : _M_current) + __i -= __x; + return *this; + } + + constexpr auto + operator[](difference_type __n) const + requires random_access_range<_Base> + { + auto __f = [&](auto& __i) -> decltype(auto) { return __i[__n]; }; + return __detail::__tuple_transform(__f, _M_current); + } + + friend constexpr bool + operator==(const _Iterator& __x, const _Iterator& __y) + { return __x._M_current.back() == __y._M_current.back(); } + + friend constexpr bool + operator<(const _Iterator& __x, const _Iterator& __y) + requires random_access_range<_Base> + { return __x._M_current.back() < __y._M_current.back(); } + + friend constexpr bool + operator>(const _Iterator& __x, const _Iterator& __y) + requires random_access_range<_Base> + { return __y < __x; } + + friend constexpr bool + operator<=(const _Iterator& __x, const _Iterator& __y) + requires random_access_range<_Base> + { return !(__y < __x); } + + friend constexpr bool + operator>=(const _Iterator& __x, const _Iterator& __y) + requires random_access_range<_Base> + { return !(__x < __y); } + + friend constexpr auto + operator<=>(const _Iterator& __x, const _Iterator& __y) + requires random_access_range<_Base> + && three_way_comparable> + { return __x._M_current.back() <=> __y._M_current.back(); } + + friend constexpr _Iterator + operator+(const _Iterator& __i, difference_type __n) + requires random_access_range<_Base> + { + auto __r = __i; + __r += __n; + return __r; + } + + friend constexpr _Iterator + operator+(difference_type __n, const _Iterator& __i) + requires random_access_range<_Base> + { + auto __r = __i; + __r += __n; + return __r; + } + + friend constexpr _Iterator + operator-(const _Iterator& __i, difference_type __n) + requires random_access_range<_Base> + { + auto __r = __i; + __r -= __n; + return __r; + } + + friend constexpr difference_type + operator-(const _Iterator& __x, const _Iterator& __y) + requires sized_sentinel_for, iterator_t<_Base>> + { return __x._M_current.back() - __y._M_current.back(); } + + friend constexpr auto + iter_move(const _Iterator& __i) + { return __detail::__tuple_transform(ranges::iter_move, __i._M_current); } + + friend constexpr void + iter_swap(const _Iterator& __l, const _Iterator& __r) + requires indirectly_swappable> + { + for (size_t __i = 0; __i < _Nm; __i++) + ranges::iter_swap(__l._M_current[__i], __r._M_current[__i]); + } + }; + + template + requires view<_Vp> && (_Nm > 0) + template + class adjacent_view<_Vp, _Nm>::_Sentinel + { + using _Base = __detail::__maybe_const_t<_Const, _Vp>; + + sentinel_t<_Base> _M_end = sentinel_t<_Base>(); + + constexpr explicit + _Sentinel(sentinel_t<_Base> __end) + : _M_end(__end) + { } + + friend class adjacent_view; + + public: + _Sentinel() = default; + + constexpr + _Sentinel(_Sentinel __i) + requires _Const && convertible_to, sentinel_t<_Base>> + : _M_end(std::move(__i._M_end)) + { } + + template + requires sentinel_for, + iterator_t<__detail::__maybe_const_t<_OtherConst, _Vp>>> + friend constexpr bool + operator==(const _Iterator<_OtherConst>& __x, const _Sentinel& __y) + { return __x._M_current.back() == __y._M_end; } + + template + requires sized_sentinel_for, + iterator_t<__detail::__maybe_const_t<_OtherConst, _Vp>>> + friend constexpr range_difference_t<__detail::__maybe_const_t<_OtherConst, _Vp>> + operator-(const _Iterator<_OtherConst>& __x, const _Sentinel& __y) + { return __x._M_current.back() - __y._M_end; } + + template + requires sized_sentinel_for, + iterator_t<__detail::__maybe_const_t<_OtherConst, _Vp>>> + friend constexpr range_difference_t<__detail::__maybe_const_t<_OtherConst, _Vp>> + operator-(const _Sentinel& __y, const _Iterator<_OtherConst>& __x) + { return __y._M_end - __x._M_current.back(); } + }; + + namespace views + { + namespace __detail + { + template + concept __can_adjacent_view + = requires { adjacent_view, _Nm>(std::declval<_Range>()); }; + } + + template + struct _Adjacent : __adaptor::_RangeAdaptorClosure + { + template + requires (_Nm == 0) || __detail::__can_adjacent_view<_Nm, _Range> + [[nodiscard]] + constexpr auto + operator()(_Range&& __r) const + { + if constexpr (_Nm == 0) + return views::empty>; + else + return adjacent_view, _Nm>(std::forward<_Range>(__r)); + } + }; + + template + inline constexpr _Adjacent<_Nm> adjacent; + + inline constexpr auto pairwise = adjacent<2>; + } #endif // C++23 } // namespace ranges diff --git a/libstdc++-v3/testsuite/std/ranges/adaptors/adjacent/1.cc b/libstdc++-v3/testsuite/std/ranges/adaptors/adjacent/1.cc new file mode 100644 index 00000000000..9829f79364f --- /dev/null +++ b/libstdc++-v3/testsuite/std/ranges/adaptors/adjacent/1.cc @@ -0,0 +1,110 @@ +// { dg-options "-std=gnu++23" } +// { dg-do run { target c++23 } } + +#include +#include +#include +#include +#include + +namespace ranges = std::ranges; +namespace views = std::views; + +constexpr bool +test01() +{ + static_assert(ranges::empty(std::array{1, 2, 3} | views::adjacent<0>)); + + auto v1 = std::array{1, 2} | views::adjacent<1>; + const auto i0 = v1.begin(), i1 = v1.begin() + 1; + VERIFY( i0 + 1 - 1 == i0 ); + VERIFY( i0 < i1 ); + VERIFY( i1 < v1.end() ); + VERIFY( i1 - i0 == 1 ); + VERIFY( i0 - i1 == -1 ); + VERIFY( v1.end() - i1 == 1 ); + VERIFY( i1 - v1.end() == -1 ); + ranges::iter_swap(i0, i1); + VERIFY( ranges::equal(std::move(v1) | views::keys, (int[]){2, 1}) ); + + int x[] = {1, 2, 3, 4}; + auto v2 = x | views::pairwise; + auto i2 = v2.begin(); + i2 += 2; + i2 -= -1; + VERIFY( i2 == v2.end() ); + VERIFY( ranges::size(v2) == 3 ); + VERIFY( ranges::size(std::as_const(v2)) == 3 ); + VERIFY( ranges::equal(v2 | views::keys, (int[]){1, 2, 3}) ); + VERIFY( ranges::equal(v2 | views::values, (int[]){2, 3, 4}) ); + + int y[] = {1, 2, 3, 4, 5}; + const auto v3 = y | views::adjacent<3>; + VERIFY( ranges::size(v3) == 3 ); + for (unsigned i = 0; i < ranges::size(x); i++) + { + VERIFY( &std::get<0>(v3[i]) == &y[i] + 0 ); + VERIFY( &std::get<1>(v3[i]) == &y[i] + 1 ); + VERIFY( &std::get<2>(v3[i]) == &y[i] + 2 ); + } + + const auto v5 = y | views::adjacent<5>; + VERIFY( ranges::equal(v5, views::single(std::make_tuple(1, 2, 3, 4, 5))) ); + + const auto v6 = y | views::adjacent<6>; + VERIFY( ranges::empty(v6) ); + + const auto v0 = y | views::adjacent<0>; + VERIFY( ranges::empty(v0) ); + + return true; +} + +constexpr bool +test02() +{ + using __gnu_test::test_input_range; + using __gnu_test::test_forward_range; + using __gnu_test::test_random_access_range; + + using ty1 = ranges::adjacent_view>, 2>; + static_assert(ranges::forward_range); + static_assert(!ranges::bidirectional_range); + static_assert(!ranges::sized_range); + + using ty2 = ranges::adjacent_view>, 3>; + static_assert(ranges::random_access_range); + static_assert(ranges::sized_range); + + return true; +} + +constexpr bool +test03() +{ + auto v = views::iota(0, 4) | views::filter([](auto) { return true; }) | views::pairwise; + using ty = decltype(v); + static_assert(ranges::forward_range); + static_assert(ranges::common_range); + static_assert(!ranges::sized_range); + VERIFY( v.begin() == v.begin() ); + VERIFY( v.begin() != v.end() ); + VERIFY( ranges::next(v.begin(), 3) == v.end() ); + auto it = v.begin(); + ++it; + it++; + VERIFY( ranges::next(it) == v.end() ); + it--; + --it; + VERIFY( it == v.begin() ); + + return true; +} + +int +main() +{ + static_assert(test01()); + static_assert(test02()); + static_assert(test03()); +} From patchwork Tue Aug 30 17:13:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 847 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:ecc5:0:0:0:0:0 with SMTP id s5csp1902059wro; Tue, 30 Aug 2022 10:16:59 -0700 (PDT) X-Google-Smtp-Source: AA6agR4UZBb0zJMougcI3beUCHYCFuj4RNGuoQpiL2BVy3a4UmR0ffjWjALNCmbbGorcEuQHu3MV X-Received: by 2002:a17:907:7254:b0:731:61c6:ecf9 with SMTP id ds20-20020a170907725400b0073161c6ecf9mr18047603ejc.101.1661879819828; Tue, 30 Aug 2022 10:16:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661879819; cv=none; d=google.com; s=arc-20160816; b=Hxnx//4pwavAoctke5SVgSREGLJF2wgrxq3jotjKlEiw2DAjGLzkSWSs0QD2OLo9jQ 3Zaq2a9xeeA3OhUzx8vpUdn+RMwEQq5CgWdLPFLEWeZTx55X/o/b25iyBIyQmxODJ3OA JhpRY46K53y0uE1lOUHB73UutX3FmRXsW9Z7AjXV7odov+Dg7ntrJlGLPRmYuyE7rsmq JkB5RYumE3IsX1yOm98TOjaD50uPldUaAje4b43A/oJBoLhrXgr0D6lDg3OO40MCeViE r0ut+nyT6nqzhYnPa06E7ZkIuOIQITs4x2OHAZH9qoSC5uYVH38lxEZKrXlD7U18mU4N +miA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:dmarc-filter:delivered-to:dkim-signature :dkim-filter; bh=Ur7u7fPOOIrJJv1MswvdLrvIynrt2iCq1ulo4h1GNrk=; b=ew7Jksj8tYOl0jS6tBPSbKrjLK1//01PRNJ/CKE0BpAx9F39Hbwr3OCE46DZeYR6SP tpAgvQLT490xh8oNCSBjhvI4GWjJL+9udxMEbWKPqPeF5KTCvVF8JNFNMtAO3BNCFmJk ZgMb+QDk7q2y9CJaiE+uBJLWHHCx7RFEwLSLRKTGZ0DqXM8DXeNEq5PNkdaTOEkZ2f/W 6EVCG8lYqeGTX/LzuzFBGIM+FGMWozzmB/zwgfAV97sU9JQzNvhGyQBpwOEVZpxamQCM SEMbmnnP1BYKdbkkC6vVfQ2pDWTJb6Sp+urZv9IN73fud7J6TCg9+CAb9GBXLx2Bf2MJ gsfw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=gy4yOQjw; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id dd16-20020a1709069b9000b0073d6c93e4d1si7104940ejc.942.2022.08.30.10.16.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Aug 2022 10:16:59 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=gy4yOQjw; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 439863AA88FB for ; Tue, 30 Aug 2022 17:16:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 439863AA88FB DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1661879767; bh=Ur7u7fPOOIrJJv1MswvdLrvIynrt2iCq1ulo4h1GNrk=; 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=gy4yOQjwe0N63HZW0X4d+AkjZgkt8pg9SencfnYjG2wXDCyJKdlsOs7QRlLbsScfD T66nYpkDkuaHFx0djdDgCbR3TdTziihjPovIiHpUgxai0iI76JVFmgGZWOTMljLkaE 1bHg2AiHQ0sSQUg6KeW5gj8nDzoBAxMQOPsXLZOo= 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.133.124]) by sourceware.org (Postfix) with ESMTPS id 302013959C5D for ; Tue, 30 Aug 2022 17:13:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 302013959C5D Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-360-4GEMZzARMlOURAipeTXRmw-1; Tue, 30 Aug 2022 13:13:42 -0400 X-MC-Unique: 4GEMZzARMlOURAipeTXRmw-1 Received: by mail-qk1-f199.google.com with SMTP id w22-20020a05620a445600b006bb7f43d1cfso9703428qkp.16 for ; Tue, 30 Aug 2022 10:13:42 -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; bh=Ur7u7fPOOIrJJv1MswvdLrvIynrt2iCq1ulo4h1GNrk=; b=xztxg6jwVXzoJ7ojeZqfVBvOYQM9lGtk9Q9mGd8jxCN3H2yGCMr0wwSHxV/EAYrf/g YorQksTRhvOGpilOtprpCGTGRFutyyiHPVJ0Kb6GoTiTCmfm9k6hQdSFKrN4lwxMPqi8 peaZQ+JPIe0rAaiR57LXageeSe+epWHPqsQpNj3W/o2Wi2yjKfeurzkUcuqfS0JWftDF TUm/BJM7eK5dR8Ws6hF6QTRjqHBmsnTPivsogyMueZyJtZv9VwPjrXDRmXJyn8Eazx5i LFVrDagZJHfWUv5dje4jzqj36aShfFsK3HOu/qWfe/pPnoLhnvGiL5pH+6vcwWWbDO6c lFGw== X-Gm-Message-State: ACgBeo0nBrzP0Mhawe9t2HJkqzszJvAHxVPhRTG9Wm7vJeeC89x2hADR haxLT17UPleCRJNq12PRUKRjEZlpqU5gIwWM/y/YvX6OweqCI+Mb2yNeVi/IX73O2FRD+OMRzSD +EnFyEuf1MMVbma5sMhA+qwa7TOnh5WX1tMT+q8DWWjKOvWCAnV+026sW6eC7qMJrVBo= X-Received: by 2002:ac8:59c3:0:b0:343:6528:db29 with SMTP id f3-20020ac859c3000000b003436528db29mr15537976qtf.575.1661879621314; Tue, 30 Aug 2022 10:13:41 -0700 (PDT) X-Received: by 2002:ac8:59c3:0:b0:343:6528:db29 with SMTP id f3-20020ac859c3000000b003436528db29mr15537935qtf.575.1661879620849; Tue, 30 Aug 2022 10:13:40 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id k17-20020a05620a415100b006bad7a2964fsm8455222qko.78.2022.08.30.10.13.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Aug 2022 10:13:40 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH 2/2] libstdc++: Implement ranges::adjacent_transform_view from P2321R2 Date: Tue, 30 Aug 2022 13:13:35 -0400 Message-Id: <20220830171335.54110-2-ppalka@redhat.com> X-Mailer: git-send-email 2.37.2.490.g6c8e4ee870 In-Reply-To: <20220830171335.54110-1-ppalka@redhat.com> References: <20220830171335.54110-1-ppalka@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1742607294222854610?= X-GMAIL-MSGID: =?utf-8?q?1742607294222854610?= Tested on x86_64-pc-linux-gnu, does this look OK for trunk? libstdc++-v3/ChangeLog: * include/std/ranges (__detail::__unarize): Define. (adjacent_view::_Iterator): Befriend adjacent_transform_view. (adjacent_transform_view): Define. (adjacent_transform_view::_Iterator): Define. (adjacent_transform_view::_Sentinel): Define. (views::__detail::__can_adjacent_transform_view): Define. (views::_AdjacentTransform): Define. (views::adjacent_transform): Define. (views::pairwise): Define. * testsuite/std/ranges/adaptors/adjacent_transform/1.cc: New test. --- libstdc++-v3/include/std/ranges | 342 ++++++++++++++++++ .../ranges/adaptors/adjacent_transform/1.cc | 106 ++++++ 2 files changed, 448 insertions(+) create mode 100644 libstdc++-v3/testsuite/std/ranges/adaptors/adjacent_transform/1.cc diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges index 4fb879a088c..3a7f0545030 100644 --- a/libstdc++-v3/include/std/ranges +++ b/libstdc++-v3/include/std/ranges @@ -5158,6 +5158,20 @@ namespace views::__adaptor // Yields tuple<_Tp, ..., _Tp> with _Nm elements. template using __repeated_tuple = decltype(std::tuple_cat(std::declval>())); + + // For a functor F that takes N arguments, the expression declval<__unarize>(x) + // is equivalent to declval(x, ..., x). + template + struct __unarize + { + template + static invoke_result_t<_Fp, _Ts...> + __tuple_apply(const tuple<_Ts...>&); // not defined + + template + decltype(__tuple_apply(std::declval<__repeated_tuple<_Tp, _Nm>>())) + operator()(_Tp&&); // not defined + }; } template @@ -5205,6 +5219,13 @@ namespace views::__adaptor friend class adjacent_view; + template + requires view<_Wp> && (_Mm > 0) && is_object_v<_Fp> + && regular_invocable<__detail::__unarize<_Fp&, _Mm>, range_reference_t<_Wp>> + && std::__detail::__can_reference, + range_reference_t<_Wp>>> + friend class adjacent_transform_view; + public: using iterator_category = input_iterator_tag; using iterator_concept = decltype(_S_iter_concept()); @@ -5440,6 +5461,327 @@ namespace views::__adaptor inline constexpr auto pairwise = adjacent<2>; } + + template + requires view<_Vp> && (_Nm > 0) && is_object_v<_Fp> + && regular_invocable<__detail::__unarize<_Fp&, _Nm>, range_reference_t<_Vp>> + && std::__detail::__can_reference, + range_reference_t<_Vp>>> + class adjacent_transform_view : public view_interface> + { + [[no_unique_address]] __detail::__box<_Fp> _M_fun; + adjacent_view<_Vp, _Nm> _M_inner; + + using _InnerView = adjacent_view<_Vp, _Nm>; + + template + using _InnerIter = iterator_t<__detail::__maybe_const_t<_Const, _InnerView>>; + + template + using _InnerSent = sentinel_t<__detail::__maybe_const_t<_Const, _InnerView>>; + + template class _Iterator; + template class _Sentinel; + + public: + adjacent_transform_view() = default; + + constexpr explicit + adjacent_transform_view(_Vp __base, _Fp __fun) + : _M_fun(std::move(__fun)), _M_inner(std::move(__base)) + { } + + constexpr auto + begin() + { return _Iterator(*this, _M_inner.begin()); } + + constexpr auto + begin() const + requires range + && regular_invocable<__detail::__unarize, + range_reference_t> + { return _Iterator(*this, _M_inner.begin()); } + + constexpr auto + end() + { + if constexpr (common_range<_InnerView>) + return _Iterator(*this, _M_inner.end()); + else + return _Sentinel(_M_inner.end()); + } + + constexpr auto + end() const + requires range + && regular_invocable<__detail::__unarize, + range_reference_t> + { + if constexpr (common_range) + return _Iterator(*this, _M_inner.end()); + else + return _Sentinel(_M_inner.end()); + } + + constexpr auto + size() requires sized_range<_InnerView> + { return _M_inner.size(); } + + constexpr auto + size() const requires sized_range + { return _M_inner.size(); } + }; + + template + requires view<_Vp> && (_Nm > 0) && is_object_v<_Fp> + && regular_invocable<__detail::__unarize<_Fp&, _Nm>, range_reference_t<_Vp>> + && std::__detail::__can_reference, + range_reference_t<_Vp>>> + template + class adjacent_transform_view<_Vp, _Fp, _Nm>::_Iterator + { + using _Parent = __detail::__maybe_const_t<_Const, adjacent_transform_view>; + using _Base = __detail::__maybe_const_t<_Const, _Vp>; + + _Parent* _M_parent = nullptr; + _InnerIter<_Const> _M_inner; + + constexpr + _Iterator(_Parent& __parent, _InnerIter<_Const> __inner) + : _M_parent(std::__addressof(__parent)), _M_inner(std::move(__inner)) + { } + + static auto + _S_iter_cat() + { + using __detail::__maybe_const_t; + using __detail::__unarize; + using _Res = invoke_result_t<__unarize<__maybe_const_t<_Const, _Fp>&, _Nm>, + range_reference_t<_Base>>; + using _Cat = iterator_traits>::iterator_category; + if constexpr (!is_lvalue_reference_v<_Res>) + return input_iterator_tag{}; + else if constexpr (derived_from<_Cat, random_access_iterator_tag>) + return random_access_iterator_tag{}; + else if constexpr (derived_from<_Cat, bidirectional_iterator_tag>) + return bidirectional_iterator_tag{}; + else if constexpr (derived_from<_Cat, forward_iterator_tag>) + return forward_iterator_tag{}; + else + return input_iterator_tag{}; + } + + friend class adjacent_transform_view; + + public: + using iterator_category = decltype(_S_iter_cat()); + using iterator_concept = typename _InnerIter<_Const>::iterator_concept; + using value_type + = remove_cvref_t&, _Nm>, + range_reference_t<_Base>>>; + using difference_type = range_difference_t<_Base>; + + _Iterator() = default; + + constexpr + _Iterator(_Iterator __i) + requires _Const && convertible_to<_InnerIter, _InnerIter<_Const>> + : _M_parent(__i._M_parent), _M_inner(std::move(__i._M_inner)) + { } + + constexpr decltype(auto) + operator*() const + { + return std::apply([&](const auto&... __iters) -> decltype(auto) { + return std::__invoke(*_M_parent->_M_fun, *__iters...); + }, _M_inner._M_current); + } + + constexpr _Iterator& + operator++() + { + ++_M_inner; + return *this; + } + + constexpr _Iterator + operator++(int) + { + auto __tmp = *this; + ++*this; + return __tmp; + } + + constexpr _Iterator& + operator--() requires bidirectional_range<_Base> + { + --_M_inner; + return *this; + } + + constexpr _Iterator + operator--(int) requires bidirectional_range<_Base> + { + auto __tmp = *this; + --*this; + return __tmp; + } + + constexpr _Iterator& + operator+=(difference_type __x) requires random_access_range<_Base> + { + _M_inner += __x; + return *this; + } + + constexpr _Iterator& + operator-=(difference_type __x) requires random_access_range<_Base> + { + _M_inner -= __x; + return *this; + } + + constexpr decltype(auto) + operator[](difference_type __n) const requires random_access_range<_Base> + { + return std::apply([&](const auto&... __iters) -> decltype(auto) { + return std::__invoke(*_M_parent->_M_fun, __iters[__n]...); + }, _M_inner._M_current); + } + + friend constexpr bool + operator==(const _Iterator& __x, const _Iterator& __y) + { return __x._M_inner == __y._M_inner; } + + friend constexpr bool + operator<(const _Iterator& __x, const _Iterator& __y) + requires random_access_range<_Base> + { return __x._M_inner < __y._M_inner; } + + friend constexpr bool + operator>(const _Iterator& __x, const _Iterator& __y) + requires random_access_range<_Base> + { return __x._M_inner > __y._M_inner; } + + friend constexpr bool + operator<=(const _Iterator& __x, const _Iterator& __y) + requires random_access_range<_Base> + { return __x._M_inner <= __y._M_inner; } + + friend constexpr bool + operator>=(const _Iterator& __x, const _Iterator& __y) + requires random_access_range<_Base> + { return __x._M_inner >= __y._M_inner; } + + friend constexpr auto + operator<=>(const _Iterator& __x, const _Iterator& __y) + requires random_access_range<_Base> && + three_way_comparable<_InnerIter<_Const>> + { return __x._M_inner <=> __y._M_inner; } + + friend constexpr _Iterator + operator+(const _Iterator& __i, difference_type __n) + requires random_access_range<_Base> + { return _Iterator(*__i._M_parent, __i._M_inner + __n); } + + friend constexpr _Iterator + operator+(difference_type __n, const _Iterator& __i) + requires random_access_range<_Base> + { return _Iterator(*__i._M_parent, __i._M_inner + __n); } + + friend constexpr _Iterator + operator-(const _Iterator& __i, difference_type __n) + requires random_access_range<_Base> + { return _Iterator(*__i._M_parent, __i._M_inner - __n); } + + friend constexpr difference_type + operator-(const _Iterator& __x, const _Iterator& __y) + requires sized_sentinel_for<_InnerIter<_Const>, _InnerIter<_Const>> + { return __x._M_inner - __y._M_inner; } + }; + + template + requires view<_Vp> && (_Nm > 0) && is_object_v<_Fp> + && regular_invocable<__detail::__unarize<_Fp&, _Nm>, range_reference_t<_Vp>> + && std::__detail::__can_reference, + range_reference_t<_Vp>>> + template + class adjacent_transform_view<_Vp, _Fp, _Nm>::_Sentinel + { + _InnerSent<_Const> _M_inner; + + constexpr explicit + _Sentinel(_InnerSent<_Const> __inner) + : _M_inner(__inner) + { } + + friend class adjacent_transform_view; + + public: + _Sentinel() = default; + + constexpr + _Sentinel(_Sentinel __i) + requires _Const && convertible_to<_InnerSent, _InnerSent<_Const>> + : _M_inner(std::move(__i._M_inner)) + { } + + template + requires sentinel_for<_InnerSent<_Const>, _InnerIter<_OtherConst>> + friend constexpr bool + operator==(const _Iterator<_OtherConst>& __x, const _Sentinel& __y) + { return __x._M_inner == __y._M_inner; } + + template + requires sized_sentinel_for<_InnerSent<_Const>, _InnerIter<_OtherConst>> + friend constexpr range_difference_t<__detail::__maybe_const_t<_OtherConst, _InnerView>> + operator-(const _Iterator<_OtherConst>& __x, const _Sentinel& __y) + { return __x._M_inner - __y._M_inner; } + + template + requires sized_sentinel_for<_InnerSent<_Const>, _InnerIter<_OtherConst>> + friend constexpr range_difference_t<__detail::__maybe_const_t<_OtherConst, _InnerView>> + operator-(const _Sentinel& __x, const _Iterator<_OtherConst>& __y) + { return __x._M_inner - __y._M_inner; } + }; + + namespace views + { + namespace __detail + { + template + concept __can_adjacent_transform_view + = requires { adjacent_transform_view, decay_t<_Fp>, _Nm> + (std::declval<_Range>(), std::declval<_Fp>()); }; + } + + template + struct _AdjacentTransform : __adaptor::_RangeAdaptor<_AdjacentTransform<_Nm>> + { + template + requires (_Nm == 0) || __detail::__can_adjacent_transform_view<_Nm, _Range, _Fp> + [[nodiscard]] + constexpr auto + operator()(_Range&& __r, _Fp&& __f) const + { + if constexpr (_Nm == 0) + return views::empty>; + else + return adjacent_transform_view, decay_t<_Fp>, _Nm> + (std::forward<_Range>(__r), std::forward<_Fp>(__f)); + } + + using __adaptor::_RangeAdaptor<_AdjacentTransform>::operator(); + static constexpr int _S_arity = 2; + static constexpr bool _S_has_simple_extra_args = true; + }; + + template + inline constexpr _AdjacentTransform<_Nm> adjacent_transform; + + inline constexpr auto pairwise_transform = adjacent_transform<2>; + } #endif // C++23 } // namespace ranges diff --git a/libstdc++-v3/testsuite/std/ranges/adaptors/adjacent_transform/1.cc b/libstdc++-v3/testsuite/std/ranges/adaptors/adjacent_transform/1.cc new file mode 100644 index 00000000000..07f20b702dd --- /dev/null +++ b/libstdc++-v3/testsuite/std/ranges/adaptors/adjacent_transform/1.cc @@ -0,0 +1,106 @@ +// { dg-options "-std=gnu++23" } +// { dg-do run { target c++23 } } + +#include +#include +#include +#include +#include + +namespace ranges = std::ranges; +namespace views = std::views; + +constexpr bool +test01() +{ + auto v1 = std::array{1, 2, 3} | views::adjacent_transform<1>(std::identity{}); + VERIFY( ranges::equal(v1, (int[]){1, 2, 3}) ); + const auto i0 = v1.begin(), i1 = v1.begin() + 1; + VERIFY( i0 + 1 - 1 == i0 ); + VERIFY( i0 < i1 ); + VERIFY( i1 < v1.end() ); + VERIFY( i1 - i0 == 1 ); + VERIFY( i0 - i1 == -1 ); + VERIFY( v1.end() - i1 == 2 ); + VERIFY( i1 - v1.end() == -2 ); + ranges::iter_swap(i0, i1); + VERIFY( ranges::equal(std::move(v1), (int[]){2, 1, 3}) ); + + auto v2 = std::array{1, -1, 2, -2} | views::pairwise_transform(std::multiplies{}); + auto i2 = v2.begin(); + i2 += 1; + i2 -= -2; + VERIFY( i2 == v2.end() ); + VERIFY( ranges::size(v2) == 3 ); + VERIFY( ranges::size(std::as_const(v2)) == 3 ); + VERIFY( ranges::equal(v2, (int[]){-1, -2, -4}) ); + + int y[] = {1, 2, 3, 4, 5, 6}; + auto v3 = y | views::adjacent_transform<3>([](auto... xs) { return ranges::max({xs...}); }); + VERIFY( ranges::size(v3) == 4 ); + VERIFY( ranges::equal(v3, (int[]){3, 4, 5, 6}) ); + + const auto v6 = y | views::adjacent_transform<6>([](auto...) { return 0; }); + VERIFY( ranges::equal(v6, (int[]){0}) ); + + const auto v7 = y | views::adjacent_transform<7>([](auto...) { return 0; }); + VERIFY( ranges::empty(v7) ); + + const auto v0 = y | views::adjacent_transform<0>([] { return 0; }); + VERIFY( ranges::empty(v0) ); + + return true; +} + +constexpr bool +test02() +{ + using __gnu_test::test_input_range; + using __gnu_test::test_forward_range; + using __gnu_test::test_random_access_range; + + using ty1 = ranges::adjacent_transform_view>, + std::plus<>, 2>; + static_assert(ranges::forward_range); + static_assert(!ranges::bidirectional_range); + static_assert(!ranges::sized_range); + + using ty2 = ranges::adjacent_transform_view>, + decltype([](int, int, int) { return 0;}), 3>; + static_assert(ranges::random_access_range); + static_assert(ranges::sized_range); + + return true; +} + +constexpr bool +test03() +{ + auto v = views::iota(0, 4) + | views::filter([](auto) { return true; }) + | views::pairwise_transform(std::plus{}); + using ty = decltype(v); + static_assert(ranges::forward_range); + static_assert(ranges::common_range); + static_assert(!ranges::sized_range); + VERIFY( v.begin() == v.begin() ); + VERIFY( v.begin() != v.end() ); + VERIFY( ranges::next(v.begin(), 3) == v.end() ); + auto it = v.begin(); + ++it; + it++; + VERIFY( ranges::next(it) == v.end() ); + it--; + --it; + VERIFY( it == v.begin() ); + + return true; +} + +int +main() +{ + static_assert(test01()); + static_assert(test02()); + static_assert(test03()); +}