Class BitSet
Fast integer set implementation based on JDK BitSet class
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
add
(int bitIndex) Sets the bit at the specified index totrue
.void
add
(int fromIndex, int toIndex) Sets the bits from the specifiedfromIndex
(inclusive) to the specifiedtoIndex
(exclusive) totrue
.boolean
boolean
addAll
(Collection<? extends Integer> c) void
Performs a logical AND of this target bit set with the argument bit set.void
Clears all of the bits in thisBitSet
whose corresponding bit is set in the specifiedBitSet
.void
checkSameSize
(BitSet set) void
clear()
Sets all of the bits in this BitSet tofalse
.clone()
Cloning thisBitSet
produces a newBitSet
that is equal to it.boolean
boolean
containsAll
(Collection<?> c) static BitSet
difference
(BitSet a, BitSet b) Returns the difference between two sets as a new set.boolean
Compares this object against the specified object.void
flip
(int bitIndex) Sets the bit at the specified index to the complement of its current value.void
flip
(int fromIndex, int toIndex) Sets each bit from the specifiedfromIndex
(inclusive) to the specifiedtoIndex
(exclusive) to the complement of its current value.boolean
get
(int bitIndex) Returns the value of the bit with the specified index.int
BitSet can contain elements in range [0, capacity).int
hashCode()
Returns the hash code value for this bit set.static BitSet
intersection
(BitSet a, BitSet b) Returns the intersection between two sets as a new setboolean
intersects
(BitSet set) Returns true if the specifiedBitSet
has any bits set totrue
that are also set totrue
in thisBitSet
.boolean
isEmpty()
Returns true if thisBitSet
contains no bits that are set totrue
.iterator()
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.static BitSet
Returns a new BitSet containing all elements in range [0, n] except those present in the given set.static BitSet
of
(int maxSize, int... numbers) Initialize a new bitset with the given numbersvoid
Performs a logical OR of this bit set with the bit set argument.int
previousClearBit
(int fromIndex) Returns the index of the nearest bit that is set tofalse
that occurs on or before the specified starting index.int
previousSetBit
(int fromIndex) Returns the index of the nearest bit that is set totrue
that occurs on or before the specified starting index.boolean
remove
(int bitIndex) Sets the bit specified by the index tofalse
.void
remove
(int fromIndex, int toIndex) Sets the bits from the specifiedfromIndex
(inclusive) to the specifiedtoIndex
(exclusive) tofalse
.boolean
boolean
removeAll
(Collection<?> c) boolean
retainAll
(Collection<?> c) int
size()
Returns the number of bits set totrue
in thisBitSet
.stream()
Returns a stream of indices for which thisBitSet
contains a bit in the set state.static BitSet
symmetricDifference
(BitSet a, BitSet b) Returns the symmetric difference between two sets as a new settoString()
Returns a string representation of this bit set.static BitSet
Returns the union between two sets as a new setvoid
Performs a logical XOR of this bit set with the bit set argument.Methods inherited from class java.util.AbstractCollection
toArray, toArray
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Collection
parallelStream, removeIf, toArray
-
Constructor Details
-
BitSet
public BitSet(int nElements) Creates a bit set whose initial size is large enough to explicitly represent bits with indices in the range0
throughnbits-1
. All bits are initiallyfalse
.- Parameters:
nElements
- the initial size of the bit set. Will be rounded to the nearest multiple of 64.- Throws:
NegativeArraySizeException
- if the specified initial size is negative
-
BitSet
-
BitSet
-
-
Method Details
-
of
Initialize a new bitset with the given numbers- Parameters:
maxSize
- biggest number that may be stored in setnumbers
- numbers initially stored in set- Returns:
- new set
-
intersection
Returns the intersection between two sets as a new set- Parameters:
a
- First setb
- Second set- Returns:
- New set, original sets are not modified
-
not
Returns a new BitSet containing all elements in range [0, n] except those present in the given set.- Parameters:
a
- set- Returns:
- New set, original set is not modified
-
union
Returns the union between two sets as a new set- Parameters:
a
- First setb
- Second set- Returns:
- New set, original sets are not modified
-
difference
Returns the difference between two sets as a new set.- Parameters:
a
- First setb
- Second set- Returns:
- New set, with all elements in set A removing those that are in set B
-
symmetricDifference
Returns the symmetric difference between two sets as a new set- Parameters:
a
- First setb
- Second set- Returns:
- New set, original sets are not modified
-
getCapacity
public int getCapacity()BitSet can contain elements in range [0, capacity). Get the upper bound.- Returns:
- capacity, as initialized in constructor method
-
flip
public void flip(int bitIndex) Sets the bit at the specified index to the complement of its current value.- Parameters:
bitIndex
- the index of the bit to flip- Throws:
IndexOutOfBoundsException
- if the specified index is negative- Since:
- 1.4
-
flip
public void flip(int fromIndex, int toIndex) Sets each bit from the specifiedfromIndex
(inclusive) to the specifiedtoIndex
(exclusive) to the complement of its current value.- Parameters:
fromIndex
- index of the first bit to fliptoIndex
- index after the last bit to flip- Throws:
IndexOutOfBoundsException
- iffromIndex
is negative, ortoIndex
is negative, orfromIndex
is larger thantoIndex
- Since:
- 1.4
-
add
public boolean add(int bitIndex) Sets the bit at the specified index totrue
.- Parameters:
bitIndex
- a bit index- Returns:
- true if an element was added, false if the set was not modified
- Throws:
IndexOutOfBoundsException
- if the specified index is negative
-
addAll
- Specified by:
addAll
in interfaceCollection<Integer>
- Specified by:
addAll
in interfaceSet<Integer>
- Overrides:
addAll
in classAbstractCollection<Integer>
-
add
public void add(int fromIndex, int toIndex) Sets the bits from the specifiedfromIndex
(inclusive) to the specifiedtoIndex
(exclusive) totrue
.- Parameters:
fromIndex
- index of the first bit to be set, inclusivetoIndex
- index after the last bit to be set, exclusive- Throws:
IndexOutOfBoundsException
- iffromIndex
is negative, ortoIndex
is negative, orfromIndex
is larger thantoIndex
- Since:
- 1.4
-
remove
public boolean remove(int bitIndex) Sets the bit specified by the index tofalse
.- Parameters:
bitIndex
- the index of the bit to be cleared- Returns:
- true if an element was removed, false if the set was not modified
- Throws:
IndexOutOfBoundsException
- if the specified index is negative
-
remove
public void remove(int fromIndex, int toIndex) Sets the bits from the specifiedfromIndex
(inclusive) to the specifiedtoIndex
(exclusive) tofalse
.- Parameters:
fromIndex
- index of the first bit to be clearedtoIndex
- index after the last bit to be cleared- Throws:
IndexOutOfBoundsException
- iffromIndex
is negative, ortoIndex
is negative, orfromIndex
is larger thantoIndex
- Since:
- 1.4
-
clear
public void clear()Sets all of the bits in this BitSet tofalse
.- Specified by:
clear
in interfaceCollection<Integer>
- Specified by:
clear
in interfaceSet<Integer>
- Overrides:
clear
in classAbstractCollection<Integer>
- Since:
- 1.4
-
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 thisBitSet
; 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
-
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.To iterate over the
true
bits in aBitSet
, use the following loop:for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) { // operate on index i here if (i == Integer.MAX_VALUE) { break; // or (i+1) would overflow } }
- 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- Since:
- 1.4
-
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. If no such bit exists then-1
is returned.- 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- Since:
- 1.4
-
previousSetBit
public int previousSetBit(int fromIndex) Returns the index of the nearest bit that is set totrue
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.To iterate over the
true
bits in aBitSet
, use the following loop:for (int i = bs.length(); (i = bs.previousSetBit(i-1)) >= 0; ) { // operate on index i here }
- Parameters:
fromIndex
- the index to start checking from (inclusive)- Returns:
- the index of the previous set bit, or
-1
if there is no such bit - Throws:
IndexOutOfBoundsException
- if the specified index is less than-1
- Since:
- 1.7
-
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
- Since:
- 1.7
-
isEmpty
public boolean isEmpty()Returns true if thisBitSet
contains no bits that are set totrue
.- Specified by:
isEmpty
in interfaceCollection<Integer>
- Specified by:
isEmpty
in interfaceSet<Integer>
- Overrides:
isEmpty
in classAbstractCollection<Integer>
- Returns:
- boolean indicating whether this
BitSet
is empty - Since:
- 1.4
-
intersects
Returns true if the specifiedBitSet
has any bits set totrue
that are also set totrue
in thisBitSet
.- Parameters:
set
-BitSet
to intersect with- Returns:
- boolean indicating whether this
BitSet
intersects the specifiedBitSet
- Since:
- 1.4
-
size
public int size()Returns the number of bits set totrue
in thisBitSet
.- Specified by:
size
in interfaceCollection<Integer>
- Specified by:
size
in interfaceSet<Integer>
- Specified by:
size
in classAbstractCollection<Integer>
- Returns:
- the number of bits set to
true
in thisBitSet
- Since:
- 1.4
-
and
Performs a logical AND of this target bit set with the argument bit set. This bit set is modified so that each bit in it has the valuetrue
if and only if it both initially had the valuetrue
and the corresponding bit in the bit set argument also had the valuetrue
.- Parameters:
set
- a bit set
-
or
Performs a logical OR of this bit set with the bit set argument. This bit set is modified so that a bit in it has the valuetrue
if and only if it either already had the valuetrue
or the corresponding bit in the bit set argument has the valuetrue
.- Parameters:
set
- a bit set
-
xor
Performs a logical XOR of this bit set with the bit set argument. This bit set is modified so that a bit in it has the valuetrue
if and only if one of the following statements holds:- The bit initially has the value
true
, and the corresponding bit in the argument has the valuefalse
. - The bit initially has the value
false
, and the corresponding bit in the argument has the valuetrue
.
- Parameters:
set
- a bit set
- The bit initially has the value
-
andNot
Clears all of the bits in thisBitSet
whose corresponding bit is set in the specifiedBitSet
.- Parameters:
set
- theBitSet
with which to mask thisBitSet
- Since:
- 1.2
-
checkSameSize
-
hashCode
public int hashCode()Returns the hash code value for this bit set. The hash code depends only on which bits are set within thisBitSet
.The hash code is defined to be the result of the following calculation:
public int hashCode() { long h = 1234; long[] words = toLongArray(); for (int i = words.length; --i >= 0; ) h ^= words[i] * (i + 1); return (int)((h >> 32) ^ h); }
- Specified by:
hashCode
in interfaceCollection<Integer>
- Specified by:
hashCode
in interfaceSet<Integer>
- Overrides:
hashCode
in classAbstractSet<Integer>
- Returns:
- the hash code value for this bit set
-
iterator
-
equals
Compares this object against the specified object. The result istrue
if and only if the argument is notnull
and is aBitSet
object that has exactly the same set of bits set totrue
as this bit set. That is, for every nonnegativeint
indexk
,((BitSet)obj).get(k) == this.get(k)
must be true. IMPORTANT: The current sizes of the two bit sets ARE COMPARED FIRST.- Specified by:
equals
in interfaceCollection<Integer>
- Specified by:
equals
in interfaceSet<Integer>
- Overrides:
equals
in classAbstractSet<Integer>
- Parameters:
obj
- the object to compare with- Returns:
true
if the objects are the same;false
otherwise- See Also:
-
clone
Cloning thisBitSet
produces a newBitSet
that is equal to it. The clone of the bit set is another bit set that has exactly the same bits set totrue
as this bit set. -
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 classAbstractCollection<Integer>
- Returns:
- a string representation of this bit set
-
add
- Specified by:
add
in interfaceCollection<Integer>
- Specified by:
add
in interfaceSet<Integer>
- Overrides:
add
in classAbstractCollection<Integer>
-
remove
- Specified by:
remove
in interfaceCollection<Integer>
- Specified by:
remove
in interfaceSet<Integer>
- Overrides:
remove
in classAbstractCollection<Integer>
-
contains
- Specified by:
contains
in interfaceCollection<Integer>
- Specified by:
contains
in interfaceSet<Integer>
- Overrides:
contains
in classAbstractCollection<Integer>
-
spliterator
-
removeAll
- Specified by:
removeAll
in interfaceCollection<Integer>
- Specified by:
removeAll
in interfaceSet<Integer>
- Overrides:
removeAll
in classAbstractSet<Integer>
-
containsAll
- Specified by:
containsAll
in interfaceCollection<Integer>
- Specified by:
containsAll
in interfaceSet<Integer>
- Overrides:
containsAll
in classAbstractCollection<Integer>
-
retainAll
- Specified by:
retainAll
in interfaceCollection<Integer>
- Specified by:
retainAll
in interfaceSet<Integer>
- Overrides:
retainAll
in classAbstractCollection<Integer>
-
stream
Returns a stream of indices for which thisBitSet
contains 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 thesize()
()} method.The stream binds to this bit set when the terminal stream operation commences (specifically, the spliterator for the stream is late-binding). If the bit set is modified during that operation then the result is undefined.
- Returns:
- a stream of integers representing set indices
- Since:
- 1.8
-