Class BitSet

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

public class BitSet extends Object implements Cloneable, 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 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
    Constructor
    Description
    BitSet(int nbits)
    Create a new empty bit set, with a given size.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    and(BitSet bs)
    Performs the logical AND operation on this bit set and the given BitSet.
    void
    Performs the logical AND operation on this bit set and the complement of the given BitSet.
    int
    Returns the number of bits set to true.
    void
    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.
    Create a clone of this bit set, that is an instance of the same class and contains the same elements.
    boolean
    Returns true if the obj is a bit set that contains exactly the same elements as this bit set, otherwise false.
    void
    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.
    get(int from, int to)
    Returns a new BitSet composed of a range of bits from this one.
    int
    Returns a hash code value for this bit set.
    boolean
    Returns true if the specified BitSet and this one share at least one common true bit.
    boolean
    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.
    int
    previousSetBit(int from)
    Returns the index of the nearest bit that is set to true 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 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
    Returns the number of bits that can be stored by this bit set.
    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 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 from 0 to nbits-1.
      Parameters:
      nbits - the size of the bit set.
      Throws:
      NegativeArraySizeException - if nbits < 0.
  • Method Details

    • 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:
      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:
      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:
      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

      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 Object
      Returns:
      the clone of this object.
    • equals

      public boolean equals(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 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:
      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)
      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:
      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).
      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 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 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:
      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:
      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:
      IndexOutOfBoundsException - if from is negative.
    • previousSetBit

      public int previousSetBit(int from)
      Returns the index of the nearest bit that is set to true 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 a BitSet, 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

      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:
      NullPointerException - if bs is null.
      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:
      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) to true.
      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

      public 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 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:
      NullPointerException - if bs is null.
      IndexOutOfBoundsException - if nbits for bs is greater than nbits for this BitSet.