Class ImmutableBitSet
java.lang.Object
com.google.mu.collect.ImmutableBitSet
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:
- There are no mutating methods. Use
builder(),valueOf(long[])ortoImmutableBitSet()to create a set, andtoBuilder()to derive a modified copy. - There is no
BitSet.nextSetBit(); in place of the legacynextSetBit()loop, usestream().forEach(...). intersects(com.google.mu.collect.ImmutableBitSet)takes anotherImmutableBitSet.- An
ImmutableBitSetis neverequalto aBitSet.
- Since:
- 11.4
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic ImmutableBitSet.Builderbuilder()Returns a new builder.static ImmutableBitSet.BuilderbuilderWithExpectedSize(int expectedBits) Returns a new builder with room for the bits belowexpectedBits, the expectedsize()of the built set.abstract intReturns the number of set bits.abstract booleanReturns true ifobjis anImmutableBitSetwith the same bits set.abstract booleanget(int index) Returns true if the bit atindexis set.abstract inthashCode()abstract booleanintersects(ImmutableBitSet that) Returns true if any bit is set in both this set andthat.abstract booleanisEmpty()Returns true if no bit is set.abstract intlength()Returns the index of the highest set bit plus one, or 0 if no bit is set, likeBitSet.length().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.abstract intsize()LikeBitSet.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.abstract IntStreamstream()Returns the indices of the set bits in ascending order, likeBitSet.stream().abstract ImmutableBitSet.BuilderReturns a new builder with the bits of this set.static Collector<Integer, ?, ImmutableBitSet> Returns a collector of bit indices, the opposite ofstream():set.stream().boxed().collect(toImmutableBitSet())equalsset.abstract long[]Returns the set bits as a new array of words, in the little-endian layout ofvalueOf(long[])and without trailing zero words, likeBitSet.toLongArray().toString()Returns the set bits in ascending order, formatted likeBitSet.toString(), for example{1, 3, 100}.static ImmutableBitSetvalueOf(long[] words) Returns the set of bits that are set inwords, in the same little-endian layout asBitSet.valueOf(long[]): bitnis in the set if(words[n / 64] & (1L << (n % 64))) != 0, for alln < 64 * words.length.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.
-
Field Details
-
EMPTY
The empty set.
-
-
Method Details
-
builder
Returns a new builder. -
builderWithExpectedSize
Returns a new builder with room for the bits belowexpectedBits, the expectedsize()of the built set. Bits at or aboveexpectedBitscan still be set, and the built set'ssize()depends only on the bits that are set.- Throws:
IllegalArgumentException- ifexpectedBitsis negative
-
valueOf
Returns the set of bits that are set inwords, in the same little-endian layout asBitSet.valueOf(long[]): bitnis in the set if(words[n / 64] & (1L << (n % 64))) != 0, for alln < 64 * words.length.- Throws:
IllegalArgumentException- if any set bit would have an index aboveInteger.MAX_VALUE
-
toImmutableBitSet
Returns a collector of bit indices, the opposite ofstream():set.stream().boxed().collect(toImmutableBitSet())equalsset. Duplicate indices are set once, and a negative index throwsIndexOutOfBoundsException. -
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
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 atindexis set. Returns false for any index past the highest set bit.- Throws:
IndexOutOfBoundsException- ifindexis negative
-
cardinality
public abstract int cardinality()Returns the number of set bits. -
size
public abstract int size()LikeBitSet.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 inBitSet.size(), this overflows toInteger.MIN_VALUEwhen 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, likeBitSet.length(). As inBitSet.length(), this overflows toInteger.MIN_VALUEwhenInteger.MAX_VALUEis set. -
intersects
Returns true if any bit is set in both this set andthat. -
stream
Returns the indices of the set bits in ascending order, likeBitSet.stream(). -
toLongArray
public abstract long[] toLongArray()Returns the set bits as a new array of words, in the little-endian layout ofvalueOf(long[])and without trailing zero words, likeBitSet.toLongArray(). -
equals
-
hashCode
-
toString
Returns the set bits in ascending order, formatted likeBitSet.toString(), for example{1, 3, 100}. -
toBuilder
Returns a new builder with the bits of this set. Changes to the builder don't affect this set.
-