public class BufferBitSet extends Object implements Cloneable
BitSet, but backed by a
ByteBuffer. Differences with BitSet
include:
BufferBitSet
Serializable.
The resizable flag controls whether or not the bitset
can grow to accommodate setting bits beyond the current buffer's limit (by
replacing the current buffer with a larger one).
All ByteBuffers allocated by this class are procured via
BufferUtils.allocate(int). The allocated buffers will be direct if
the tech.bitey.allocateDirect system property is set to "true".
BitSet,
ByteBuffer| Modifier and Type | Field and Description |
|---|---|
static BufferBitSet |
EMPTY_BITSET
An empty, non-resizable
BufferBitSet |
| Constructor and Description |
|---|
BufferBitSet()
Creates an empty, resizable
BufferBitSet |
BufferBitSet(boolean resizable)
Creates an empty
BufferBitSet with the specified resize behavior. |
BufferBitSet(ByteBuffer buffer)
Creates a
BufferBitSet which wraps the provided buffer. |
BufferBitSet(ByteBuffer buffer,
boolean resizable)
Creates a
BufferBitSet which wraps the provided buffer. |
| Modifier and Type | Method and Description |
|---|---|
void |
and(BufferBitSet set)
Performs a logical AND of this target bitset with the argument bitset.
|
void |
andNot(BufferBitSet set)
Clears all of the bits in this bitset whose corresponding bit is set in the
specified bitset.
|
int |
cardinality()
Returns the number of bits set to
true in this BufferBitSet. |
int |
cardinality(int fromIndex,
int toIndex)
Returns the number of bits set to true within the given range.
|
void |
clear(int bitIndex)
Sets the bit specified by the index to
false. |
void |
clear(int fromIndex,
int toIndex)
Sets the bits from the specified
fromIndex (inclusive) to the
specified toIndex (exclusive) to false. |
Object |
clone()
Cloning this bitset produces a new bitset that is equal to it.
|
BufferBitSet |
copy()
|
boolean |
equals(Object obj)
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 specified
fromIndex (inclusive) to the
specified toIndex (exclusive) to the complement of its current value. |
boolean |
get(int bitIndex)
Returns the value of the bit with the specified index.
|
BufferBitSet |
get(int fromIndex,
int toIndex)
Returns a new
BufferBitSet composed of bits from this bitset from
fromIndex (inclusive) to toIndex (exclusive). |
ByteBuffer |
getBuffer()
Returns the
ByteBuffer backing this BufferBitSet. |
int |
hashCode()
Returns the hashcode value for this bitset.
|
boolean |
isEmpty()
Returns true if this
BufferBitSet contains no bits that are set to
true. |
boolean |
isResizable()
Returns true if this bitset's buffer can be resized (replaced with a larger
buffer).
|
int |
length()
Returns the "logical size" of this bitset: the index of the highest set bit
in the bitset plus one.
|
int |
nextClearBit(int fromIndex)
Returns the index of the first bit that is set to
false that occurs
on or after the specified starting index. |
int |
nextSetBit(int fromIndex)
Returns the index of the first bit that is set to
true that occurs on
or after the specified starting index. |
void |
or(BufferBitSet set)
Performs a logical OR of this bitset with the bitset argument.
|
int |
previousClearBit(int fromIndex)
Returns the index of the nearest bit that is set to
false that occurs
on or before the specified starting index. |
int |
previousSetBit(int fromIndex)
Returns the index of the nearest bit that is set to
true that occurs
on or before the specified starting index. |
static BufferBitSet |
random(int n,
int size)
|
static BufferBitSet |
readFrom(ReadableByteChannel channel)
Read a bitset from the specified
ReadableByteChannel. |
BufferBitSet |
resizable(boolean resizable)
Returns a new
BufferBitSet with the specified resizability. |
void |
set(int bitIndex)
Sets the bit at the specified index to
true. |
void |
set(int bitIndex,
boolean value)
Sets the bit at the specified 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 from the specified
fromIndex (inclusive) to the
specified toIndex (exclusive) to the specified value. |
BufferBitSet |
shiftRight(int offset)
Returns a copy of this bitset with each bit shifted right by
offset. |
int |
size()
Returns the number of bits of space actually in use by this
BufferBitSet to represent bit values. |
BitSet |
toBitSet()
Returns a new
BitSet containing all of the bits in this
BufferBitSet. |
byte[] |
toByteArray()
Returns a new byte array containing all the bits in this bit set.
|
String |
toString()
Returns a string representation of this
BufferBitSet equivalent to
the representation of a SortedSet containing the indices of the bits
which are set in this bitset. |
static BufferBitSet |
valueOf(BitSet bs)
Returns a new resizable
BufferBitSet containing all of the bits in
the given BitSet. |
static BufferBitSet |
valueOf(byte[] bytes)
Returns a new resizable bitset containing all of the bits in the given byte
array.
|
void |
writeTo(WritableByteChannel channel)
Write this bitset to the specified
WritableByteChannel. |
void |
writeTo(WritableByteChannel channel,
int fromIndex,
int toIndex)
Write a range from this bitset to the specified
WritableByteChannel. |
void |
xor(BufferBitSet set)
Performs a logical XOR of this bitset with the bitset argument.
|
public static final BufferBitSet EMPTY_BITSET
BufferBitSetpublic BufferBitSet()
BufferBitSetpublic BufferBitSet(boolean resizable)
BufferBitSet with the specified resize behavior.resizable - - specifies whether or not the buffer can be resized
(replaced with a larger buffer)public BufferBitSet(ByteBuffer buffer)
BufferBitSet which wraps the provided buffer. This bitset
will only make use of the space demarked by Buffer.position() and
Buffer.limit(). The provided buffer object will not itself be
modified, though the buffer's content can be via writes to this bitset.
The resulting bitset is not resizable.
buffer - - the ByteBuffer to be wrapped by this bitset. Writes
to this bitset will modify the buffer's content.NullPointerException - if the provided buffer is nullpublic BufferBitSet(ByteBuffer buffer, boolean resizable)
BufferBitSet which wraps the provided buffer. This bitset
will only make use of the space demarked by Buffer.position() and
Buffer.limit(). The provided buffer object will not itself be
modified, though the buffer's content can be via writes to this bitset.buffer - - the ByteBuffer to be wrapped by this bitset.
Writes to this bitset will modify the buffer's content.resizable - - specifies whether or not the buffer can be resized
(replaced with a larger buffer)public ByteBuffer getBuffer()
ByteBuffer backing this BufferBitSet.ByteBuffer backing this BufferBitSet.public boolean isResizable()
public static BufferBitSet valueOf(byte[] bytes)
More precisely,
BufferBitSet.valueOf(bytes).get(n) == ((bytes[n/8] & (1<<(n%8))) != 0)
for all n < 8 * bytes.length.
The provided array is wrapped, it is not copied. Writes to this bitset can modify the array.
bytes - - a byte array containing a sequence of bits to be used as the
initial bits of the new bit setpublic static BufferBitSet valueOf(BitSet bs)
BufferBitSet containing all of the bits in
the given BitSet.bs - - the bitset to copyBufferBitSet containing all of the bits in
the given java.util.BitSet.public BufferBitSet resizable(boolean resizable)
BufferBitSet with the specified resizability. The
buffer object itself will be duplicated, but
will share the underlying space.resizable - - specifies whether or not the buffer can be resized
(replaced with a larger buffer)public static BufferBitSet random(int n, int size)
n - - the number of bits to setsize - - bits are set within the range zero to size (exclusive)IllegalArgumentException - if size < 0IllegalArgumentException - if n < 0 || n > sizepublic byte[] toByteArray()
public BitSet toBitSet()
BitSet containing all of the bits in this
BufferBitSet.BitSet containing all of the bits in this
BufferBitSet.public void writeTo(WritableByteChannel channel) throws IOException
WritableByteChannel. Equivalent to
writeTo(channel, 0, length())channel - - the channel to write toIOException - if some I/O error occurspublic void writeTo(WritableByteChannel channel, int fromIndex, int toIndex) throws IOException
WritableByteChannel.
This method will write a 5-byte header followed by the bytes which store the
bits in the specified range.channel - - the channel to write tofromIndex - - index of the first bit to writetoIndex - - index after the last bit to writeIOException - if some I/O error occursIndexOutOfBoundsException - if fromIndex is negative, or
toIndex is negative, or
fromIndex is larger than
toIndexpublic static BufferBitSet readFrom(ReadableByteChannel channel) throws IOException
ReadableByteChannel. The bitset must
have been previously written with one of the writeTo methods.channel - - the channel to read fromIOException - if some I/O error occurspublic boolean get(int bitIndex)
true if the bit with the index bitIndex is currently set in
this bitset; otherwise, the result is false.bitIndex - the bit indexIndexOutOfBoundsException - if the specified index is negativepublic BufferBitSet get(int fromIndex, int toIndex)
BufferBitSet composed of bits from this bitset from
fromIndex (inclusive) to toIndex (exclusive).
The resulting bitset will always be stored in newly allocated space, and will have the same resizable settings as this bitset.
fromIndex - - index of the first bit to includetoIndex - - index after the last bit to includeIndexOutOfBoundsException - if fromIndex is negative, or
toIndex is negative, or
fromIndex is larger than
toIndexpublic void set(int bitIndex)
true.bitIndex - a bit indexIndexOutOfBoundsException - if the specified index is negativepublic void set(int bitIndex,
boolean value)
bitIndex - a bit indexvalue - a boolean value to setIndexOutOfBoundsException - if the specified index is negativepublic void set(int fromIndex,
int toIndex)
fromIndex (inclusive) to the
specified toIndex (exclusive) to true.fromIndex - index of the first bit to be settoIndex - index after the last bit to be setIndexOutOfBoundsException - if fromIndex is negative, or
toIndex is negative, or
fromIndex is larger than
toIndexpublic void set(int fromIndex,
int toIndex,
boolean value)
fromIndex (inclusive) to the
specified toIndex (exclusive) to the specified value.fromIndex - index of the first bit to be settoIndex - index after the last bit to be setvalue - value to set the selected bits toIndexOutOfBoundsException - if fromIndex is negative, or
toIndex is negative, or
fromIndex is larger than
toIndexpublic void flip(int bitIndex)
bitIndex - the index of the bit to flipIndexOutOfBoundsException - if the specified index is negativepublic void flip(int fromIndex,
int toIndex)
fromIndex (inclusive) to the
specified toIndex (exclusive) to the complement of its current value.fromIndex - index of the first bit to fliptoIndex - index after the last bit to flipIndexOutOfBoundsException - if fromIndex is negative, or
toIndex is negative, or
fromIndex is larger than
toIndexpublic void clear(int bitIndex)
false.bitIndex - the index of the bit to be clearedIndexOutOfBoundsException - if the specified index is negativepublic void clear(int fromIndex,
int toIndex)
fromIndex (inclusive) to the
specified toIndex (exclusive) to false.fromIndex - index of the first bit to be clearedtoIndex - index after the last bit to be clearedIndexOutOfBoundsException - if fromIndex is negative, or
toIndex is negative, or
fromIndex is larger than
toIndexpublic int nextSetBit(int fromIndex)
true that occurs on
or after the specified starting index. If no such bit exists then -1
is returned.fromIndex - the index to start checking from (inclusive)-1 if there is no such bitIndexOutOfBoundsException - if the specified index is negativepublic int nextClearBit(int fromIndex)
false that occurs
on or after the specified starting index.fromIndex - the index to start checking from (inclusive)IndexOutOfBoundsException - if the specified index is negativepublic int previousSetBit(int fromIndex)
true 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.fromIndex - the index to start checking from (inclusive)-1 if there is no such
bitIndexOutOfBoundsException - if the specified index is less than
-1public int previousClearBit(int fromIndex)
false 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.fromIndex - the index to start checking from (inclusive)-1 if there is no
such bitIndexOutOfBoundsException - if the specified index is less than
-1public void and(BufferBitSet set)
true if
and only if it both initially had the value true and the
corresponding bit in the bitset argument also had the value true.set - - a BufferBitSetpublic void or(BufferBitSet set)
true if and only
if it either already had the value true or the corresponding bit in
the bitset argument has the value true.set - - a BufferBitSetpublic void xor(BufferBitSet set)
true if and only
if one of the following statements holds:
true, and the corresponding bit
in the argument has the value false.
false, and the corresponding bit
in the argument has the value true.
set - - a BufferBitSetpublic void andNot(BufferBitSet set)
set - - the BufferBitSet with which to mask this bitsetpublic BufferBitSet shiftRight(int offset)
offset.
The resulting bitset will always be stored in newly allocated space, and will
have the same resizable setting as this bitset.offset - - number of bits to shift byoffsetIllegalArgumentException - if offset is negativepublic String toString()
BufferBitSet equivalent to
the representation of a SortedSet containing the indices of the bits
which are set in this bitset.public int length()
public int size()
BufferBitSet to represent bit values. The maximum element that can be
set without resizing is size()-1public boolean isEmpty()
BufferBitSet contains no bits that are set to
true.public int cardinality()
true in this BufferBitSet.true in this BufferBitSetpublic int cardinality(int fromIndex,
int toIndex)
fromIndex - - index of the first bit in the rangetoIndex - - index after the last bit in the rangeIndexOutOfBoundsException - if fromIndex is negative, or
toIndex is negative, or
fromIndex is larger than
toIndexpublic int hashCode()
BufferBitSet.
Hashcode is computed using formula from
Arrays.hashCode(byte[])
public boolean equals(Object obj)
true
if and only if the argument is not null and is a BufferBitset
object that has exactly the same set of bits set to true as this bit
set. That is, for every nonnegative int index k,
((BitBufferSet) obj).get(k) == this.get(k)must be true. The current sizes of the two bit sets are not compared.
public Object clone()
public BufferBitSet copy()
true as
this oneCopyright © 2020. All rights reserved.