public class BufferBitSet extends Object
BitSet, but backed by a
ByteBuffer. Differences with BitSet
include:
BufferBitSet
Cloneable nor Serializable.
resize behavior.
BitSet,
ByteBuffer| Constructor and Description |
|---|
BufferBitSet()
|
BufferBitSet(ByteBuffer buffer)
Creates a
BufferBitSet which wraps the provided buffer. |
BufferBitSet(ByteBuffer buffer,
ResizeBehavior resizeBehavior)
Creates a
BufferBitSet which wraps the provided buffer. |
BufferBitSet(ResizeBehavior resizeBehavior)
Creates a
BufferBitSet with the specified resize behavior. |
| 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. |
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. |
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. |
ResizeBehavior |
getResizeBehavior()
Returns this bitset's
resize behavior. |
int |
hashCode()
Returns the hashcode value for this bitset.
|
boolean |
isEmpty()
Returns true if this
BufferBitSet contains no bits that are set to
true. |
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. |
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
BufferBitSet containing all of the bits in the given
BitSet. |
static BufferBitSet |
valueOf(byte[] bytes)
Returns a new bit set containing all of the bits in the given byte array.
|
BufferBitSet |
withResizeBehavior(ResizeBehavior resizeBehavior)
Returns a new
BufferBitSet with the specified resize behavior. |
void |
xor(BufferBitSet set)
Performs a logical XOR of this bitset with the bitset argument.
|
public BufferBitSet()
public BufferBitSet(ResizeBehavior resizeBehavior)
BufferBitSet with the specified resize behavior.resizeBehavior - - the ResizeBehavior. If
NO_RESIZE is specified
then this bitset will always be empty.NullPointerException - if resizeBehavior is nullpublic 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 resize behavior defaults to NO_RESIZE.
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, ResizeBehavior resizeBehavior)
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.resizeBehavior - - the ResizeBehaviorNullPointerException - if the provided buffer or resizeBehavior is nullpublic ByteBuffer getBuffer()
ByteBuffer backing this BufferBitSet.ByteBuffer backing this BufferBitSet.public ResizeBehavior getResizeBehavior()
resize behavior.resize behavior.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 setBufferBitSet containing all the bits in the byte array, and
with resize behavior ALLOCATE.public static BufferBitSet valueOf(BitSet bs)
BufferBitSet containing all of the bits in the given
BitSet.bs - - the bitset to copyBufferBitSet containing all the bits in the given bitset,
with resize behavior ALLOCATE.public BufferBitSet withResizeBehavior(ResizeBehavior resizeBehavior)
BufferBitSet with the specified resize behavior. The buffer object itself will be
duplicated, but will share the underlying space.
Concurrent modifications to this bitset and the returned bitset can put both into a bad state.
resizeBehavior - - ResizeBehaviorpublic byte[] toByteArray()
public BitSet toBitSet()
BitSet containing all of the bits in this
BufferBitSet.BitSet containing all of the bits in this
BufferBitSet.public 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 ResizeBehavior 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 ResizeBehavior 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 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.
Copyright © 2019. All rights reserved.