Package pascal.taie.util.collection
Class SparseBitSet
java.lang.Object
pascal.taie.util.collection.AbstractBitSet
pascal.taie.util.collection.SparseBitSet
- All Implemented Interfaces:
Serializable
,IBitSet
,Copyable<IBitSet>
Sparse bit set. This implementation groups bits into blocks, and it could
avoid allocating words to represent continuous zero bits when possible.
This design saves memory and improves efficiency of set iterations.
This implementation uses core design and some code from https://github.com/brettwooldridge/SparseBitSet We rewrite most code to support the operations that we need and improve the readability.
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface pascal.taie.util.collection.IBitSet
IBitSet.Action<R>
-
Field Summary
Fields inherited from class pascal.taie.util.collection.AbstractBitSet
ADDRESS_BITS_PER_WORD, BITS_PER_WORD
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
Performs a logical AND of this target bit set with the argument bit set.boolean
Clears all of the bits in thisBitSet
whose corresponding bit is set in the specifiedBitSet
.int
Returns the number of bits set totrue
in thisBitSet
.void
clear()
Sets all of the bits in this BitSet tofalse
.boolean
clear
(int bitIndex) Sets the bit specified by the index tofalse
.boolean
Returnstrue
if thisBitSet
contains all set bits in the specifiedBitSet
.copy()
Creates and returns a copy of this object.boolean
void
flip
(int bitIndex) Sets the bit at the specified index to the complement of its current value.boolean
get
(int bitIndex) Returns the value of the bit with the specified index.int
hashCode()
boolean
intersects
(IBitSet set) Returnstrue
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
.int
length()
Returns the "logical size" of thisBitSet
: the index of the highest set bit in theBitSet
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.boolean
Performs a logical OR of this bit set with the bit set argument.Performs a logical OR of this bit set with the bit set argument, computes and returns the difference set between given bit set and this set (before performing logical OR).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
set
(int bitIndex) Sets the bit at the specified index totrue
.int
size()
Returns the number of bits of space actually in use by thisBitSet
to represent bit values.boolean
Performs a logical XOR of this bit set with the bit set argument.Methods inherited from class pascal.taie.util.collection.AbstractBitSet
disjoints, set, setTo, toString, wordIndex
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface pascal.taie.util.collection.IBitSet
iterateBits
-
Constructor Details
-
SparseBitSet
public SparseBitSet() -
SparseBitSet
public SparseBitSet(int nbits)
-
-
Method Details
-
set
public boolean set(int bitIndex) Description copied from interface:IBitSet
Sets the bit at the specified index totrue
. -
clear
public boolean clear(int bitIndex) Description copied from interface:IBitSet
Sets the bit specified by the index tofalse
. -
get
public boolean get(int bitIndex) Description copied from interface:IBitSet
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
. -
flip
public void flip(int bitIndex) Description copied from interface:IBitSet
Sets the bit at the specified index to the complement of its current value. This operation must modify the BitSet. -
nextSetBit
public int nextSetBit(int fromIndex) Description copied from interface:IBitSet
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 } }
- Specified by:
nextSetBit
in interfaceIBitSet
- 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
-
nextClearBit
public int nextClearBit(int fromIndex) Description copied from interface:IBitSet
Returns the index of the first bit that is set tofalse
that occurs on or after the specified starting index.- Specified by:
nextClearBit
in interfaceIBitSet
- Parameters:
fromIndex
- the index to start checking from (inclusive)- Returns:
- the index of the next clear bit
-
previousSetBit
public int previousSetBit(int fromIndex) Description copied from interface:IBitSet
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 }
- Specified by:
previousSetBit
in interfaceIBitSet
- 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
-
previousClearBit
public int previousClearBit(int fromIndex) Description copied from interface:IBitSet
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.- Specified by:
previousClearBit
in interfaceIBitSet
- 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
-
intersects
Description copied from interface:IBitSet
Returnstrue
if the specifiedBitSet
has any bits set totrue
that are also set totrue
in thisBitSet
.- Specified by:
intersects
in interfaceIBitSet
- Overrides:
intersects
in classAbstractBitSet
- Parameters:
set
-BitSet
to intersect with- Returns:
- boolean indicating whether this
BitSet
intersects the specifiedBitSet
-
contains
Description copied from interface:IBitSet
Returnstrue
if thisBitSet
contains all set bits in the specifiedBitSet
.- Specified by:
contains
in interfaceIBitSet
- Overrides:
contains
in classAbstractBitSet
- Parameters:
set
- bit set to be checked for containment in this set.- Returns:
- boolean indicating whether this
BitSet
contains all set bits in the specifiedBitSet
-
and
Performs a logical AND of this target bit set with the argument bit set. This operation cannot skip zero blocks in the other set, thus we cannot implement it viaiterateBlocks(pascal.taie.util.collection.SparseBitSet, pascal.taie.util.collection.SparseBitSet, pascal.taie.util.collection.SparseBitSet.BlockAction<R>)
. -
andNot
Description copied from interface:IBitSet
Clears all of the bits in thisBitSet
whose corresponding bit is set in the specifiedBitSet
.- Specified by:
andNot
in interfaceIBitSet
- Overrides:
andNot
in classAbstractBitSet
- Parameters:
set
- theBitSet
with which to mask thisBitSet
- Returns:
true
if this bit set changed as a result of the call
-
or
Description copied from interface:IBitSet
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
.- Specified by:
or
in interfaceIBitSet
- Overrides:
or
in classAbstractBitSet
- Parameters:
set
- a bit set- Returns:
true
if this bit set changed as a result of the call
-
orDiff
Description copied from interface:IBitSet
Performs a logical OR of this bit set with the bit set argument, computes and returns the difference set between given bit set and this set (before performing logical OR).- Specified by:
orDiff
in interfaceIBitSet
- Overrides:
orDiff
in classAbstractBitSet
- Parameters:
set
- a bit set.- Returns:
- a new bit set of bit values that are present in the bit set argument and were absent in this bit set before.
-
xor
Description copied from interface:IBitSet
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
.
- Specified by:
xor
in interfaceIBitSet
- Overrides:
xor
in classAbstractBitSet
- Parameters:
set
- a bit set- Returns:
true
if this bit set changed as a result of the call
- The bit initially has the value
-
clear
public void clear()Description copied from interface:IBitSet
Sets all of the bits in this BitSet tofalse
. -
isEmpty
public boolean isEmpty()Description copied from interface:IBitSet
Returns true if thisBitSet
contains no bits that are set totrue
. -
length
public int length()Description copied from interface:IBitSet
Returns the "logical size" of thisBitSet
: the index of the highest set bit in theBitSet
plus one. Returns zero if theBitSet
contains no set bits. -
size
public int size()Description copied from interface:IBitSet
Returns the number of bits of space actually in use by thisBitSet
to represent bit values. The maximum element in the set is the size - 1st element. -
cardinality
public int cardinality()Description copied from interface:IBitSet
Returns the number of bits set totrue
in thisBitSet
.- Specified by:
cardinality
in interfaceIBitSet
- Returns:
- the number of bits set to
true
in thisBitSet
-
hashCode
public int hashCode() -
equals
-
copy
Description copied from interface:Copyable
Creates and returns a copy of this object.
-