From f8c30759d98603908b408464ea6d98413fc81eec Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Mon, 26 Oct 2020 09:30:49 +0100 Subject: [PATCH] Make the QMultiHash(const QHash &) constructor explicit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit And add a QMultiHash::unite(const QHash &) method to avoid a copy of the data when inserting a QHash into a multi hash. Change-Id: I864aa9d2b9b7b2c367c3c4d140a2ce2f5408ae09 Reviewed-by: MÃ¥rten Nordheim Reviewed-by: Thiago Macieira --- src/corelib/tools/qhash.cpp | 10 ++++++++++ src/corelib/tools/qhash.h | 9 ++++++++- src/tools/moc/generator.cpp | 2 +- tests/auto/corelib/tools/qhash/tst_qhash.cpp | 6 ++++++ 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/corelib/tools/qhash.cpp b/src/corelib/tools/qhash.cpp index 9ca49f13eb4..112a492526b 100644 --- a/src/corelib/tools/qhash.cpp +++ b/src/corelib/tools/qhash.cpp @@ -2538,6 +2538,16 @@ size_t qHash(long double key, size_t seed) noexcept \sa insert() */ + +/*! \fn template QMultiHash &QMultiHash::unite(const QHash &other) + \since 6.0 + + Inserts all the items in the \a other hash into this hash + and returns a reference to this hash. + + \sa insert() +*/ + /*! \fn template QList QMultiHash::uniqueKeys() const \since 5.13 diff --git a/src/corelib/tools/qhash.h b/src/corelib/tools/qhash.h index 67b3e026df8..1f0d5d76ade 100644 --- a/src/corelib/tools/qhash.h +++ b/src/corelib/tools/qhash.h @@ -1219,7 +1219,7 @@ public: return *this; } - QMultiHash(const QHash &other) + explicit QMultiHash(const QHash &other) : QMultiHash(other.begin(), other.end()) {} void swap(QMultiHash &other) noexcept { qSwap(d, other.d); qSwap(m_size, other.m_size); } @@ -1808,6 +1808,13 @@ public: return *this; } + QMultiHash &unite(const QHash &other) + { + for (auto cit = other.cbegin(); cit != other.cend(); ++cit) + insert(cit.key(), *cit); + return *this; + } + QPair equal_range(const Key &key) { detach(); diff --git a/src/tools/moc/generator.cpp b/src/tools/moc/generator.cpp index 9a1998e764a..78829ce93c0 100644 --- a/src/tools/moc/generator.cpp +++ b/src/tools/moc/generator.cpp @@ -465,7 +465,7 @@ void Generator::generateCode() // Build extra array // QList extraList; - QMultiHash knownExtraMetaObject = knownGadgets; + QMultiHash knownExtraMetaObject(knownGadgets); knownExtraMetaObject.unite(knownQObjectClasses); for (int i = 0; i < cdef->propertyList.count(); ++i) { diff --git a/tests/auto/corelib/tools/qhash/tst_qhash.cpp b/tests/auto/corelib/tools/qhash/tst_qhash.cpp index d63ed7043ec..3174bd60b86 100644 --- a/tests/auto/corelib/tools/qhash/tst_qhash.cpp +++ b/tests/auto/corelib/tools/qhash/tst_qhash.cpp @@ -1320,6 +1320,12 @@ void tst_QHash::qmultihash_specific() QVERIFY(map1.remove(42,5)); QVERIFY(map2.remove(42,5)); QVERIFY(map1 == map2); + + QHash hash; + hash.insert(-1, -1); + map2.unite(hash); + QCOMPARE(map2.count(), 6); + QCOMPARE(map2[-1], -1); } }