Class ImmutableBitSet
- java.lang.Object
-
- org.apache.calcite.util.ImmutableBitSet
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Comparable<ImmutableBitSet>
,java.lang.Iterable<java.lang.Integer>
public class ImmutableBitSet extends java.lang.Object implements java.lang.Iterable<java.lang.Integer>, java.io.Serializable, java.lang.Comparable<ImmutableBitSet>
An immutable list of bits.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ImmutableBitSet.Builder
Builder.private static class
ImmutableBitSet.Closure
Setup equivalence Sets for each position.private static class
ImmutableBitSet.Rebuilder
Refinement ofImmutableBitSet.Builder
that remembers its originalImmutableBitSet
and tries to use it whenImmutableBitSet.Rebuilder.build()
is called.
-
Field Summary
Fields Modifier and Type Field Description private static int
ADDRESS_BITS_PER_WORD
private static int
BITS_PER_WORD
static java.util.Comparator<ImmutableBitSet>
COMPARATOR
Compares bit sets topologically, so that enclosing bit sets come first, using natural ordering to break ties.private static ImmutableBitSet
EMPTY
private static long[]
EMPTY_LONGS
static com.google.common.base.Function<? super java.util.BitSet,ImmutableBitSet>
FROM_BIT_SET
Deprecated.static com.google.common.collect.Ordering<ImmutableBitSet>
ORDERING
private static long
WORD_MASK
private long[]
words
-
Constructor Summary
Constructors Modifier Constructor Description private
ImmutableBitSet(long[] words)
Private constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description java.util.List<java.lang.Integer>
asList()
Creates a view onto this bit set as a list of integers.java.util.Set<java.lang.Integer>
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
cardinality()
Returns the number of bits set totrue
in thisImmutableBitSet
.private static void
checkRange(int fromIndex, int toIndex)
Checks that fromIndex ...ImmutableBitSet
clear(int i)
Returns a bit set the same as this but with a given bit cleared.ImmutableBitSet
clearIf(int i, boolean condition)
Returns a bit set the same as this but with a given bit cleared if condition is true.static java.util.SortedMap<java.lang.Integer,ImmutableBitSet>
closure(java.util.SortedMap<java.lang.Integer,ImmutableBitSet> equivalence)
Computes the closure of a map from integers to bits.int
compareTo(ImmutableBitSet o)
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.private static int
countBits(long[] words)
boolean
equals(java.lang.Object obj)
Compares this object against the specified object.ImmutableBitSet
except(ImmutableBitSet that)
Returns a bit set with all the bits in this set that are not in another.static ImmutableBitSet
fromBitSet(java.util.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.ImmutableBitSet
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.ImmutableBitSet
intersect(ImmutableBitSet that)
Returns a bit set with all the bits set in both this set and in another.boolean
intersects(ImmutableBitSet set)
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
.java.util.Iterator<java.lang.Integer>
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... bits)
static ImmutableBitSet
of(java.lang.Iterable<java.lang.Integer> bits)
static ImmutableBitSet
of(ImmutableIntList bits)
Creates an ImmutableBitSet with given bits set.static java.lang.Iterable<ImmutableBitSet>
permute(java.lang.Iterable<ImmutableBitSet> bitSets, java.util.Map<java.lang.Integer,java.lang.Integer> map)
Permutes a collection of bit sets according to a given mapping.ImmutableBitSet
permute(java.util.Map<java.lang.Integer,java.lang.Integer> map)
Permutes a bit set according to a given mapping.java.lang.Iterable<ImmutableBitSet>
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
.ImmutableBitSet.Builder
rebuild()
Creates a Builder whose initial contents are the same as this ImmutableBitSet.ImmutableBitSet
set(int i)
Returns a bit set the same as this but with a given bit set.ImmutableBitSet
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).ImmutableBitSet
setIf(int bit, boolean condition)
Returns a bit set the same as this but with a given bit set if condition is true.ImmutableBitSet
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.java.util.BitSet
toBitSet()
Returns aBitSet
that has the same contents as thisImmutableBitSet
.java.util.List<java.lang.Integer>
toList()
Converts this bit set to a list.long[]
toLongArray()
Converts this bit set to an array of little-endian words.java.lang.String
toString()
Returns a string representation of this bit set.static ImmutableBitSet
union(java.lang.Iterable<? extends ImmutableBitSet> sets)
Returns the union of a number of bit sets.ImmutableBitSet
union(java.util.BitSet other)
Returns the union of this immutable bit set with aBitSet
.ImmutableBitSet
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(java.nio.LongBuffer longs)
Returns a new immutable bit set containing all the bits in the given long buffer.private static int
wordIndex(int bitIndex)
Given a bit index, return word index containing it.
-
-
-
Field Detail
-
COMPARATOR
public static final java.util.Comparator<ImmutableBitSet> COMPARATOR
Compares bit sets topologically, so that enclosing bit sets come first, using natural ordering to break ties.
-
ORDERING
public static final com.google.common.collect.Ordering<ImmutableBitSet> ORDERING
-
ADDRESS_BITS_PER_WORD
private static final int ADDRESS_BITS_PER_WORD
- See Also:
- Constant Field Values
-
BITS_PER_WORD
private static final int BITS_PER_WORD
- See Also:
- Constant Field Values
-
WORD_MASK
private static final long WORD_MASK
- See Also:
- Constant Field Values
-
EMPTY_LONGS
private static final long[] EMPTY_LONGS
-
EMPTY
private static final ImmutableBitSet EMPTY
-
FROM_BIT_SET
@Deprecated public static final com.google.common.base.Function<? super java.util.BitSet,ImmutableBitSet> FROM_BIT_SET
Deprecated.
-
words
private final long[] words
-
-
Method Detail
-
of
public static ImmutableBitSet of()
Creates an ImmutableBitSet with no bits.
-
of
public static ImmutableBitSet of(int... bits)
-
of
public static ImmutableBitSet of(java.lang.Iterable<java.lang.Integer> bits)
-
of
public static ImmutableBitSet of(ImmutableIntList bits)
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
public static ImmutableBitSet valueOf(long... longs)
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
public static ImmutableBitSet valueOf(java.nio.LongBuffer longs)
Returns a new immutable bit set containing all the bits in the given long buffer.
-
fromBitSet
public static ImmutableBitSet fromBitSet(java.util.BitSet input)
Returns a new immutable bit set containing all the bits in the givenBitSet
.
-
range
public static ImmutableBitSet range(int fromIndex, int toIndex)
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
public static ImmutableBitSet range(int toIndex)
Creates an ImmutableBitSet with bits between 0 andtoIndex
set.
-
wordIndex
private static int wordIndex(int bitIndex)
Given a bit index, return word index containing it.
-
powerSet
public java.lang.Iterable<ImmutableBitSet> 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:
java.lang.IndexOutOfBoundsException
- if the specified index is negative
-
get
public ImmutableBitSet get(int fromIndex, int toIndex)
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:
java.lang.IndexOutOfBoundsException
- iffromIndex
is negative, ortoIndex
is negative, orfromIndex
is larger thantoIndex
-
checkRange
private static void checkRange(int fromIndex, int toIndex)
Checks that fromIndex ... toIndex is a valid range of bit indices.
-
toString
public java.lang.String 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}
".- Overrides:
toString
in classjava.lang.Object
- Returns:
- a string representation of this bit set
-
intersects
public boolean intersects(ImmutableBitSet set)
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:
size()
-
countBits
private static int countBits(long[] words)
-
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()
.- Overrides:
hashCode
in classjava.lang.Object
- Returns:
- the hash code value for this bit set
-
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:
cardinality()
-
equals
public boolean equals(java.lang.Object obj)
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.- Overrides:
equals
in classjava.lang.Object
- Parameters:
obj
- the object to compare with- Returns:
true
if the objects are the same;false
otherwise- See Also:
size()
-
compareTo
public int compareTo(@Nonnull ImmutableBitSet o)
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 interfacejava.lang.Comparable<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:
java.lang.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:
java.lang.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:
java.lang.IndexOutOfBoundsException
- if the specified index is less than-1
-
iterator
public java.util.Iterator<java.lang.Integer> iterator()
- Specified by:
iterator
in interfacejava.lang.Iterable<java.lang.Integer>
-
toList
public java.util.List<java.lang.Integer> toList()
Converts this bit set to a list.
-
asList
public java.util.List<java.lang.Integer> 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
public java.util.Set<java.lang.Integer> 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
public ImmutableBitSet union(java.util.BitSet other)
Returns the union of this immutable bit set with aBitSet
.
-
union
public ImmutableBitSet union(ImmutableBitSet other)
Returns the union of this bit set with another.
-
union
public static ImmutableBitSet union(java.lang.Iterable<? extends ImmutableBitSet> sets)
Returns the union of a number of bit sets.
-
except
public ImmutableBitSet except(ImmutableBitSet that)
Returns a bit set with all the bits in this set that are not in another.- See Also:
BitSet.andNot(java.util.BitSet)
-
intersect
public ImmutableBitSet intersect(ImmutableBitSet that)
Returns a bit set with all the bits set in both this set and in another.- See Also:
BitSet.and(java.util.BitSet)
-
contains
public boolean contains(ImmutableBitSet set1)
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 java.util.SortedMap<java.lang.Integer,ImmutableBitSet> closure(java.util.SortedMap<java.lang.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
public static ImmutableBitSet.Builder builder()
Creates an empty Builder.
-
builder
@Deprecated public static ImmutableBitSet.Builder builder(ImmutableBitSet bitSet)
Deprecated.
-
rebuild
public ImmutableBitSet.Builder 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:
java.lang.IndexOutOfBoundsException
- if n is less than 0 or greater than the number of bits set
-
set
public ImmutableBitSet set(int i)
Returns a bit set the same as this but with a given bit set.
-
set
public ImmutableBitSet 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
public ImmutableBitSet setIf(int bit, boolean condition)
Returns a bit set the same as this but with a given bit set if condition is true.
-
clear
public ImmutableBitSet clear(int i)
Returns a bit set the same as this but with a given bit cleared.
-
clearIf
public ImmutableBitSet clearIf(int i, boolean condition)
Returns a bit set the same as this but with a given bit cleared if condition is true.
-
toBitSet
public java.util.BitSet toBitSet()
Returns aBitSet
that has the same contents as thisImmutableBitSet
.
-
permute
public ImmutableBitSet permute(java.util.Map<java.lang.Integer,java.lang.Integer> map)
Permutes a bit set according to a given mapping.
-
permute
public static java.lang.Iterable<ImmutableBitSet> permute(java.lang.Iterable<ImmutableBitSet> bitSets, java.util.Map<java.lang.Integer,java.lang.Integer> map)
Permutes a collection of bit sets according to a given mapping.
-
shift
public ImmutableBitSet shift(int offset)
Returns a bit set with every bit moved upoffset
positions. Offset may be negative, but throws if any bit ends up negative.
-
-