Class BitSet

All Implemented Interfaces:
Iterable<Integer>, Collection<Integer>, Set<Integer>

public class BitSet extends AbstractSet<Integer>
Fast integer set implementation based on JDK BitSet class
  • Constructor Summary

    Constructors
    Constructor
    Description
    BitSet(int nElements)
    Creates a bit set whose initial size is large enough to explicitly represent bits with indices in the range 0 through nbits-1.
    BitSet(int nElements, Collection<Integer> other)
     
    BitSet(BitSet other)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    add(int bitIndex)
    Sets the bit at the specified index to true.
    void
    add(int fromIndex, int toIndex)
    Sets the bits from the specified fromIndex (inclusive) to the specified toIndex (exclusive) to true.
    boolean
    add(Integer integer)
     
    boolean
    addAll(Collection<? extends Integer> c)
     
    void
    and(BitSet set)
    Performs a logical AND of this target bit set with the argument bit set.
    void
    Clears all of the bits in this BitSet whose corresponding bit is set in the specified BitSet.
    void
     
    void
    Sets all of the bits in this BitSet to false.
    Cloning this BitSet produces a new BitSet that is equal to it.
    boolean
     
    boolean
     
    static BitSet
    Returns the difference between two sets as a new set.
    boolean
    Compares this object against the specified object.
    void
    flip(int bitIndex)
    Sets the bit at the specified index to the complement of its current value.
    void
    flip(int fromIndex, int toIndex)
    Sets each bit from the specified fromIndex (inclusive) to the specified toIndex (exclusive) to the complement of its current value.
    boolean
    get(int bitIndex)
    Returns the value of the bit with the specified index.
    int
    BitSet can contain elements in range [0, capacity).
    int
    Returns the hash code value for this bit set.
    static BitSet
    Returns the intersection between two sets as a new set
    boolean
    Returns true if the specified BitSet has any bits set to true that are also set to true in this BitSet.
    boolean
    Returns true if this BitSet contains no bits that are set to true.
     
    int
    nextClearBit(int fromIndex)
    Returns the index of the first bit that is set to false that occurs on or after the specified starting index.
    int
    nextSetBit(int fromIndex)
    Returns the index of the first bit that is set to true that occurs on or after the specified starting index.
    static BitSet
    Returns a new BitSet containing all elements in range [0, n] except those present in the given set.
    static BitSet
    of(int maxSize, int... numbers)
    Initialize a new bitset with the given numbers
    void
    or(BitSet set)
    Performs a logical OR of this bit set with the bit set argument.
    int
    previousClearBit(int fromIndex)
    Returns the index of the nearest bit that is set to false that occurs on or before the specified starting index.
    int
    previousSetBit(int fromIndex)
    Returns the index of the nearest bit that is set to true that occurs on or before the specified starting index.
    boolean
    remove(int bitIndex)
    Sets the bit specified by the index to false.
    void
    remove(int fromIndex, int toIndex)
    Sets the bits from the specified fromIndex (inclusive) to the specified toIndex (exclusive) to false.
    boolean
     
    boolean
     
    boolean
     
    int
    Returns the number of bits set to true in this BitSet.
     
    Returns a stream of indices for which this BitSet contains a bit in the set state.
    static BitSet
    Returns the symmetric difference between two sets as a new set
    Returns a string representation of this bit set.
    static BitSet
    Returns the union between two sets as a new set
    void
    xor(BitSet set)
    Performs a logical XOR of this bit set with the bit set argument.

    Methods inherited from class java.util.AbstractCollection

    toArray, toArray

    Methods inherited from class java.lang.Object

    finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface java.util.Collection

    parallelStream, removeIf, toArray

    Methods inherited from interface java.lang.Iterable

    forEach

    Methods inherited from interface java.util.Set

    toArray, toArray
  • Constructor Details

    • BitSet

      public BitSet(int nElements)
      Creates a bit set whose initial size is large enough to explicitly represent bits with indices in the range 0 through nbits-1. All bits are initially false.
      Parameters:
      nElements - the initial size of the bit set. Will be rounded to the nearest multiple of 64.
      Throws:
      NegativeArraySizeException - if the specified initial size is negative
    • BitSet

      public BitSet(BitSet other)
    • BitSet

      public BitSet(int nElements, Collection<Integer> other)
  • Method Details

    • of

      public static BitSet of(int maxSize, int... numbers)
      Initialize a new bitset with the given numbers
      Parameters:
      maxSize - biggest number that may be stored in set
      numbers - numbers initially stored in set
      Returns:
      new set
    • intersection

      public static BitSet intersection(BitSet a, BitSet b)
      Returns the intersection between two sets as a new set
      Parameters:
      a - First set
      b - Second set
      Returns:
      New set, original sets are not modified
    • not

      public static BitSet not(BitSet a)
      Returns a new BitSet containing all elements in range [0, n] except those present in the given set.
      Parameters:
      a - set
      Returns:
      New set, original set is not modified
    • union

      public static BitSet union(BitSet a, BitSet b)
      Returns the union between two sets as a new set
      Parameters:
      a - First set
      b - Second set
      Returns:
      New set, original sets are not modified
    • difference

      public static BitSet difference(BitSet a, BitSet b)
      Returns the difference between two sets as a new set.
      Parameters:
      a - First set
      b - Second set
      Returns:
      New set, with all elements in set A removing those that are in set B
    • symmetricDifference

      public static BitSet symmetricDifference(BitSet a, BitSet b)
      Returns the symmetric difference between two sets as a new set
      Parameters:
      a - First set
      b - Second set
      Returns:
      New set, original sets are not modified
    • getCapacity

      public int getCapacity()
      BitSet can contain elements in range [0, capacity). Get the upper bound.
      Returns:
      capacity, as initialized in constructor method
    • flip

      public void flip(int bitIndex)
      Sets the bit at the specified index to the complement of its current value.
      Parameters:
      bitIndex - the index of the bit to flip
      Throws:
      IndexOutOfBoundsException - if the specified index is negative
      Since:
      1.4
    • flip

      public void flip(int fromIndex, int toIndex)
      Sets each bit from the specified fromIndex (inclusive) to the specified toIndex (exclusive) to the complement of its current value.
      Parameters:
      fromIndex - index of the first bit to flip
      toIndex - index after the last bit to flip
      Throws:
      IndexOutOfBoundsException - if fromIndex is negative, or toIndex is negative, or fromIndex is larger than toIndex
      Since:
      1.4
    • add

      public boolean add(int bitIndex)
      Sets the bit at the specified index to true.
      Parameters:
      bitIndex - a bit index
      Returns:
      true if an element was added, false if the set was not modified
      Throws:
      IndexOutOfBoundsException - if the specified index is negative
    • addAll

      public boolean addAll(Collection<? extends Integer> c)
      Specified by:
      addAll in interface Collection<Integer>
      Specified by:
      addAll in interface Set<Integer>
      Overrides:
      addAll in class AbstractCollection<Integer>
    • add

      public void add(int fromIndex, int toIndex)
      Sets the bits from the specified fromIndex (inclusive) to the specified toIndex (exclusive) to true.
      Parameters:
      fromIndex - index of the first bit to be set, inclusive
      toIndex - index after the last bit to be set, exclusive
      Throws:
      IndexOutOfBoundsException - if fromIndex is negative, or toIndex is negative, or fromIndex is larger than toIndex
      Since:
      1.4
    • remove

      public boolean remove(int bitIndex)
      Sets the bit specified by the index to false.
      Parameters:
      bitIndex - the index of the bit to be cleared
      Returns:
      true if an element was removed, false if the set was not modified
      Throws:
      IndexOutOfBoundsException - if the specified index is negative
    • remove

      public void remove(int fromIndex, int toIndex)
      Sets the bits from the specified fromIndex (inclusive) to the specified toIndex (exclusive) to false.
      Parameters:
      fromIndex - index of the first bit to be cleared
      toIndex - index after the last bit to be cleared
      Throws:
      IndexOutOfBoundsException - if fromIndex is negative, or toIndex is negative, or fromIndex is larger than toIndex
      Since:
      1.4
    • clear

      public void clear()
      Sets all of the bits in this BitSet to false.
      Specified by:
      clear in interface Collection<Integer>
      Specified by:
      clear in interface Set<Integer>
      Overrides:
      clear in class AbstractCollection<Integer>
      Since:
      1.4
    • get

      public boolean get(int bitIndex)
      Returns the value of the bit with the specified index. The value is true if the bit with the index bitIndex is currently set in this BitSet; otherwise, the result is false.
      Parameters:
      bitIndex - the bit index
      Returns:
      the value of the bit with the specified index
      Throws:
      IndexOutOfBoundsException - if the specified index is negative
    • nextSetBit

      public int nextSetBit(int fromIndex)
      Returns the index of the first bit that is set to true that occurs on or after the specified starting index. If no such bit exists then -1 is returned.

      To iterate over the true bits in a BitSet, use the following loop:

       
       for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) {
           // operate on index i here
           if (i == Integer.MAX_VALUE) {
               break; // or (i+1) would overflow
           }
       }
      Parameters:
      fromIndex - the index to start checking from (inclusive)
      Returns:
      the index of the next set bit, or -1 if there is no such bit
      Throws:
      IndexOutOfBoundsException - if the specified index is negative
      Since:
      1.4
    • nextClearBit

      public int nextClearBit(int fromIndex)
      Returns the index of the first bit that is set to false that occurs on or after the specified starting index. If no such bit exists then -1 is returned.
      Parameters:
      fromIndex - the index to start checking from (inclusive)
      Returns:
      the index of the next clear bit
      Throws:
      IndexOutOfBoundsException - if the specified index is negative
      Since:
      1.4
    • previousSetBit

      public int previousSetBit(int fromIndex)
      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.length(); (i = bs.previousSetBit(i-1)) >= 0; ) {
           // operate on index i here
       }
      Parameters:
      fromIndex - 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
      Since:
      1.7
    • previousClearBit

      public int previousClearBit(int fromIndex)
      Returns the index of the nearest bit that is set to false 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.
      Parameters:
      fromIndex - the index to start checking from (inclusive)
      Returns:
      the index of the previous clear bit, or -1 if there is no such bit
      Throws:
      IndexOutOfBoundsException - if the specified index is less than -1
      Since:
      1.7
    • isEmpty

      public boolean isEmpty()
      Returns true if this BitSet contains no bits that are set to true.
      Specified by:
      isEmpty in interface Collection<Integer>
      Specified by:
      isEmpty in interface Set<Integer>
      Overrides:
      isEmpty in class AbstractCollection<Integer>
      Returns:
      boolean indicating whether this BitSet is empty
      Since:
      1.4
    • intersects

      public boolean intersects(BitSet set)
      Returns true if the specified BitSet has any bits set to true that are also set to true in this BitSet.
      Parameters:
      set - BitSet to intersect with
      Returns:
      boolean indicating whether this BitSet intersects the specified BitSet
      Since:
      1.4
    • size

      public int size()
      Returns the number of bits set to true in this BitSet.
      Specified by:
      size in interface Collection<Integer>
      Specified by:
      size in interface Set<Integer>
      Specified by:
      size in class AbstractCollection<Integer>
      Returns:
      the number of bits set to true in this BitSet
      Since:
      1.4
    • and

      public void and(BitSet set)
      Performs a logical AND of this target bit set with the argument bit set. This bit set is modified so that each bit in it has the value true if and only if it both initially had the value true and the corresponding bit in the bit set argument also had the value true.
      Parameters:
      set - a bit set
    • or

      public void or(BitSet set)
      Performs a logical OR of this bit set with the bit set argument. This bit set is modified so that a bit in it has the value true if and only if it either already had the value true or the corresponding bit in the bit set argument has the value true.
      Parameters:
      set - a bit set
    • xor

      public void xor(BitSet set)
      Performs a logical XOR of this bit set with the bit set argument. This bit set is modified so that a bit in it has the value true if and only if one of the following statements holds:
      • The bit initially has the value true, and the corresponding bit in the argument has the value false.
      • The bit initially has the value false, and the corresponding bit in the argument has the value true.
      Parameters:
      set - a bit set
    • andNot

      public void andNot(BitSet set)
      Clears all of the bits in this BitSet whose corresponding bit is set in the specified BitSet.
      Parameters:
      set - the BitSet with which to mask this BitSet
      Since:
      1.2
    • checkSameSize

      public void checkSameSize(BitSet set)
    • hashCode

      public int hashCode()
      Returns the hash code value for this bit set. The hash code depends only on which bits are set within this BitSet.

      The hash code is defined to be the result of the following calculation:

       
       public int hashCode() {
           long h = 1234;
           long[] words = toLongArray();
           for (int i = words.length; --i >= 0; )
               h ^= words[i] * (i + 1);
           return (int)((h >> 32) ^ h);
       }
      Note that the hash code changes if the set of bits is altered.
      Specified by:
      hashCode in interface Collection<Integer>
      Specified by:
      hashCode in interface Set<Integer>
      Overrides:
      hashCode in class AbstractSet<Integer>
      Returns:
      the hash code value for this bit set
    • iterator

      public Iterator<Integer> iterator()
      Specified by:
      iterator in interface Collection<Integer>
      Specified by:
      iterator in interface Iterable<Integer>
      Specified by:
      iterator in interface Set<Integer>
      Specified by:
      iterator in class AbstractCollection<Integer>
    • equals

      public boolean equals(Object obj)
      Compares this object against the specified object. The result is true if and only if the argument is not null and is a BitSet object that has exactly the same set of bits set to true as this bit set. That is, for every nonnegative int index k,
      ((BitSet)obj).get(k) == this.get(k)
      must be true. IMPORTANT: The current sizes of the two bit sets ARE COMPARED FIRST.
      Specified by:
      equals in interface Collection<Integer>
      Specified by:
      equals in interface Set<Integer>
      Overrides:
      equals in class AbstractSet<Integer>
      Parameters:
      obj - the object to compare with
      Returns:
      true if the objects are the same; false otherwise
      See Also:
    • clone

      public BitSet clone()
      Cloning this BitSet produces a new BitSet that is equal to it. The clone of the bit set is another bit set that has exactly the same bits set to true as this bit set.
      Overrides:
      clone in class Object
      Returns:
      a clone of this bit set
      See Also:
    • toString

      public String toString()
      Returns a string representation of this bit set. For every index for which this BitSet contains a bit in the set state, the decimal representation of that index is included in the result. Such indices are listed in order from lowest to highest, separated by ", " (a comma and a space) and surrounded by braces, resulting in the usual mathematical notation for a set of integers.

      Example:

       BitSet drPepper = new BitSet();
      Now drPepper.toString() returns "{}".
       drPepper.set(2);
      Now drPepper.toString() returns "{2}".
       drPepper.set(4);
       drPepper.set(10);
      Now drPepper.toString() returns "{2, 4, 10}".
      Overrides:
      toString in class AbstractCollection<Integer>
      Returns:
      a string representation of this bit set
    • add

      public boolean add(Integer integer)
      Specified by:
      add in interface Collection<Integer>
      Specified by:
      add in interface Set<Integer>
      Overrides:
      add in class AbstractCollection<Integer>
    • remove

      public boolean remove(Object o)
      Specified by:
      remove in interface Collection<Integer>
      Specified by:
      remove in interface Set<Integer>
      Overrides:
      remove in class AbstractCollection<Integer>
    • contains

      public boolean contains(Object o)
      Specified by:
      contains in interface Collection<Integer>
      Specified by:
      contains in interface Set<Integer>
      Overrides:
      contains in class AbstractCollection<Integer>
    • spliterator

      public Spliterator<Integer> spliterator()
    • removeAll

      public boolean removeAll(Collection<?> c)
      Specified by:
      removeAll in interface Collection<Integer>
      Specified by:
      removeAll in interface Set<Integer>
      Overrides:
      removeAll in class AbstractSet<Integer>
    • containsAll

      public boolean containsAll(Collection<?> c)
      Specified by:
      containsAll in interface Collection<Integer>
      Specified by:
      containsAll in interface Set<Integer>
      Overrides:
      containsAll in class AbstractCollection<Integer>
    • retainAll

      public boolean retainAll(Collection<?> c)
      Specified by:
      retainAll in interface Collection<Integer>
      Specified by:
      retainAll in interface Set<Integer>
      Overrides:
      retainAll in class AbstractCollection<Integer>
    • stream

      public Stream<Integer> stream()
      Returns a stream of indices for which this BitSet contains a bit in the set state. The indices are returned in order, from lowest to highest. The size of the stream is the number of bits in the set state, equal to the value returned by the size() ()} method.

      The stream binds to this bit set when the terminal stream operation commences (specifically, the spliterator for the stream is late-binding). If the bit set is modified during that operation then the result is undefined.

      Returns:
      a stream of integers representing set indices
      Since:
      1.8