From patchwork Mon Jun 20 16:58:07 2022 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: 55203 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BACB73851AB3 for ; Mon, 20 Jun 2022 17:02:38 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BACB73851AB3 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1655744558; bh=+Qhlmz6sGUoT/0AA0SISo8TFS4y4hFAC/IO94nnDRTU=; h=Date:Subject:To:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=jd96q+xK3dXaQfqTgr5o/ElGsZBxT+ZSYZd06MsdMUGxyymtjOVpsTuGxPiv+xujf mMioU/gsdiAA/EDnhG6Ixny6CX95EuOc2kJc3drX3XBnO9AgpRGCYEHt/lE4CGO6JY kZauaZFhecoSXy1yZJnQHWCBWWwgt5qDUql/sNhw= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-ej1-x62d.google.com (mail-ej1-x62d.google.com [IPv6:2a00:1450:4864:20::62d]) by sourceware.org (Postfix) with ESMTPS id D2A343851ABC; Mon, 20 Jun 2022 16:58:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D2A343851ABC Received: by mail-ej1-x62d.google.com with SMTP id me5so22344340ejb.2; Mon, 20 Jun 2022 09:58:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:from :subject:to:cc:content-language; bh=+Qhlmz6sGUoT/0AA0SISo8TFS4y4hFAC/IO94nnDRTU=; b=2ZdXN+371rGMwWc8zydq/49m/jSLxomdZzIkLeg7uUBfnL28CLYVAT+777Gw/+DHD/ C/PFFv6oJbvwNaHfDLfWJ2uj3NZK3Xn9UJUXA188jYl+20eCEOE1KZ9UI9rnA9bYhfA3 sZVp4+LltfE8NJAoVRfkaaq7PROz4CYKEMsTusC9syC2gz0Hj4GNKJ6Y9YgRAD5g50/x GWGKWm5Zk+BPwwVUXb9HXGvirlZiPpQ/8T9T5f38+vLJyWDcfT7JNCtkyrzfm6UsM2zs LM8On0jRcaDGmquNNR0tMIGjf9nPmMqbe9wHkUN/wYpFqQE8Dopk+/l5Rf9+mV1fXnUb NV7A== X-Gm-Message-State: AJIora9NbQ+xX2gu+GNVpnj7zlJ7Sleq+tjWbWswuMdRQkUySWpiYg29 gHO/CYpBqQTzWbsp+YMfFp4N4EeemzI= X-Google-Smtp-Source: AGRyM1vy+2VdN+/DtYB3ycyVQhk3Q38K0zXdVBBS4mfBECN2VYoQ3aRLiAJvpCsCVAQ81RfT98FXOQ== X-Received: by 2002:a17:907:c24:b0:711:d4c6:9161 with SMTP id ga36-20020a1709070c2400b00711d4c69161mr22326368ejc.760.1655744293416; Mon, 20 Jun 2022 09:58:13 -0700 (PDT) Received: from [10.126.3.254] ([109.190.253.11]) by smtp.googlemail.com with ESMTPSA id d5-20020a170906344500b006fed062c68esm6173555ejb.182.2022.06.20.09.58.11 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 20 Jun 2022 09:58:12 -0700 (PDT) Message-ID: Date: Mon, 20 Jun 2022 18:58:07 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1 Subject: [PATCH 3/5][_Hashtable] std::initializer_list insertion To: "libstdc++@gcc.gnu.org" Content-Language: fr X-Spam-Status: No, score=-9.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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: =?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?= Cc: gcc-patches Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" libstdc++: [_Hashtable] Consider all initializer_list elements are inserted When instantiated using an initializer_list the container is pre-sized based on initializer_list size. libstdc++-v3/ChangeLog:     * include/bits/hashtable_policy.h (_Insert_base<>::insert(initializer_list<>)):     Use assignment operator if container is empty and has default bucket count.     * include/bits/hashtable.h (_Hashtable<>(initializer_list<>)): Use initializer_list     size as bucket count hint if user did not provide any value that is to say if it is     the default 0 value.     * testsuite/23_containers/unordered_set/init-list.cc (test02): New test case. Tested under Linux x86_64. François diff --git a/libstdc++-v3/include/bits/hashtable.h b/libstdc++-v3/include/bits/hashtable.h index e53cbaf0644..b0d1bc1f08a 100644 --- a/libstdc++-v3/include/bits/hashtable.h +++ b/libstdc++-v3/include/bits/hashtable.h @@ -575,7 +575,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION const _Hash& __hf = _Hash(), const key_equal& __eql = key_equal(), const allocator_type& __a = allocator_type()) - : _Hashtable(__l.begin(), __l.end(), __bkt_count_hint, + : _Hashtable(__l.begin(), __l.end(), + __bkt_count_hint == 0 ? __l.size() : __bkt_count_hint, __hf, __eql, __a, __unique_keys{}) { } diff --git a/libstdc++-v3/include/bits/hashtable_policy.h b/libstdc++-v3/include/bits/hashtable_policy.h index e848ba1d3f7..139d0ec27df 100644 --- a/libstdc++-v3/include/bits/hashtable_policy.h +++ b/libstdc++-v3/include/bits/hashtable_policy.h @@ -969,7 +969,16 @@ namespace __detail void insert(initializer_list __l) - { this->insert(__l.begin(), __l.end()); } + { + __hashtable& __h = _M_conjure_hashtable(); + if (__h.empty() && __h.bucket_count() == 1) + { + __h = __l; + return; + } + + this->insert(__l.begin(), __l.end()); + } template void diff --git a/libstdc++-v3/testsuite/23_containers/unordered_set/init-list.cc b/libstdc++-v3/testsuite/23_containers/unordered_set/init-list.cc index fc11498c718..70789d03e63 100644 --- a/libstdc++-v3/testsuite/23_containers/unordered_set/init-list.cc +++ b/libstdc++-v3/testsuite/23_containers/unordered_set/init-list.cc @@ -48,8 +48,27 @@ void test01() VERIFY(m.count(1) == 0); } +void test02() +{ + unordered_set u({ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }); + VERIFY( u.size() == 13 ); + VERIFY( u.count(0) == 1 ); + VERIFY( u.count(13) == 0 ); + + auto bkt_count = u.bucket_count(); + u.insert({ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }); + VERIFY( u.size() == 13 ); + VERIFY( u.bucket_count() == bkt_count ); + + u.clear(); + u.insert({ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }); + VERIFY( u.size() == 13 ); + VERIFY( u.bucket_count() == bkt_count ); +} + int main() { __gnu_test::set_memory_limits(); test01(); + test02(); }