Make the QMultiHash(const QHash &) constructor explicit

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 <marten.nordheim@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Lars Knoll 2020-10-26 09:30:49 +01:00
parent a9c52dbdf4
commit f8c30759d9
4 changed files with 25 additions and 2 deletions

View File

@ -2538,6 +2538,16 @@ size_t qHash(long double key, size_t seed) noexcept
\sa insert()
*/
/*! \fn template <class Key, class T> QMultiHash &QMultiHash<Key, T>::unite(const QHash<Key, T> &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 <class Key, class T> QList<Key> QMultiHash<Key, T>::uniqueKeys() const
\since 5.13

View File

@ -1219,7 +1219,7 @@ public:
return *this;
}
QMultiHash(const QHash<Key, T> &other)
explicit QMultiHash(const QHash<Key, T> &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<Key, T> &other)
{
for (auto cit = other.cbegin(); cit != other.cend(); ++cit)
insert(cit.key(), *cit);
return *this;
}
QPair<iterator, iterator> equal_range(const Key &key)
{
detach();

View File

@ -465,7 +465,7 @@ void Generator::generateCode()
// Build extra array
//
QList<QByteArray> extraList;
QMultiHash<QByteArray, QByteArray> knownExtraMetaObject = knownGadgets;
QMultiHash<QByteArray, QByteArray> knownExtraMetaObject(knownGadgets);
knownExtraMetaObject.unite(knownQObjectClasses);
for (int i = 0; i < cdef->propertyList.count(); ++i) {

View File

@ -1320,6 +1320,12 @@ void tst_QHash::qmultihash_specific()
QVERIFY(map1.remove(42,5));
QVERIFY(map2.remove(42,5));
QVERIFY(map1 == map2);
QHash<int, int> hash;
hash.insert(-1, -1);
map2.unite(hash);
QCOMPARE(map2.count(), 6);
QCOMPARE(map2[-1], -1);
}
}