Class ImmutableBitSet
- All Implemented Interfaces:
Serializable
,Comparable<ImmutableBitSet>
,Iterable<Integer>
- See Also:
-
Nested Class Summary
-
Field Summary
Modifier 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 boolean
allContain
(Collection<ImmutableBitSet> bitSets, int bit) Checks if all bit sets contain a particular bit.boolean
allMatch
(IntPredicate predicate) Returns whether a given predicate evaluates to true for all bits in this set.boolean
anyMatch
(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.Builder
builder()
Creates an empty Builder.static ImmutableBitSet.Builder
builder
(ImmutableBitSet bitSet) Deprecated.int
Returns the number of bits set totrue
in 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.int
Compares this ImmutableBitSet with another, using a lexicographic ordering.boolean
contains
(ImmutableBitSet set1) Returns true if all bits set in the second parameter are also set in the first.boolean
Compares 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.void
void
forEachInt
(IntConsumer action) AsforEach(Consumer)
but on primitiveint
values.static ImmutableBitSet
fromBitSet
(BitSet input) Returns a new immutable bit set containing all the bits in the givenBitSet
.boolean
get
(int bitIndex) Returns the value of the bit with the specified index.get
(int fromIndex, int toIndex) Returns a newImmutableBitSet
composed of bits from thisImmutableBitSet
fromfromIndex
(inclusive) totoIndex
(exclusive).int
hashCode()
Returns the hash code value for this bit set.int
indexOf
(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.boolean
Returns true if the specifiedImmutableBitSet
has any bits set totrue
that are also set totrue
in thisImmutableBitSet
.boolean
isEmpty()
Returns true if thisImmutableBitSet
contains no bits that are set totrue
.iterator()
int
length()
Returns the "logical size" of thisImmutableBitSet
: the index of the highest set bit in theImmutableBitSet
plus one.int
nextClearBit
(int fromIndex) Returns the index of the first bit that is set tofalse
that occurs on or after the specified starting index.int
nextSetBit
(int fromIndex) Returns the index of the first bit that is set totrue
that occurs on or after the specified starting index.int
nth
(int n) Returns then
th set bit.static ImmutableBitSet
of()
Creates an ImmutableBitSet with no bits.static ImmutableBitSet
of
(int bit) Creates an ImmutableBitSet with the given bit set.static ImmutableBitSet
of
(int... bits) Creates an ImmutableBitSet with the given bits set.static ImmutableBitSet
static ImmutableBitSet
of
(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.int
previousClearBit
(int fromIndex) Returns the index of the nearest bit that is set tofalse
that occurs on or before the specified starting index.static ImmutableBitSet
range
(int toIndex) Creates an ImmutableBitSet with bits between 0 andtoIndex
set.static ImmutableBitSet
range
(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 upoffset
positions.int
size()
Returns the number of bits of space actually in use by thisImmutableBitSet
to represent bit values.int[]
toArray()
Converts this bit set to an array.toBitSet()
Returns aBitSet
that 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 ImmutableBitSet
union
(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 ImmutableBitSet
valueOf
(long... longs) Returns a new immutable bit set containing all the bits in the given long array.static ImmutableBitSet
valueOf
(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, wait
Methods 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
-
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
ImmutableBitSet
containing 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 andtoIndex
set. -
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 istrue
if the bit with the indexbitIndex
is 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 newImmutableBitSet
composed of bits from thisImmutableBitSet
fromfromIndex
(inclusive) totoIndex
(exclusive).- Parameters:
fromIndex
- index of the first bit to includetoIndex
- index after the last bit to include- Returns:
- a new
ImmutableBitSet
from a range of thisImmutableBitSet
- Throws:
IndexOutOfBoundsException
- iffromIndex
is negative, ortoIndex
is negative, orfromIndex
is larger thantoIndex
-
toString
Returns a string representation of this bit set. For every index for which thisBitSet
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();
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 specifiedImmutableBitSet
has any bits set totrue
that are also set totrue
in thisImmutableBitSet
.- Parameters:
set
-ImmutableBitSet
to intersect with- Returns:
- boolean indicating whether this
ImmutableBitSet
intersects the specifiedImmutableBitSet
-
cardinality
public int cardinality()Returns the number of bits set totrue
in 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 thisImmutableBitSet
to 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 istrue
if and only if the argument is notnull
and is aImmutableBitSet
object that has exactly the same set of bits set totrue
as 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:
compareTo
in interfaceComparable<ImmutableBitSet>
-
nextSetBit
public int nextSetBit(int fromIndex) Returns the index of the first bit that is set totrue
that occurs on or after the specified starting index. If no such bit exists then-1
is returned.Based upon
BitSet.nextSetBit(int)
.- 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
-
nextClearBit
public int nextClearBit(int fromIndex) Returns the index of the first bit that is set tofalse
that 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 tofalse
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
-
iterator
-
toList
Converts this bit set to a list. -
forEach
-
forEachInt
AsforEach(Consumer)
but on primitiveint
values. -
asList
Creates a view onto this bit set as a list of integers.The
cardinality
andget
methods 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
size
andcontains
methods 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 theImmutableBitSet
plus one. Returns zero if theImmutableBitSet
contains no set bits.- Returns:
- the logical size of this
ImmutableBitSet
-
isEmpty
public boolean isEmpty()Returns true if thisImmutableBitSet
contains 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 then
th 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 aBitSet
that 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 upoffset
positions. 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.
-