Qt 4.8
|
The QBitArray class provides an array of bits. More...
#include <qbitarray.h>
Public Types | |
typedef QByteArray::DataPtr | DataPtr |
Public Functions | |
bool | at (int i) const |
Returns the value of the bit at index position i. More... | |
void | clear () |
Clears the contents of the bit array and makes it empty. More... | |
void | clearBit (int i) |
Sets the bit at index position i to 0. More... | |
int | count () const |
Same as size(). More... | |
int | 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. More... | |
DataPtr & | data_ptr () |
void | detach () |
bool | fill (bool val, int size=-1) |
Sets every bit in the bit array to value, returning true if successful; otherwise returns false. More... | |
void | fill (bool val, int first, int last) |
Sets bits at index positions begin up to and excluding end to value. More... | |
bool | isDetached () const |
bool | isEmpty () const |
Returns true if this bit array has size 0; otherwise returns false. More... | |
bool | isNull () const |
Returns true if this bit array is null; otherwise returns false. More... | |
QBitArray & | operator &= (const QBitArray &) |
bool | operator!= (const QBitArray &a) const |
Returns true if other is not equal to this bit array; otherwise returns false. More... | |
QBitArray & | operator= (const QBitArray &other) |
Assigns other to this bit array and returns a reference to this bit array. More... | |
bool | operator== (const QBitArray &a) const |
Returns true if other is equal to this bit array; otherwise returns false. More... | |
QBitRef | operator[] (int i) |
Returns the bit at index position i as a modifiable reference. More... | |
bool | operator[] (int i) const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More... | |
QBitRef | operator[] (uint i) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More... | |
bool | operator[] (uint i) const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More... | |
QBitArray & | operator^= (const QBitArray &) |
Performs the XOR operation between all bits in this bit array and other. More... | |
QBitArray & | operator|= (const QBitArray &) |
Performs the OR operation between all bits in this bit array and other. More... | |
QBitArray | operator~ () const |
Returns a bit array that contains the inverted bits of this bit array. More... | |
QBitArray () | |
Constructs an empty bit array. More... | |
QBitArray (int size, bool val=false) | |
Constructs a bit array containing size bits. More... | |
QBitArray (const QBitArray &other) | |
Constructs a copy of other. More... | |
void | resize (int size) |
Resizes the bit array to size bits. More... | |
void | setBit (int i) |
Sets the bit at index position i to 1. More... | |
void | setBit (int i, bool val) |
Sets the bit at index position i to value. More... | |
int | size () const |
Returns the number of bits stored in the bit array. More... | |
void | swap (QBitArray &other) |
Swaps bit array other with this bit array. More... | |
bool | testBit (int i) const |
Returns true if the bit at index position i is 1; otherwise returns false. More... | |
bool | toggleBit (int 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). More... | |
void | truncate (int pos) |
Truncates the bit array at index position pos. More... | |
Properties | |
QByteArray | d |
Friends | |
Q_CORE_EXPORT QDataStream & | operator<< (QDataStream &, const QBitArray &) |
Writes bit array ba to stream out. More... | |
Q_CORE_EXPORT QDataStream & | operator>> (QDataStream &, QBitArray &) |
Reads a bit array into ba from stream in. More... | |
Q_CORE_EXPORT uint | qHash (const QBitArray &key) |
Related Functions | |
(Note that these are not member functions.) | |
QBitArray | operator& (const QBitArray &a1, const QBitArray &a2) |
Returns a bit array that is the AND of the bit arrays a1 and a2. More... | |
QBitArray | operator^ (const QBitArray &a1, const QBitArray &a2) |
Returns a bit array that is the XOR of the bit arrays a1 and a2. More... | |
QBitArray | operator| (const QBitArray &a1, const QBitArray &a2) |
Returns a bit array that is the OR of the bit arrays a1 and a2. More... | |
The QBitArray class provides an array of bits.
A QBitArray is an array that gives access to individual bits and provides operators (AND, OR, XOR, and NOT) that work on entire arrays of bits. It uses 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 &
(AND), |
(OR), ^
(XOR), ~
(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 54 of file qbitarray.h.
Definition at line 113 of file qbitarray.h.
|
inline |
|
explicit |
Constructs a bit array containing size bits.
The bits are initialized with value, which defaults to false (0).
Definition at line 129 of file qbitarray.cpp.
|
inline |
Constructs a copy of other.
This operation takes constant time, because QBitArray is 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 linear time.
Definition at line 64 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 146 of file qbitarray.h.
Referenced by QHeaderViewPrivate::isVisualIndexHidden().
|
inline |
Clears the contents of the bit array and makes it empty.
Definition at line 85 of file qbitarray.h.
Referenced by QXIMInputContext::ICData::clear(), and QHeaderViewPrivate::prepareSectionSelected().
|
inline |
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 132 of file qbitarray.h.
Referenced by QLCDNumberPrivate::internalSetString(), and xic_draw_callback().
|
inline |
Same as size().
Definition at line 74 of file qbitarray.h.
Referenced by QHeaderViewPrivate::_q_layoutChanged(), QHeaderViewPrivate::prepareSectionSelected(), and QHeaderView::sectionsInserted().
int 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 166 of file qbitarray.cpp.
|
inline |
Definition at line 114 of file qbitarray.h.
|
inline |
Definition at line 83 of file qbitarray.h.
|
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 117 of file qbitarray.h.
Referenced by QHeaderViewPrivate::prepareSectionSelected(), and QHeaderView::sectionsInserted().
void QBitArray::fill | ( | bool | value, |
int | begin, | ||
int | end | ||
) |
Sets bits at index positions begin up to and excluding end to value.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
begin and end must be a valid index position in the bit array (i.e., 0 <= begin <= size() and 0 <= end <= size()).
Definition at line 287 of file qbitarray.cpp.
|
inline |
Definition at line 84 of file qbitarray.h.
|
inline |
Returns true if this bit array has size 0; otherwise returns false.
Definition at line 78 of file qbitarray.h.
Referenced by QHeaderViewPrivate::isVisualIndexHidden(), QDeclarativeVMEMetaObject::metaCall(), QHeaderView::moveSection(), QDeclarativeVMEMetaObject::registerInterceptor(), and QHeaderViewPrivate::setVisualIndexHidden().
|
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 79 of file qbitarray.h.
|
inline |
Returns true if other is not equal to this bit array; otherwise returns false.
Definition at line 105 of file qbitarray.h.
Assigns other to this bit array and returns a reference to this bit array.
Definition at line 65 of file qbitarray.h.
|
inline |
Returns true if other is equal to this bit array; otherwise returns false.
Definition at line 104 of file qbitarray.h.
|
inline |
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 162 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 144 of file qbitarray.h.
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 164 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 145 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 580 of file qbitarray.cpp.
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 554 of file qbitarray.cpp.
QBitArray QBitArray::operator~ | ( | ) | const |
Returns a bit array that contains the inverted bits of this bit array.
Example:
Definition at line 601 of file qbitarray.cpp.
void QBitArray::resize | ( | int | 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 214 of file qbitarray.cpp.
Referenced by QAbstractItemModel::decodeData(), QStandardItemModel::dropMimeData(), fill(), operator^=(), operator|=(), QDeclarativeVMEMetaObject::QDeclarativeVMEMetaObject(), QDeclarativeVMEMetaObject::registerInterceptor(), registerInterface(), registerType(), QHeaderView::sectionsInserted(), and xic_draw_callback().
|
inline |
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 128 of file qbitarray.h.
Referenced by QHeaderViewPrivate::_q_layoutChanged(), QDeclarativeVMEMetaObject::connectAlias(), QAbstractItemModel::decodeData(), QTableViewPrivate::drawAndClipSpans(), QStandardItemModel::dropMimeData(), fill(), QGridLayoutEngine::fillRowData(), QLCDNumberPrivate::internalSetString(), QRegExpMatchState::matchHere(), QHeaderView::moveSection(), QBitRef::operator=(), QTableView::paintEvent(), QDeclarativeVMEMetaObject::registerInterceptor(), registerInterface(), registerType(), QHeaderView::sectionsInserted(), QHeaderViewPrivate::setVisualIndexHidden(), and xic_draw_callback().
|
inline |
Sets the bit at index position i to value.
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 136 of file qbitarray.h.
|
inline |
Returns the number of bits stored in the bit array.
Definition at line 73 of file qbitarray.h.
Referenced by count(), fill(), QDeclarativeMetaType::isInterface(), QDeclarativeMetaType::isList(), QDeclarativeMetaType::isQObject(), operator^=(), operator|=(), operator~(), QTableView::paintEvent(), QBitArray(), qHash(), registerInterface(), registerType(), resize(), and xic_draw_callback().
|
inline |
Swaps bit array other with this bit array.
This operation is very fast and never fails.
Definition at line 71 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 124 of file qbitarray.h.
Referenced by QHeaderViewPrivate::_q_layoutChanged(), QDeclarativeVMEMetaObject::connectAlias(), count(), QAbstractItemModel::decodeData(), QLCDNumberPrivate::drawString(), QStandardItemModel::dropMimeData(), QGridLayoutEngine::fillRowData(), QLCDNumberPrivate::internalSetString(), QDeclarativeMetaType::isInterface(), QDeclarativeMetaType::isList(), QDeclarativeMetaType::isQObject(), QRegExpMatchState::matchHere(), QDeclarativeVMEMetaObject::metaCall(), QHeaderView::moveSection(), QBitRef::operator bool(), QBitRef::operator!(), QTableView::paintEvent(), QLCDNumber::setDigitCount(), QDeclarativeMetaType::typeCategory(), and xic_draw_callback().
|
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 139 of file qbitarray.h.
|
inline |
Truncates the bit array at index position pos.
If pos is beyond the end of the array, nothing happens.
Definition at line 110 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 636 of file qbitarray.cpp.
|
friend |
Writes bit array ba to stream out.
Definition at line 767 of file qbitarray.cpp.
|
friend |
Reads a bit array into ba from stream in.
Definition at line 787 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 688 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 662 of file qbitarray.cpp.
Referenced by QRect::QRect().
|
friend |
|
private |
Definition at line 59 of file qbitarray.h.
Referenced by count(), fill(), operator!=(), operator=(), operator==(), operator^=(), operator|=(), operator~(), QBitArray(), qHash(), resize(), and swap().