Class ImmutableBitSet
- All Implemented Interfaces:
Serializable,Comparable<ImmutableBitSet>,Iterable<Integer>
- See Also:
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Comparator<ImmutableBitSet>Compares bit sets topologically, so that enclosing bit sets come first, using natural ordering to break ties.static final com.google.common.base.Function<? super BitSet,ImmutableBitSet> Deprecated.static final com.google.common.collect.Ordering<ImmutableBitSet> -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanallContain(Collection<ImmutableBitSet> bitSets, int bit) Checks if all bit sets contain a particular bit.booleanallMatch(IntPredicate predicate) Returns whether a given predicate evaluates to true for all bits in this set.booleananyMatch(IntPredicate predicate) Returns whether a given predicate evaluates to true for any bit in this set.asList()Creates a view onto this bit set as a list of integers.asSet()Creates a view onto this bit set as a set of integers.static ImmutableBitSet.Builderbuilder()Creates an empty Builder.static ImmutableBitSet.Builderbuilder(ImmutableBitSet bitSet) Deprecated.intReturns the number of bits set totruein thisImmutableBitSet.clear(int i) Returns a bit set the same as this but with a given bit cleared.clearIf(int i, boolean condition) Returns a bit set the same as this but with a given bit cleared if condition is true.static SortedMap<Integer,ImmutableBitSet> closure(SortedMap<Integer, ImmutableBitSet> equivalence) Computes the closure of a map from integers to bits.intCompares this ImmutableBitSet with another, using a lexicographic ordering.booleancontains(ImmutableBitSet set1) Returns true if all bits set in the second parameter are also set in the first.booleanCompares this object against the specified object.except(ImmutableBitSet that) Returns a bit set with all the bits in this set that are not in another.voidvoidforEachInt(IntConsumer action) AsforEach(Consumer)but on primitiveintvalues.static ImmutableBitSetfromBitSet(BitSet input) Returns a new immutable bit set containing all the bits in the givenBitSet.booleanget(int bitIndex) Returns the value of the bit with the specified index.get(int fromIndex, int toIndex) Returns a newImmutableBitSetcomposed of bits from thisImmutableBitSetfromfromIndex(inclusive) totoIndex(exclusive).inthashCode()Returns the hash code value for this bit set.intindexOf(int bit) The ordinal of a given bit, or -1 if it is not set.intersect(ImmutableBitSet that) Returns a bit set with all the bits set in both this set and in another.booleanReturns true if the specifiedImmutableBitSethas any bits set totruethat are also set totruein thisImmutableBitSet.booleanisEmpty()Returns true if thisImmutableBitSetcontains no bits that are set totrue.iterator()intlength()Returns the "logical size" of thisImmutableBitSet: the index of the highest set bit in theImmutableBitSetplus one.intnextClearBit(int fromIndex) Returns the index of the first bit that is set tofalsethat occurs on or after the specified starting index.intnextSetBit(int fromIndex) Returns the index of the first bit that is set totruethat occurs on or after the specified starting index.intnth(int n) Returns thenth set bit.static ImmutableBitSetof()Creates an ImmutableBitSet with no bits.static ImmutableBitSetof(int bit) Creates an ImmutableBitSet with the given bit set.static ImmutableBitSetof(int... bits) Creates an ImmutableBitSet with the given bits set.static ImmutableBitSetCreates an ImmutableBitSet whose contents are the bits denoted by a given collection of integers.static ImmutableBitSetof(ImmutableIntList bits) Creates an ImmutableBitSet with given bits set.static Iterable<ImmutableBitSet>Permutes a collection of bit sets according to a given mapping.Permutes a bit set according to a given mapping.permute(Mappings.TargetMapping mapping) Permutes a bit set according to a given mapping.powerSet()Computes the power set (set of all sets) of this bit set.intpreviousClearBit(int fromIndex) Returns the index of the nearest bit that is set tofalsethat occurs on or before the specified starting index.static ImmutableBitSetrange(int toIndex) Creates an ImmutableBitSet with bits between 0 andtoIndexset.static ImmutableBitSetrange(int fromIndex, int toIndex) Creates an ImmutableBitSet with bits fromfromIndex(inclusive) to specifiedtoIndex(exclusive) set totrue.rebuild()Creates a Builder whose initial contents are the same as this ImmutableBitSet.set(int i) Returns a bit set the same as this but with a given bit set.set(int i, boolean b) Returns a bit set the same as this but with a given bit set (if b is true) or unset (if b is false).setIf(int bit, boolean condition) Returns a bit set the same as this but with a given bit set if condition is true.shift(int offset) Returns a bit set with every bit moved upoffsetpositions.intsize()Returns the number of bits of space actually in use by thisImmutableBitSetto represent bit values.stream()Returns a stream of indices for which thisImmutableBitSetcontains a bit in the set state.int[]toArray()Converts this bit set to an array.toBitSet()Returns aBitSetthat has the same contents as thisImmutableBitSet.Creates a Collector.toList()Converts this bit set to a list.long[]Converts this bit set to an array of little-endian words.toString()Returns a string representation of this bit set.static ImmutableBitSetunion(Iterable<? extends ImmutableBitSet> sets) Returns the union of a number of bit sets.Returns the union of this immutable bit set with aBitSet.union(ImmutableBitSet other) Returns the union of this bit set with another.static ImmutableBitSetvalueOf(long... longs) Returns a new immutable bit set containing all the bits in the given long array.static ImmutableBitSetvalueOf(LongBuffer longs) Returns a new immutable bit set containing all the bits in the given long buffer.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
spliterator
-
Field Details
-
COMPARATOR
Compares bit sets topologically, so that enclosing bit sets come first, using natural ordering to break ties. -
ORDERING
-
FROM_BIT_SET
@Deprecated public static final com.google.common.base.Function<? super BitSet,ImmutableBitSet> FROM_BIT_SETDeprecated.
-
-
Method Details
-
of
Creates an ImmutableBitSet with no bits. -
of
Creates an ImmutableBitSet with the given bit set. -
of
Creates an ImmutableBitSet with the given bits set. -
of
Creates an ImmutableBitSet whose contents are the bits denoted by a given collection of integers. -
of
Creates an ImmutableBitSet with given bits set.For example,
of(ImmutableIntList.of(0, 3))returns a bit set with bits {0, 3} set.- Parameters:
bits- Collection of bits to set- Returns:
- Bit set
-
valueOf
Returns a new immutable bit set containing all the bits in the given long array.More precisely,
ImmutableBitSet.valueOf(longs).get(n) == ((longs[n/64] & (1L<<(n%64))) != 0)for all
n < 64 * longs.length.This method is equivalent to
ImmutableBitSet.valueOf(LongBuffer.wrap(longs)).- Parameters:
longs- a long array containing a little-endian representation of a sequence of bits to be used as the initial bits of the new bit set- Returns:
- a
ImmutableBitSetcontaining all the bits in the long array
-
valueOf
Returns a new immutable bit set containing all the bits in the given long buffer. -
fromBitSet
Returns a new immutable bit set containing all the bits in the givenBitSet. -
range
Creates an ImmutableBitSet with bits fromfromIndex(inclusive) to specifiedtoIndex(exclusive) set totrue.For example,
range(0, 3)returns a bit set with bits {0, 1, 2} set.- Parameters:
fromIndex- Index of the first bit to be set.toIndex- Index after the last bit to be set.- Returns:
- Bit set
-
range
Creates an ImmutableBitSet with bits between 0 andtoIndexset. -
toImmutableBitSet
Creates a Collector. -
powerSet
Computes the power set (set of all sets) of this bit set. -
get
public boolean get(int bitIndex) Returns the value of the bit with the specified index. The value istrueif the bit with the indexbitIndexis currently set in thisImmutableBitSet; otherwise, the result isfalse.- Parameters:
bitIndex- the bit index- Returns:
- the value of the bit with the specified index
- Throws:
IndexOutOfBoundsException- if the specified index is negative
-
get
Returns a newImmutableBitSetcomposed of bits from thisImmutableBitSetfromfromIndex(inclusive) totoIndex(exclusive).- Parameters:
fromIndex- index of the first bit to includetoIndex- index after the last bit to include- Returns:
- a new
ImmutableBitSetfrom a range of thisImmutableBitSet - Throws:
IndexOutOfBoundsException- iffromIndexis negative, ortoIndexis negative, orfromIndexis larger thantoIndex
-
toString
Returns a string representation of this bit set. For every index for which thisBitSetcontains 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();
NowdrPepper.toString()returns "{}".drPepper.set(2);
NowdrPepper.toString()returns "{2}".drPepper.set(4); drPepper.set(10);
NowdrPepper.toString()returns "{2, 4, 10}". -
intersects
Returns true if the specifiedImmutableBitSethas any bits set totruethat are also set totruein thisImmutableBitSet.- Parameters:
set-ImmutableBitSetto intersect with- Returns:
- boolean indicating whether this
ImmutableBitSetintersects the specifiedImmutableBitSet
-
cardinality
public int cardinality()Returns the number of bits set totruein thisImmutableBitSet.- See Also:
-
hashCode
public int hashCode()Returns the hash code value for this bit set. The hash code depends only on which bits are set within thisImmutableBitSet.The hash code is defined using the same calculation as
BitSet.hashCode(). -
size
public int size()Returns the number of bits of space actually in use by thisImmutableBitSetto represent bit values. The maximum element in the set is the size - 1st element.- Returns:
- the number of bits currently in this bit set
- See Also:
-
equals
Compares this object against the specified object. The result istrueif and only if the argument is notnulland is aImmutableBitSetobject that has exactly the same set of bits set totrueas this bit set. -
compareTo
Compares this ImmutableBitSet with another, using a lexicographic ordering.Bit sets
(), (0), (0, 1), (0, 1, 3), (1), (2, 3)are in sorted order.- Specified by:
compareToin interfaceComparable<ImmutableBitSet>
-
stream
Returns a stream of indices for which thisImmutableBitSetcontains 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 thecardinality()method.- Returns:
- a stream of integers representing set indices
-
nextSetBit
public int nextSetBit(int fromIndex) Returns the index of the first bit that is set totruethat occurs on or after the specified starting index. If no such bit exists then-1is returned.Based upon
BitSet.nextSetBit(int).- Parameters:
fromIndex- the index to start checking from (inclusive)- Returns:
- the index of the next set bit, or
-1if there is no such bit - Throws:
IndexOutOfBoundsException- if the specified index is negative
-
nextClearBit
public int nextClearBit(int fromIndex) Returns the index of the first bit that is set tofalsethat occurs on or after the specified starting index.- 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
-
previousClearBit
public int previousClearBit(int fromIndex) Returns the index of the nearest bit that is set tofalsethat occurs on or before the specified starting index. If no such bit exists, or if-1is given as the starting index, then-1is returned.- Parameters:
fromIndex- the index to start checking from (inclusive)- Returns:
- the index of the previous clear bit, or
-1if there is no such bit - Throws:
IndexOutOfBoundsException- if the specified index is less than-1
-
iterator
-
toList
Converts this bit set to a list. -
forEach
-
forEachInt
AsforEach(Consumer)but on primitiveintvalues. -
asList
Creates a view onto this bit set as a list of integers.The
cardinalityandgetmethods are both O(n), but the iterator is efficient. The list is memory efficient, and the CPU cost breaks even (versustoList()) if you intend to scan it only once. -
asSet
Creates a view onto this bit set as a set of integers.The
sizeandcontainsmethods are both O(n), but the iterator is efficient. -
toArray
public int[] toArray()Converts this bit set to an array.Each entry of the array is the ordinal of a set bit. The array is sorted.
- Returns:
- Array of set bits
-
toLongArray
public long[] toLongArray()Converts this bit set to an array of little-endian words. -
union
Returns the union of this immutable bit set with aBitSet. -
union
Returns the union of this bit set with another. -
union
Returns the union of a number of bit sets. -
except
Returns a bit set with all the bits in this set that are not in another.- See Also:
-
intersect
Returns a bit set with all the bits set in both this set and in another.- See Also:
-
contains
Returns true if all bits set in the second parameter are also set in the first. In other words, whether x is a super-set of y.- Parameters:
set1- Bitmap to be checked- Returns:
- Whether all bits in set1 are set in set0
-
indexOf
public int indexOf(int bit) The ordinal of a given bit, or -1 if it is not set. -
closure
public static SortedMap<Integer,ImmutableBitSet> closure(SortedMap<Integer, ImmutableBitSet> equivalence) Computes the closure of a map from integers to bits.The input must have an entry for each position.
Does not modify the input map or its bit sets.
-
length
public int length()Returns the "logical size" of thisImmutableBitSet: the index of the highest set bit in theImmutableBitSetplus one. Returns zero if theImmutableBitSetcontains no set bits.- Returns:
- the logical size of this
ImmutableBitSet
-
isEmpty
public boolean isEmpty()Returns true if thisImmutableBitSetcontains no bits that are set totrue. -
builder
Creates an empty Builder. -
builder
Deprecated. -
rebuild
Creates a Builder whose initial contents are the same as this ImmutableBitSet. -
nth
public int nth(int n) Returns thenth set bit.- Throws:
IndexOutOfBoundsException- if n is less than 0 or greater than the number of bits set
-
set
Returns a bit set the same as this but with a given bit set. -
set
Returns a bit set the same as this but with a given bit set (if b is true) or unset (if b is false). -
setIf
Returns a bit set the same as this but with a given bit set if condition is true. -
clear
Returns a bit set the same as this but with a given bit cleared. -
clearIf
Returns a bit set the same as this but with a given bit cleared if condition is true. -
toBitSet
Returns aBitSetthat has the same contents as thisImmutableBitSet. -
permute
Permutes a bit set according to a given mapping. -
permute
Permutes a bit set according to a given mapping. -
permute
public static Iterable<ImmutableBitSet> permute(Iterable<ImmutableBitSet> bitSets, Map<Integer, Integer> map) Permutes a collection of bit sets according to a given mapping. -
shift
Returns a bit set with every bit moved upoffsetpositions. Offset may be negative, but throws if any bit ends up negative. -
allContain
Checks if all bit sets contain a particular bit. -
allMatch
Returns whether a given predicate evaluates to true for all bits in this set. -
anyMatch
Returns whether a given predicate evaluates to true for any bit in this set.
-