com.xinapse.util
Class BitSet

java.lang.Object
  extended by com.xinapse.util.BitSet
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable

public class BitSet
extends java.lang.Object
implements java.lang.Cloneable, java.io.Serializable

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 java.util.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:
Serialized Form

Constructor Summary
BitSet(int nbits)
          Create a new empty bit set, with a given size.
 
Method Summary
 void and(BitSet bs)
          Performs the logical AND operation on this bit set and the given BitSet.
 void andNot(BitSet bs)
          Performs the logical AND operation on this bit set and the complement of the given BitSet.
 int cardinality()
          Returns the number of bits set to true.
 void clear()
          Sets all bits in the set to false.
 void clear(int bitIndex)
          Removes the integer bitIndex from this set.
 void clear(int from, int to)
          Sets the bits between from (inclusive) and to (exclusive) to false.
 BitSet clone()
          Create a clone of this bit set, that is an instance of the same class and contains the same elements.
 boolean equals(java.lang.Object obj)
          Returns true if the obj 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)
          Returns true if the integer bitIndex is in this bit set, otherwise false.
 BitSet get(int from, int to)
          Returns a new BitSet composed of a range of bits from this one.
 int hashCode()
          Returns a hash code value for this bit set.
 boolean intersects(BitSet bs)
          Returns true if the specified BitSet and this one share at least one common true bit.
 boolean isEmpty()
          Returns true if this set contains no true 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 next true bit, from the specified bit (inclusive).
 void or(BitSet bs)
          Performs the logical OR operation on this bit set and the given BitSet.
 void set()
          Sets all bits in the set to true.
 void set(int bitIndex)
          Add the integer bitIndex 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) to true.
 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.
 java.lang.String toString()
          Returns the string representation of this bit set.
 void xor(BitSet bs)
          Performs the logical XOR operation on this bit set and the given BitSet.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

BitSet

public BitSet(int nbits)
Create a new empty bit set, with a given size. This constructor creates enough space to represent the integers from 0 to nbits-1.

Parameters:
nbits - the size of the bit set.
Throws:
java.lang.NegativeArraySizeException - if nbits < 0.
Method Detail

and

public void and(BitSet bs)
Performs the logical AND operation on this bit set and the given BitSet. 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:
java.lang.NullPointerException - if set is null.

andNot

public void andNot(BitSet bs)
Performs the logical AND operation on this bit set and the complement of the given BitSet. 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:
java.lang.NullPointerException - if set is null.

cardinality

public int cardinality()
Returns the number of bits set to true.

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 integer bitIndex 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:
java.lang.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:
java.lang.IndexOutOfBoundsException - if from < 0 || from > to || to > nbits.

clone

public BitSet 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.

Overrides:
clone in class java.lang.Object
Returns:
the clone of this object.

equals

public boolean equals(java.lang.Object obj)
Returns true if the obj is a bit set that contains exactly the same elements as this bit set, otherwise false.

Overrides:
equals in class java.lang.Object
Parameters:
obj - the object to compare to.
Returns:
true if obj equals this bit set.

flip

public void flip(int bitIndex)
Sets the bit at the index to the opposite value.

Parameters:
bitIndex - the index of the bit.
Throws:
java.lang.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:
java.lang.IndexOutOfBoundsException - if from > to || from < 0 || to > nbits.

get

public boolean get(int bitIndex)
Returns true if the integer bitIndex is in this bit set, otherwise false.

Parameters:
bitIndex - a non-negative integer.
Returns:
the value of the bit at the specified index.
Throws:
java.lang.IndexOutOfBoundsException - if the bitIndex is negative or if bitIndex >= nbits.

get

public BitSet get(int from,
                  int to)
Returns a new BitSet composed of a range of bits from this one.

Parameters:
from - the low index (inclusive).
to - the high index (exclusive).
Throws:
java.lang.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 called bits, in such a manner that bit k is set in the BitSet (for non-negative values of k) 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.

Overrides:
hashCode in class java.lang.Object
Returns:
the hash code value for this bit set.

intersects

public boolean intersects(BitSet bs)
Returns true if the specified BitSet and this one share at least one common true bit.

Parameters:
bs - the set to check for intersection.
Returns:
true if the sets intersect.
Throws:
java.lang.NullPointerException - if set is null.

isEmpty

public boolean isEmpty()
Returns true if this set contains no true 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:
java.lang.IndexOutOfBoundsException - if from is negative or if from is >= nbits.

nextSetBit

public int nextSetBit(int from)
Returns the index of the next true bit, from the specified bit (inclusive). If there is none, -1 is returned. You can iterate over all true 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:
java.lang.IndexOutOfBoundsException - if from is negative.

or

public void or(BitSet bs)
Performs the logical OR operation on this bit set and the given BitSet. 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:
java.lang.NullPointerException - if bs is null.
java.lang.IndexOutOfBoundsException - if nbits for bs is greater than nbits for this BitSet.

set

public void set(int bitIndex)
Add the integer bitIndex to this set. That is the corresponding bit is set to true. 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:
java.lang.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:
java.lang.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) to true.

Parameters:
from - the start range (inclusive).
to - the end range (exclusive).
Throws:
java.lang.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:
java.lang.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

public java.lang.String 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}".

Overrides:
toString in class java.lang.Object
Returns:
the string representation.

xor

public void xor(BitSet bs)
Performs the logical XOR operation on this bit set and the given BitSet. 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:
java.lang.NullPointerException - if bs is null.
java.lang.IndexOutOfBoundsException - if nbits for bs is greater than nbits for this BitSet.


Copyright 2006-2011 Xinapse Systems Limited. All Rights Reserved.