Package com.xinapse.util
Class BitSet
java.lang.Object
com.xinapse.util.BitSet
- All Implemented Interfaces:
Serializable,Cloneable
A class representing an array of binary (1-bit) values in a more compact way than
does a boolean[]. It is based on the standard class
BitSet, but the
number of bits that can be stored is fixed at instantiation.
This implementation is NOT synchronized against concurrent access from
multiple threads. Specifically, if one thread is reading from a BitSet
while another thread is simultaneously modifying it, the results are
undefined.- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidPerforms the logical AND operation on this bit set and the givenBitSet.voidPerforms the logical AND operation on this bit set and the complement of the givenBitSet.intReturns the number of bits set totrue.voidclear()Sets all bits in the set to false.voidclear(int bitIndex) Removes the integerbitIndexfrom this set.voidclear(int from, int to) Sets the bits between from (inclusive) and to (exclusive) to false.clone()Create a clone of this bit set, that is an instance of the same class and contains the same elements.booleanReturnstrueif theobjis a bit set that contains exactly the same elements as this bit set, otherwise false.voidflip()Sets all bits to the opposite value.voidflip(int bitIndex) Sets the bit at the index to the opposite value.voidflip(int from, int to) Sets a range of bits to the opposite value.booleanget(int bitIndex) Returnstrueif the integerbitIndexis in this bit set, otherwise false.get(int from, int to) Returns a newBitSetcomposed of a range of bits from this one.inthashCode()Returns a hash code value for this bit set.booleanintersects(BitSet bs) Returnstrueif the specified BitSet and this one share at least one commontruebit.booleanisEmpty()Returnstrueif this set contains notruebits.intnextClearBit(int from) Returns the index of the next false bit, from the specified bit (inclusive).intnextSetBit(int from) Returns the index of the nexttruebit, from the specified bit (inclusive).voidPerforms the logical OR operation on this bit set and the givenBitSet.intpreviousSetBit(int from) Returns the index of the nearest bit that is set totruethat occurs on or before the specified starting index.voidset()Sets all bits in the set to true.voidset(int bitIndex) Add the integerbitIndexto this set.voidset(int bitIndex, boolean value) Sets the bit at the given index to the specified value.voidset(int from, int to) Sets the bits between from (inclusive) and to (exclusive) totrue.voidset(int from, int to, boolean value) Sets the bits between from (inclusive) and to (exclusive) to the specified value.intsize()Returns the number of bits that can be stored by this bit set.toString()Returns the string representation of this bit set.voidPerforms the logical XOR operation on this bit set and the givenBitSet.
-
Constructor Details
-
BitSet
public BitSet(int nbits) Create a new empty bit set, with a given size. This constructor creates enough space to represent the integers from0tonbits-1.- Parameters:
nbits- the size of the bit set.- Throws:
NegativeArraySizeException- if nbits < 0.
-
-
Method Details
-
and
Performs the logical AND operation on this bit set and the givenBitSet. This means it builds the intersection of the two sets. The result is stored into this bit set.- Parameters:
bs- the second bit set.- Throws:
NullPointerException- if set isnull.
-
andNot
Performs the logical AND operation on this bit set and the complement of the givenBitSet. This means it selects every element in the first set, that isn't in the second set. The result is stored into this bit set.- Parameters:
bs- the second bit set.- Throws:
NullPointerException- if set isnull.
-
cardinality
public int cardinality()Returns the number of bits set totrue.- Returns:
- the number of
truebits.
-
clear
public void clear()Sets all bits in the set to false. -
set
public void set()Sets all bits in the set to true. -
clear
public void clear(int bitIndex) Removes the integerbitIndexfrom this set. That is the corresponding bit is cleared. If the index is not in the set, this method does nothing.- Parameters:
bitIndex- a non-negative integer.- Throws:
IndexOutOfBoundsException- if bitIndex < 0 or if bitIndex >= nbits.
-
clear
public void clear(int from, int to) Sets the bits between from (inclusive) and to (exclusive) to false.- Parameters:
from- the start range (inclusive).to- the end range (exclusive).- Throws:
IndexOutOfBoundsException- if from < 0 || from > to || to > nbits.
-
clone
Create a clone of this bit set, that is an instance of the same class and contains the same elements. But it doesn't change when this bit set changes. -
equals
Returnstrueif theobjis a bit set that contains exactly the same elements as this bit set, otherwise false. -
flip
public void flip(int bitIndex) Sets the bit at the index to the opposite value.- Parameters:
bitIndex- the index of the bit.- Throws:
IndexOutOfBoundsException- if bitIndex is negative or if bitIndex >= nbits.
-
flip
public void flip()Sets all bits to the opposite value. -
flip
public void flip(int from, int to) Sets a range of bits to the opposite value.- Parameters:
from- the low index (inclusive).to- the high index (exclusive).- Throws:
IndexOutOfBoundsException- if from > to || from < 0 || to > nbits.
-
get
public boolean get(int bitIndex) Returnstrueif the integerbitIndexis in this bit set, otherwise false.- Parameters:
bitIndex- a non-negative integer.- Returns:
- the value of the bit at the specified index.
- Throws:
IndexOutOfBoundsException- if the bitIndex is negative or if bitIndex >= nbits.
-
get
Returns a newBitSetcomposed of a range of bits from this one.- Parameters:
from- the low index (inclusive).to- the high index (exclusive).- Returns:
- a new
BitSet. - Throws:
IndexOutOfBoundsException- if from > to || from < 0 || to >= nbits.
-
hashCode
public int hashCode()Returns a hash code value for this bit set. The hash code of two bit sets containing the same integers is identical. The algorithm used to compute it is as follows: Suppose the bits in the BitSet were to be stored in an array of long integers calledbits, in such a manner that bitkis set in the BitSet (for non-negative values ofk) if and only if((k/64) < bits.length) && ((bits[k/64] & (1L << (bit % 64))) != 0)Then the following definition of the hashCode method would be a correct implementation of the actual algorithm:public int hashCode() { long h = 1234; for (int i = bits.length-1; i >= 0; i--) { h ^= bits[i] * (i + 1); } return (int)((h >> 32) ^ h); }Note that the hash code values changes, if the set is changed. -
intersects
Returnstrueif the specified BitSet and this one share at least one commontruebit.- Parameters:
bs- the set to check for intersection.- Returns:
trueif the sets intersect.- Throws:
NullPointerException- if set isnull.
-
isEmpty
public boolean isEmpty()Returnstrueif this set contains notruebits.- Returns:
trueif all bits are false.
-
nextClearBit
public int nextClearBit(int from) Returns the index of the next false bit, from the specified bit (inclusive). If there is none, -1 is returned.- Parameters:
from- the start location.- Returns:
- the first false bit.
- Throws:
IndexOutOfBoundsException- if from is negative or if from is >= nbits.
-
nextSetBit
public int nextSetBit(int from) Returns the index of the nexttruebit, from the specified bit (inclusive). If there is none, -1 is returned. You can iterate over alltruebits with this loop:
for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i + 1)) { // operate on i here }- Parameters:
from- the start location.- Returns:
- the first
truebit, or -1. - Throws:
IndexOutOfBoundsException- if from is negative.
-
previousSetBit
public int previousSetBit(int from) Returns the index of the nearest bit that is set totruethat occurs on or before the specified starting index. If no such bit exists, or if-1is given as the starting index, then-1is returned.To iterate over the
truebits in aBitSet, use the following loop:for (int i = bs.previousBitSet(bs.size()-1); (i = bs.previousSetBit(i-1)) >= 0; ) { // operate on index i here }- Parameters:
from- the index to start checking from (inclusive).- Returns:
- the index of the previous set bit, or
-1if there is no such bit. - Throws:
IndexOutOfBoundsException- if the specified index is less than-1
-
or
Performs the logical OR operation on this bit set and the givenBitSet. This means it builds the union of the two sets. The result is stored into this bit set.- Parameters:
bs- the second bit set.- Throws:
NullPointerException- if bs isnull.IndexOutOfBoundsException- if nbits for bs is greater than nbits for thisBitSet.
-
set
public void set(int bitIndex) Add the integerbitIndexto this set. That is the corresponding bit is set totrue. If the index was already in the set, this method does nothing. The size of this structure is automatically increased as necessary.- Parameters:
bitIndex- a non-negative integer.- Throws:
IndexOutOfBoundsException- if bitIndex is negative or >=nbits.
-
set
public void set(int bitIndex, boolean value) Sets the bit at the given index to the specified value. The size of this structure is automatically increased as necessary.- Parameters:
bitIndex- the position to set.value- the value to set it to.- Throws:
IndexOutOfBoundsException- if bitIndex is negative or if bitIndex is >= nbits.
-
set
public void set(int from, int to) Sets the bits between from (inclusive) and to (exclusive) totrue.- Parameters:
from- the start range (inclusive).to- the end range (exclusive).- Throws:
IndexOutOfBoundsException- if from < 0 || from > to || to > nbits.
-
set
public void set(int from, int to, boolean value) Sets the bits between from (inclusive) and to (exclusive) to the specified value.- Parameters:
from- the start range (inclusive).to- the end range (exclusive).value- the value to set it to.- Throws:
IndexOutOfBoundsException- if from < 0 || from > to || to >= nbits.
-
size
public int size()Returns the number of bits that can be stored by this bit set.- Returns:
- the number of bits that can be stored.
-
toString
Returns the string representation of this bit set. This consists of a comma separated list of the integers in this set surrounded by curly braces. There is a space after each comma. A sample string is thus "{1, 3, 53}". -
xor
Performs the logical XOR operation on this bit set and the givenBitSet. This means it builds the symmetric remainder of the two sets (the elements that are in one set, but not in the other). The result is stored into this bit set.- Parameters:
bs- the second bit set.- Throws:
NullPointerException- if bs isnull.IndexOutOfBoundsException- if nbits for bs is greater than nbits for thisBitSet.
-