diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2020-09-23 11:37:40 +0200 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2020-09-29 08:53:00 +0200 |
commit | a45920c86ce7266f11b9aabf33e18c4e7632db23 (patch) | |
tree | e07b492d2ed95df2e5b9f47401761af1064713eb /src/corelib/tools | |
parent | 92e108bff83ac92b2fa2cc2bb77c2501e259814f (diff) |
Switch QBitArray to qsizetype
To make it consistent with other containers in Qt6.
Change-Id: I5578845390248baf80daa282237b706857e57661
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Diffstat (limited to 'src/corelib/tools')
-rw-r--r-- | src/corelib/tools/qbitarray.cpp | 118 | ||||
-rw-r--r-- | src/corelib/tools/qbitarray.h | 70 | ||||
-rw-r--r-- | src/corelib/tools/qhash.cpp | 4 |
3 files changed, 96 insertions, 96 deletions
diff --git a/src/corelib/tools/qbitarray.cpp b/src/corelib/tools/qbitarray.cpp index 9bcd6a9306..c13604abb8 100644 --- a/src/corelib/tools/qbitarray.cpp +++ b/src/corelib/tools/qbitarray.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2020 The Qt Company Ltd. ** Copyright (C) 2019 Intel Corporation. ** Contact: https://www.qt.io/licensing/ ** @@ -137,14 +137,14 @@ QT_BEGIN_NAMESPACE * *d.constData() is the QByteArray's terminating NUL (0) byte. * * This allows for fast calculation of the bit array size: - * inline int size() const { return (d.size() << 3) - *d.constData(); } + * inline qsizetype size() const { return (d.size() << 3) - *d.constData(); } */ /*! Constructs a bit array containing \a size bits. The bits are initialized with \a value, which defaults to false (0). */ -QBitArray::QBitArray(int size, bool value) +QBitArray::QBitArray(qsizetype size, bool value) : d(size <= 0 ? 0 : 1 + (size + 7)/8, Qt::Uninitialized) { Q_ASSERT_X(size >= 0, "QBitArray::QBitArray", "Size must be greater than or equal to 0."); @@ -158,14 +158,14 @@ QBitArray::QBitArray(int size, bool value) *(c+1+size/8) &= (1 << (size & 7)) - 1; } -/*! \fn int QBitArray::size() const +/*! \fn qsizetype QBitArray::size() const Returns the number of bits stored in the bit array. \sa resize() */ -/*! \fn int QBitArray::count() const +/*! \fn qsizetype QBitArray::count() const Same as size(). */ @@ -175,9 +175,9 @@ QBitArray::QBitArray(int size, bool value) 1-bits stored in the bit array; otherwise the number of 0-bits is returned. */ -int QBitArray::count(bool on) const +qsizetype QBitArray::count(bool on) const { - int numBits = 0; + qsizetype numBits = 0; const quint8 *bits = reinterpret_cast<const quint8 *>(d.data()) + 1; // the loops below will try to read from *end @@ -187,20 +187,20 @@ int QBitArray::count(bool on) const while (bits + 7 <= end) { quint64 v = qFromUnaligned<quint64>(bits); bits += 8; - numBits += int(qPopulationCount(v)); + numBits += qsizetype(qPopulationCount(v)); } if (bits + 3 <= end) { quint32 v = qFromUnaligned<quint32>(bits); bits += 4; - numBits += int(qPopulationCount(v)); + numBits += qsizetype(qPopulationCount(v)); } if (bits + 1 < end) { quint16 v = qFromUnaligned<quint16>(bits); bits += 2; - numBits += int(qPopulationCount(v)); + numBits += qsizetype(qPopulationCount(v)); } if (bits < end) - numBits += int(qPopulationCount(bits[0])); + numBits += qsizetype(qPopulationCount(bits[0])); return on ? numBits : size() - numBits; } @@ -217,12 +217,12 @@ int QBitArray::count(bool on) const \sa size() */ -void QBitArray::resize(int size) +void QBitArray::resize(qsizetype size) { if (!size) { d.resize(0); } else { - int s = d.size(); + qsizetype s = d.size(); d.resize(1 + (size+7)/8); uchar* c = reinterpret_cast<uchar*>(d.data()); if (size > (s << 3)) @@ -256,7 +256,7 @@ void QBitArray::resize(int size) \sa isEmpty() */ -/*! \fn bool QBitArray::fill(bool value, int size = -1) +/*! \fn bool QBitArray::fill(bool value, qsizetype size = -1) Sets every bit in the bit array to \a value, returning true if successful; otherwise returns \c false. If \a size is different from -1 (the default), @@ -285,14 +285,14 @@ void QBitArray::resize(int size) \snippet code/src_corelib_tools_qbitarray.cpp 15 */ -void QBitArray::fill(bool value, int begin, int end) +void QBitArray::fill(bool value, qsizetype begin, qsizetype end) { while (begin < end && begin & 0x7) setBit(begin++, value); - int len = end - begin; + qsizetype len = end - begin; if (len <= 0) return; - int s = len & ~0x7; + qsizetype s = len & ~qsizetype(0x7); uchar *c = reinterpret_cast<uchar*>(d.data()); memset(c + (begin >> 3) + 1, value ? 0xff : 0, s >> 3); begin += s; @@ -391,7 +391,7 @@ quint32 QBitArray::toUInt32(QSysInfo::Endian endianness, bool *ok) const noexcep \sa resize(), isEmpty() */ -/*! \fn void QBitArray::truncate(int pos) +/*! \fn void QBitArray::truncate(qsizetype pos) Truncates the bit array at index position \a pos. @@ -400,7 +400,7 @@ quint32 QBitArray::toUInt32(QSysInfo::Endian endianness, bool *ok) const noexcep \sa resize() */ -/*! \fn bool QBitArray::toggleBit(int i) +/*! \fn bool QBitArray::toggleBit(qsizetype i) Inverts the value of the bit at index position \a i, returning the previous value of that bit as either true (if it was set) or false (if @@ -415,7 +415,7 @@ quint32 QBitArray::toUInt32(QSysInfo::Endian endianness, bool *ok) const noexcep \sa setBit(), clearBit() */ -/*! \fn bool QBitArray::testBit(int i) const +/*! \fn bool QBitArray::testBit(qsizetype i) const Returns \c true if the bit at index position \a i is 1; otherwise returns \c false. @@ -426,7 +426,7 @@ quint32 QBitArray::toUInt32(QSysInfo::Endian endianness, bool *ok) const noexcep \sa setBit(), clearBit() */ -/*! \fn bool QBitArray::setBit(int i) +/*! \fn bool QBitArray::setBit(qsizetype i) Sets the bit at index position \a i to 1. @@ -436,14 +436,14 @@ quint32 QBitArray::toUInt32(QSysInfo::Endian endianness, bool *ok) const noexcep \sa clearBit(), toggleBit() */ -/*! \fn void QBitArray::setBit(int i, bool value) +/*! \fn void QBitArray::setBit(qsizetype i, bool value) \overload Sets the bit at index position \a i to \a value. */ -/*! \fn void QBitArray::clearBit(int i) +/*! \fn void QBitArray::clearBit(qsizetype i) Sets the bit at index position \a i to 0. @@ -453,7 +453,7 @@ quint32 QBitArray::toUInt32(QSysInfo::Endian endianness, bool *ok) const noexcep \sa setBit(), toggleBit() */ -/*! \fn bool QBitArray::at(int i) const +/*! \fn bool QBitArray::at(qsizetype i) const Returns the value of the bit at index position \a i. @@ -463,7 +463,7 @@ quint32 QBitArray::toUInt32(QSysInfo::Endian endianness, bool *ok) const noexcep \sa operator[]() */ -/*! \fn QBitRef QBitArray::operator[](int i) +/*! \fn QBitRef QBitArray::operator[](qsizetype i) Returns the bit at index position \a i as a modifiable reference. @@ -484,17 +484,7 @@ quint32 QBitArray::toUInt32(QSysInfo::Endian endianness, bool *ok) const noexcep \sa at(), testBit(), setBit(), clearBit() */ -/*! \fn bool QBitArray::operator[](int i) const - - \overload -*/ - -/*! \fn QBitRef QBitArray::operator[](uint i) - - \overload -*/ - -/*! \fn bool QBitArray::operator[](uint i) const +/*! \fn bool QBitArray::operator[](qsizetype i) const \overload */ @@ -567,8 +557,8 @@ QBitArray &QBitArray::operator&=(const QBitArray &other) resize(qMax(size(), other.size())); uchar *a1 = reinterpret_cast<uchar*>(d.data()) + 1; const uchar *a2 = reinterpret_cast<const uchar*>(other.d.constData()) + 1; - int n = other.d.size() -1 ; - int p = d.size() - 1 - n; + qsizetype n = other.d.size() -1 ; + qsizetype p = d.size() - 1 - n; while (n-- > 0) *a1++ &= *a2++; while (p-- > 0) @@ -596,7 +586,7 @@ QBitArray &QBitArray::operator|=(const QBitArray &other) resize(qMax(size(), other.size())); uchar *a1 = reinterpret_cast<uchar*>(d.data()) + 1; const uchar *a2 = reinterpret_cast<const uchar *>(other.d.constData()) + 1; - int n = other.d.size() - 1; + qsizetype n = other.d.size() - 1; while (n-- > 0) *a1++ |= *a2++; return *this; @@ -622,7 +612,7 @@ QBitArray &QBitArray::operator^=(const QBitArray &other) resize(qMax(size(), other.size())); uchar *a1 = reinterpret_cast<uchar*>(d.data()) + 1; const uchar *a2 = reinterpret_cast<const uchar *>(other.d.constData()) + 1; - int n = other.d.size() - 1; + qsizetype n = other.d.size() - 1; while (n-- > 0) *a1++ ^= *a2++; return *this; @@ -640,11 +630,11 @@ QBitArray &QBitArray::operator^=(const QBitArray &other) QBitArray QBitArray::operator~() const { - int sz = size(); + qsizetype sz = size(); QBitArray a(sz); const uchar *a1 = reinterpret_cast<const uchar *>(d.constData()) + 1; uchar *a2 = reinterpret_cast<uchar*>(a.d.data()) + 1; - int n = d.size() - 1; + qsizetype n = d.size() - 1; while (n-- > 0) *a2++ = ~*a1++; @@ -735,7 +725,7 @@ QBitArray operator^(const QBitArray &a1, const QBitArray &a2) It is not for use in any other context. */ -/*! \fn QBitRef::QBitRef (QBitArray& a, int i) +/*! \fn QBitRef::QBitRef (QBitArray& a, qsizetype i) Constructs a reference to element \a i in the QBitArray \a a. This is what QBitArray::operator[] constructs its return value @@ -780,11 +770,19 @@ QBitArray operator^(const QBitArray &a1, const QBitArray &a2) QDataStream &operator<<(QDataStream &out, const QBitArray &ba) { - quint32 len = ba.size(); - out << len; - if (len > 0) - out.writeRawData(ba.d.constData() + 1, ba.d.size() - 1); - return out; + if (out.version() < QDataStream::Qt_6_0) { + quint32 len = ba.size(); + out << len; + if (len > 0) + out.writeRawData(ba.d.constData() + 1, ba.d.size() - 1); + return out; + } else { + quint64 len = ba.size(); + out << len; + if (len > 0) + out.writeRawData(ba.d.constData() + 1, ba.d.size() - 1); + return out; + } } /*! @@ -798,19 +796,27 @@ QDataStream &operator<<(QDataStream &out, const QBitArray &ba) QDataStream &operator>>(QDataStream &in, QBitArray &ba) { ba.clear(); - quint32 len; - in >> len; + qsizetype len; + if (in.version() < QDataStream::Qt_6_0) { + quint32 tmp; + in >> tmp; + len = tmp; + } else { + quint64 tmp; + in >> tmp; + len = tmp; + } if (len == 0) { ba.clear(); return in; } - const quint32 Step = 8 * 1024 * 1024; - quint32 totalBytes = (len + 7) / 8; - quint32 allocated = 0; + const qsizetype Step = 8 * 1024 * 1024; + qsizetype totalBytes = (len + 7) / 8; + qsizetype allocated = 0; while (allocated < totalBytes) { - int blockSize = qMin(Step, totalBytes - allocated); + qsizetype blockSize = qMin(Step, totalBytes - allocated); ba.d.resize(allocated + blockSize + 1); if (in.readRawData(ba.d.data() + 1 + allocated, blockSize) != blockSize) { ba.clear(); @@ -820,7 +826,7 @@ QDataStream &operator>>(QDataStream &in, QBitArray &ba) allocated += blockSize; } - int paddingMask = ~((0x1 << (len & 0x7)) - 1); + qsizetype paddingMask = ~((0x1 << (len & 0x7)) - 1); if (paddingMask != ~0x0 && (ba.d.constData()[ba.d.size() - 1] & paddingMask)) { ba.clear(); in.setStatus(QDataStream::ReadCorruptData); @@ -837,7 +843,7 @@ QDebug operator<<(QDebug dbg, const QBitArray &array) { QDebugStateSaver saver(dbg); dbg.nospace() << "QBitArray("; - for (int i = 0; i < array.size();) { + for (qsizetype i = 0; i < array.size();) { if (array.testBit(i)) dbg << '1'; else diff --git a/src/corelib/tools/qbitarray.h b/src/corelib/tools/qbitarray.h index 25c95bc2a5..8abb69c0e6 100644 --- a/src/corelib/tools/qbitarray.h +++ b/src/corelib/tools/qbitarray.h @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2020 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtCore module of the Qt Toolkit. @@ -57,7 +57,7 @@ class Q_CORE_EXPORT QBitArray public: inline QBitArray() noexcept {} - explicit QBitArray(int size, bool val = false); + explicit QBitArray(qsizetype size, bool val = false); QBitArray(const QBitArray &other) : d(other.d) {} inline QBitArray &operator=(const QBitArray &other) { d = other.d; return *this; } inline QBitArray(QBitArray &&other) noexcept : d(std::move(other.d)) {} @@ -66,30 +66,28 @@ public: inline void swap(QBitArray &other) noexcept { qSwap(d, other.d); } - inline int size() const { return (d.size() << 3) - *d.constData(); } - inline int count() const { return (d.size() << 3) - *d.constData(); } - int count(bool on) const; + inline qsizetype size() const { return (d.size() << 3) - *d.constData(); } + inline qsizetype count() const { return (d.size() << 3) - *d.constData(); } + qsizetype count(bool on) const; inline bool isEmpty() const { return d.isEmpty(); } inline bool isNull() const { return d.isNull(); } - void resize(int size); + void resize(qsizetype size); inline void detach() { d.detach(); } inline bool isDetached() const { return d.isDetached(); } inline void clear() { d.clear(); } - bool testBit(int i) const; - void setBit(int i); - void setBit(int i, bool val); - void clearBit(int i); - bool toggleBit(int i); + bool testBit(qsizetype i) const; + void setBit(qsizetype i); + void setBit(qsizetype i, bool val); + void clearBit(qsizetype i); + bool toggleBit(qsizetype i); - bool at(int i) const; - QBitRef operator[](int i); - bool operator[](int i) const; - QBitRef operator[](uint i); - bool operator[](uint i) const; + bool at(qsizetype i) const; + QBitRef operator[](qsizetype i); + bool operator[](qsizetype i) const; QBitArray& operator&=(const QBitArray &); QBitArray& operator|=(const QBitArray &); @@ -99,10 +97,10 @@ public: inline bool operator==(const QBitArray& other) const { return d == other.d; } inline bool operator!=(const QBitArray& other) const { return d != other.d; } - inline bool fill(bool val, int size = -1); - void fill(bool val, int first, int last); + inline bool fill(bool val, qsizetype size = -1); + void fill(bool val, qsizetype first, qsizetype last); - inline void truncate(int pos) { if (pos < size()) resize(pos); } + inline void truncate(qsizetype pos) { if (pos < size()) resize(pos); } const char *bits() const { return isEmpty() ? nullptr : d.constData() + 1; } static QBitArray fromBits(const char *data, qsizetype len); @@ -114,43 +112,42 @@ public: inline DataPtr &data_ptr() { return d.data_ptr(); } }; -inline bool QBitArray::fill(bool aval, int asize) +inline bool QBitArray::fill(bool aval, qsizetype asize) { *this = QBitArray((asize < 0 ? this->size() : asize), aval); return true; } Q_CORE_EXPORT QBitArray operator&(const QBitArray &, const QBitArray &); Q_CORE_EXPORT QBitArray operator|(const QBitArray &, const QBitArray &); Q_CORE_EXPORT QBitArray operator^(const QBitArray &, const QBitArray &); -inline bool QBitArray::testBit(int i) const -{ Q_ASSERT(uint(i) < uint(size())); +inline bool QBitArray::testBit(qsizetype i) const +{ Q_ASSERT(size_t(i) < size_t(size())); return (*(reinterpret_cast<const uchar*>(d.constData())+1+(i>>3)) & (1 << (i & 7))) != 0; } -inline void QBitArray::setBit(int i) -{ Q_ASSERT(uint(i) < uint(size())); +inline void QBitArray::setBit(qsizetype i) +{ Q_ASSERT(size_t(i) < size_t(size())); *(reinterpret_cast<uchar*>(d.data())+1+(i>>3)) |= uchar(1 << (i & 7)); } -inline void QBitArray::clearBit(int i) -{ Q_ASSERT(uint(i) < uint(size())); +inline void QBitArray::clearBit(qsizetype i) +{ Q_ASSERT(size_t(i) < size_t(size())); *(reinterpret_cast<uchar*>(d.data())+1+(i>>3)) &= ~uchar(1 << (i & 7)); } -inline void QBitArray::setBit(int i, bool val) +inline void QBitArray::setBit(qsizetype i, bool val) { if (val) setBit(i); else clearBit(i); } -inline bool QBitArray::toggleBit(int i) -{ Q_ASSERT(uint(i) < uint(size())); +inline bool QBitArray::toggleBit(qsizetype i) +{ Q_ASSERT(size_t(i) < size_t(size())); uchar b = uchar(1<<(i&7)); uchar* p = reinterpret_cast<uchar*>(d.data())+1+(i>>3); uchar c = uchar(*p&b); *p^=b; return c!=0; } -inline bool QBitArray::operator[](int i) const { return testBit(i); } -inline bool QBitArray::operator[](uint i) const { return testBit(i); } -inline bool QBitArray::at(int i) const { return testBit(i); } +inline bool QBitArray::operator[](qsizetype i) const { return testBit(i); } +inline bool QBitArray::at(qsizetype i) const { return testBit(i); } class Q_CORE_EXPORT QBitRef { private: QBitArray& a; - int i; - inline QBitRef(QBitArray& array, int idx) : a(array), i(idx) {} + qsizetype i; + inline QBitRef(QBitArray& array, qsizetype idx) : a(array), i(idx) {} friend class QBitArray; public: inline operator bool() const { return a.testBit(i); } @@ -159,11 +156,8 @@ public: QBitRef& operator=(bool val) { a.setBit(i, val); return *this; } }; -inline QBitRef QBitArray::operator[](int i) +inline QBitRef QBitArray::operator[](qsizetype i) { Q_ASSERT(i >= 0); return QBitRef(*this, i); } -inline QBitRef QBitArray::operator[](uint i) -{ return QBitRef(*this, i); } - #ifndef QT_NO_DATASTREAM Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QBitArray &); diff --git a/src/corelib/tools/qhash.cpp b/src/corelib/tools/qhash.cpp index b73e29594d..0f7d08e06d 100644 --- a/src/corelib/tools/qhash.cpp +++ b/src/corelib/tools/qhash.cpp @@ -549,12 +549,12 @@ size_t qHash(QStringView key, size_t seed) noexcept size_t qHash(const QBitArray &bitArray, size_t seed) noexcept { - int m = bitArray.d.size() - 1; + qsizetype m = bitArray.d.size() - 1; size_t result = qHashBits(reinterpret_cast<const uchar *>(bitArray.d.constData()), size_t(qMax(0, m)), seed); // deal with the last 0 to 7 bits manually, because we can't trust that // the padding is initialized to 0 in bitArray.d - int n = bitArray.size(); + qsizetype n = bitArray.size(); if (n & 0x7) result = ((result << 4) + bitArray.d.at(m)) & ((1 << n) - 1); return result; |