public class BitSet
extends java.lang.Object
implements java.lang.Cloneable
Unlike java.util.bitset, the fact that bits are packed into an array of longs is part of the interface. This allows efficient implementation of other algorithms by someone other than the author. It also allows one to efficiently implement alternate serialization or interchange formats.
The index range for a bitset can easily exceed positive int
range in Java
(0x7fffffff), so many methods in this class accept or return a long
. There
are adapter methods that return views compatible with
LongLookupContainer
and IntLookupContainer
interfaces.
asIntLookupContainer()
,
asLongLookupContainer()
Modifier and Type | Field and Description |
---|---|
long[] |
bits
Internal representation of bits in this bit set.
|
int |
wlen
The number of words (longs) used in the
bits array. |
Constructor and Description |
---|
BitSet()
Constructs a bit set with the default capacity.
|
BitSet(long numBits)
Constructs an BitSet large enough to hold numBits.
|
BitSet(long[] bits,
int numWords)
Constructs an BitSet from an existing long[].
|
Modifier and Type | Method and Description |
---|---|
void |
and(BitSet other) |
void |
andNot(BitSet other) |
static long |
andNotCount(BitSet a,
BitSet b) |
IntLookupContainer |
asIntLookupContainer()
Returns a view over this bitset data compatible with
IntLookupContainer . |
LongLookupContainer |
asLongLookupContainer()
Returns a view over this bitset data compatible with
LongLookupContainer . |
static int |
bits2words(long numBits) |
long |
capacity() |
long |
cardinality() |
void |
clear()
Clears all bits.
|
void |
clear(int startIndex,
int endIndex)
Clears a range of bits.
|
void |
clear(long index)
clears a bit, allowing access beyond the current set size without changing the
size.
|
void |
clear(long startIndex,
long endIndex)
Clears a range of bits.
|
java.lang.Object |
clone() |
void |
ensureCapacity(long numBits)
Ensure that the long[] is big enough to hold numBits, expanding it if necessary.
|
void |
ensureCapacityWords(int numWords)
Expand the long[] with the size given as a number of words (64 bit longs).
|
boolean |
equals(java.lang.Object o) |
protected int |
expandingWordNum(long index) |
void |
flip(long index)
Flips a bit, expanding the set size if necessary.
|
void |
flip(long startIndex,
long endIndex)
Flips a range of bits, expanding the set size if necessary
|
boolean |
flipAndGet(int index)
flips a bit and returns the resulting bit value.
|
boolean |
flipAndGet(long index)
flips a bit and returns the resulting bit value.
|
boolean |
get(int index) |
boolean |
get(long index) |
boolean |
getAndSet(int index)
Sets a bit and returns the previous value.
|
boolean |
getAndSet(long index)
Sets a bit and returns the previous value.
|
static int |
getNextSize(int targetSize) |
static long[] |
grow(long[] array,
int minSize) |
int |
hashCode() |
void |
intersect(BitSet other)
this = this AND other
|
static long |
intersectionCount(BitSet a,
BitSet b) |
boolean |
intersects(BitSet other) |
boolean |
isEmpty() |
BitSetIterator |
iterator() |
long |
length() |
static BitSet |
newInstance()
Static constructor-like method similar to other (generic) collections.
|
int |
nextSetBit(int index) |
long |
nextSetBit(long index) |
void |
or(BitSet other) |
void |
remove(BitSet other)
Remove all elements set in other: this = this AND_NOT other
|
void |
set(long index)
Sets a bit, expanding the set size if necessary.
|
void |
set(long startIndex,
long endIndex)
Sets a range of bits, expanding the set size if necessary
|
long |
size() |
java.lang.String |
toString() |
void |
trimTrailingZeros()
Lowers
wlen , the number of words in use, by checking for trailing zero
words. |
void |
union(BitSet other)
this = this OR other
|
static long |
unionCount(BitSet a,
BitSet b) |
void |
xor(BitSet other)
this = this XOR other
|
static long |
xorCount(BitSet a,
BitSet b) |
public long[] bits
public int wlen
bits
array.public BitSet()
public BitSet(long numBits)
numBits
- Number of bitspublic BitSet(long[] bits, int numWords)
bits
- underlying bits buffernumWords
- the number of elements in the array that contain set bitspublic static BitSet newInstance()
public BitSetIterator iterator()
nextSetBit(int)
.public long capacity()
public long size()
cardinality()
.cardinality()
,
BitSet.size()
public long length()
BitSet
: the index of
the highest set bit in the BitSet
plus one.BitSet.length()
public boolean isEmpty()
public boolean get(int index)
index
- The index.public boolean get(long index)
index
- The index.public void set(long index)
index
- the index to setpublic void set(long startIndex, long endIndex)
startIndex
- lower indexendIndex
- one-past the last bit to setprotected int expandingWordNum(long index)
public void clear()
public void clear(long index)
index
- the index to clearpublic void clear(int startIndex, int endIndex)
startIndex
- lower indexendIndex
- one-past the last bit to clearpublic void clear(long startIndex, long endIndex)
startIndex
- lower indexendIndex
- one-past the last bit to clearpublic boolean getAndSet(int index)
index
- the index to setpublic boolean getAndSet(long index)
index
- the index to setpublic void flip(long index)
index
- the index to flippublic boolean flipAndGet(int index)
index
- the index to flippublic boolean flipAndGet(long index)
index
- the index to flippublic void flip(long startIndex, long endIndex)
startIndex
- lower indexendIndex
- one-past the last bit to flippublic long cardinality()
public static long intersectionCount(BitSet a, BitSet b)
a
- The first setb
- The second setpublic static long unionCount(BitSet a, BitSet b)
a
- The first setb
- The second setpublic static long andNotCount(BitSet a, BitSet b)
a
- The first setb
- The second setpublic static long xorCount(BitSet a, BitSet b)
a
- The first setb
- The second setpublic int nextSetBit(int index)
index
- The index to start scanning from, inclusive.public long nextSetBit(long index)
index
- The index to start scanning from, inclusive.public java.lang.Object clone()
clone
in class java.lang.Object
public void intersect(BitSet other)
other
- The bitset to intersect with.public void union(BitSet other)
other
- The bitset to union with.public void remove(BitSet other)
other
- The other bitset.public void xor(BitSet other)
other
- The other bitset.public void and(BitSet other)
public void or(BitSet other)
public void andNot(BitSet other)
public boolean intersects(BitSet other)
other
- The other bitset.public void ensureCapacityWords(int numWords)
numWords
- The size to expand to (64-bit long words)public static long[] grow(long[] array, int minSize)
public static int getNextSize(int targetSize)
public void ensureCapacity(long numBits)
numBits
- The number of bits to expand topublic void trimTrailingZeros()
wlen
, the number of words in use, by checking for trailing zero
words.public static int bits2words(long numBits)
public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object
public IntLookupContainer asIntLookupContainer()
IntLookupContainer
. A new
object is always returned, but its methods reflect the current state of the bitset
(the view is not a snapshot).
Methods of the returned IntLookupContainer
may throw a RuntimeException
if the cardinality of this bitset exceeds the int range.
IntLookupContainer
.public LongLookupContainer asLongLookupContainer()
LongLookupContainer
. A new
object is always returned, but its methods reflect the current state of the bitset
(the view is not a snapshot).LongLookupContainer
.Copyright © 2015 Carrot Search s.c.. All Rights Reserved.