public class BitSet extends java.lang.Object implements java.lang.Cloneable, java.lang.Iterable<java.lang.Integer>, java.io.Externalizable, WordArray
This is an optimized version of Java's BitSet. In many cases, it can be used as a drop-in replacement.
It differs from the basic Java BitSet class in the following ways:
for(int bs: myBitset)
.Modifier and Type | Field and Description |
---|---|
(package private) long[] |
data |
(package private) static long |
serialVersionUID |
Constructor and Description |
---|
BitSet() |
BitSet(int sizeInBits)
Construct a bitset with the specified number of bits (initially all
false).
|
Modifier and Type | Method and Description |
---|---|
void |
and(WordArray bs)
Compute bitwise AND.
|
int |
andcardinality(WordArray bs)
Compute cardinality of bitwise AND.
|
void |
andNot(WordArray bs)
Compute bitwise AND NOT.
|
int |
andNotcardinality(WordArray bs)
Compute cardinality of bitwise AND NOT.
|
static BitSet |
bitmapOf(int... setBits)
Return a bitmap with the bit set to true at the given positions.
|
int |
cardinality()
Compute the number of bits set to 1
|
void |
clear()
Reset all bits to false.
|
void |
clear(int index)
Set the bit to false.
|
void |
clear(int start,
int end)
Set the bits in the range of indexes to false.
|
BitSet |
clone() |
void |
deserialize(java.io.DataInput in)
Deserialize.
|
boolean |
empty()
Check whether a bitset contains a set bit.
|
boolean |
equals(java.lang.Object o) |
void |
flip(int i)
Flip the bit.
|
void |
flip(int start,
int end)
Flip the bits in the range of indexes.
|
boolean |
get(int i)
Get the value of the bit.
|
int |
getNumberOfWords()
Get the total number of words contained in this data structure.
|
long |
getWord(int index)
Get the word at the given index
|
int |
hashCode() |
boolean |
intersects(WordArray bs)
Checks whether two bitsets intersect.
|
IntIterator |
intIterator()
Iterate over the set bits
|
java.util.Iterator<java.lang.Integer> |
iterator() |
int |
nextSetBit(int i)
Usage: for(int i=bs.nextSetBit(0); i>=0; i=bs.nextSetBit(i+1)) {
operate on index i here }
|
int |
nextUnsetBit(int i)
Usage: for(int i=bs.nextUnsetBit(0); i>=0; i=bs.nextUnsetBit(i+1))
{ operate on index i here }
|
void |
or(WordArray bs)
Compute bitwise OR.
|
int |
orcardinality(WordArray bs)
Compute cardinality of bitwise OR.
|
void |
readExternal(java.io.ObjectInput in) |
void |
resize(int sizeInBits)
Resize the bitset
|
void |
serialize(java.io.DataOutput out)
Serialize.
|
void |
set(int i)
Set to true.
|
void |
set(int i,
boolean b)
Set to some value.
|
void |
set(int start,
int end)
Set the bits in the range of indexes true.
|
void |
set(int start,
int end,
boolean v)
Set the bits in the range of indexes to the specified Boolean value.
|
int |
size()
Query the size
|
java.lang.String |
toString() |
void |
trim()
Recovers wasted memory
|
void |
unset(int i)
Set to false
|
IntIterator |
unsetIntIterator()
Iterate over the unset bits
|
void |
writeExternal(java.io.ObjectOutput out) |
void |
xor(WordArray bs)
Compute bitwise XOR.
|
int |
xorcardinality(WordArray bs)
Compute cardinality of bitwise XOR.
|
long[] data
static final long serialVersionUID
public BitSet(int sizeInBits)
sizeInBits
- the size in bitspublic BitSet()
public void and(WordArray bs)
bs
- other bitsetpublic int andcardinality(WordArray bs)
bs
- other bitsetpublic void andNot(WordArray bs)
bs
- other bitsetpublic int andNotcardinality(WordArray bs)
bs
- other bitsetpublic int cardinality()
public void clear()
public void clear(int index)
unset(int)
index
- location of the bitpublic void clear(int start, int end)
start
- location of the first bit to set to zeroend
- location of the last bit to set to zero (not included)public BitSet clone()
clone
in class java.lang.Object
public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public boolean empty()
public void flip(int i)
i
- index of the bitpublic void flip(int start, int end)
start
- location of the first bitend
- location of the last bit (not included)public boolean get(int i)
i
- indexpublic int hashCode()
hashCode
in class java.lang.Object
public IntIterator intIterator()
public java.util.Iterator<java.lang.Integer> iterator()
iterator
in interface java.lang.Iterable<java.lang.Integer>
public boolean intersects(WordArray bs)
bs
- other bitsetpublic int nextSetBit(int i)
i
- current set bitpublic int nextUnsetBit(int i)
i
- current unset bitpublic void or(WordArray bs)
bs
- other bitsetpublic int orcardinality(WordArray bs)
bs
- other bitsetpublic void resize(int sizeInBits)
sizeInBits
- new number of bitspublic void set(int i)
i
- index of the bitpublic void set(int i, boolean b)
i
- indexb
- value of the bitpublic void set(int start, int end)
start
- location of the first bitend
- location of the last bit (not included)public void set(int start, int end, boolean v)
start
- location of the first bitend
- location of the last bit (not included)v
- Boolean valuepublic int size()
public void trim()
public void unset(int i)
i
- index of the bitpublic IntIterator unsetIntIterator()
public void writeExternal(java.io.ObjectOutput out) throws java.io.IOException
writeExternal
in interface java.io.Externalizable
java.io.IOException
public void readExternal(java.io.ObjectInput in) throws java.io.IOException, java.lang.ClassNotFoundException
readExternal
in interface java.io.Externalizable
java.io.IOException
java.lang.ClassNotFoundException
public void serialize(java.io.DataOutput out) throws java.io.IOException
out
- the DataOutput streamjava.io.IOException
- Signals that an I/O exception has occurred.public void deserialize(java.io.DataInput in) throws java.io.IOException
in
- the DataInput streamjava.io.IOException
- Signals that an I/O exception has occurred.public void xor(WordArray bs)
bs
- other bitsetpublic int xorcardinality(WordArray bs)
bs
- other bitsetpublic int getNumberOfWords()
WordArray
getNumberOfWords
in interface WordArray
public long getWord(int index)
WordArray
public static BitSet bitmapOf(int... setBits)
setBits
- list of set bit positionspublic java.lang.String toString()
toString
in class java.lang.Object