|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.trolltech.qt.internal.QSignalEmitterInternal
com.trolltech.qt.QSignalEmitter
com.trolltech.qt.QtJambiObject
com.trolltech.qt.core.QBitArray
public class QBitArray
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):
QBitArray ba = new QBitArray(200);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:
//The following snipplet is not applicable in Java since there are no //support for operator overloading. Use QBitArray.setBit(int). QBitArray ba; ba.resize(3); ba[0] = true; ba[1] = false; ba[2] = true;For technical reasons, it is more efficient to use
testBit()
and setBit()
to access bits in the array than operator[](). For example: QBitArray ba = new QBitArray(3); ba.setBit(0, true); ba.setBit(1, false); ba.setBit(2, true);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:
QBitArray x = new QBitArray(5); x.setBit(3, true); // x: [ 0, 0, 0, 1, 0 ] QBitArray y = new QBitArray(5); y.setBit(4, true); // y: [ 0, 0, 0, 0, 1 ] x.or(y); // x: [ 0, 0, 0, 1, 1 ]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:
new QBitArray().isNull(); // returns true new QBitArray().isEmpty(); // returns true new QBitArray(0).isNull(); // returns false new QBitArray(0).isEmpty(); // returns true new QBitArray(3).isNull(); // returns false new QBitArray(3).isEmpty(); // returns falseAll 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()
. QByteArray
, and QVector.
Nested Class Summary |
---|
Nested classes/interfaces inherited from class com.trolltech.qt.QSignalEmitter |
---|
QSignalEmitter.AbstractSignal, QSignalEmitter.Signal0, QSignalEmitter.Signal1, QSignalEmitter.Signal2, QSignalEmitter.Signal3, QSignalEmitter.Signal4, QSignalEmitter.Signal5, QSignalEmitter.Signal6, QSignalEmitter.Signal7, QSignalEmitter.Signal8, QSignalEmitter.Signal9 |
Nested classes/interfaces inherited from class com.trolltech.qt.internal.QSignalEmitterInternal |
---|
com.trolltech.qt.internal.QSignalEmitterInternal.AbstractSignalInternal |
Field Summary |
---|
Fields inherited from class com.trolltech.qt.internal.QSignalEmitterInternal |
---|
currentSender |
Constructor Summary | |
---|---|
QBitArray()
Constructs an empty bit array. |
|
QBitArray(int size)
Constructs a bit array containing size bits. |
|
QBitArray(int size,
boolean val)
Constructs a bit array containing size bits. |
|
QBitArray(QBitArray other)
Constructs a copy of other. |
Method Summary | |
---|---|
void |
and(QBitArray other)
Returns a bit array that is the AND of this bit array and other. |
boolean |
at(int i)
Returns the value of the bit at index position i. |
void |
clear()
Clears the contents of the bit array and makes it empty. |
void |
clearBit(int i)
Sets the bit at index position i to 0. |
QBitArray |
clone()
This method is reimplemented for internal reasons |
int |
count()
Same as size() . |
int |
count(boolean on)
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. |
boolean |
fill(boolean val)
Sets every bit in the bit array to value, returning true if successful; otherwise returns false. |
boolean |
fill(boolean val,
int size)
Sets every bit in the bit array to value, returning true if successful; otherwise returns false. |
void |
fill(boolean val,
int first,
int last)
Sets bits at index positions begin up to and excluding end to value. |
QBitArray |
inverted()
Returns a bit array that contains the inverted bits of this bit array. |
boolean |
isEmpty()
Returns true if this bit array has size 0; otherwise returns false. |
boolean |
isNull()
Returns true if this bit array is null; otherwise returns false. |
void |
or(QBitArray other)
Returns a bit array that is the OR of this bit array and other. |
void |
readFrom(QDataStream arg__1)
|
void |
resize(int size)
Resizes the bit array to size bits. |
void |
set(QBitArray other)
This function sets the contents of this array to the same as others. |
void |
setBit(int i)
Sets the bit at index position i to 1. |
void |
setBit(int i,
boolean val)
Sets the bit at index position i to value. |
int |
size()
Returns the number of bits stored in the bit array. |
boolean |
testBit(int i)
Returns true if the bit at index position i is 1; otherwise returns false. |
boolean |
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). |
void |
truncate(int pos)
Truncates the bit array at index position pos. |
void |
writeTo(QDataStream arg__1)
|
void |
xor(QBitArray other)
Returns a bit array that is the XOR of this bit array and other. |
Methods inherited from class com.trolltech.qt.QtJambiObject |
---|
dispose, disposed, equals, finalize, reassignNativeResources, tr, tr, tr |
Methods inherited from class com.trolltech.qt.QSignalEmitter |
---|
blockSignals, disconnect, disconnect, signalsBlocked, signalSender, thread |
Methods inherited from class com.trolltech.qt.internal.QSignalEmitterInternal |
---|
__qt_signalInitialization |
Methods inherited from class java.lang.Object |
---|
getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Methods inherited from interface com.trolltech.qt.QtJambiInterface |
---|
disableGarbageCollection, nativeId, nativePointer, reenableGarbageCollection, setJavaOwnership |
Constructor Detail |
---|
public QBitArray()
isEmpty()
.
public QBitArray(QBitArray 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.
public QBitArray(int size)
public QBitArray(int size, boolean val)
Method Detail |
---|
public final boolean at(int i)
i must be a valid index position in the bit array (i.e., 0 <= i < size()
).
public final void clear()
resize()
, and isEmpty()
.
public final void clearBit(int i)
i must be a valid index position in the bit array (i.e., 0 <= i < size()
).
setBit()
, and toggleBit()
.
public final int count()
size()
.
public final int count(boolean on)
public final void fill(boolean val, int first, int last)
begin and end must be a valid index position in the bit array (i.e., 0 <= begin <= size()
and 0 <= end <= size()
).
public final boolean fill(boolean val)
Example:
QBitArray ba = new QBitArray(8); ba.fill(true); // ba: [ 1, 1, 1, 1, 1, 1, 1, 1 ] ba.fill(false, 2); // ba: [ 0, 0 ]
resize()
.
public final boolean fill(boolean val, int size)
Example:
QBitArray ba = new QBitArray(8); ba.fill(true); // ba: [ 1, 1, 1, 1, 1, 1, 1, 1 ] ba.fill(false, 2); // ba: [ 0, 0 ]
resize()
.
public final boolean isEmpty()
size()
.
public final boolean isNull()
Example:
new QBitArray().isNull(); // returns true new QBitArray(0).isNull(); // returns false new QBitArray(3).isNull(); // returns falseQt 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()
. isEmpty()
.
public final void writeTo(QDataStream arg__1)
public final void readFrom(QDataStream arg__1)
public final void resize(int size)
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.
size()
.
public final void setBit(int i)
i must be a valid index position in the bit array (i.e., 0 <= i < size()
).
clearBit()
, and toggleBit()
.
public final void setBit(int i, boolean val)
public final int size()
resize()
.
public final boolean testBit(int i)
i must be a valid index position in the bit array (i.e., 0 <= i < size()
).
setBit()
, and clearBit()
.
public final boolean toggleBit(int i)
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()
).
setBit()
, and clearBit()
.
public final void truncate(int pos)
If pos is beyond the end of the array, nothing happens.
resize()
.
public final void xor(QBitArray other)
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:
QBitArray a(3);
QBitArray b(2);
QBitArray c;
a[0] = 1; a[1] = 0; a[2] = 1; // a: [ 1, 0, 1 ]
b[0] = 1; b[1] = 0; // b: [ 1, 1 ]
c = a ^ b; // c: [ 0, 1, 1 ]
public final void and(QBitArray other)
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:
QBitArray a(3);
QBitArray b(2);
QBitArray c;
a[0] = 1; a[1] = 0; a[2] = 1; // a: [ 1, 0, 1 ]
b[0] = 1; b[1] = 0; // b: [ 1, 1 ]
c = a & b; // c: [ 1, 0, 0 ]
public final void or(QBitArray other)
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:
QBitArray a(3);
QBitArray b(2);
QBitArray c;
a[0] = 1; a[1] = 0; a[2] = 1; // a: [ 1, 0, 1 ]
b[0] = 1; b[1] = 0; // b: [ 1, 1 ]
c = a | b; // c: [ 1, 1, 1 ]
public final void set(QBitArray other)
public final QBitArray inverted()
Example:
QBitArray a(3);
QBitArray b;
a[0] = 1; a[1] = 0; a[2] = 1; // a: [ 1, 0, 1 ]
b = ~a; // b: [ 0, 1, 0 ]
public QBitArray clone()
clone
in class java.lang.Object
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |