Methods Summary |
---|
public void | clear(int pos)Clears the bit at index pos. Grows the BitSet if pos > size.
if (pos >= 0) {
if (pos < bits.length * ELM_SIZE) {
bits[pos / ELM_SIZE] &= ~(1L << (pos % ELM_SIZE));
} else {
growBits(pos); // Bit is cleared for free if we have to grow
}
} else {
throw new IndexOutOfBoundsException("Negative index specified");
}
|
public boolean | get(int pos)Retrieve the bit at index pos. Grows the BitSet if pos > size.
if (pos >= 0) {
if (pos < bits.length * ELM_SIZE) {
return (bits[pos / ELM_SIZE] & (1L << (pos % ELM_SIZE))) != 0;
}
return false;
}
throw new IndexOutOfBoundsException("Negative index specified");
|
private void | growBits(int pos)Increase the size of the internal array to accomodate pos bits. The new
array max index will be a multiple of 64
pos++; // Inc to get correct bit count
long[] tempBits = new long[(pos / ELM_SIZE)
+ (pos % ELM_SIZE > 0 ? 1 : 0)];
System.arraycopy(bits, 0, tempBits, 0, bits.length);
bits = tempBits;
|
public int | length()Returns the number of bits up to and including the highest bit set.
int idx = bits.length - 1;
while (idx >= 0 && bits[idx] == 0) {
--idx;
}
if (idx == -1) {
return 0;
}
int i = ELM_SIZE - 1;
long val = bits[idx];
while ((val & (1L << i)) == 0 && i > 0) {
i--;
}
return idx * ELM_SIZE + i + 1;
|
public void | set(int pos)Sets the bit at index pos to 1. Grows the BitSet if pos > size.
if (pos >= 0) {
if (pos >= bits.length * ELM_SIZE) {
growBits(pos);
}
bits[pos / ELM_SIZE] |= 1L << (pos % ELM_SIZE);
} else {
throw new IndexOutOfBoundsException("Negative index specified");
}
|
public int | size()Clears the bit at index pos.
return bits.length * ELM_SIZE;
|
public java.lang.String | toString()Answers a string containing a concise, human-readable description of the
receiver.
StringBuffer sb = new StringBuffer(bits.length / 2);
int bitCount = 0;
sb.append('{");
boolean comma = false;
for (long element : bits) {
if (element == 0) {
bitCount += ELM_SIZE;
continue;
}
for (int j = 0; j < ELM_SIZE; j++) {
if (((element & (1L << j)) != 0)) {
if (comma) {
sb.append(", ");
}
sb.append(bitCount);
comma = true;
}
bitCount++;
}
}
sb.append('}");
return sb.toString();
|