public class FastBitSet extends FastCollection<Index> implements java.util.Set<Index>, Reusable
This class represents either a table of bits or a set of non-negative numbers.
This class is integrated with the collection framework (as
a set of indices
and obeys the collection semantic
for methods such as size()
(cardinality) or equals(java.lang.Object)
(same set of indices).
FastCollection.Record
Constructor and Description |
---|
FastBitSet()
Creates a bit set of small initial capacity.
|
FastBitSet(int bitSize)
Creates a bit set of specified initial capacity (in bits).
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(Index index)
Adds the specified index to this set.
|
void |
and(FastBitSet that)
Performs the logical AND operation on this bit set and the
given bit set.
|
void |
andNot(FastBitSet that)
Performs the logical AND operation on this bit set and the
complement of the given bit set.
|
int |
cardinality()
Returns the number of bits set to
true (or the size of this
set). |
void |
clear()
Sets all bits in the set to
false (empty the set). |
void |
clear(int bitIndex)
Removes the specified integer value from this set.
|
void |
clear(int fromIndex,
int toIndex)
Sets the bits from the specified
fromIndex (inclusive) to the
specified toIndex (exclusive) to false . |
void |
delete(FastCollection.Record record)
Deletes the specified record from this collection.
|
boolean |
equals(java.lang.Object obj)
Compares the specified object with this collection for equality.
|
void |
flip(int bitIndex)
Sets the bit at the index to the opposite value.
|
void |
flip(int fromIndex,
int toIndex)
Sets a range of bits to the opposite value.
|
boolean |
get(int bitIndex)
Returns
true > if the specified integer is in
this bit set; false otherwise. |
FastBitSet |
get(int fromIndex,
int toIndex)
Returns a new bit set composed of a range of bits from this one.
|
int |
hashCode()
Returns the hash code for this collection.
|
FastCollection.Record |
head()
Returns the head record of this collection; it is the record such as
head().getNext() holds the first collection value. |
boolean |
intersects(FastBitSet that)
Returns
true if this bit set shares at least one
common bit with the specified bit set. |
int |
length()
Returns the logical number of bits actually used by this bit
set.
|
static FastBitSet |
newInstance()
Returns a new, preallocated or
recycled set instance
(on the stack when executing in a StackContext ). |
int |
nextClearBit(int fromIndex)
Returns the index of the next
false bit, from the specified bit
(inclusive). |
int |
nextSetBit(int fromIndex)
Returns the index of the next
true bit, from the specified bit
(inclusive). |
void |
or(FastBitSet that)
Performs the logical OR operation on this bit set and the one specified.
|
static void |
recycle(FastBitSet instance)
Recycles a set
instance immediately
(on the stack when executing in a StackContext ). |
void |
reset()
Resets the internal state of this object to its default values.
|
void |
set(int bitIndex)
Adds the specified integer to this set (corresponding bit is set to
true . |
void |
set(int bitIndex,
boolean value)
Sets the bit at the given index to the specified value.
|
void |
set(int fromIndex,
int toIndex)
Sets the bits from the specified
fromIndex (inclusive) to the
specified toIndex (exclusive) to true . |
void |
set(int fromIndex,
int toIndex,
boolean value)
Sets the bits between from (inclusive) and to (exclusive) to the
specified value.
|
int |
size()
Returns the cardinality of this bit set (number of bits set).
|
FastCollection.Record |
tail()
Returns the tail record of this collection; it is the record such as
tail().getPrevious() holds the last collection value. |
Index |
valueOf(FastCollection.Record record)
Returns the collection value for the specified record.
|
void |
xor(FastBitSet that)
Performs the logical XOR operation on this bit set and the one specified.
|
addAll, contains, containsAll, getValueComparator, isEmpty, iterator, remove, removeAll, retainAll, shared, toArray, toArray, toString, toText, unmodifiable
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
public FastBitSet()
false
.public FastBitSet(int bitSize)
false
. This
constructor reserves enough space to represent the integers
from 0
to bitSize-1
.bitSize
- the initial capacity in bits.public static FastBitSet newInstance()
recycled
set instance
(on the stack when executing in a StackContext
).public static void recycle(FastBitSet instance)
instance
immediately
(on the stack when executing in a StackContext
).public boolean add(Index index)
set(index.intValue())
.add
in interface java.util.Collection<Index>
add
in interface java.util.Set<Index>
add
in class FastCollection<Index>
index
- the integer value to be appended to this set.true
if this set did not contains the specified
index; false
otherwise.public void and(FastBitSet that)
that
- the second bit set.public void andNot(FastBitSet that)
that
- the second bit setpublic int cardinality()
true
(or the size of this
set).public void clear()
false
(empty the set).clear
in interface java.util.Collection<Index>
clear
in interface java.util.Set<Index>
clear
in class FastCollection<Index>
public void clear(int bitIndex)
bitIndex
- a non-negative integer.java.lang.IndexOutOfBoundsException
- if index < 0
public void clear(int fromIndex, int toIndex)
fromIndex
(inclusive) to the
specified toIndex
(exclusive) to false
.fromIndex
- index of the first bit to be cleared.toIndex
- index after the last bit to be cleared.java.lang.IndexOutOfBoundsException
- if
(fromIndex < 0) | (toIndex < fromIndex)
public void flip(int bitIndex)
bitIndex
- the index of the bit.java.lang.IndexOutOfBoundsException
- if bitIndex < 0
public void flip(int fromIndex, int toIndex)
fromIndex
- the low index (inclusive).toIndex
- the high index (exclusive).java.lang.IndexOutOfBoundsException
- if
(fromIndex < 0) | (toIndex < fromIndex)
public boolean get(int bitIndex)
true
> if the specified integer is in
this bit set; false
otherwise.bitIndex
- a non-negative integer.java.lang.IndexOutOfBoundsException
- if bitIndex < 0
public FastBitSet get(int fromIndex, int toIndex)
fromIndex
- the low index (inclusive).toIndex
- the high index (exclusive).java.lang.IndexOutOfBoundsException
- if
(fromIndex < 0) | (toIndex < fromIndex)
public boolean intersects(FastBitSet that)
true
if this bit set shares at least one
common bit with the specified bit set.that
- the bit set to check for intersectiontrue
if the sets intersect; false
otherwise.public int length()
Note: This method does not return the number of set bits
which is returned by size()
public int nextClearBit(int fromIndex)
false
bit, from the specified bit
(inclusive).fromIndex
- the start location.false
bit.java.lang.IndexOutOfBoundsException
- if fromIndex < 0
public int nextSetBit(int fromIndex)
true
bit, from the specified bit
(inclusive). If there is none, -1
is returned.
The following code will iterates through the bit set:
for (int i=nextSetBit(0); i >= 0; i = nextSetBit(i)) {
...
}
fromIndex
- the start location.false
bit.java.lang.IndexOutOfBoundsException
- if fromIndex < 0
public void or(FastBitSet that)
that
- the second bit set.public void set(int bitIndex)
true
.bitIndex
- a non-negative integer.java.lang.IndexOutOfBoundsException
- if bitIndex < 0
public void set(int bitIndex, boolean value)
bitIndex
- the position to set.value
- the value to set it to.java.lang.IndexOutOfBoundsException
- if bitIndex < 0
public void set(int fromIndex, int toIndex)
fromIndex
(inclusive) to the
specified toIndex
(exclusive) to true
.fromIndex
- index of the first bit to be set.toIndex
- index after the last bit to be set.java.lang.IndexOutOfBoundsException
- if
(fromIndex < 0) | (toIndex < fromIndex)
public void set(int fromIndex, int toIndex, boolean value)
fromIndex
- the start range (inclusive).toIndex
- the end range (exclusive).value
- the value to set it to.java.lang.IndexOutOfBoundsException
- if bitIndex < 0
public int size()
Note: Unlike java.util.BitSet
this method does not
returns an approximation of the number of bits of space
actually in use. This method is compliant with
java.util.Collection meaning for size().
size
in interface java.util.Collection<Index>
size
in interface java.util.Set<Index>
size
in class FastCollection<Index>
public void xor(FastBitSet that)
that
- the second bit set.public boolean equals(java.lang.Object obj)
FastCollection
List
instances).
Equality comparisons are performed using this collection
value comparator
.equals
in interface java.util.Collection<Index>
equals
in interface java.util.Set<Index>
equals
in class FastCollection<Index>
obj
- the object to be compared for equality with this collectiontrue
if the specified object is a collection with
the same content and iteration order when necessary;
false
otherwise.public int hashCode()
FastCollection
hashCode
in interface java.util.Collection<Index>
hashCode
in interface java.util.Set<Index>
hashCode
in class FastCollection<Index>
public void reset()
Reusable
public FastCollection.Record head()
FastCollection
head().getNext()
holds the first collection value.head
in class FastCollection<Index>
public FastCollection.Record tail()
FastCollection
tail().getPrevious()
holds the last collection value.tail
in class FastCollection<Index>
public Index valueOf(FastCollection.Record record)
FastCollection
valueOf
in class FastCollection<Index>
record
- the record whose current value is returned.public void delete(FastCollection.Record record)
FastCollection
Implementation must ensure that removing a record from the collection does not affect in any way the records preceding the record being removed (it might affect the next records though, e.g. in a list collection, the indices of the subsequent records will change).
delete
in class FastCollection<Index>
record
- the record to be removed.Copyright © 2005 - 2007 Javolution.