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
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Performs the logical AND operation on this bit set and the givenBitSet
.void
Performs the logical AND operation on this bit set and the complement of the givenBitSet
.int
Returns the number of bits set totrue
.void
clear()
Sets all bits in the set to false.void
clear
(int bitIndex) Removes the integerbitIndex
from this set.void
clear
(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.boolean
Returnstrue
if theobj
is a bit set that contains exactly the same elements as this bit set, otherwise false.void
flip()
Sets all bits to the opposite value.void
flip
(int bitIndex) Sets the bit at the index to the opposite value.void
flip
(int from, int to) Sets a range of bits to the opposite value.boolean
get
(int bitIndex) Returnstrue
if the integerbitIndex
is in this bit set, otherwise false.get
(int from, int to) Returns a newBitSet
composed of a range of bits from this one.int
hashCode()
Returns a hash code value for this bit set.boolean
intersects
(BitSet bs) Returnstrue
if the specified BitSet and this one share at least one commontrue
bit.boolean
isEmpty()
Returnstrue
if this set contains notrue
bits.int
nextClearBit
(int from) Returns the index of the next false bit, from the specified bit (inclusive).int
nextSetBit
(int from) Returns the index of the nexttrue
bit, from the specified bit (inclusive).void
Performs the logical OR operation on this bit set and the givenBitSet
.int
previousSetBit
(int from) Returns the index of the nearest bit that is set totrue
that occurs on or before the specified starting index.void
set()
Sets all bits in the set to true.void
set
(int bitIndex) Add the integerbitIndex
to this set.void
set
(int bitIndex, boolean value) Sets the bit at the given index to the specified value.void
set
(int from, int to) Sets the bits between from (inclusive) and to (exclusive) totrue
.void
set
(int from, int to, boolean value) Sets the bits between from (inclusive) and to (exclusive) to the specified value.int
size()
Returns the number of bits that can be stored by this bit set.toString()
Returns the string representation of this bit set.void
Performs 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 from0
tonbits-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
true
bits.
-
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 integerbitIndex
from 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
Returnstrue
if theobj
is 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) Returnstrue
if the integerbitIndex
is 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 newBitSet
composed 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 bitk
is 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
Returnstrue
if the specified BitSet and this one share at least one commontrue
bit.- Parameters:
bs
- the set to check for intersection.- Returns:
true
if the sets intersect.- Throws:
NullPointerException
- if set isnull
.
-
isEmpty
public boolean isEmpty()Returnstrue
if this set contains notrue
bits.- Returns:
true
if 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 nexttrue
bit, from the specified bit (inclusive). If there is none, -1 is returned. You can iterate over alltrue
bits 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
true
bit, or -1. - Throws:
IndexOutOfBoundsException
- if from is negative.
-
previousSetBit
public int previousSetBit(int from) Returns the index of the nearest bit that is set totrue
that occurs on or before the specified starting index. If no such bit exists, or if-1
is given as the starting index, then-1
is returned.To iterate over the
true
bits 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
-1
if 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 integerbitIndex
to 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
.
-