From f9a154e07f74199ba856ab7b0f7309a4ff93afe0 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Wed, 26 Feb 2020 22:32:13 +0100 Subject: Use qsizetype for size related methods in QMap MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This allows QMap to hold more than 2^31 entries on 64 bit systeems. Change-Id: Ia6abd3441f9bc0c7e1a01b78726b5c32209542fa Reviewed-by: MÃ¥rten Nordheim --- src/corelib/tools/qmap.cpp | 38 +++++++++++++++---------------- src/corelib/tools/qmap.h | 56 +++++++++++++++++++++++----------------------- 2 files changed, 47 insertions(+), 47 deletions(-) diff --git a/src/corelib/tools/qmap.cpp b/src/corelib/tools/qmap.cpp index e2582ccdf4..1dcbaeec94 100644 --- a/src/corelib/tools/qmap.cpp +++ b/src/corelib/tools/qmap.cpp @@ -301,7 +301,7 @@ void QMapDataBase::recalcMostLeftNode() mostLeftNode = mostLeftNode->left; } -static inline int qMapAlignmentThreshold() +static inline size_t qMapAlignmentThreshold() { // malloc on 32-bit platforms should return pointers that are 8-byte // aligned or more while on 64-bit platforms they should be 16-byte aligned @@ -309,14 +309,14 @@ static inline int qMapAlignmentThreshold() return 2 * sizeof(void*); } -static inline void *qMapAllocate(int alloc, int alignment) +static inline void *qMapAllocate(size_t alloc, size_t alignment) { return alignment > qMapAlignmentThreshold() ? qMallocAligned(alloc, alignment) : ::malloc(alloc); } -static inline void qMapDeallocate(QMapNodeBase *node, int alignment) +static inline void qMapDeallocate(QMapNodeBase *node, size_t alignment) { if (alignment > qMapAlignmentThreshold()) qFreeAligned(node); @@ -324,7 +324,7 @@ static inline void qMapDeallocate(QMapNodeBase *node, int alignment) ::free(node); } -QMapNodeBase *QMapDataBase::createNode(int alloc, int alignment, QMapNodeBase *parent, bool left) +QMapNodeBase *QMapDataBase::createNode(size_t alloc, size_t alignment, QMapNodeBase *parent, bool left) { QMapNodeBase *node = static_cast(qMapAllocate(alloc, alignment)); Q_CHECK_PTR(node); @@ -346,7 +346,7 @@ QMapNodeBase *QMapDataBase::createNode(int alloc, int alignment, QMapNodeBase *p return node; } -void QMapDataBase::freeTree(QMapNodeBase *root, int alignment) +void QMapDataBase::freeTree(QMapNodeBase *root, size_t alignment) { if (root->left) freeTree(root->left, alignment); @@ -619,7 +619,7 @@ void QMapDataBase::freeData(QMapDataBase *d) \sa operator==() */ -/*! \fn template int QMap::size() const +/*! \fn template qsizetype QMap::size() const Returns the number of (key, value) pairs in the map. @@ -672,7 +672,7 @@ void QMapDataBase::freeData(QMapDataBase *d) \sa remove() */ -/*! \fn template int QMap::remove(const Key &key) +/*! \fn template qsizetype QMap::remove(const Key &key) Removes all the items that have the key \a key from the map. Returns the number of items removed which will be 1 if the key @@ -813,14 +813,14 @@ void QMapDataBase::freeData(QMapDataBase *d) \sa QMultiMap::values() */ -/*! \fn template int QMap::count(const Key &key) const +/*! \fn template qsizetype QMap::count(const Key &key) const Returns the number of items associated with key \a key. \sa contains(), QMultiMap::count() */ -/*! \fn template int QMap::count() const +/*! \fn template qsizetype QMap::count() const \overload @@ -1467,7 +1467,7 @@ void QMapDataBase::freeData(QMapDataBase *d) current item. */ -/*! \fn template QMap::iterator QMap::iterator::operator+(int j) const +/*! \fn template QMap::iterator QMap::iterator::operator+(qsizetype j) const Returns an iterator to the item at \a j positions forward from this iterator. (If \a j is negative, the iterator goes backward.) @@ -1478,7 +1478,7 @@ void QMapDataBase::freeData(QMapDataBase *d) */ -/*! \fn template QMap::iterator QMap::iterator::operator-(int j) const +/*! \fn template QMap::iterator QMap::iterator::operator-(qsizetype j) const Returns an iterator to the item at \a j positions backward from this iterator. (If \a j is negative, the iterator goes forward.) @@ -1488,7 +1488,7 @@ void QMapDataBase::freeData(QMapDataBase *d) \sa operator+() */ -/*! \fn template QMap::iterator &QMap::iterator::operator+=(int j) +/*! \fn template QMap::iterator &QMap::iterator::operator+=(qsizetype j) Advances the iterator by \a j items. (If \a j is negative, the iterator goes backward.) @@ -1496,7 +1496,7 @@ void QMapDataBase::freeData(QMapDataBase *d) \sa operator-=(), operator+() */ -/*! \fn template QMap::iterator &QMap::iterator::operator-=(int j) +/*! \fn template QMap::iterator &QMap::iterator::operator-=(qsizetype j) Makes the iterator go back by \a j items. (If \a j is negative, the iterator goes forward.) @@ -1681,7 +1681,7 @@ void QMapDataBase::freeData(QMapDataBase *d) current item. */ -/*! \fn template QMap::const_iterator QMap::const_iterator::operator+(int j) const +/*! \fn template QMap::const_iterator QMap::const_iterator::operator+(qsizetype j) const Returns an iterator to the item at \a j positions forward from this iterator. (If \a j is negative, the iterator goes backward.) @@ -1691,7 +1691,7 @@ void QMapDataBase::freeData(QMapDataBase *d) \sa operator-() */ -/*! \fn template QMap::const_iterator QMap::const_iterator::operator-(int j) const +/*! \fn template QMap::const_iterator QMap::const_iterator::operator-(qsizetype j) const Returns an iterator to the item at \a j positions backward from this iterator. (If \a j is negative, the iterator goes forward.) @@ -1701,7 +1701,7 @@ void QMapDataBase::freeData(QMapDataBase *d) \sa operator+() */ -/*! \fn template QMap::const_iterator &QMap::const_iterator::operator+=(int j) +/*! \fn template QMap::const_iterator &QMap::const_iterator::operator+=(qsizetype j) Advances the iterator by \a j items. (If \a j is negative, the iterator goes backward.) @@ -1711,7 +1711,7 @@ void QMapDataBase::freeData(QMapDataBase *d) \sa operator-=(), operator+() */ -/*! \fn template QMap::const_iterator &QMap::const_iterator::operator-=(int j) +/*! \fn template QMap::const_iterator &QMap::const_iterator::operator-=(qsizetype j) Makes the iterator go back by \a j items. (If \a j is negative, the iterator goes forward.) @@ -2043,7 +2043,7 @@ void QMapDataBase::freeData(QMapDataBase *d) */ /*! - \fn template int QMultiMap::remove(const Key &key, const T &value) + \fn template qsizetype QMultiMap::remove(const Key &key, const T &value) \since 4.3 Removes all the items that have the key \a key and the value \a @@ -2053,7 +2053,7 @@ void QMapDataBase::freeData(QMapDataBase *d) */ /*! - \fn template int QMultiMap::count(const Key &key, const T &value) const + \fn template qsizetype QMultiMap::count(const Key &key, const T &value) const \since 4.3 Returns the number of items with key \a key and value \a value. diff --git a/src/corelib/tools/qmap.h b/src/corelib/tools/qmap.h index be8ba6c4ba..848ba5c4d4 100644 --- a/src/corelib/tools/qmap.h +++ b/src/corelib/tools/qmap.h @@ -180,7 +180,7 @@ inline QMapNode *QMapNode::upperBound(const Key &akey) struct Q_CORE_EXPORT QMapDataBase { QtPrivate::RefCount ref; - int size; + qsizetype size; QMapNodeBase header; QMapNodeBase *mostLeftNode; @@ -190,8 +190,8 @@ struct Q_CORE_EXPORT QMapDataBase void freeNodeAndRebalance(QMapNodeBase *z); void recalcMostLeftNode(); - QMapNodeBase *createNode(int size, int alignment, QMapNodeBase *parent, bool left); - void freeTree(QMapNodeBase *root, int alignment); + QMapNodeBase *createNode(size_t size, size_t alignment, QMapNodeBase *parent, bool left); + void freeTree(QMapNodeBase *root, size_t alignment); static const QMapDataBase shared_null; @@ -350,7 +350,7 @@ public: bool operator==(const QMap &other) const; inline bool operator!=(const QMap &other) const { return !(*this == other); } - inline int size() const { return d->size; } + inline qsizetype size() const { return d->size; } inline bool isEmpty() const { return d->size == 0; } @@ -360,7 +360,7 @@ public: void clear(); - int remove(const Key &key); + qsizetype remove(const Key &key); T take(const Key &key); bool contains(const Key &key) const; @@ -376,7 +376,7 @@ public: QT_DEPRECATED_VERSION_X_5_15("Use QMultiMap for maps storing multiple values with the same key.") QList uniqueKeys() const; QT_DEPRECATED_VERSION_X_5_15("Use QMultiMap for maps storing multiple values with the same key.") QList values(const Key &key) const; #endif - int count(const Key &key) const; + qsizetype count(const Key &key) const; inline const Key &firstKey() const { Q_ASSERT(!isEmpty()); return constBegin().key(); } @@ -429,12 +429,12 @@ public: i = i->previousNode(); return r; } - inline iterator operator+(int j) const + inline iterator operator+(qsizetype j) const { iterator r = *this; if (j > 0) while (j--) ++r; else while (j++) --r; return r; } - inline iterator operator-(int j) const { return operator+(-j); } - inline iterator &operator+=(int j) { return *this = *this + j; } - inline iterator &operator-=(int j) { return *this = *this - j; } - friend inline iterator operator+(int j, iterator k) { return k + j; } + inline iterator operator-(qsizetype j) const { return operator+(-j); } + inline iterator &operator+=(qsizetype j) { return *this = *this + j; } + inline iterator &operator-=(qsizetype j) { return *this = *this - j; } + friend inline iterator operator+(qsizetype j, iterator k) { return k + j; } inline bool operator==(const const_iterator &o) const { return i == o.i; } inline bool operator!=(const const_iterator &o) const { return i != o.i; } @@ -484,12 +484,12 @@ public: i = i->previousNode(); return r; } - inline const_iterator operator+(int j) const + inline const_iterator operator+(qsizetype j) const { const_iterator r = *this; if (j > 0) while (j--) ++r; else while (j++) --r; return r; } - inline const_iterator operator-(int j) const { return operator+(-j); } - inline const_iterator &operator+=(int j) { return *this = *this + j; } - inline const_iterator &operator-=(int j) { return *this = *this - j; } - friend inline const_iterator operator+(int j, const_iterator k) { return k + j; } + inline const_iterator operator-(qsizetype j) const { return operator+(-j); } + inline const_iterator &operator+=(qsizetype j) { return *this = *this + j; } + inline const_iterator &operator-=(qsizetype j) { return *this = *this - j; } + friend inline const_iterator operator+(qsizetype j, const_iterator k) { return k + j; } friend class QMap; friend class QMultiMap; @@ -547,7 +547,7 @@ public: // more Qt typedef iterator Iterator; typedef const_iterator ConstIterator; - inline int count() const { return d->size; } + inline qsizetype count() const { return d->size; } iterator find(const Key &key); const_iterator find(const Key &key) const; const_iterator constFind(const Key &key) const; @@ -568,7 +568,7 @@ public: typedef Key key_type; typedef T mapped_type; typedef qptrdiff difference_type; - typedef int size_type; + typedef qsizetype size_type; inline bool empty() const { return isEmpty(); } QPair equal_range(const Key &akey); QPair equal_range(const Key &akey) const; @@ -655,7 +655,7 @@ Q_INLINE_TEMPLATE T &QMap::operator[](const Key &akey) } template -Q_INLINE_TEMPLATE int QMap::count(const Key &akey) const +Q_INLINE_TEMPLATE qsizetype QMap::count(const Key &akey) const { Node *firstNode; Node *lastNode; @@ -878,10 +878,10 @@ void QMap::dump() const #endif template -Q_OUTOFLINE_TEMPLATE int QMap::remove(const Key &akey) +Q_OUTOFLINE_TEMPLATE qsizetype QMap::remove(const Key &akey) { detach(); - int n = 0; + qsizetype n = 0; while (Node *node = d->findNode(akey)) { d->deleteNode(node); ++n; @@ -914,7 +914,7 @@ Q_OUTOFLINE_TEMPLATE typename QMap::iterator QMap::erase(iterato if (d->ref.isShared()) { const_iterator oldBegin = constBegin(); const_iterator old = const_iterator(it); - int backStepsWithSameKey = 0; + qsizetype backStepsWithSameKey = 0; while (old != oldBegin) { --old; @@ -1131,9 +1131,9 @@ public: bool contains(const Key &key, const T &value) const; - int remove(const Key &key, const T &value); + qsizetype remove(const Key &key, const T &value); - int count(const Key &key, const T &value) const; + qsizetype count(const Key &key, const T &value) const; typename QMap::iterator find(const Key &key, const T &value) { typename QMap::iterator i(find(key)); @@ -1289,9 +1289,9 @@ Q_INLINE_TEMPLATE bool QMultiMap::contains(const Key &key, const T &valu } template -Q_INLINE_TEMPLATE int QMultiMap::remove(const Key &key, const T &value) +Q_INLINE_TEMPLATE qsizetype QMultiMap::remove(const Key &key, const T &value) { - int n = 0; + qsizetype n = 0; typename QMap::iterator i(find(key)); typename QMap::iterator end(QMap::end()); while (i != end && !qMapLessThanKey(key, i.key())) { @@ -1306,9 +1306,9 @@ Q_INLINE_TEMPLATE int QMultiMap::remove(const Key &key, const T &value) } template -Q_INLINE_TEMPLATE int QMultiMap::count(const Key &key, const T &value) const +Q_INLINE_TEMPLATE qsizetype QMultiMap::count(const Key &key, const T &value) const { - int n = 0; + qsizetype n = 0; typename QMap::const_iterator i(constFind(key)); typename QMap::const_iterator end(QMap::constEnd()); while (i != end && !qMapLessThanKey(key, i.key())) { -- cgit v1.2.3