From 7b34da9ef12554025bb4a8f4750e5807cc37f38c Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Mon, 3 Dec 2018 11:16:18 +0100 Subject: Add QHash::insert(const QHash &other) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As opposed to unite(), this inserts one hash into the other without duplicating elements. Change-Id: Ifc786c48f5dc3ab18c29782e73eac3c1a3ef8981 Reviewed-by: Anton Kudryavtsev Reviewed-by: MÃ¥rten Nordheim Reviewed-by: Lars Knoll --- src/corelib/tools/qhash.cpp | 12 ++++++++++++ src/corelib/tools/qhash.h | 26 ++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) (limited to 'src') diff --git a/src/corelib/tools/qhash.cpp b/src/corelib/tools/qhash.cpp index a53d6db9975..dcac91778f0 100644 --- a/src/corelib/tools/qhash.cpp +++ b/src/corelib/tools/qhash.cpp @@ -1807,6 +1807,18 @@ uint qHash(long double key, uint seed) noexcept \sa insertMulti() */ +/*! \fn template void QHash::insert(const QHash &other) + \since 5.15 + + Inserts all the items in the \a other hash into this hash. + + If a key is common to both hashes, its value will be replaced with the + value stored in \a other. + + \note If \a other contains multiple entries with the same key then the + final value of the key is undefined. +*/ + /*! \fn template QHash::iterator QHash::insertMulti(const Key &key, const T &value) Inserts a new item with the \a key and a value of \a value. diff --git a/src/corelib/tools/qhash.h b/src/corelib/tools/qhash.h index 42f8dbd155b..89697b1fd16 100644 --- a/src/corelib/tools/qhash.h +++ b/src/corelib/tools/qhash.h @@ -526,6 +526,7 @@ public: const_iterator find(const Key &key) const; const_iterator constFind(const Key &key) const; iterator insert(const Key &key, const T &value); + void insert(const QHash &hash); iterator insertMulti(const Key &key, const T &value); QHash &unite(const QHash &other); @@ -840,6 +841,31 @@ Q_INLINE_TEMPLATE typename QHash::iterator QHash::insert(const K return iterator(*node); } +template +Q_INLINE_TEMPLATE void QHash::insert(const QHash &hash) +{ + if (d == hash.d) + return; + + detach(); + + QHashData::Node *i = hash.d->firstNode(); + QHashData::Node *end = reinterpret_cast(hash.e); + while (i != end) { + Node *n = concrete(i); + Node **node = findNode(n->key, n->h); + if (*node == e) { + if (d->willGrow()) + node = findNode(n->key, n->h); + createNode(n->h, n->key, n->value, node); + } else { + if (!std::is_same::value) + (*node)->value = n->value; + } + i = QHashData::nextNode(i); + } +} + template Q_INLINE_TEMPLATE typename QHash::iterator QHash::insertMulti(const Key &akey, const T &avalue) -- cgit v1.2.3