From 9c124b1b0a3730978699b8a6420308b5e5ab4e4e Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Mon, 26 Nov 2018 10:37:10 +0100 Subject: Qt 6: Deprecate QHash::insertMulti [ChangeLog][QtCore][QHash] insertMulti(), unite() and values(const Key &key) are now deprecated. Please use QMultiHash instead. Change-Id: Ic14907fd5fd38d585708e2dcf2c0200d221ebb25 Reviewed-by: Timur Pocheptsov --- src/corelib/tools/qhash.h | 250 ++++++++++++++++++++++++++++------------------ 1 file changed, 151 insertions(+), 99 deletions(-) (limited to 'src/corelib/tools/qhash.h') diff --git a/src/corelib/tools/qhash.h b/src/corelib/tools/qhash.h index 89697b1fd1..0b8a0b283d 100644 --- a/src/corelib/tools/qhash.h +++ b/src/corelib/tools/qhash.h @@ -308,12 +308,14 @@ public: T &operator[](const Key &key); const T operator[](const Key &key) const; - QList uniqueKeys() const; QList keys() const; QList keys(const T &value) const; QList values() const; - QList values(const Key &key) const; - int count(const Key &key) const; +#if QT_DEPRECATED_SINCE(5, 15) + QT_DEPRECATED_X("Use QMultiHash for hashes storing multiple values with the same key.") QList uniqueKeys() const; + QT_DEPRECATED_X("Use QMultiHash for hashes storing multiple values with the same key.") QList values(const Key &key) const; + QT_DEPRECATED_X("Use QMultiHash for hashes storing multiple values with the same key.") int count(const Key &key) const; +#endif class const_iterator; @@ -388,6 +390,7 @@ public: { friend class iterator; friend class QHash; + friend class QMultiHash; friend class QSet; QHashData::Node *i; @@ -527,8 +530,10 @@ public: 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); +#if QT_DEPRECATED_SINCE(5, 15) + QT_DEPRECATED_X("Use QMultiHash for hashes storing multiple values with the same key.") iterator insertMulti(const Key &key, const T &value); + QT_DEPRECATED_X("Use QMultiHash for hashes storing multiple values with the same key.") QHash &unite(const QHash &other); +#endif // STL compatibility typedef T mapped_type; @@ -570,6 +575,7 @@ private: #endif } friend class QSet; + friend class QMultiHash; }; @@ -607,38 +613,6 @@ QHash::createNode(uint ah, const Key &akey, const T &avalue, Node **anex return node; } -template -Q_INLINE_TEMPLATE QHash &QHash::unite(const QHash &other) -{ - if (d == &QHashData::shared_null) { - *this = other; - } else { -#if QT_DEPRECATED_SINCE(5, 15) - QHash copy(other); - const_iterator it = copy.constEnd(); - while (it != copy.constBegin()) { - it.i = QHashData::previousNode(it.i); - insertMulti(it.key(), it.value()); - } -#else - QHash copy(other); - const_iterator it = copy.cbegin(); - const const_iterator end = copy.cend(); - while (it != end) { - const auto rangeStart = it++; - while (it != end && rangeStart.key() == it.key()) - ++it; - const qint64 last = std::distance(rangeStart, it) - 1; - for (qint64 i = last; i >= 0; --i) { - auto next = std::next(rangeStart, i); - insertMulti(next.key(), next.value()); - } - } -#endif - } - return *this; -} - template Q_OUTOFLINE_TEMPLATE void QHash::freeData(QHashData *x) { @@ -697,26 +671,6 @@ Q_INLINE_TEMPLATE const T QHash::value(const Key &akey, const T &adefaul } } -template -Q_OUTOFLINE_TEMPLATE QList QHash::uniqueKeys() const -{ - QList res; - res.reserve(size()); // May be too much, but assume short lifetime - const_iterator i = begin(); - if (i != end()) { - for (;;) { - const Key &aKey = i.key(); - res.append(aKey); - do { - if (++i == end()) - goto break_out_of_outer_loop; - } while (aKey == i.key()); - } - } -break_out_of_outer_loop: - return res; -} - template Q_OUTOFLINE_TEMPLATE QList QHash::keys() const { @@ -775,32 +729,6 @@ Q_OUTOFLINE_TEMPLATE QList QHash::values() const return res; } -template -Q_OUTOFLINE_TEMPLATE QList QHash::values(const Key &akey) const -{ - QList res; - Node *node = *findNode(akey); - if (node != e) { - do { - res.append(node->value); - } while ((node = node->next) != e && node->key == akey); - } - return res; -} - -template -Q_OUTOFLINE_TEMPLATE int QHash::count(const Key &akey) const -{ - int cnt = 0; - Node *node = *findNode(akey); - if (node != e) { - do { - ++cnt; - } while ((node = node->next) != e && node->key == akey); - } - return cnt; -} - template Q_INLINE_TEMPLATE const T QHash::operator[](const Key &akey) const { @@ -866,18 +794,6 @@ Q_INLINE_TEMPLATE void QHash::insert(const QHash &hash) } } -template -Q_INLINE_TEMPLATE typename QHash::iterator QHash::insertMulti(const Key &akey, - const T &avalue) -{ - detach(); - d->willGrow(); - - uint h; - Node **nextNode = findNode(akey, &h); - return iterator(createNode(h, akey, avalue, nextNode)); -} - template Q_OUTOFLINE_TEMPLATE int QHash::remove(const Key &akey) { @@ -1128,11 +1044,12 @@ public: inline typename QHash::iterator replace(const Key &key, const T &value) { return QHash::insert(key, value); } - inline typename QHash::iterator insert(const Key &key, const T &value) - { return QHash::insertMulti(key, value); } + typename QHash::iterator insert(const Key &key, const T &value); + + inline QMultiHash &unite(const QMultiHash &other); inline QMultiHash &operator+=(const QMultiHash &other) - { this->unite(other); return *this; } + { return unite(other); } inline QMultiHash operator+(const QMultiHash &other) const { QMultiHash result = *this; result += other; return result; } @@ -1141,13 +1058,27 @@ public: using QHash::count; using QHash::find; using QHash::constFind; + using QHash::values; + using QHash::findNode; + using QHash::createNode; + using QHash::concrete; + using QHash::detach; + + using typename QHash::Node; + using typename QHash::iterator; + using typename QHash::const_iterator; bool contains(const Key &key, const T &value) const; int remove(const Key &key, const T &value); + int count(const Key &key) const; int count(const Key &key, const T &value) const; + QList uniqueKeys() const; + + QList values(const Key &akey) const; + typename QHash::iterator find(const Key &key, const T &value) { typename QHash::iterator i(find(key)); typename QHash::iterator end(this->end()); @@ -1175,6 +1106,50 @@ private: const T operator[](const Key &key) const; }; +template +Q_INLINE_TEMPLATE typename QHash::iterator QMultiHash::insert(const Key &akey, const T &avalue) +{ + detach(); + this->d->willGrow(); + + uint h; + Node **nextNode = findNode(akey, &h); + return iterator(createNode(h, akey, avalue, nextNode)); +} + +template +Q_INLINE_TEMPLATE QMultiHash &QMultiHash::unite(const QMultiHash &other) +{ + if (this->d == &QHashData::shared_null) { + *this = other; + } else { +#if QT_DEPRECATED_SINCE(5, 15) + QMultiHash copy(other); + const_iterator it = copy.constEnd(); + while (it != copy.constBegin()) { + it.i = QHashData::previousNode(it.i); + insert(it.key(), it.value()); + } +#else + const QMultiHash copy(other); + const_iterator it = copy.cbegin(); + const const_iterator end = copy.cend(); + while (it != end) { + const auto rangeStart = it++; + while (it != end && rangeStart.key() == it.key()) + ++it; + const qint64 last = std::distance(rangeStart, it) - 1; + for (qint64 i = last; i >= 0; --i) { + auto next = std::next(rangeStart, i); + insert(next.key(), next.value()); + } + } +#endif + } + return *this; +} + + template Q_INLINE_TEMPLATE bool QMultiHash::contains(const Key &key, const T &value) const { @@ -1212,7 +1187,84 @@ Q_INLINE_TEMPLATE int QMultiHash::count(const Key &key, const T &value) return n; } -template +template +Q_OUTOFLINE_TEMPLATE QList QMultiHash::uniqueKeys() const +{ + QList res; + res.reserve(QHash::size()); // May be too much, but assume short lifetime + typename QHash::const_iterator i = QHash::begin(); + if (i != QHash::end()) { + for (;;) { + const Key &aKey = i.key(); + res.append(aKey); + do { + if (++i == QHash::end()) + goto break_out_of_outer_loop; + } while (aKey == i.key()); + } + } +break_out_of_outer_loop: + return res; +} + +#if QT_DEPRECATED_SINCE(5, 15) + +template +Q_OUTOFLINE_TEMPLATE typename QHash::iterator QHash::insertMulti(const Key &key, const T &value) { + return static_cast *>(this)->insert(key, value); +} + +template +Q_OUTOFLINE_TEMPLATE QHash &QHash::unite(const QHash &other) { + return static_cast *>(this)->unite(other); +} + +template +Q_OUTOFLINE_TEMPLATE QList QHash::values(const Key &akey) const +{ + return static_cast *>(this)->values(akey); +} + +template +Q_OUTOFLINE_TEMPLATE int QHash::count(const Key &akey) const +{ + return static_cast *>(this)->count(akey); +} + +template +Q_OUTOFLINE_TEMPLATE QList QHash::uniqueKeys() const +{ + return static_cast *>(this)->uniqueKeys(); +} +#endif + +template +Q_OUTOFLINE_TEMPLATE QList QMultiHash::values(const Key &akey) const +{ + QList res; + Node *node = *findNode(akey); + if (node != this->e) { + do { + res.append(node->value); + } while ((node = node->next) != this->e && node->key == akey); + } + return res; +} + +template +Q_OUTOFLINE_TEMPLATE int QMultiHash::count(const Key &akey) const +{ + int cnt = 0; + Node *node = *findNode(akey); + if (node != this->e) { + do { + ++cnt; + } while ((node = node->next) != this->e && node->key == akey); + } + return cnt; +} + +template class QHashIterator { typedef typename QHash::const_iterator const_iterator; -- cgit v1.2.3