![]() |
Qt 6.x
The Qt SDK
|
\inmodule QtCore More...
#include <qbitarray.h>
Public Types | |
typedef QByteArray::DataPointer | DataPtr |
Public Member Functions | |
QBitArray () noexcept | |
Constructs an empty bit array. | |
QBitArray (qsizetype size, bool val=false) | |
Constructs a bit array containing size bits. | |
QBitArray (const QBitArray &other) noexcept | |
Constructs a copy of other. | |
QBitArray & | operator= (const QBitArray &other) noexcept |
Assigns other to this bit array and returns a reference to this bit array. | |
QBitArray (QBitArray &&other) noexcept | |
Move-constructs a QBitArray instance, making it point at the same object that other was pointing to. | |
void | swap (QBitArray &other) noexcept |
qsizetype | size () const |
Returns the number of bits stored in the bit array. | |
qsizetype | count () const |
Same as size(). | |
qsizetype | count (bool on) const |
If on is true, this function returns the number of 1-bits stored in the bit array; otherwise the number of 0-bits is returned. | |
bool | isEmpty () const |
Returns true if this bit array has size 0; otherwise returns false. | |
bool | isNull () const |
Returns true if this bit array is null; otherwise returns false . | |
void | resize (qsizetype size) |
Resizes the bit array to size bits. | |
void | detach () |
bool | isDetached () const |
void | clear () |
Clears the contents of the bit array and makes it empty. | |
bool | testBit (qsizetype i) const |
Returns true if the bit at index position i is 1; otherwise returns false . | |
void | setBit (qsizetype i) |
Sets the bit at index position i to 1. | |
void | setBit (qsizetype i, bool val) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Sets the bit at index position i to value. | |
void | clearBit (qsizetype i) |
Sets the bit at index position i to 0. | |
bool | toggleBit (qsizetype i) |
Inverts the value of the bit at index position i, returning the previous value of that bit as either true (if it was set) or false (if it was unset). | |
bool | at (qsizetype i) const |
Returns the value of the bit at index position i. | |
QBitRef | operator[] (qsizetype i) |
Returns the bit at index position i as a modifiable reference. | |
bool | operator[] (qsizetype i) const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
QBitArray & | operator&= (const QBitArray &) |
Performs the AND operation between all bits in this bit array and other. | |
QBitArray & | operator|= (const QBitArray &) |
Performs the OR operation between all bits in this bit array and other. | |
QBitArray & | operator^= (const QBitArray &) |
Performs the XOR operation between all bits in this bit array and other. | |
QBitArray | operator~ () const |
Returns a bit array that contains the inverted bits of this bit array. | |
bool | operator== (const QBitArray &other) const |
Returns true if other is equal to this bit array; otherwise returns false . | |
bool | operator!= (const QBitArray &other) const |
Returns true if other is not equal to this bit array; otherwise returns false . | |
bool | fill (bool val, qsizetype size=-1) |
Sets every bit in the bit array to value, returning true if successful; otherwise returns false . | |
void | fill (bool val, qsizetype first, qsizetype last) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Sets bits at index positions begin up to (but not including) end to value. | |
void | truncate (qsizetype pos) |
Truncates the bit array at index position pos. | |
const char * | bits () const |
quint32 | toUInt32 (QSysInfo::Endian endianness, bool *ok=nullptr) const noexcept |
DataPtr & | data_ptr () |
Static Public Member Functions | |
static QBitArray | fromBits (const char *data, qsizetype len) |
Friends | |
Q_CORE_EXPORT QDataStream & | operator<< (QDataStream &, const QBitArray &) |
Writes bit array ba to stream out. | |
Q_CORE_EXPORT QDataStream & | operator>> (QDataStream &, QBitArray &) |
Reads a bit array into ba from stream in. | |
Q_CORE_EXPORT size_t | qHash (const QBitArray &key, size_t seed) noexcept |
Related Symbols | |
(Note that these are not member symbols.) | |
QBitArray | operator& (const QBitArray &a1, const QBitArray &a2) |
Returns a bit array that is the AND of the bit arrays a1 and a2. | |
QBitArray | operator| (const QBitArray &a1, const QBitArray &a2) |
Returns a bit array that is the OR of the bit arrays a1 and a2. | |
QBitArray | operator^ (const QBitArray &a1, const QBitArray &a2) |
Returns a bit array that is the XOR of the bit arrays a1 and a2. | |
QDataStream & | operator<< (QDataStream &out, const QBitArray &ba) |
Writes bit array ba to stream out. | |
QDataStream & | operator>> (QDataStream &in, QBitArray &ba) |
Reads a bit array into ba from stream in. | |
\inmodule QtCore
The QBitArray class provides an array of bits.
\reentrant
A QBitArray is an array that gives access to individual bits and provides operators (\l{operator&()}{AND}, \l{operator|()}{OR}, \l{operator^()}{XOR}, and \l{operator~()}{NOT}) that work on entire arrays of bits. It uses \l{implicit sharing} (copy-on-write) to reduce memory usage and to avoid the needless copying of data.
The following code constructs a QBitArray containing 200 bits initialized to false (0):
To initialize the bits to true, either pass true
as second argument to the constructor, or call fill() later on.
QBitArray uses 0-based indexes, just like C++ arrays. To access the bit at a particular index position, you can use operator[](). On non-const bit arrays, operator[]() returns a reference to a bit that can be used on the left side of an assignment. For example:
For technical reasons, it is more efficient to use testBit() and setBit() to access bits in the array than operator[](). For example:
QBitArray supports {&} (\l{operator&()}{AND}),
{|} (\l{operator|()}{OR}),
{^} (\l{operator^()}{XOR}),
{~} (\l{operator~()}{NOT}), as well as
{&=},
{|=}, and
{^=}. These operators work in the same way as the built-in C++ bitwise operators of the same name. For example:
For historical reasons, QBitArray distinguishes between a null bit array and an empty bit array. A null bit array is a bit array that is initialized using QBitArray's default constructor. An empty bit array is any bit array with size 0. A null bit array is always empty, but an empty bit array isn't necessarily null:
All functions except isNull() treat null bit arrays the same as empty bit arrays; for example, QBitArray() compares equal to QBitArray(0). We recommend that you always use isEmpty() and avoid isNull().
Definition at line 12 of file qbitarray.h.
Definition at line 73 of file qbitarray.h.
|
inlinenoexcept |
Constructs an empty bit array.
Definition at line 22 of file qbitarray.h.
Referenced by fill().
Constructs a bit array containing size bits.
The bits are initialized with value, which defaults to false (0).
Definition at line 111 of file qbitarray.cpp.
References QByteArray::data(), Q_ASSERT_X, and QByteArray::size().
|
inlinenoexcept |
Constructs a copy of other.
This operation takes \l{constant time}, because QBitArray is \l{implicitly shared}. This makes returning a QBitArray from a function very fast. If a shared instance is modified, it will be copied (copy-on-write), and that takes \l{linear time}.
Definition at line 24 of file qbitarray.h.
|
inlinenoexcept |
Move-constructs a QBitArray instance, making it point at the same object that other was pointing to.
Definition at line 26 of file qbitarray.h.
|
inline |
Returns the value of the bit at index position i.
i must be a valid index position in the bit array (i.e., 0 <= i < size()).
Definition at line 105 of file qbitarray.h.
Referenced by QTriangulator< T >::ComplexToSimple::decompose(), QTriangulator< T >::SimpleToMonotone::decompose(), and QHeaderViewPrivate::setHiddenSectionsFromBitVector().
|
inline |
Returns a pointer to a dense bit array for this QBitArray. Bits are counted upwards from the least significant bit in each byte. The number of bits relevant in the last byte is given by {size() % 8}.
Definition at line 67 of file qbitarray.h.
|
inline |
Clears the contents of the bit array and makes it empty.
Definition at line 42 of file qbitarray.h.
References d.
Referenced by QHeaderViewPrivate::clear(), QIconModeViewBase::clear(), and QHeaderViewPrivate::prepareSectionSelected().
Sets the bit at index position i to 0.
i must be a valid index position in the bit array (i.e., 0 <= i < size()).
Definition at line 92 of file qbitarray.h.
References d, i, and Q_ASSERT.
Referenced by QRhiVulkan::beginFrame(), QRhiVulkan::endOffscreenFrame(), QLCDNumberPrivate::internalSetString(), and setBit().
|
inline |
qsizetype QBitArray::count | ( | bool | on | ) | const |
If on is true, this function returns the number of 1-bits stored in the bit array; otherwise the number of 0-bits is returned.
Definition at line 142 of file qbitarray.cpp.
References QByteArray::data(), QByteArray::end(), and qPopulationCount().
|
inline |
Definition at line 74 of file qbitarray.h.
References d.
|
inline |
Definition at line 40 of file qbitarray.h.
References d.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Sets bits at index positions begin up to (but not including) end to value.
begin must be a valid index position in the bit array (0 <= begin < size()).
end must be either a valid index position or equal to size(), in which case the fill operation runs until the end of the array (0 <= end <= size()).
Example:
Definition at line 252 of file qbitarray.cpp.
References begin(), QByteArray::data(), and setBit().
|
inline |
Sets every bit in the bit array to value, returning true if successful; otherwise returns false
.
If size is different from -1 (the default), the bit array is resized to size beforehand.
Example:
Definition at line 77 of file qbitarray.h.
References QBitArray().
Referenced by QHeaderViewPrivate::prepareSectionSelected(), and QGridLayoutRowData::reset().
Creates a QBitArray with the dense bit array located at data, with size bits. The byte array at data must be at least size / 8 (rounded up) bytes long.
If size is not a multiple of 8, this function will include the lowest size % 8 bits from the last byte in data.
Definition at line 290 of file qbitarray.cpp.
References Qt::Uninitialized.
|
inline |
Definition at line 41 of file qbitarray.h.
References d.
|
inline |
Returns true
if this bit array has size 0; otherwise returns false.
Definition at line 35 of file qbitarray.h.
References d.
|
inline |
Returns true
if this bit array is null; otherwise returns false
.
Example:
Qt makes a distinction between null bit arrays and empty bit arrays for historical reasons. For most applications, what matters is whether or not a bit array contains any data, and this can be determined using isEmpty().
Definition at line 36 of file qbitarray.h.
References d.
|
inline |
Returns true
if other is not equal to this bit array; otherwise returns false
.
Definition at line 60 of file qbitarray.h.
Performs the AND operation between all bits in this bit array and other.
Assigns the result to this bit array, and returns a reference to it.
The result has the length of the longest of the two bit arrays, with any missing bits (if one array is shorter than the other) taken to be 0.
Example:
Definition at line 519 of file qbitarray.cpp.
References a1, a2, QByteArray::data(), other(), qMax(), resize(), and QByteArray::size().
Assigns other to this bit array and returns a reference to this bit array.
Moves other to this bit array and returns a reference to this bit array.
Definition at line 25 of file qbitarray.h.
|
inline |
Returns true
if other is equal to this bit array; otherwise returns false
.
Definition at line 59 of file qbitarray.h.
Returns the bit at index position i as a modifiable reference.
i must be a valid index position in the bit array (i.e., 0 <= i < size()).
Example:
The return value is of type QBitRef, a helper class for QBitArray. When you get an object of type QBitRef, you can assign to it, and the assignment will apply to the bit in the QBitArray from which you got the reference.
The functions testBit(), setBit(), and clearBit() are slightly faster.
Definition at line 122 of file qbitarray.h.
|
inline |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 104 of file qbitarray.h.
Performs the XOR operation between all bits in this bit array and other.
Assigns the result to this bit array, and returns a reference to it.
The result has the length of the longest of the two bit arrays, with any missing bits (if one array is shorter than the other) taken to be 0.
Example:
Definition at line 574 of file qbitarray.cpp.
References a1, a2, QByteArray::data(), other(), qMax(), and resize().
Performs the OR operation between all bits in this bit array and other.
Assigns the result to this bit array, and returns a reference to it.
The result has the length of the longest of the two bit arrays, with any missing bits (if one array is shorter than the other) taken to be 0.
Example:
Definition at line 548 of file qbitarray.cpp.
References a1, a2, QByteArray::data(), other(), qMax(), and resize().
QBitArray QBitArray::operator~ | ( | ) | const |
Returns a bit array that contains the inverted bits of this bit array.
Example:
Definition at line 595 of file qbitarray.cpp.
References a1, a2, QByteArray::constData(), and QByteArray::size().
Resizes the bit array to size bits.
If size is greater than the current size, the bit array is extended to make it size bits with the extra bits added to the end. The new bits are initialized to false (0).
If size is less than the current size, bits are removed from the end.
Definition at line 184 of file qbitarray.cpp.
References QByteArray::data(), QByteArray::resize(), and QByteArray::size().
Referenced by QAbstractItemModel::decodeData(), QStandardItemModel::dropMimeData(), operator&=(), operator^=(), operator|=(), and QHeaderViewPrivate::sectionsHiddenToBitVector().
Sets the bit at index position i to 1.
i must be a valid index position in the bit array (i.e., 0 <= i < size()).
Definition at line 88 of file qbitarray.h.
References d, i, and Q_ASSERT.
Referenced by addOpaqueDepthPrePassBindings(), QRhiVulkan::beginFrame(), QRhiVulkan::beginOffscreenFrame(), QAbstractItemModel::decodeData(), QTriangulator< T >::ComplexToSimple::decompose(), QTriangulator< T >::SimpleToMonotone::decompose(), QTableViewPrivate::drawAndClipSpans(), QStandardItemModel::dropMimeData(), fill(), QLCDNumberPrivate::internalSetString(), QHeaderViewPrivate::isSectionSelected(), QTableView::paintEvent(), QSSGCustomMaterialSystem::rhiPrepareRenderable(), and setBit().
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Sets the bit at index position i to value.
Definition at line 96 of file qbitarray.h.
References clearBit(), i, and setBit().
|
inline |
Returns the number of bits stored in the bit array.
Definition at line 31 of file qbitarray.h.
References d.
Referenced by QRhiVulkan::beginFrame(), QRhiVulkan::beginOffscreenFrame(), QHeaderViewPrivate::isSectionSelected(), QTableView::paintEvent(), QHeaderViewPrivate::prepareSectionSelected(), and QHeaderViewPrivate::setHiddenSectionsFromBitVector().
Swaps bit array other with this bit array. This operation is very fast and never fails.
Definition at line 29 of file qbitarray.h.
|
inline |
Returns true
if the bit at index position i is 1; otherwise returns false
.
i must be a valid index position in the bit array (i.e., 0 <= i < size()).
Definition at line 84 of file qbitarray.h.
References d, i, and Q_ASSERT.
Referenced by addOpaqueDepthPrePassBindings(), at(), QRhiVulkan::beginFrame(), QRhiVulkan::beginOffscreenFrame(), QGridLayoutRowData::calculateGeometries(), QAbstractItemModel::decodeData(), QLCDNumberPrivate::drawString(), QStandardItemModel::dropMimeData(), QLCDNumberPrivate::internalSetString(), QHeaderViewPrivate::isSectionSelected(), QBitRef::operator bool(), operator[](), QTableView::paintEvent(), QSSGCustomMaterialSystem::rhiPrepareRenderable(), QLCDNumber::setDigitCount(), QGridLayoutRowData::stealBox(), and QGridLayoutRowData::totalBox().
|
inline |
Inverts the value of the bit at index position i, returning the previous value of that bit as either true (if it was set) or false (if it was unset).
If the previous value was 0, the new value will be 1. If the previous value was 1, the new value will be 0.
i must be a valid index position in the bit array (i.e., 0 <= i < size()).
Definition at line 99 of file qbitarray.h.
|
noexcept |
Returns the array of bit converted to an int. The conversion is based on endianness. Converts up to the first 32 bits of the array to quint32
and returns it, obeying endianness. If ok is not a null pointer, and the array has more than 32 bits, ok is set to false and this function returns zero; otherwise, it's set to true.
Definition at line 318 of file qbitarray.cpp.
References i, QSysInfo::LittleEndian, ok, and testBit().
Truncates the bit array at index position pos.
If pos is beyond the end of the array, nothing happens.
Definition at line 65 of file qbitarray.h.
Returns a bit array that is the AND of the bit arrays a1 and a2.
The result has the length of the longest of the two bit arrays, with any missing bits (if one array is shorter than the other) taken to be 0.
Example:
Definition at line 627 of file qbitarray.cpp.
|
friend |
Writes bit array ba to stream out.
Definition at line 734 of file qbitarray.cpp.
|
related |
Writes bit array ba to stream out.
Definition at line 734 of file qbitarray.cpp.
|
friend |
Reads a bit array into ba from stream in.
Definition at line 759 of file qbitarray.cpp.
|
related |
Reads a bit array into ba from stream in.
Definition at line 759 of file qbitarray.cpp.
Returns a bit array that is the XOR of the bit arrays a1 and a2.
The result has the length of the longest of the two bit arrays, with any missing bits (if one array is shorter than the other) taken to be 0.
Example:
Definition at line 673 of file qbitarray.cpp.
Returns a bit array that is the OR of the bit arrays a1 and a2.
The result has the length of the longest of the two bit arrays, with any missing bits (if one array is shorter than the other) taken to be 0.
Example:
Definition at line 650 of file qbitarray.cpp.
|
friend |