Class ImmutableBitSet

java.lang.Object
com.google.mu.collect.ImmutableBitSet

public abstract class ImmutableBitSet extends Object
An immutable set of non-negative bit indices.

ImmutableBitSet is not a subclass of BitSet, but it emulates the read-only API of BitSet (get(), cardinality(), length(), stream(), toLongArray() etc.), so it can mostly be a drop-in replacement for BitSet where immutability is desirable. Instances are safe to share between threads. The differences from BitSet:

Since:
11.4
  • Field Details

  • Method Details

    • builder

      public static ImmutableBitSet.Builder builder()
      Returns a new builder.
    • builderWithExpectedSize

      public static ImmutableBitSet.Builder builderWithExpectedSize(int expectedBits)
      Returns a new builder with room for the bits below expectedBits, the expected size() of the built set. Bits at or above expectedBits can still be set, and the built set's size() depends only on the bits that are set.
      Throws:
      IllegalArgumentException - if expectedBits is negative
    • valueOf

      public static ImmutableBitSet valueOf(long[] words)
      Returns the set of bits that are set in words, in the same little-endian layout as BitSet.valueOf(long[]): bit n is in the set if (words[n / 64] & (1L << (n % 64))) != 0, for all n < 64 * words.length.
      Throws:
      IllegalArgumentException - if any set bit would have an index above Integer.MAX_VALUE
    • toImmutableBitSet

      public static Collector<Integer, ?, ImmutableBitSet> toImmutableBitSet()
      Returns a collector of bit indices, the opposite of stream(): set.stream().boxed().collect(toImmutableBitSet()) equals set. Duplicate indices are set once, and a negative index throws IndexOutOfBoundsException.
    • or

      public static Collector<ImmutableBitSet, ?, ImmutableBitSet> or()
      Returns a collector of the union of the input sets: a bit is in the result if it's set in any input set. Collecting no sets returns the empty set.
    • xor

      public static Collector<ImmutableBitSet, ?, ImmutableBitSet> xor()
      Returns a collector of the symmetric difference of the input sets: a bit is in the result if it's set in an odd number of input sets. Collecting no sets returns the empty set.
    • get

      public abstract boolean get(int index)
      Returns true if the bit at index is set. Returns false for any index past the highest set bit.
      Throws:
      IndexOutOfBoundsException - if index is negative
    • cardinality

      public abstract int cardinality()
      Returns the number of set bits.
    • size

      public abstract int size()
      Like BitSet.size(), returns the number of bits of space used: 64 if every set bit is below 64 (including the empty set), otherwise 64 times the number of 64-bit words up to and including the one holding the highest set bit. As in BitSet.size(), this overflows to Integer.MIN_VALUE when any bit in [Integer.MAX_VALUE - 63, Integer.MAX_VALUE] is set.
    • isEmpty

      public abstract boolean isEmpty()
      Returns true if no bit is set.
    • length

      public abstract int length()
      Returns the index of the highest set bit plus one, or 0 if no bit is set, like BitSet.length(). As in BitSet.length(), this overflows to Integer.MIN_VALUE when Integer.MAX_VALUE is set.
    • intersects

      public abstract boolean intersects(ImmutableBitSet that)
      Returns true if any bit is set in both this set and that.
    • stream

      public abstract IntStream stream()
      Returns the indices of the set bits in ascending order, like BitSet.stream().
    • toLongArray

      public abstract long[] toLongArray()
      Returns the set bits as a new array of words, in the little-endian layout of valueOf(long[]) and without trailing zero words, like BitSet.toLongArray().
    • equals

      public abstract boolean equals(Object obj)
      Returns true if obj is an ImmutableBitSet with the same bits set.
      Overrides:
      equals in class Object
    • hashCode

      public abstract int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Returns the set bits in ascending order, formatted like BitSet.toString(), for example {1, 3, 100}.
      Overrides:
      toString in class Object
    • toBuilder

      public abstract ImmutableBitSet.Builder toBuilder()
      Returns a new builder with the bits of this set. Changes to the builder don't affect this set.