BitVectorpublic final class BitVector extends Object Optimized implementation of a vector of bits. This is more-or-less like
java.util.BitSet, but also includes the following:
- a count() method, which efficiently computes the number of one bits;
- optimized read from and write to disk;
- inlinable get() method;
|
Fields Summary |
---|
private byte[] | bits | private int | size | private int | count | private static final byte[] | BYTE_COUNTS |
Constructors Summary |
---|
public BitVector(int n)Constructs a vector capable of holding n bits.
size = n;
bits = new byte[(size >> 3) + 1];
| public BitVector(Directory d, String name)Constructs a bit vector from the file name in Directory
d , as written by the {@link #write} method.
IndexInput input = d.openInput(name);
try {
size = input.readInt(); // read size
count = input.readInt(); // read count
bits = new byte[(size >> 3) + 1]; // allocate bits
input.readBytes(bits, 0, bits.length); // read bits
} finally {
input.close();
}
|
Methods Summary |
---|
public final void | clear(int bit)Sets the value of bit to zero.
bits[bit >> 3] &= ~(1 << (bit & 7));
count = -1;
| public final int | count()Returns the total number of one bits in this vector. This is efficiently
computed and cached, so that, if the vector is not changed, no
recomputation is done for repeated calls.
// if the vector has been modified
if (count == -1) {
int c = 0;
int end = bits.length;
for (int i = 0; i < end; i++)
c += BYTE_COUNTS[bits[i] & 0xFF]; // sum bits per byte
count = c;
}
return count;
| public final boolean | get(int bit)Returns true if bit is one and
false if it is zero.
return (bits[bit >> 3] & (1 << (bit & 7))) != 0;
| public final void | set(int bit)Sets the value of bit to one.
bits[bit >> 3] |= 1 << (bit & 7);
count = -1;
| public final int | size()Returns the number of bits in this vector. This is also one greater than
the number of the largest valid bit number.
return size;
| public final void | write(org.apache.lucene.store.Directory d, java.lang.String name)Writes this vector to the file name in Directory
d , in a format that can be read by the constructor {@link
#BitVector(Directory, String)}.
IndexOutput output = d.createOutput(name);
try {
output.writeInt(size()); // write size
output.writeInt(count()); // write count
output.writeBytes(bits, bits.length); // write bits
} finally {
output.close();
}
|
|