From patchwork Mon Nov 14 04:50:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 19542 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1964700wru; Sun, 13 Nov 2022 20:53:00 -0800 (PST) X-Google-Smtp-Source: AA0mqf6Cx/4demZEvFhmUjQmgHhKSKpMu3ifb7IkZ29jDzVqma/wojj9rDi4ZvDxauMGFxP6S0TN X-Received: by 2002:aa7:dc03:0:b0:462:6a0c:cfa with SMTP id b3-20020aa7dc03000000b004626a0c0cfamr9901660edu.349.1668401580099; Sun, 13 Nov 2022 20:53:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668401580; cv=none; d=google.com; s=arc-20160816; b=feMaud30yuUojSHjFPgvCD8dc3DMotfRg5fTIC1DlmMEl8zVwy3nIXHhTjqWd3nKW7 MjK7K8GarhnlXr4IqbjDD2dobVaP6ir/ORt8c0jPcS+yJ0HxOeFgQzMnkznw7zIlXGJI FGx89g4CvVvFqtTvRN6gpaZ5kQLuYENQGtJEkf/kxPhNnS+RWs+grnsq8GBpIJPo/Ed5 0rJK8HBjGxJSyoXCDAK8ly8HxZnFunnDoT6MgmyRWDMZUOZuRnlTMCppjVJ7OPrp+qsd xJ4B7GxeUQJdJVWAjEqi6lHVXZOotiV43ROUswEH5C6XaH57gnyFd+vxiNOfJKyBraN5 spVQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to: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:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=2pAnce0g7TJHx3/BjfLiCZGyTlrq+4t8a0UtAFhdkTY=; b=tSj3H3aEUz25HKXKjkjym2PwoqemHAe5r6DBS+XBAnXwnOavBC+oF+rlXJemvn/GMZ FWZn6jC/KLnfWbwFS0XFM0LAvlXUBX5l4LwQZiiJjiCD0R51vdsoGGwYuaJzCE2rPsWc kBt/ubY3a8mm790mTKgJZ+f5ytSrWHOTkt+KZvJI3fLLx3aDD2o4Yvo9HXIsa2CTuDqs bPVMsgSO7CwOZ94KK5IEgN5tdiONwHlTMmBSecWrByfYfw1yyGN5pLuFrLzOA7RtuiIQ iSP2nVDxsWc7yu2LG0gkPiOl81ftbLV1s5J6gSa9hIyifiGK4hsyNVBybWRwIvLWzmeE anuQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=JW4c30yU; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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. [8.43.85.97]) by mx.google.com with ESMTPS id a3-20020a50e703000000b00462dca18096si7972708edn.520.2022.11.13.20.52.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 13 Nov 2022 20:53:00 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=JW4c30yU; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 2A1663835782 for ; Mon, 14 Nov 2022 04:52:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2A1663835782 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1668401529; bh=2pAnce0g7TJHx3/BjfLiCZGyTlrq+4t8a0UtAFhdkTY=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=JW4c30yUPyv/AjaFB0BpczTvPkxjaHkTfmcHD6Y2KBx38DJdhmjxkzu1MZoTVXcAY 9n1JAoHVbLR9GcNYrc+Sw4leIuCXfR0LHhfZ36lIjWj8apoKxb2hwwMAkFaSYNxR42 fLMIleV0QTKe7HzJVAXc5yitnBsyFSov64xjsyKs= 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 1C55B3851885 for ; Mon, 14 Nov 2022 04:51:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 1C55B3851885 Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-270-Cn863VYRNJyF5xuWCWzf2w-1; Sun, 13 Nov 2022 23:51:18 -0500 X-MC-Unique: Cn863VYRNJyF5xuWCWzf2w-1 Received: by mail-qv1-f72.google.com with SMTP id mo15-20020a056214330f00b004b96d712bccso7910790qvb.22 for ; Sun, 13 Nov 2022 20:51:18 -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:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=2pAnce0g7TJHx3/BjfLiCZGyTlrq+4t8a0UtAFhdkTY=; b=e090DlQPRPDKpgyCy/3dycca88K7mC86SNxEomMNI4mUlZFcuVSk3ZR2RTdix+sox+ XI3qxWnA/5FpoYcYqedz6QLm8F92wwiUDIhYixv8fS6lx0IYG/Ttbp+BSKrWhMow+53g 6wbhp5wf685g3JLYtCskuqGT2xkoxPRfdWLslKIGnMV7le2VIeWBoh4ADjilECwl9umv FKDUFG5EX2k2E6T+5armU8OkQmTWF9ssD+3mSaUPg73gTWDAxuPr0ozMt2ykUs4Gcq6g 9EGoGLzJKa+8m4kZpDj9/dfR0QuDG0njPvnfnje1SXkaKzMLdZ/z9B7NYBVSWHRSZbj+ aRrw== X-Gm-Message-State: ANoB5pkE1BF9w2IBc0TX/wBamTaTPuEGjadNixIywHPliMUC2JPa5qdO o2PwXzVUa6lQSqdgooROGsuRCorsakylI1+db5hy0R1r74bGmFyPrI3RZivd/hBlV2e2CWb+MeO UUiaFSZjgStuCx/w7LKQOT2lvIMNOsrl1TpL5D+2e4sVEvoeaQk2X3zBSUptQaGs3WHo= X-Received: by 2002:a37:b086:0:b0:6fa:330c:fb05 with SMTP id z128-20020a37b086000000b006fa330cfb05mr9990463qke.73.1668401477129; Sun, 13 Nov 2022 20:51:17 -0800 (PST) X-Received: by 2002:a37:b086:0:b0:6fa:330c:fb05 with SMTP id z128-20020a37b086000000b006fa330cfb05mr9990452qke.73.1668401476815; Sun, 13 Nov 2022 20:51:16 -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.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 13 Nov 2022 20:51:16 -0800 (PST) To: gcc-patches@gcc.gnu.org Cc: libstdc++@gcc.gnu.org, Patrick Palka Subject: [PATCH 1/3] libstdc++: Implement ranges::contains/contains_subrange from P2302R4 Date: Sun, 13 Nov 2022 23:50:45 -0500 Message-Id: <20221114045047.362745-1-ppalka@redhat.com> X-Mailer: git-send-email 2.38.1.420.g319605f8f0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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 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 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?1749445855640385007?= X-GMAIL-MSGID: =?utf-8?q?1749445855640385007?= Tested on x86_64-pc-linux-gnu, does this look OK for trunk? libstdc++-v3/ChangeLog: * include/bits/ranges_algo.h (__contains_fn, contains): Define. (__contains_subrange_fn, contains_subrange): Define. * testsuite/25_algorithms/contains/1.cc: New test. * testsuite/25_algorithms/contains_subrange/1.cc: New test. --- libstdc++-v3/include/bits/ranges_algo.h | 54 +++++++++++++++++++ .../testsuite/25_algorithms/contains/1.cc | 33 ++++++++++++ .../25_algorithms/contains_subrange/1.cc | 35 ++++++++++++ 3 files changed, 122 insertions(+) create mode 100644 libstdc++-v3/testsuite/25_algorithms/contains/1.cc create mode 100644 libstdc++-v3/testsuite/25_algorithms/contains_subrange/1.cc diff --git a/libstdc++-v3/include/bits/ranges_algo.h b/libstdc++-v3/include/bits/ranges_algo.h index de71bd07a2f..da0ca981dc3 100644 --- a/libstdc++-v3/include/bits/ranges_algo.h +++ b/libstdc++-v3/include/bits/ranges_algo.h @@ -3464,6 +3464,60 @@ namespace ranges inline constexpr __prev_permutation_fn prev_permutation{}; +#if __cplusplus > 202002L + struct __contains_fn + { + template _Sent, + typename _Tp, typename _Proj = identity> + requires indirect_binary_predicate, const _Tp*> + constexpr bool + operator()(_Iter __first, _Sent __last, const _Tp& __value, _Proj __proj = {}) const + { return ranges::find(std::move(__first), __last, __value, __proj) != __last; } + + template + requires indirect_binary_predicate, _Proj>, const _Tp*> + constexpr bool + operator()(_Range&& __r, const _Tp& __value, _Proj __proj = {}) const + { return (*this)(ranges::begin(__r), ranges::end(__r), __value, std::move(__proj)); } + }; + + inline constexpr __contains_fn contains{}; + + struct __contains_subrange_fn + { + template _Sent1, + forward_iterator _Iter2, sentinel_for<_Iter2> _Sent2, + typename _Pred = ranges::equal_to, + typename Proj1 = identity, typename Proj2 = identity> + requires indirectly_comparable<_Iter1, _Iter2, _Pred, Proj1, Proj2> + constexpr bool + operator()(_Iter1 __first1, _Sent1 __last1, _Iter2 __first2, _Sent2 __last2, + _Pred __pred = {}, Proj1 __proj1 = {}, Proj2 __proj2 = {}) const + { + return __first2 == __last2 + || !ranges::search(__first1, __last1, __first2, __last2, + std::move(__pred), std::move(__proj1), std::move(__proj2)).empty(); + } + + template + requires indirectly_comparable, iterator_t<_Range2>, + _Pred, _Proj1, _Proj2> + constexpr bool + operator()(_Range1&& __r1, _Range2&& __r2, _Pred __pred = {}, + _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const + { + return (*this)(ranges::begin(__r1), ranges::end(__r1), + ranges::begin(__r2), ranges::end(__r2), + std::move(__pred), std::move(__proj1), std::move(__proj2)); + } + }; + + inline constexpr __contains_subrange_fn contains_subrange{}; +#endif // C++23 } // namespace ranges #define __cpp_lib_shift 201806L diff --git a/libstdc++-v3/testsuite/25_algorithms/contains/1.cc b/libstdc++-v3/testsuite/25_algorithms/contains/1.cc new file mode 100644 index 00000000000..146ab593b70 --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/contains/1.cc @@ -0,0 +1,33 @@ +// { dg-options "-std=gnu++23" } +// { dg-do run { target c++23 } } + +#include +#include +#include + +namespace ranges = std::ranges; + +void +test01() +{ + int x[] = {1,2,3}; + using to_input = __gnu_test::test_input_range; + VERIFY( ranges::contains(to_input(x), 1) ); + VERIFY( ranges::contains(to_input(x), 2) ); + VERIFY( ranges::contains(to_input(x), 3) ); + VERIFY( !ranges::contains(to_input(x), 4) ); + VERIFY( !ranges::contains(x, x+2, 3) ); + auto neg = [](int n) { return -n; }; + VERIFY( ranges::contains(to_input(x), -1, neg) ); + VERIFY( ranges::contains(to_input(x), -2, neg) ); + VERIFY( ranges::contains(to_input(x), -3, neg) ); + VERIFY( !ranges::contains(to_input(x), -4, neg) ); + + VERIFY( !ranges::contains(x, x+2, -3, neg) ); +} + +int +main() +{ + test01(); +} diff --git a/libstdc++-v3/testsuite/25_algorithms/contains_subrange/1.cc b/libstdc++-v3/testsuite/25_algorithms/contains_subrange/1.cc new file mode 100644 index 00000000000..62b92795f94 --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/contains_subrange/1.cc @@ -0,0 +1,35 @@ +// { dg-options "-std=gnu++23" } +// { dg-do run { target c++23 } } + +#include +#include +#include + +namespace ranges = std::ranges; + +void +test01() +{ + int x[] = {1,2,3,4,5}; + int y[] = {2,3,4}; + int z[] = {4,5,6}; + __gnu_test::test_forward_range rx(x); + __gnu_test::test_forward_range ry(y); + __gnu_test::test_forward_range rz(z); + VERIFY( ranges::contains_subrange(rx, ry) ); + VERIFY( !ranges::contains_subrange(rx, rz) ); + VERIFY( ranges::contains_subrange(rx, ry, ranges::less{}) ); + VERIFY( ranges::contains_subrange(rx, rz, ranges::less{}) ); + auto plus3 = [](int n) { return n+3; }; + VERIFY( !ranges::contains_subrange(rx, ry, ranges::equal_to{}, plus3) ); + VERIFY( ranges::contains_subrange(rx, rz, ranges::equal_to{}, plus3) ); + + VERIFY( ranges::contains_subrange(x, x+2, y, y+1) ); + VERIFY( !ranges::contains_subrange(x, x+2, y, y+2) ); +} + +int +main() +{ + test01(); +} From patchwork Mon Nov 14 04:50:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 19543 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1964939wru; Sun, 13 Nov 2022 20:53:48 -0800 (PST) X-Google-Smtp-Source: AA0mqf5tGvIdTbY4OHJSdGhh8a4lNPwQes1gkp1+L4huwHYPeP2+fQKzJ3UxOJ136ASwLAFbdiYf X-Received: by 2002:a17:907:a602:b0:7ad:aeda:f814 with SMTP id vt2-20020a170907a60200b007adaedaf814mr9555650ejc.441.1668401628799; Sun, 13 Nov 2022 20:53:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668401628; cv=none; d=google.com; s=arc-20160816; b=hoEFJlgfe12rZzEzu4tw8JbA/fHyqTHtUGYYOZYR3z6bmOz3cD3bxnZThzCDRq0z0T 55eXSDdwi77K7VnHZzWzHoN9tWr1nHaoXmfwIoU3iXoVOjRjGyO49lrF2FYFvYon9WZr i30SpwGN+8maLNZCam3ntpoi/KdGcW9KYLNlnyfEy9zpGfxiCbU0HYnp50AqmCmXzhax FcNeupVz3XNX8QcVfbLmLd75pnoGoLgqva/8IVAejUoQDOAQr9s/kVWAac8u+KbQo1VX f6uf7FMx+k4lbyAzjP+OHAF0ohGrIjAFA/QGFE/gBCQvc82GvptoTtA1FqoYLjidlEcy Skcw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to: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:cc:to:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=drlh8yvJH9WnyQMIDFpSFVEc1bXUUGazvfNFD920NwU=; b=rnrKQzhjCk4EgYgc6VCo+1ACqxwJXSFVeXqa3hJCnU9EQmStHSTWI+5AnDtlI6aSrK KuUfErnkLZPT9W9/s/b+Tcg0sogORwbu9Wj0nBbTDoxpZn2qZVsDfBDjUCJkfWXT88/x 1Wfr2uEhoAj5RRkB++On7uTv1F/5tVqk1jPuTlgSJ2wrbO4LUdA+ZY20+bzBWzQnIoh5 K8G6s81ISDjqLDpurG9z+iA9K1x21WhPYllW5F8ti9jEYDkPi6jkQrPTq/Nrreq9vW/S Zy/vVCW+yVAj2KmGUuPFtMAYF4jKgQOexcHoOyVAoqRmqCjmL2VKXE97L6zZto6wgOSL hdWQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=I9V9Myr4; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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. [8.43.85.97]) by mx.google.com with ESMTPS id k3-20020a170906680300b0072ef52514f6si6494578ejr.629.2022.11.13.20.53.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 13 Nov 2022 20:53:48 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=I9V9Myr4; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 2AEEA383236C for ; Mon, 14 Nov 2022 04:53:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2AEEA383236C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1668401588; 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=I9V9Myr4MiYmBupg0soNs3Qxcl6rS8vHSX5YpO1EuUT20gTTCY7Sdj3cEmaZqusmL nTdxQFA39n02ulMG6KgwrhdP+yH2lFbYuDWhjNCfZ5WvRrU8QcAGfjE8iULEiPcpGP 9wrul5ybelUzvnXxoP7ACEOX0HNqeIUTUNcUhUVM= 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 ; 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 ; 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-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 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 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 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 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?1749445906516281079?= X-GMAIL-MSGID: =?utf-8?q?1749445906516281079?= 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); + ++__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(); +} From patchwork Mon Nov 14 04:50:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 19544 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1965118wru; Sun, 13 Nov 2022 20:54:31 -0800 (PST) X-Google-Smtp-Source: AA0mqf5sFbnzu7z8Z1jMX0+LSjDeFdLqI0AyLQVb1oIbwJkjKOL/ITRLZSNkF+2XbAnTUn+TnZFN X-Received: by 2002:a17:906:1b47:b0:7ae:5884:81b1 with SMTP id p7-20020a1709061b4700b007ae588481b1mr8990812ejg.155.1668401671196; Sun, 13 Nov 2022 20:54:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668401671; cv=none; d=google.com; s=arc-20160816; b=itY1R5IO7yAvvUkjmSiKAnAuCnmZX8IAeV0P4BwFO7fKNoJqphLee2mw2fzZy9tVva bamV6x2/qmnlO0lKdhKKPeV5ncp/kr+ccnks4uLeyxtSTmVxp1l9j16yOKRiSB1ZATtS uk9UR9WXvHeMyg2tGzS8tpCFa4WeAmK+T1GFps8gwqUCRDUpu9QpxihalD3MhazMuJ/1 4c/seAq1LA4eCVDzhzdIQLPBctgG8huQzJgKjyjPuCNpD29gOImNJmY2+JEGXkTI5oD9 sCBvskFnEu8KhEpobI+i7AbolvTdUkiSPL3BOv724bHbCnnnp5Cq1BPJYSVe248caKVb sI/w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to: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:cc:to:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=cTMsClLu69V24ipC436HTvzCSER6MSf6UfJ4WIbnMJE=; b=Q6seQ/AaV5Sc3ePWdukVQuhjGKkgi1K+DqnHT8HynhbfkTS+EwnaBnjRq78LhneulQ SL9DpoNa7ij/53HnnPtAttzdch2EDZDr/V55RNN0MIlIUgY3IVsuq2dEZ8COOQ3+4fJo v25cWYN86YwZjne9+rSTFoA/Bxs+Uenl1CcGWWIuiv05XYcmqRiSyvRl5tyn+V/y3jqx Olzj1jcc+i7aJATDLDkf9UFtkWsJfR0dU92xZFvCcdiY6NJMNq29wQ2QboVhJmGEpQp7 kGk5zwDmyWlGH7xY7EzPHecfVv7fpPIOVRgE/M35tbavoWbxqTqQtnvwtDZsUlISSCfH VBQA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="Uwhj/gOH"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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. [8.43.85.97]) by mx.google.com with ESMTPS id lt21-20020a170906fa9500b00791a41d1b39si6579337ejb.656.2022.11.13.20.54.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 13 Nov 2022 20:54:31 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="Uwhj/gOH"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 B22843832370 for ; Mon, 14 Nov 2022 04:54:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B22843832370 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1668401646; bh=cTMsClLu69V24ipC436HTvzCSER6MSf6UfJ4WIbnMJE=; 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=Uwhj/gOHWBUzdjlvIeFptMtwkyTT2Tfnl2FIY6+yjeTuiejNfyYb8aNTQO4fQ1qPg Y4OKcAKWmKef+m8pJYg8tUTYqP2SDHAo/x+WOUrMX6xYNhw+0ZBUIRIgBOwN/2YLpM n5E8rCb9V5sUas9y4cvgUTpF11OMdI3+Kr7fXpAM= 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 E7DED38518B2 for ; Mon, 14 Nov 2022 04:51:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org E7DED38518B2 Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-567-KHbQRDOnPo-F2Nkdua1uVQ-1; Sun, 13 Nov 2022 23:51:22 -0500 X-MC-Unique: KHbQRDOnPo-F2Nkdua1uVQ-1 Received: by mail-qt1-f198.google.com with SMTP id f4-20020a05622a114400b003a57f828277so7490471qty.22 for ; Sun, 13 Nov 2022 20:51:22 -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=cTMsClLu69V24ipC436HTvzCSER6MSf6UfJ4WIbnMJE=; b=tbyQlv++ovST2B+eLZR+WEyf5ht4b5BQlC/6MrwCW/5IO9vHjVwNAtf1DXBi5tYv6b wukhosR9uiCVvjOcinx+sfLEW2Wf4m6zzTTb/r/rYTvWYVe0H1NHYFOKZtPY3tDU190k iviZN5O0fKc2JAHffPLMUplQLghc2KkkJjFoUOtqiR1b7ue7Su/HgEHFKxYoK4SLBihG KsYvn4yMs68XiKPMVTwtFL81XmxFlHgzu7AcSTc4uKUhSlhq7wyUfMokkkajDcUa0gZl CngGOgt/fswOx5OrSCBhVYe4en6JCcf4LZpYY1f0NFRyj/Zy0W26wrPGrCQ+Z27D3ymt 45gg== X-Gm-Message-State: ANoB5pn31I0sUZGb7hS4itFnB/lkW7xfsDJ820T4PEeC4Gn6coB37Ne4 OruHFDZfnCCCrXUdeQuXiGZQVqGvuMOouPQ+tSBTZak2Rc7kMT9RyQi8NxKxzH4jM8y/xUsKCUi r4fjDYCYkhL8FSKUDYa5ynSfaUPF3Tg4UEZ5cZCp0NfGNvk6UFh11tDdrpNHVezSO0GU= X-Received: by 2002:ac8:43c4:0:b0:3a5:c8c6:a886 with SMTP id w4-20020ac843c4000000b003a5c8c6a886mr10819709qtn.455.1668401481298; Sun, 13 Nov 2022 20:51:21 -0800 (PST) X-Received: by 2002:ac8:43c4:0:b0:3a5:c8c6:a886 with SMTP id w4-20020ac843c4000000b003a5c8c6a886mr10819700qtn.455.1668401480893; Sun, 13 Nov 2022 20:51:20 -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.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 13 Nov 2022 20:51:20 -0800 (PST) To: gcc-patches@gcc.gnu.org Cc: libstdc++@gcc.gnu.org, Patrick Palka Subject: [PATCH 3/3] libstdc++: Implement ranges::find_last{, _if, _if_not} from P1223R5 Date: Sun, 13 Nov 2022 23:50:47 -0500 Message-Id: <20221114045047.362745-3-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 X-Spam-Status: No, score=-13.6 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 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 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?1749445951209839848?= X-GMAIL-MSGID: =?utf-8?q?1749445951209839848?= Tested on x86_64-pc-linux-gnu, does this look OK for trunk? libstdc++-v3/ChangeLog: * include/bits/ranges_algo.h (__find_last_fn, find_last): Define. (__find_last_if_fn, find_last_if): Define. (__find_last_if_not_fn, find_last_if_not): Define. * testsuite/25_algorithms/find_last/1.cc: New test. * testsuite/25_algorithms/find_last_if/1.cc: New test. * testsuite/25_algorithms/find_last_if_not/1.cc: New test. --- libstdc++-v3/include/bits/ranges_algo.h | 123 ++++++++++++++++++ .../testsuite/25_algorithms/find_last/1.cc | 90 +++++++++++++ .../testsuite/25_algorithms/find_last_if/1.cc | 92 +++++++++++++ .../25_algorithms/find_last_if_not/1.cc | 92 +++++++++++++ 4 files changed, 397 insertions(+) create mode 100644 libstdc++-v3/testsuite/25_algorithms/find_last/1.cc create mode 100644 libstdc++-v3/testsuite/25_algorithms/find_last_if/1.cc create mode 100644 libstdc++-v3/testsuite/25_algorithms/find_last_if_not/1.cc diff --git a/libstdc++-v3/include/bits/ranges_algo.h b/libstdc++-v3/include/bits/ranges_algo.h index f003117c569..0e4329382eb 100644 --- a/libstdc++-v3/include/bits/ranges_algo.h +++ b/libstdc++-v3/include/bits/ranges_algo.h @@ -3565,6 +3565,129 @@ namespace ranges }; inline constexpr __iota_fn iota{}; + + struct __find_last_fn + { + template _Sent, typename T, typename _Proj = identity> + requires indirect_binary_predicate, const T*> + constexpr subrange<_Iter> + operator()(_Iter __first, _Sent __last, const T& __value, _Proj __proj = {}) const + { + if constexpr (same_as<_Iter, _Sent> && bidirectional_iterator<_Iter>) + { + _Iter __found = ranges::find(reverse_iterator<_Iter>{__last}, + reverse_iterator<_Iter>{__first}, + __value, __proj).base(); + if (__found == __first) + return {__last, __last}; + else + return {ranges::prev(__found), __last}; + } + else + { + _Iter __found = ranges::find(__first, __last, __value, __proj); + if (__found == __last) + return {__found, __found}; + for (;;) + { + __first = ranges::find(ranges::next(__first), __last, __value, __proj); + if (__first == __last) + return {__found, __first}; + __found = __first; + } + } + } + + template + requires indirect_binary_predicate, _Proj>, const T*> + constexpr borrowed_subrange_t<_Range> + operator()(_Range&& __r, const T& __value, _Proj __proj = {}) const + { return (*this)(ranges::begin(__r), ranges::end(__r), __value, std::move(__proj)); } + }; + + inline constexpr __find_last_fn find_last{}; + + struct __find_last_if_fn + { + template _Sent, typename _Proj = identity, + indirect_unary_predicate> _Pred> + constexpr subrange<_Iter> + operator()(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {}) const + { + if constexpr (same_as<_Iter, _Sent> && bidirectional_iterator<_Iter>) + { + _Iter __found = ranges::find_if(reverse_iterator<_Iter>{__last}, + reverse_iterator<_Iter>{__first}, + __pred, __proj).base(); + if (__found == __first) + return {__last, __last}; + else + return {ranges::prev(__found), __last}; + } + else + { + _Iter __found = ranges::find_if(__first, __last, __pred, __proj); + if (__found == __last) + return {__found, __found}; + for (;;) + { + __first = ranges::find_if(ranges::next(__first), __last, __pred, __proj); + if (__first == __last) + return {__found, __first}; + __found = __first; + } + } + } + + template, _Proj>> _Pred> + constexpr borrowed_subrange_t<_Range> + operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const + { return (*this)(ranges::begin(__r), ranges::end(__r), std::move(__pred), std::move(__proj)); } + }; + + inline constexpr __find_last_if_fn find_last_if{}; + + struct __find_last_if_not_fn + { + template _Sent, typename _Proj = identity, + indirect_unary_predicate> _Pred> + constexpr subrange<_Iter> + operator()(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {}) const + { + if constexpr (same_as<_Iter, _Sent> && bidirectional_iterator<_Iter>) + { + _Iter __found = ranges::find_if_not(reverse_iterator<_Iter>{__last}, + reverse_iterator<_Iter>{__first}, + __pred, __proj).base(); + if (__found == __first) + return {__last, __last}; + else + return {ranges::prev(__found), __last}; + } + else + { + _Iter __found = ranges::find_if_not(__first, __last, __pred, __proj); + if (__found == __last) + return {__found, __found}; + for (;;) + { + __first = ranges::find_if_not(ranges::next(__first), __last, __pred, __proj); + if (__first == __last) + return {__found, __first}; + __found = __first; + } + } + } + + template, _Proj>> _Pred> + constexpr borrowed_subrange_t<_Range> + operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const + { return (*this)(ranges::begin(__r), ranges::end(__r), std::move(__pred), std::move(__proj)); } + }; + + inline constexpr __find_last_if_not_fn find_last_if_not{}; #endif // C++23 } // namespace ranges diff --git a/libstdc++-v3/testsuite/25_algorithms/find_last/1.cc b/libstdc++-v3/testsuite/25_algorithms/find_last/1.cc new file mode 100644 index 00000000000..ef5844c8afd --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/find_last/1.cc @@ -0,0 +1,90 @@ +// { dg-options "-std=gnu++23" } +// { dg-do run { target c++23 } } + +#include +#include +#include + +namespace ranges = std::ranges; + +constexpr bool +test01() +{ + int x[] = {1, 2, 1, 2, 1, 2, 1, 2}; + + auto sr0 = ranges::find_last(x, 0); + VERIFY( ranges::empty(sr0) ); + VERIFY( sr0.begin() == ranges::end(x) ); + + auto sr1 = ranges::find_last(x, 1); + VERIFY( ranges::equal(sr1, (int[]){1, 2}) ); + VERIFY( sr1.begin() == &x[6] ); + + auto sr2 = ranges::find_last(x, 2); + VERIFY( ranges::equal(sr2, (int[]){2}) ); + VERIFY( sr2.begin() == &x[7] ); + + auto plus3 = [](int n) { return n+3; }; + + auto sr3 = ranges::find_last(x, 3, plus3); + VERIFY( ranges::empty(sr3) ); + VERIFY( sr3.begin() == ranges::end(x) ); + + auto sr4 = ranges::find_last(x, 4, plus3); + VERIFY( ranges::equal(sr4, (int[]){1, 2}) ); + VERIFY( sr4.begin() == &x[6] ); + + auto sr5 = ranges::find_last(x, 5, plus3); + VERIFY( ranges::equal(sr5, (int[]){2}) ); + VERIFY( sr5.begin() == &x[7] ); + + return true; +} + +void +test02() +{ + int x[] = {1, 2, 3, 1, 2, 3, 1, 2, 3}; + __gnu_test::test_forward_range rx(x); + + auto sr0 = ranges::find_last(rx, 0); + VERIFY( ranges::empty(sr0) ); + VERIFY( sr0.begin() == ranges::end(rx) ); + + auto sr1 = ranges::find_last(rx, 1); + VERIFY( ranges::equal(sr1, (int[]){1, 2, 3}) ); + VERIFY( sr1.begin().ptr == &x[6] ); + + auto sr2 = ranges::find_last(rx, 2); + VERIFY( ranges::equal(sr2, (int[]){2, 3}) ); + VERIFY( sr2.begin().ptr == &x[7] ); + + auto sr3 = ranges::find_last(rx, 3); + VERIFY( ranges::equal(sr3, (int[]){3}) ); + VERIFY( sr3.begin().ptr == &x[8] ); + + auto plus4 = [](int n) { return n+4; }; + + auto sr4 = ranges::find_last(rx, 4, plus4); + VERIFY( ranges::empty(sr4) ); + VERIFY( sr4.begin() == ranges::end(rx) ); + + auto sr5 = ranges::find_last(rx, 5, plus4); + VERIFY( ranges::equal(sr5, (int[]){1, 2, 3}) ); + VERIFY( sr5.begin().ptr == &x[6] ); + + auto sr6 = ranges::find_last(rx, 6, plus4); + VERIFY( ranges::equal(sr6, (int[]){2, 3}) ); + VERIFY( sr6.begin().ptr == &x[7] ); + + auto sr7 = ranges::find_last(rx, 7, plus4); + VERIFY( ranges::equal(sr7, (int[]){3}) ); + VERIFY( sr7.begin().ptr == &x[8] ); +} + +int +main() +{ + static_assert(test01()); + test02(); +} diff --git a/libstdc++-v3/testsuite/25_algorithms/find_last_if/1.cc b/libstdc++-v3/testsuite/25_algorithms/find_last_if/1.cc new file mode 100644 index 00000000000..0a723475dec --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/find_last_if/1.cc @@ -0,0 +1,92 @@ +// { dg-options "-std=gnu++23" } +// { dg-do run { target c++23 } } + +#include +#include +#include + +namespace ranges = std::ranges; + +template constexpr auto eq = [](int m) { return m == N; }; + +constexpr bool +test01() +{ + int x[] = {1, 2, 1, 2, 1, 2, 1, 2}; + + auto sr0 = ranges::find_last_if(x, eq<0>); + VERIFY( ranges::empty(sr0) ); + VERIFY( sr0.begin() == ranges::end(x) ); + + auto sr1 = ranges::find_last_if(x, eq<1>); + VERIFY( ranges::equal(sr1, (int[]){1, 2}) ); + VERIFY( sr1.begin() == &x[6] ); + + auto sr2 = ranges::find_last_if(x, eq<2>); + VERIFY( ranges::equal(sr2, (int[]){2}) ); + VERIFY( sr2.begin() == &x[7] ); + + auto plus3 = [](int n) { return n+3; }; + + auto sr3 = ranges::find_last_if(x, eq<3>, plus3); + VERIFY( ranges::empty(sr3) ); + VERIFY( sr3.begin() == ranges::end(x) ); + + auto sr4 = ranges::find_last_if(x, eq<4>, plus3); + VERIFY( ranges::equal(sr4, (int[]){1, 2}) ); + VERIFY( sr4.begin() == &x[6] ); + + auto sr5 = ranges::find_last_if(x, eq<5>, plus3); + VERIFY( ranges::equal(sr5, (int[]){2}) ); + VERIFY( sr5.begin() == &x[7] ); + + return true; +} + +void +test02() +{ + int x[] = {1, 2, 3, 1, 2, 3, 1, 2, 3}; + __gnu_test::test_forward_range rx(x); + + auto sr0 = ranges::find_last_if(rx, eq<0>); + VERIFY( ranges::empty(sr0) ); + VERIFY( sr0.begin() == ranges::end(rx) ); + + auto sr1 = ranges::find_last_if(rx, eq<1>); + VERIFY( ranges::equal(sr1, (int[]){1, 2, 3}) ); + VERIFY( sr1.begin().ptr == &x[6] ); + + auto sr2 = ranges::find_last_if(rx, eq<2>); + VERIFY( ranges::equal(sr2, (int[]){2, 3}) ); + VERIFY( sr2.begin().ptr == &x[7] ); + + auto sr3 = ranges::find_last_if(rx, eq<3>); + VERIFY( ranges::equal(sr3, (int[]){3}) ); + VERIFY( sr3.begin().ptr == &x[8] ); + + auto plus4 = [](int n) { return n+4; }; + + auto sr4 = ranges::find_last_if(rx, eq<4>, plus4); + VERIFY( ranges::empty(sr4) ); + VERIFY( sr4.begin() == ranges::end(rx) ); + + auto sr5 = ranges::find_last_if(rx, eq<5>, plus4); + VERIFY( ranges::equal(sr5, (int[]){1, 2, 3}) ); + VERIFY( sr5.begin().ptr == &x[6] ); + + auto sr6 = ranges::find_last_if(rx, eq<6>, plus4); + VERIFY( ranges::equal(sr6, (int[]){2, 3}) ); + VERIFY( sr6.begin().ptr == &x[7] ); + + auto sr7 = ranges::find_last_if(rx, eq<7>, plus4); + VERIFY( ranges::equal(sr7, (int[]){3}) ); + VERIFY( sr7.begin().ptr == &x[8] ); +} + +int +main() +{ + static_assert(test01()); + test02(); +} diff --git a/libstdc++-v3/testsuite/25_algorithms/find_last_if_not/1.cc b/libstdc++-v3/testsuite/25_algorithms/find_last_if_not/1.cc new file mode 100644 index 00000000000..98aa94b7f2c --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/find_last_if_not/1.cc @@ -0,0 +1,92 @@ +// { dg-options "-std=gnu++23" } +// { dg-do run { target c++23 } } + +#include +#include +#include + +namespace ranges = std::ranges; + +template constexpr auto ne = [](int m) { return m != N; }; + +constexpr bool +test01() +{ + int x[] = {1, 2, 1, 2, 1, 2, 1, 2}; + + auto sr0 = ranges::find_last_if_not(x, ne<0>); + VERIFY( ranges::empty(sr0) ); + VERIFY( sr0.begin() == ranges::end(x) ); + + auto sr1 = ranges::find_last_if_not(x, ne<1>); + VERIFY( ranges::equal(sr1, (int[]){1, 2}) ); + VERIFY( sr1.begin() == &x[6] ); + + auto sr2 = ranges::find_last_if_not(x, ne<2>); + VERIFY( ranges::equal(sr2, (int[]){2}) ); + VERIFY( sr2.begin() == &x[7] ); + + auto plus3 = [](int n) { return n+3; }; + + auto sr3 = ranges::find_last_if_not(x, ne<3>, plus3); + VERIFY( ranges::empty(sr3) ); + VERIFY( sr3.begin() == ranges::end(x) ); + + auto sr4 = ranges::find_last_if_not(x, ne<4>, plus3); + VERIFY( ranges::equal(sr4, (int[]){1, 2}) ); + VERIFY( sr4.begin() == &x[6] ); + + auto sr5 = ranges::find_last_if_not(x, ne<5>, plus3); + VERIFY( ranges::equal(sr5, (int[]){2}) ); + VERIFY( sr5.begin() == &x[7] ); + + return true; +} + +void +test02() +{ + int x[] = {1, 2, 3, 1, 2, 3, 1, 2, 3}; + __gnu_test::test_forward_range rx(x); + + auto sr0 = ranges::find_last_if_not(rx, ne<0>); + VERIFY( ranges::empty(sr0) ); + VERIFY( sr0.begin() == ranges::end(rx) ); + + auto sr1 = ranges::find_last_if_not(rx, ne<1>); + VERIFY( ranges::equal(sr1, (int[]){1, 2, 3}) ); + VERIFY( sr1.begin().ptr == &x[6] ); + + auto sr2 = ranges::find_last_if_not(rx, ne<2>); + VERIFY( ranges::equal(sr2, (int[]){2, 3}) ); + VERIFY( sr2.begin().ptr == &x[7] ); + + auto sr3 = ranges::find_last_if_not(rx, ne<3>); + VERIFY( ranges::equal(sr3, (int[]){3}) ); + VERIFY( sr3.begin().ptr == &x[8] ); + + auto plus4 = [](int n) { return n+4; }; + + auto sr4 = ranges::find_last_if_not(rx, ne<4>, plus4); + VERIFY( ranges::empty(sr4) ); + VERIFY( sr4.begin() == ranges::end(rx) ); + + auto sr5 = ranges::find_last_if_not(rx, ne<5>, plus4); + VERIFY( ranges::equal(sr5, (int[]){1, 2, 3}) ); + VERIFY( sr5.begin().ptr == &x[6] ); + + auto sr6 = ranges::find_last_if_not(rx, ne<6>, plus4); + VERIFY( ranges::equal(sr6, (int[]){2, 3}) ); + VERIFY( sr6.begin().ptr == &x[7] ); + + auto sr7 = ranges::find_last_if_not(rx, ne<7>, plus4); + VERIFY( ranges::equal(sr7, (int[]){3}) ); + VERIFY( sr7.begin().ptr == &x[8] ); +} + +int +main() +{ + static_assert(test01()); + test02(); +}