From patchwork Tue Nov 17 20:51:39 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Fran=C3=A7ois_Dumont?= X-Patchwork-Id: 1401840 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=YCPkVXsh; dkim-atps=neutral Received: from sourceware.org (unknown [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4CbJ5j2WrRz9sPB for ; Wed, 18 Nov 2020 07:51:51 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9CC713896835; Tue, 17 Nov 2020 20:51:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9CC713896835 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1605646307; bh=4JuPq1QOxGgIWOfoe6UJDKD6TBgUsZIUm9r71MhfIgk=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=YCPkVXsh76ep7nDH8tpanLbbfSWuG56Qr0kRrzbUZXJXCRBN7aFRlxq48jlOp0mgC BhdYkoZMUKg9GMflkGwugePsD/Uuxd/ogdEmYtLfIBTHPh2CF+V8tqR3cWHDfFoC2L 5U9dRy+DB8DVvKvfhTha4f5PujfNPwoIhlUn99u4= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by sourceware.org (Postfix) with ESMTPS id 431373858006; Tue, 17 Nov 2020 20:51:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 431373858006 Received: by mail-wr1-x42d.google.com with SMTP id l1so24756421wrb.9; Tue, 17 Nov 2020 12:51:44 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:from:subject:message-id:date:user-agent :mime-version:content-language; bh=4JuPq1QOxGgIWOfoe6UJDKD6TBgUsZIUm9r71MhfIgk=; b=XYf7TqUBbanDKHd8hmF2WGpmcUicEB40e2dRUsLbmiBMN1HELZWeGR5nRCKlCdqZiI sPmj0mdo5UL+L82ImmZDbLTGB1JWdfYzmULS5dyZc+NDbia9qMNSBGykgrdJf6lHwsVb bItgnS0uRTxUpkFcqHOVUyopo4Sa5TRMyq4BdpMQIoXllYwK0me4j+NjEfoL+4Az92V1 vhW8t6pC6hpaowOwU+gAoAWGBx3JZH2FB+wzUKx2RvakxIanbha9OZzzxDbJpW6jsKzO znM/WxACsL2DOVgGrU03gh+tMgcSmwX8mYcX4jwrnV5+fnNvFlzL2ju9DoGeGlsfloPA NBJg== X-Gm-Message-State: AOAM532vL8zsaPJj/p0XsohdHQJ5DU/PxuVU5RO1+40RoqlJXykKkp8V M3jtKGtt3XJGjL3oIucITrykyPTA924= X-Google-Smtp-Source: ABdhPJxagvRZMZ2fZMMiRa2kLKiNQ6y6TB9XUjA+IGiI2bUj1hnaN7gB81krPZPhYJB1iJtLdblt2A== X-Received: by 2002:a5d:52c1:: with SMTP id r1mr1434084wrv.255.1605646302599; Tue, 17 Nov 2020 12:51:42 -0800 (PST) Received: from ?IPv6:2a01:e0a:1dc:b1c0:4d33:c573:a02a:80c4? ([2a01:e0a:1dc:b1c0:4d33:c573:a02a:80c4]) by smtp.googlemail.com with ESMTPSA id e5sm22713267wra.80.2020.11.17.12.51.40 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 17 Nov 2020 12:51:41 -0800 (PST) To: "libstdc++@gcc.gnu.org" , gcc-patches Subject: [PATCH] Remove lambdas from _Rb_tree Message-ID: <7c66cd1c-3f62-5f2c-fb75-2641d672a3d2@gmail.com> Date: Tue, 17 Nov 2020 21:51:39 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 Content-Language: fr X-Spam-Status: No, score=-10.3 required=5.0 tests=BAYES_00, BODY_8BITS, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: =?utf-8?q?Fran=C3=A7ois_Dumont_via_Gcc-patches?= From: =?utf-8?q?Fran=C3=A7ois_Dumont?= Reply-To: =?utf-8?q?Fran=C3=A7ois_Dumont?= Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" This is a change that has been done to _Hashtable and that I forgot to propose for _Rb_tree. The _GLIBCXX_XREF macro can be easily removed of course.     libstdc++: _Rb_tree code cleanup, remove lambdas.     Use an additional template parameter on the clone method to propagate if the values must be     copy or move rather than lambdas.     libstdc++-v3/ChangeLog:             * include/bits/move.h (_GLIBCXX_XREF): New.             * include/bits/stl_tree.h: Adapt to use latter.             (_Rb_tree<>::_S_fwd_value_for): New.             (_Rb_tree<>::_M_clone_node): Add _Tree template parameter.             Use _S_fwd_value_for.             (_Rb_tree<>::_M_cbegin): New.             (_Rb_tree<>::_M_begin): Use latter.             (_Rb_tree<>::_M_copy): Add _Tree template parameter.             (_Rb_tree<>::_M_move_data): Use rvalue reference for _Rb_tree parameter.             (_Rb_tree<>::_M_move_assign): Likewise. Tested under Linux x86_64. Ok to commit ? François diff --git a/libstdc++-v3/include/bits/move.h b/libstdc++-v3/include/bits/move.h index 5a4dbdc823c..e0d68ca9108 100644 --- a/libstdc++-v3/include/bits/move.h +++ b/libstdc++-v3/include/bits/move.h @@ -158,9 +158,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION /// @} group utilities +#define _GLIBCXX_XREF(_Tp) _Tp&& #define _GLIBCXX_MOVE(__val) std::move(__val) #define _GLIBCXX_FORWARD(_Tp, __val) std::forward<_Tp>(__val) #else +#define _GLIBCXX_XREF(_Tp) const _Tp& #define _GLIBCXX_MOVE(__val) (__val) #define _GLIBCXX_FORWARD(_Tp, __val) (__val) #endif diff --git a/libstdc++-v3/include/bits/stl_tree.h b/libstdc++-v3/include/bits/stl_tree.h index ec141ea01c7..128c7e2c892 100644 --- a/libstdc++-v3/include/bits/stl_tree.h +++ b/libstdc++-v3/include/bits/stl_tree.h @@ -478,11 +478,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template _Link_type -#if __cplusplus < 201103L - operator()(const _Arg& __arg) -#else - operator()(_Arg&& __arg) -#endif + operator()(_GLIBCXX_XREF(_Arg) __arg) { _Link_type __node = static_cast<_Link_type>(_M_extract()); if (__node) @@ -544,11 +540,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template _Link_type -#if __cplusplus < 201103L - operator()(const _Arg& __arg) const -#else - operator()(_Arg&& __arg) const -#endif + operator()(_GLIBCXX_XREF(_Arg) __arg) const { return _M_t._M_create_node(_GLIBCXX_FORWARD(_Arg, __arg)); } private: @@ -655,11 +647,27 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _M_put_node(__p); } - template +#if __cplusplus >= 201103L + template + static constexpr + typename conditional::value, + const value_type&, value_type&&>::type + _S_fwd_value_for(value_type& __val) noexcept + { return std::move(__val); } +#else + template + static const value_type& + _S_fwd_value_for(value_type& __val) + { return __val; } +#endif + + template _Link_type - _M_clone_node(_Const_Link_type __x, _NodeGen& __node_gen) + _M_clone_node(_GLIBCXX_XREF(_Tree), + _Link_type __x, _NodeGen& __node_gen) { - _Link_type __tmp = __node_gen(*__x->_M_valptr()); + _Link_type __tmp + = __node_gen(_S_fwd_value_for<_Tree>(*__x->_M_valptr())); __tmp->_M_color = __x->_M_color; __tmp->_M_left = 0; __tmp->_M_right = 0; @@ -748,9 +756,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { return this->_M_impl._M_header._M_right; } _Link_type - _M_begin() _GLIBCXX_NOEXCEPT + _M_cbegin() const _GLIBCXX_NOEXCEPT { return static_cast<_Link_type>(this->_M_impl._M_header._M_parent); } + _Link_type + _M_begin() _GLIBCXX_NOEXCEPT + { return _M_cbegin(); } + _Const_Link_type _M_begin() const _GLIBCXX_NOEXCEPT { @@ -889,15 +901,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _M_insert_equal_lower(const value_type& __x); #endif - template + template _Link_type - _M_copy(_Const_Link_type __x, _Base_ptr __p, _NodeGen&); + _M_copy(_GLIBCXX_XREF(_Tree), _Link_type, _Base_ptr, _NodeGen&); - template + template _Link_type - _M_copy(const _Rb_tree& __x, _NodeGen& __gen) + _M_copy(_GLIBCXX_XREF(_Tree) __x, _NodeGen& __gen) { - _Link_type __root = _M_copy(__x._M_begin(), _M_end(), __gen); + _Link_type __root = _M_copy(_GLIBCXX_FORWARD(_Tree, __x), + __x._M_cbegin(), _M_end(), __gen); _M_leftmost() = _S_minimum(__root); _M_rightmost() = _S_maximum(__root); _M_impl._M_node_count = __x._M_impl._M_node_count; @@ -977,7 +990,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION : _M_impl(__x._M_impl._M_key_compare, std::move(__a)) { if (__x._M_root() != nullptr) - _M_move_data(__x, false_type{}); + _M_move_data(std::move(__x), false_type{}); } public: @@ -1426,22 +1439,22 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION private: // Move elements from container with equal allocator. void - _M_move_data(_Rb_tree& __x, true_type) + _M_move_data(_Rb_tree&& __x, true_type) { _M_impl._M_move_data(__x._M_impl); } // Move elements from container with possibly non-equal allocator, // which might result in a copy not a move. void - _M_move_data(_Rb_tree&, false_type); + _M_move_data(_Rb_tree&&, false_type); // Move assignment from container with equal allocator. void - _M_move_assign(_Rb_tree&, true_type); + _M_move_assign(_Rb_tree&&, true_type); // Move assignment from container with possibly non-equal allocator, // which might result in a copy not a move. void - _M_move_assign(_Rb_tree&, false_type); + _M_move_assign(_Rb_tree&&, false_type); #endif #if __cplusplus > 201402L @@ -1648,20 +1661,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION typename _Compare, typename _Alloc> void _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>:: - _M_move_data(_Rb_tree& __x, false_type) + _M_move_data(_Rb_tree&& __x, false_type) { if (_M_get_Node_allocator() == __x._M_get_Node_allocator()) - _M_move_data(__x, true_type()); + _M_move_data(std::move(__x), true_type()); else { + using _Fwd_Rbt = typename + conditional<__move_if_noexcept_cond::value, + const _Rb_tree&, _Rb_tree&&>::type; _Alloc_node __an(*this); - auto __lbd = - [&__an](const value_type& __cval) - { - auto& __val = const_cast(__cval); - return __an(std::move_if_noexcept(__val)); - }; - _M_root() = _M_copy(__x, __lbd); + _M_root() = _M_copy(std::forward<_Fwd_Rbt>(__x), __an); } } @@ -1669,11 +1679,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION typename _Compare, typename _Alloc> inline void _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>:: - _M_move_assign(_Rb_tree& __x, true_type) + _M_move_assign(_Rb_tree&& __x, true_type) { clear(); if (__x._M_root() != nullptr) - _M_move_data(__x, true_type()); + _M_move_data(std::move(__x), true_type()); std::__alloc_on_move(_M_get_Node_allocator(), __x._M_get_Node_allocator()); } @@ -1682,10 +1692,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION typename _Compare, typename _Alloc> void _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>:: - _M_move_assign(_Rb_tree& __x, false_type) + _M_move_assign(_Rb_tree&& __x, false_type) { if (_M_get_Node_allocator() == __x._M_get_Node_allocator()) - return _M_move_assign(__x, true_type{}); + return _M_move_assign(std::move(__x), true_type{}); // Try to move each node reusing existing nodes and copying __x nodes // structure. @@ -1693,13 +1703,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _M_impl._M_reset(); if (__x._M_root() != nullptr) { - auto __lbd = - [&__roan](const value_type& __cval) - { - auto& __val = const_cast(__cval); - return __roan(std::move(__val)); - }; - _M_root() = _M_copy(__x, __lbd); + _M_root() = _M_copy(std::move(__x), __roan); __x.clear(); } } @@ -1713,7 +1717,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION && is_nothrow_move_assignable<_Compare>::value) { _M_impl._M_key_compare = std::move(__x._M_impl._M_key_compare); - _M_move_assign(__x, __bool_constant<_Alloc_traits::_S_nothrow_move()>()); + _M_move_assign(std::move(__x), + __bool_constant<_Alloc_traits::_S_nothrow_move()>()); return *this; } @@ -1859,29 +1864,34 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template - template + template typename _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::_Link_type _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>:: - _M_copy(_Const_Link_type __x, _Base_ptr __p, _NodeGen& __node_gen) + _M_copy(_GLIBCXX_XREF(_Tree) __t, + _Link_type __x, _Base_ptr __p, _NodeGen& __node_gen) { // Structural copy. __x and __p must be non-null. - _Link_type __top = _M_clone_node(__x, __node_gen); + _Link_type __top = _M_clone_node(_GLIBCXX_FORWARD(_Tree, __t), + __x, __node_gen); __top->_M_parent = __p; __try { if (__x->_M_right) - __top->_M_right = _M_copy(_S_right(__x), __top, __node_gen); + __top->_M_right = _M_copy(_GLIBCXX_FORWARD(_Tree, __t), + _S_right(__x), __top, __node_gen); __p = __top; __x = _S_left(__x); while (__x != 0) { - _Link_type __y = _M_clone_node(__x, __node_gen); + _Link_type __y = _M_clone_node(_GLIBCXX_FORWARD(_Tree, __t), + __x, __node_gen); __p->_M_left = __y; __y->_M_parent = __p; if (__x->_M_right) - __y->_M_right = _M_copy(_S_right(__x), __y, __node_gen); + __y->_M_right = _M_copy(_GLIBCXX_FORWARD(_Tree, __t), + _S_right(__x), __y, __node_gen); __p = __y; __x = _S_left(__x); }