Methods Summary |
---|
public void | and(java.util.BitSet bs)Performs the logical AND of this {@code BitSet} with another
{@code BitSet}. The values of this {@code BitSet} are changed accordingly.
long[] bsBits = bs.bits;
int length1 = bits.length, length2 = bsBits.length;
if (length1 <= length2) {
for (int i = 0; i < length1; i++) {
bits[i] &= bsBits[i];
}
} else {
for (int i = 0; i < length2; i++) {
bits[i] &= bsBits[i];
}
for (int i = length2; i < length1; i++) {
bits[i] = 0;
}
}
|
public void | andNot(java.util.BitSet bs)Clears all bits in the receiver which are also set in the parameter
{@code BitSet}. The values of this {@code BitSet} are changed accordingly.
long[] bsBits = bs.bits;
int range = bits.length < bsBits.length ? bits.length : bsBits.length;
for (int i = 0; i < range; i++) {
bits[i] &= ~bsBits[i];
}
|
public int | cardinality()Returns the number of bits that are {@code true} in this {@code BitSet}.
int count = 0;
for (int idx = 0; idx < bits.length; idx++) {
long temp = bits[idx];
if (temp != 0L) {
for (int i = 0; i < ELM_SIZE; i++) {
if ((temp & (1L << i)) != 0L) {
count++;
}
}
}
}
return count;
|
public void | clear()Clears all the bits in this {@code BitSet}.
for (int i = 0; i < bits.length; i++) {
bits[i] = 0L;
}
|
public void | clear(int pos)Clears the bit at index {@code pos}. Grows the {@code BitSet} if
{@code pos > size}.
if (pos >= 0) {
if (pos < bits.length * ELM_SIZE) {
bits[pos / ELM_SIZE] &= ~(1L << (pos % ELM_SIZE));
}
} else {
// Negative index specified
throw new IndexOutOfBoundsException(Msg.getString("K0006")); //$NON-NLS-1$
}
|
public void | clear(int pos1, int pos2)Clears the bits starting from {@code pos1} to {@code pos2}. Grows the
{@code BitSet} if {@code pos2 > size}.
if (pos1 >= 0 && pos2 >= 0 && pos2 >= pos1) {
int last = (bits.length * ELM_SIZE);
if (pos1 >= last || pos1 == pos2) {
return;
}
if (pos2 > last) {
pos2 = last;
}
int idx1 = pos1 / ELM_SIZE;
int idx2 = (pos2 - 1) / ELM_SIZE;
long factor1 = (~0L) << (pos1 % ELM_SIZE);
long factor2 = (~0L) >>> (ELM_SIZE - (pos2 % ELM_SIZE));
if (idx1 == idx2) {
bits[idx1] &= ~(factor1 & factor2);
} else {
bits[idx1] &= ~factor1;
bits[idx2] &= ~factor2;
for (int i = idx1 + 1; i < idx2; i++) {
bits[i] = 0L;
}
}
} else {
throw new IndexOutOfBoundsException(Msg.getString("K0006")); //$NON-NLS-1$
}
|
public java.lang.Object | clone()Creates a copy of this {@code BitSet}.
try {
BitSet clone = (BitSet) super.clone();
clone.bits = bits.clone();
return clone;
} catch (CloneNotSupportedException e) {
return null;
}
|
public boolean | equals(java.lang.Object obj)Compares the argument to this {@code BitSet} and returns whether they are
equal. The object must be an instance of {@code BitSet} with the same
bits set.
if (this == obj) {
return true;
}
if (obj instanceof BitSet) {
long[] bsBits = ((BitSet) obj).bits;
int length1 = bits.length, length2 = bsBits.length;
// If one of the BitSets is larger than the other, check to see if
// any of
// its extra bits are set. If so return false.
if (length1 <= length2) {
for (int i = 0; i < length1; i++) {
if (bits[i] != bsBits[i]) {
return false;
}
}
for (int i = length1; i < length2; i++) {
if (bsBits[i] != 0) {
return false;
}
}
} else {
for (int i = 0; i < length2; i++) {
if (bits[i] != bsBits[i]) {
return false;
}
}
for (int i = length2; i < length1; i++) {
if (bits[i] != 0) {
return false;
}
}
}
return true;
}
return false;
|
public void | flip(int pos)Flips the bit at index {@code pos}. Grows the {@code BitSet} if
{@code 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(Msg.getString("K0006")); //$NON-NLS-1$
}
|
public void | flip(int pos1, int pos2)Flips the bits starting from {@code pos1} to {@code pos2}. Grows the
{@code BitSet} if {@code pos2 > size}.
if (pos1 >= 0 && pos2 >= 0 && pos2 >= pos1) {
if (pos1 == pos2) {
return;
}
if (pos2 >= bits.length * ELM_SIZE) {
growBits(pos2);
}
int idx1 = pos1 / ELM_SIZE;
int idx2 = (pos2 - 1) / ELM_SIZE;
long factor1 = (~0L) << (pos1 % ELM_SIZE);
long factor2 = (~0L) >>> (ELM_SIZE - (pos2 % ELM_SIZE));
if (idx1 == idx2) {
bits[idx1] ^= (factor1 & factor2);
} else {
bits[idx1] ^= factor1;
bits[idx2] ^= factor2;
for (int i = idx1 + 1; i < idx2; i++) {
bits[i] ^= (~0L);
}
}
} else {
throw new IndexOutOfBoundsException(Msg.getString("K0006")); //$NON-NLS-1$
}
|
public boolean | get(int pos)Retrieves the bit at index {@code pos}. Grows the {@code BitSet} if
{@code pos > size}.
if (pos >= 0) {
if (pos < bits.length * ELM_SIZE) {
return (bits[pos / ELM_SIZE] & (1L << (pos % ELM_SIZE))) != 0;
}
return false;
}
// Negative index specified
throw new IndexOutOfBoundsException(Msg.getString("K0006")); //$NON-NLS-1$
|
public java.util.BitSet | get(int pos1, int pos2)Retrieves the bits starting from {@code pos1} to {@code pos2} and returns
back a new bitset made of these bits. Grows the {@code BitSet} if
{@code pos2 > size}.
if (pos1 >= 0 && pos2 >= 0 && pos2 >= pos1) {
int last = (bits.length * ELM_SIZE);
if (pos1 >= last || pos1 == pos2) {
return new BitSet(0);
}
if (pos2 > last) {
pos2 = last;
}
int idx1 = pos1 / ELM_SIZE;
int idx2 = (pos2 - 1) / ELM_SIZE;
long factor1 = (~0L) << (pos1 % ELM_SIZE);
long factor2 = (~0L) >>> (ELM_SIZE - (pos2 % ELM_SIZE));
if (idx1 == idx2) {
long result = (bits[idx1] & (factor1 & factor2)) >>> (pos1 % ELM_SIZE);
return new BitSet(new long[] { result });
}
long[] newbits = new long[idx2 - idx1 + 1];
// first fill in the first and last indexes in the new bitset
newbits[0] = bits[idx1] & factor1;
newbits[newbits.length - 1] = bits[idx2] & factor2;
// fill in the in between elements of the new bitset
for (int i = 1; i < idx2 - idx1; i++) {
newbits[i] = bits[idx1 + i];
}
// shift all the elements in the new bitset to the right by pos1
// % ELM_SIZE
int numBitsToShift = pos1 % ELM_SIZE;
if (numBitsToShift != 0) {
for (int i = 0; i < newbits.length; i++) {
// shift the current element to the right regardless of
// sign
newbits[i] = newbits[i] >>> (numBitsToShift);
// apply the last x bits of newbits[i+1] to the current
// element
if (i != newbits.length - 1) {
newbits[i] |= newbits[i + 1] << (ELM_SIZE - (numBitsToShift));
}
}
}
return new BitSet(newbits);
}
throw new IndexOutOfBoundsException(Msg.getString("K0006")); //$NON-NLS-1$
|
private void | growBits(int pos)Increase the size of the internal array to accommodate {@code 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 | hashCode()Computes the hash code for this {@code BitSet}. If two {@code BitSet}s are equal
the have to return the same result for {@code hashCode()}.
long x = 1234;
// for (int i = 0, length = bits.length; i < length; i+=2)
// x ^= (bits[i] + ((long)bits[i+1] << 32)) * (i/2 + 1);
for (int i = 0, length = bits.length; i < length; i++) {
x ^= bits[i] * (i + 1);
}
return (int) ((x >> 32) ^ x);
|
public boolean | intersects(java.util.BitSet bs)Checks if these two {@code BitSet}s have at least one bit set to true in the same
position.
long[] bsBits = bs.bits;
int length1 = bits.length, length2 = bsBits.length;
if (length1 <= length2) {
for (int i = 0; i < length1; i++) {
if ((bits[i] & bsBits[i]) != 0L) {
return true;
}
}
} else {
for (int i = 0; i < length2; i++) {
if ((bits[i] & bsBits[i]) != 0L) {
return true;
}
}
}
return false;
|
public boolean | isEmpty()Returns true if all the bits in this {@code BitSet} are set to false.
for (int idx = 0; idx < bits.length; idx++) {
if (bits[idx] != 0L) {
return false;
}
}
return true;
|
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 int | nextClearBit(int pos)Returns the position of the first bit that is {@code false} on or after {@code pos}.
if (pos >= 0) {
int bssize = bits.length * ELM_SIZE;
if (pos >= bssize) {
return pos;
}
int idx = pos / ELM_SIZE;
// first check in the same bit set element
if (bits[idx] != (~0L)) {
for (int j = pos % ELM_SIZE; j < ELM_SIZE; j++) {
if (((bits[idx] & (1L << j)) == 0)) {
return idx * ELM_SIZE + j;
}
}
}
idx++;
while (idx < bits.length && bits[idx] == (~0L)) {
idx++;
}
if (idx == bits.length) {
return bssize;
}
// we know for sure there is a bit set to true in this element
// since the bitset value is not 0L
for (int j = 0; j < ELM_SIZE; j++) {
if (((bits[idx] & (1L << j)) == 0)) {
return idx * ELM_SIZE + j;
}
}
return bssize;
}
throw new IndexOutOfBoundsException(Msg.getString("K0006")); //$NON-NLS-1$
|
public int | nextSetBit(int pos)Returns the position of the first bit that is {@code true} on or after {@code pos}.
if (pos >= 0) {
if (pos >= bits.length * ELM_SIZE) {
return -1;
}
int idx = pos / ELM_SIZE;
// first check in the same bit set element
if (bits[idx] != 0L) {
for (int j = pos % ELM_SIZE; j < ELM_SIZE; j++) {
if (((bits[idx] & (1L << j)) != 0)) {
return idx * ELM_SIZE + j;
}
}
}
idx++;
while (idx < bits.length && bits[idx] == 0L) {
idx++;
}
if (idx == bits.length) {
return -1;
}
// we know for sure there is a bit set to true in this element
// since the bitset value is not 0L
for (int j = 0; j < ELM_SIZE; j++) {
if (((bits[idx] & (1L << j)) != 0)) {
return idx * ELM_SIZE + j;
}
}
return -1;
}
throw new IndexOutOfBoundsException(Msg.getString("K0006")); //$NON-NLS-1$
|
public void | or(java.util.BitSet bs)Performs the logical OR of this {@code BitSet} with another {@code BitSet}.
The values of this {@code BitSet} are changed accordingly.
int nbits = bs.length();
int length = nbits / ELM_SIZE + (nbits % ELM_SIZE > 0 ? 1 : 0);
if (length > bits.length) {
growBits(nbits - 1);
}
long[] bsBits = bs.bits;
for (int i = 0; i < length; i++) {
bits[i] |= bsBits[i];
}
|
public void | set(int pos)Sets the bit at index {@code pos} to 1. Grows the {@code BitSet} if
{@code 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(Msg.getString("K0006")); //$NON-NLS-1$
}
|
public void | set(int pos, boolean val)Sets the bit at index {@code pos} to {@code val}. Grows the
{@code BitSet} if {@code pos > size}.
if (val) {
set(pos);
} else {
clear(pos);
}
|
public void | set(int pos1, int pos2)Sets the bits starting from {@code pos1} to {@code pos2}. Grows the
{@code BitSet} if {@code pos2 > size}.
if (pos1 >= 0 && pos2 >= 0 && pos2 >= pos1) {
if (pos1 == pos2) {
return;
}
if (pos2 >= bits.length * ELM_SIZE) {
growBits(pos2);
}
int idx1 = pos1 / ELM_SIZE;
int idx2 = (pos2 - 1) / ELM_SIZE;
long factor1 = (~0L) << (pos1 % ELM_SIZE);
long factor2 = (~0L) >>> (ELM_SIZE - (pos2 % ELM_SIZE));
if (idx1 == idx2) {
bits[idx1] |= (factor1 & factor2);
} else {
bits[idx1] |= factor1;
bits[idx2] |= factor2;
for (int i = idx1 + 1; i < idx2; i++) {
bits[i] |= (~0L);
}
}
} else {
throw new IndexOutOfBoundsException(Msg.getString("K0006")); //$NON-NLS-1$
}
|
public void | set(int pos1, int pos2, boolean val)Sets the bits starting from {@code pos1} to {@code pos2} to the given
{@code val}. Grows the {@code BitSet} if {@code pos2 > size}.
if (val) {
set(pos1, pos2);
} else {
clear(pos1, pos2);
}
|
public int | size()Returns the number of bits this {@code BitSet} has.
return bits.length * ELM_SIZE;
|
public java.lang.String | toString()Returns 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 (int i = 0; i < bits.length; i++) {
if (bits[i] == 0) {
bitCount += ELM_SIZE;
continue;
}
for (int j = 0; j < ELM_SIZE; j++) {
if (((bits[i] & (1L << j)) != 0)) {
if (comma) {
sb.append(", "); //$NON-NLS-1$
}
sb.append(bitCount);
comma = true;
}
bitCount++;
}
}
sb.append('}");
return sb.toString();
|
public void | xor(java.util.BitSet bs)Performs the logical XOR of this {@code BitSet} with another {@code BitSet}.
The values of this {@code BitSet} are changed accordingly.
int nbits = bs.length();
int length = nbits / ELM_SIZE + (nbits % ELM_SIZE > 0 ? 1 : 0);
if (length > bits.length) {
growBits(nbits - 1);
}
long[] bsBits = bs.bits;
for (int i = 0; i < length; i++) {
bits[i] ^= bsBits[i];
}
|