From patchwork Mon Sep 3 14:24:53 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 965455 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-485020-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="hHgnW6r4"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 423sgk2b1fz9s4Z for ; Tue, 4 Sep 2018 00:25:20 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; q=dns; s= default; b=CK1Qh9DV3Ew3/Y8y179+4hpGdftc7JtTuS/e1OfMOaWQ+fT8IO3Lz A5b6ipe/135ah8CmBxEu5xAfd2Bm811mqJ+B0wB0yf25T0KjzNgvlsBTTEieygbb zzxO8OcWteWPvJOSpf2hP2uTkQFTAKBV7mwRTs2UtwkDN/8Ne30LlU= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; s= default; bh=glQXroeYQSCLHT5bzTkximzRGfc=; b=hHgnW6r4VH+rplEFn0fp upMLRtW5ktkNkxOjrGHUd5gJ6ZtZDPq7NEiH/CO6vonF+zDat1BCJmsLNHjEUNzs GJt4V0cpZ8+wIe70EuGNQdOtlGP8bQLG81ng3I+Xi4ABJAPhB/kXZXvRKruDSaI+ 4hpCy96VJKpsImbGxkR/EGo= Received: (qmail 64563 invoked by alias); 3 Sep 2018 14:25:10 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 64478 invoked by uid 89); 3 Sep 2018 14:25:06 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, SPF_HELO_PASS, TIME_LIMIT_EXCEEDED autolearn=unavailable version=3.3.2 spammy=zz, sk:testsui X-HELO: mx1.redhat.com Received: from mx3-rdu2.redhat.com (HELO mx1.redhat.com) (66.187.233.73) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 03 Sep 2018 14:24:56 +0000 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A09FF8573A; Mon, 3 Sep 2018 14:24:54 +0000 (UTC) Received: from localhost (unknown [10.33.36.76]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3A9FE2166BA4; Mon, 3 Sep 2018 14:24:54 +0000 (UTC) Date: Mon, 3 Sep 2018 15:24:53 +0100 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] PR libstdc++/78595 implement insertion into maps in terms of emplace Message-ID: <20180903142453.GA4064@redhat.com> MIME-Version: 1.0 Content-Disposition: inline X-Clacks-Overhead: GNU Terry Pratchett User-Agent: Mutt/1.9.2 (2017-12-15) C++14 simplified the specification of the generic insert function templates to be equivalent to calling emplace (or emplace_hint). Defining them in terms of emplace takes care of the problems described in PR 78595, ensuring a single conversion to value_type is done at the right time. PR libstdc++/78595 * include/bits/stl_map.h (map::insert(_Pair&&)) (map::insert(const_iterator, _Pair&&)): Do emplace instead of insert. * include/bits/stl_multimap.h (multimap::insert(_Pair&&)) (multimap::insert(const_iterator, _Pair&&)): Likewise. * include/bits/unordered_map.h (unordered_map::insert(_Pair&&)) (unordered_map::insert(const_iterator, _Pair&&)) (unordered_multimap::insert(_Pair&&)) (unordered_multimap::insert(const_iterator, _Pair&&)): Likewise. * testsuite/23_containers/map/modifiers/insert/78595.cc: New test. * testsuite/23_containers/multimap/modifiers/insert/78595.cc: New test. * testsuite/23_containers/unordered_map/modifiers/78595.cc: New test. * testsuite/23_containers/unordered_multimap/modifiers/78595.cc: New test. Tested x86_64-linux, committed to trunk. commit 29118cb0aa7cfee613b2f597421532562fd53318 Author: Jonathan Wakely Date: Mon Sep 3 13:49:09 2018 +0100 PR libstdc++/78595 implement insertion into maps in terms of emplace C++14 simplified the specification of the generic insert function templates to be equivalent to calling emplace (or emplace_hint). Defining them in terms of emplace takes care of the problems described in PR 78595, ensuring a single conversion to value_type is done at the right time. PR libstdc++/78595 * include/bits/stl_map.h (map::insert(_Pair&&)) (map::insert(const_iterator, _Pair&&)): Do emplace instead of insert. * include/bits/stl_multimap.h (multimap::insert(_Pair&&)) (multimap::insert(const_iterator, _Pair&&)): Likewise. * include/bits/unordered_map.h (unordered_map::insert(_Pair&&)) (unordered_map::insert(const_iterator, _Pair&&)) (unordered_multimap::insert(_Pair&&)) (unordered_multimap::insert(const_iterator, _Pair&&)): Likewise. * testsuite/23_containers/map/modifiers/insert/78595.cc: New test. * testsuite/23_containers/multimap/modifiers/insert/78595.cc: New test. * testsuite/23_containers/unordered_map/modifiers/78595.cc: New test. * testsuite/23_containers/unordered_multimap/modifiers/78595.cc: New test. diff --git a/libstdc++-v3/include/bits/stl_map.h b/libstdc++-v3/include/bits/stl_map.h index fdd058b060d..6ce9c3e1f60 100644 --- a/libstdc++-v3/include/bits/stl_map.h +++ b/libstdc++-v3/include/bits/stl_map.h @@ -808,12 +808,11 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER insert(value_type&& __x) { return _M_t._M_insert_unique(std::move(__x)); } - template::value>::type> - std::pair + template + __enable_if_t::value, + pair> insert(_Pair&& __x) - { return _M_t._M_insert_unique(std::forward<_Pair>(__x)); } + { return _M_t._M_emplace_unique(std::forward<_Pair>(__x)); } #endif // @} @@ -869,13 +868,13 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER insert(const_iterator __position, value_type&& __x) { return _M_t._M_insert_unique_(__position, std::move(__x)); } - template::value>::type> - iterator + template + __enable_if_t::value, iterator> insert(const_iterator __position, _Pair&& __x) - { return _M_t._M_insert_unique_(__position, - std::forward<_Pair>(__x)); } + { + return _M_t._M_emplace_hint_unique(__position, + std::forward<_Pair>(__x)); + } #endif // @} diff --git a/libstdc++-v3/include/bits/stl_multimap.h b/libstdc++-v3/include/bits/stl_multimap.h index 9357d1db6aa..d4973870763 100644 --- a/libstdc++-v3/include/bits/stl_multimap.h +++ b/libstdc++-v3/include/bits/stl_multimap.h @@ -544,12 +544,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER insert(value_type&& __x) { return _M_t._M_insert_equal(std::move(__x)); } - template::value>::type> - iterator + template + __enable_if_t::value, iterator> insert(_Pair&& __x) - { return _M_t._M_insert_equal(std::forward<_Pair>(__x)); } + { return _M_t._M_emplace_equal(std::forward<_Pair>(__x)); } #endif // @} @@ -589,13 +587,13 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER insert(const_iterator __position, value_type&& __x) { return _M_t._M_insert_equal_(__position, std::move(__x)); } - template::value>::type> - iterator + template + __enable_if_t::value, iterator> insert(const_iterator __position, _Pair&& __x) - { return _M_t._M_insert_equal_(__position, - std::forward<_Pair>(__x)); } + { + return _M_t._M_emplace_hint_equal(__position, + std::forward<_Pair>(__x)); + } #endif // @} diff --git a/libstdc++-v3/include/bits/unordered_map.h b/libstdc++-v3/include/bits/unordered_map.h index 9a9332f0305..94ce8cdb482 100644 --- a/libstdc++-v3/include/bits/unordered_map.h +++ b/libstdc++-v3/include/bits/unordered_map.h @@ -585,12 +585,11 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER insert(value_type&& __x) { return _M_h.insert(std::move(__x)); } - template::value>::type> - std::pair + template + __enable_if_t::value, + pair> insert(_Pair&& __x) - { return _M_h.insert(std::forward<_Pair>(__x)); } + { return _M_h.emplace(std::forward<_Pair>(__x)); } //@} //@{ @@ -625,12 +624,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER insert(const_iterator __hint, value_type&& __x) { return _M_h.insert(__hint, std::move(__x)); } - template::value>::type> - iterator + template + __enable_if_t::value, iterator> insert(const_iterator __hint, _Pair&& __x) - { return _M_h.insert(__hint, std::forward<_Pair>(__x)); } + { return _M_h.emplace_hint(__hint, std::forward<_Pair>(__x)); } //@} /** @@ -1571,12 +1568,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER insert(value_type&& __x) { return _M_h.insert(std::move(__x)); } - template::value>::type> - iterator + template + __enable_if_t::value, iterator> insert(_Pair&& __x) - { return _M_h.insert(std::forward<_Pair>(__x)); } + { return _M_h.emplace(std::forward<_Pair>(__x)); } //@} //@{ @@ -1609,12 +1604,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER insert(const_iterator __hint, value_type&& __x) { return _M_h.insert(__hint, std::move(__x)); } - template::value>::type> - iterator + template + __enable_if_t::value, iterator> insert(const_iterator __hint, _Pair&& __x) - { return _M_h.insert(__hint, std::forward<_Pair>(__x)); } + { return _M_h.emplace_hint(__hint, std::forward<_Pair>(__x)); } //@} /** diff --git a/libstdc++-v3/testsuite/23_containers/map/modifiers/insert/78595.cc b/libstdc++-v3/testsuite/23_containers/map/modifiers/insert/78595.cc new file mode 100644 index 00000000000..187d165b083 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/map/modifiers/insert/78595.cc @@ -0,0 +1,115 @@ +// Copyright (C) 2018 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-do run { target c++11 } } + +#include +#include + +void +test01() +{ + struct X { + mutable int conversions = 0; + + operator std::pair() const { + if (++conversions > 1) + throw 1; + return {}; + } + }; + + std::map m; + m.insert(X()); + VERIFY( m.size() == 1 ); + m.insert(m.begin(), X()); + VERIFY( m.size() == 1 ); + +} +void +test02() +{ + struct Y { + int conversions = 0; + + operator std::pair() && { + if (++conversions > 1) + throw 1; + return {}; + } + }; + + std::map m; + m.insert(Y()); + VERIFY( m.size() == 1 ); + m.insert(m.begin(), Y()); + VERIFY( m.size() == 1 ); +} + +struct Key { + int key; + bool operator<(const Key& r) const { return key < r.key; } +}; + +struct Z { + operator std::pair() const { return { { z }, 0 }; } + int z; +}; + +template +struct Alloc +{ + Alloc() = default; + + template + Alloc(const Alloc&) { } + + using value_type = T; + + T* allocate(std::size_t n) { return std::allocator().allocate(n); } + + void deallocate(T* p, std::size_t n) { std::allocator().deallocate(p, n); } + + template + void construct(U* p, const Z& z) { ::new (p) U{ { z.z+1 }, 0}; } + + template + bool operator==(const Alloc&) { return true; } + + template + bool operator!=(const Alloc&) { return false; } +}; + +void +test03() +{ + std::map, Alloc>> m; + m.insert(Z{}); + m.insert(Z{}); + VERIFY( m.size() == 1 ); + m.insert(Z{}); + m.insert(Z{1}); + VERIFY( m.size() == 2 ); +} + +int +main() +{ + test01(); + test02(); + test03(); +} diff --git a/libstdc++-v3/testsuite/23_containers/multimap/modifiers/insert/78595.cc b/libstdc++-v3/testsuite/23_containers/multimap/modifiers/insert/78595.cc new file mode 100644 index 00000000000..2f357fcae95 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/multimap/modifiers/insert/78595.cc @@ -0,0 +1,115 @@ +// Copyright (C) 2018 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-do run { target c++11 } } + +#include +#include + +void +test01() +{ + struct X { + mutable int conversions = 0; + + operator std::pair() const { + if (++conversions > 1) + throw 1; + return {}; + } + }; + + std::multimap m; + m.insert(X()); + VERIFY( m.size() == 1 ); + m.insert(m.begin(), X()); + VERIFY( m.size() == 2 ); + +} +void +test02() +{ + struct Y { + int conversions = 0; + + operator std::pair() && { + if (++conversions > 1) + throw 1; + return {}; + } + }; + + std::multimap m; + m.insert(Y()); + VERIFY( m.size() == 1 ); + m.insert(m.begin(), Y()); + VERIFY( m.size() == 2 ); +} + +struct Key { + int key; + bool operator<(const Key& r) const { return key < r.key; } +}; + +struct Z { + operator std::pair() const { return { { z }, 0 }; } + int z; +}; + +template +struct Alloc +{ + Alloc() = default; + + template + Alloc(const Alloc&) { } + + using value_type = T; + + T* allocate(std::size_t n) { return std::allocator().allocate(n); } + + void deallocate(T* p, std::size_t n) { std::allocator().deallocate(p, n); } + + template + void construct(U* p, const Z& z) { ::new (p) U{ { z.z+1 }, 0}; } + + template + bool operator==(const Alloc&) { return true; } + + template + bool operator!=(const Alloc&) { return false; } +}; + +void +test03() +{ + std::multimap, Alloc>> m; + m.insert(Z{}); + m.insert(Z{}); + VERIFY( m.size() == 2 ); + m.insert(Z{}); + m.insert(Z{1}); + VERIFY( m.size() == 4 ); +} + +int +main() +{ + test01(); + test02(); + test03(); +} diff --git a/libstdc++-v3/testsuite/23_containers/unordered_map/modifiers/78595.cc b/libstdc++-v3/testsuite/23_containers/unordered_map/modifiers/78595.cc new file mode 100644 index 00000000000..47cd67b86d8 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/unordered_map/modifiers/78595.cc @@ -0,0 +1,122 @@ +// Copyright (C) 2018 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-do run { target c++11 } } + +#include +#include + +void +test01() +{ + struct X { + mutable int conversions = 0; + + operator std::pair() const { + if (++conversions > 1) + throw 1; + return {}; + } + }; + + std::unordered_map m; + m.insert(X()); + VERIFY( m.size() == 1 ); + m.insert(m.begin(), X()); + VERIFY( m.size() == 1 ); + +} +void +test02() +{ + struct Y { + int conversions = 0; + + operator std::pair() && { + if (++conversions > 1) + throw 1; + return {}; + } + }; + + std::unordered_map m; + m.insert(Y()); + VERIFY( m.size() == 1 ); + m.insert(m.begin(), Y()); + VERIFY( m.size() == 1 ); +} + +struct Key { + int key; + bool operator==(const Key& r) const { return key == r.key; } +}; + +namespace std { + template<> struct hash { + size_t operator()(const Key& k) const { return std::hash()(k.key); } + }; +} + +struct Z { + operator std::pair() const { return { { z }, 0 }; } + int z; +}; + +template +struct Alloc +{ + Alloc() = default; + + template + Alloc(const Alloc&) { } + + using value_type = T; + + T* allocate(std::size_t n) { return std::allocator().allocate(n); } + + void deallocate(T* p, std::size_t n) { std::allocator().deallocate(p, n); } + + template + void construct(U* p, const Z& z) { ::new (p) U{ { z.z+1 }, 0}; } + + template + bool operator==(const Alloc&) { return true; } + + template + bool operator!=(const Alloc&) { return false; } +}; + +void +test03() +{ + std::unordered_map, std::equal_to, + Alloc>> m; + m.insert(Z{}); + m.insert(Z{}); + VERIFY( m.size() == 1 ); + m.insert(Z{}); + m.insert(Z{1}); + VERIFY( m.size() == 2 ); +} + +int +main() +{ + test01(); + test02(); + test03(); +} diff --git a/libstdc++-v3/testsuite/23_containers/unordered_multimap/modifiers/78595.cc b/libstdc++-v3/testsuite/23_containers/unordered_multimap/modifiers/78595.cc new file mode 100644 index 00000000000..e47c20cad81 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/unordered_multimap/modifiers/78595.cc @@ -0,0 +1,122 @@ +// Copyright (C) 2018 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-do run { target c++11 } } + +#include +#include + +void +test01() +{ + struct X { + mutable int conversions = 0; + + operator std::pair() const { + if (++conversions > 1) + throw 1; + return {}; + } + }; + + std::unordered_multimap m; + m.insert(X()); + VERIFY( m.size() == 1 ); + m.insert(m.begin(), X()); + VERIFY( m.size() == 2 ); + +} +void +test02() +{ + struct Y { + int conversions = 0; + + operator std::pair() && { + if (++conversions > 1) + throw 1; + return {}; + } + }; + + std::unordered_multimap m; + m.insert(Y()); + VERIFY( m.size() == 1 ); + m.insert(m.begin(), Y()); + VERIFY( m.size() == 2 ); +} + +struct Key { + int key; + bool operator==(const Key& r) const { return key == r.key; } +}; + +namespace std { + template<> struct hash { + size_t operator()(const Key& k) const { return std::hash()(k.key); } + }; +} + +struct Z { + operator std::pair() const { return { { z }, 0 }; } + int z; +}; + +template +struct Alloc +{ + Alloc() = default; + + template + Alloc(const Alloc&) { } + + using value_type = T; + + T* allocate(std::size_t n) { return std::allocator().allocate(n); } + + void deallocate(T* p, std::size_t n) { std::allocator().deallocate(p, n); } + + template + void construct(U* p, const Z& z) { ::new (p) U{ { z.z+1 }, 0}; } + + template + bool operator==(const Alloc&) { return true; } + + template + bool operator!=(const Alloc&) { return false; } +}; + +void +test03() +{ + std::unordered_multimap, std::equal_to, + Alloc>> m; + m.insert(Z{}); + m.insert(Z{}); + VERIFY( m.size() == 2 ); + m.insert(Z{}); + m.insert(Z{1}); + VERIFY( m.size() == 4 ); +} + +int +main() +{ + test01(); + test02(); + test03(); +}