Class BufferBitSet
- java.lang.Object
-
- tech.bitey.bufferstuff.BufferBitSet
-
- All Implemented Interfaces:
java.lang.Cloneable
public class BufferBitSet extends java.lang.Object implements java.lang.CloneableSimilar toBitSet, but backed by aByteBuffer. Differences withBitSetinclude:BufferBitSet- ... is not
Serializable. - ... does not hide the backing buffer, and offers copy-free methods for wrapping an existing buffer.
- ... allows for specifying whether or not the buffer can be resized (replaced with a larger buffer)
The
resizableflag 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
ByteBuffersallocated by this class are procured viaBufferUtils.allocate(int). The allocated buffers will be direct if thetech.bitey.allocateDirectsystem property is set to "true".- Author:
- biteytech@protonmail.com, adapted from java.util.BitSet
- See Also:
BitSet,ByteBuffer
-
-
Field Summary
Fields Modifier and Type Field Description static BufferBitSetEMPTY_BITSETAn empty, non-resizableBufferBitSet
-
Constructor Summary
Constructors Constructor Description BufferBitSet()Creates an empty, resizableBufferBitSetBufferBitSet(boolean resizable)Creates an emptyBufferBitSetwith the specified resize behavior.BufferBitSet(java.nio.ByteBuffer buffer)Creates aBufferBitSetwhich wraps the provided buffer.BufferBitSet(java.nio.ByteBuffer buffer, boolean resizable)Creates aBufferBitSetwhich wraps the provided buffer.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidand(BufferBitSet set)Performs a logical AND of this target bitset with the argument bitset.voidandNot(BufferBitSet set)Clears all of the bits in this bitset whose corresponding bit is set in the specified bitset.intcardinality()Returns the number of bits set totruein thisBufferBitSet.intcardinality(int fromIndex, int toIndex)Returns the number of bits set to true within the given range.voidclear(int bitIndex)Sets the bit specified by the index tofalse.voidclear(int fromIndex, int toIndex)Sets the bits from the specifiedfromIndex(inclusive) to the specifiedtoIndex(exclusive) tofalse.java.lang.Objectclone()Cloning this bitset produces a new bitset that is equal to it.BufferBitSetcopy()booleanequals(java.lang.Object obj)Compares this object against the specified object.voidflip(int bitIndex)Sets the bit at the specified index to the complement of its current value.voidflip(int fromIndex, int toIndex)Sets each bit from the specifiedfromIndex(inclusive) to the specifiedtoIndex(exclusive) to the complement of its current value.booleanget(int bitIndex)Returns the value of the bit with the specified index.BufferBitSetget(int fromIndex, int toIndex)Returns a newBufferBitSetcomposed of bits from this bitset fromfromIndex(inclusive) totoIndex(exclusive).java.nio.ByteBuffergetBuffer()Returns theByteBufferbacking thisBufferBitSet.inthashCode()Returns the hashcode value for this bitset.booleanisEmpty()Returns true if thisBufferBitSetcontains no bits that are set totrue.booleanisResizable()Returns true if this bitset's buffer can be resized (replaced with a larger buffer).intlastSetBit()Returns the index of the highest set bit in the bitset, or -1 if the bitset contains no set bits.intnextClearBit(int fromIndex)Returns the index of the first bit that is set tofalsethat occurs on or after the specified starting index.intnextSetBit(int fromIndex)Returns the index of the first bit that is set totruethat occurs on or after the specified starting index.voidor(BufferBitSet set)Performs a logical OR of this bitset with the bitset argument.intpreviousClearBit(int fromIndex)Returns the index of the nearest bit that is set tofalsethat occurs on or before the specified starting index.intpreviousSetBit(int fromIndex)Returns the index of the nearest bit that is set totruethat occurs on or before the specified starting index.static BufferBitSetrandom(int n, int size)static BufferBitSetrandom(int n, int size, java.util.Random random)static BufferBitSetreadFrom(java.nio.channels.ReadableByteChannel channel)Read a bitset from the specifiedReadableByteChannel.BufferBitSetresizable(boolean resizable)Returns a newBufferBitSetwith the specified resizability.voidset(int bitIndex)Sets the bit at the specified index totrue.voidset(int bitIndex, boolean value)Sets the bit at the specified index to the specified value.voidset(int fromIndex, int toIndex)Sets the bits from the specifiedfromIndex(inclusive) to the specifiedtoIndex(exclusive) totrue.voidset(int fromIndex, int toIndex, boolean value)Sets the bits from the specifiedfromIndex(inclusive) to the specifiedtoIndex(exclusive) to the specified value.BufferBitSetshiftRight(int offset)Returns a copy of this bitset with each bit shifted right byoffset.intsize()Returns the number of bits of space actually in use by thisBufferBitSetto represent bit values.java.util.BitSettoBitSet()Returns a newBitSetcontaining all of the bits in thisBufferBitSet.byte[]toByteArray()Returns a new byte array containing all the bits in this bit set.java.lang.StringtoString()Returns a string representation of thisBufferBitSetequivalent to the representation of aSortedSetcontaining the indices of the bits which are set in this bitset.static BufferBitSetvalueOf(byte[] bytes)Returns a new resizable bitset containing all of the bits in the given byte array.static BufferBitSetvalueOf(java.util.BitSet bs)Returns a new resizableBufferBitSetcontaining all of the bits in the givenBitSet.voidwriteTo(java.nio.channels.WritableByteChannel channel)Write this bitset to the specifiedWritableByteChannel.voidwriteTo(java.nio.channels.WritableByteChannel channel, int fromIndex, int toIndex)Write a range from this bitset to the specifiedWritableByteChannel.voidxor(BufferBitSet set)Performs a logical XOR of this bitset with the bitset argument.
-
-
-
Field Detail
-
EMPTY_BITSET
public static final BufferBitSet EMPTY_BITSET
An empty, non-resizableBufferBitSet
-
-
Constructor Detail
-
BufferBitSet
public BufferBitSet()
Creates an empty, resizableBufferBitSet
-
BufferBitSet
public BufferBitSet(boolean resizable)
Creates an emptyBufferBitSetwith the specified resize behavior.- Parameters:
resizable- - specifies whether or not the buffer can be resized (replaced with a larger buffer)
-
BufferBitSet
public BufferBitSet(java.nio.ByteBuffer buffer)
Creates aBufferBitSetwhich wraps the provided buffer. This bitset will only make use of the space demarked byBuffer.position()andBuffer.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.
- Parameters:
buffer- - theByteBufferto be wrapped by this bitset. Writes to this bitset will modify the buffer's content.- Throws:
java.lang.NullPointerException- if the provided buffer is null
-
BufferBitSet
public BufferBitSet(java.nio.ByteBuffer buffer, boolean resizable)Creates aBufferBitSetwhich wraps the provided buffer. This bitset will only make use of the space demarked byBuffer.position()andBuffer.limit(). The provided buffer object will not itself be modified, though the buffer's content can be via writes to this bitset.- Parameters:
buffer- - theByteBufferto 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)
-
-
Method Detail
-
getBuffer
public java.nio.ByteBuffer getBuffer()
Returns theByteBufferbacking thisBufferBitSet.- Returns:
- the
ByteBufferbacking thisBufferBitSet.
-
isResizable
public boolean isResizable()
Returns true if this bitset's buffer can be resized (replaced with a larger buffer).- Returns:
- true if this bitset's buffer can be resized.
-
valueOf
public static BufferBitSet valueOf(byte[] bytes)
Returns a new resizable bitset containing all of the bits in the given byte array.More precisely,
BufferBitSet.valueOf(bytes).get(n) == ((bytes[n/8] & (1<<(n%8))) != 0)
for alln < 8 * bytes.length.The provided array is wrapped, it is not copied. Writes to this bitset can modify the array.
- Parameters:
bytes- - a byte array containing a sequence of bits to be used as the initial bits of the new bit set- Returns:
- a new resizable bitset containing all of the bits in the given byte array.
-
valueOf
public static BufferBitSet valueOf(java.util.BitSet bs)
Returns a new resizableBufferBitSetcontaining all of the bits in the givenBitSet.- Parameters:
bs- - the bitset to copy- Returns:
- a new resizable
BufferBitSetcontaining all of the bits in the givenjava.util.BitSet.
-
resizable
public BufferBitSet resizable(boolean resizable)
Returns a newBufferBitSetwith the specified resizability. The buffer object itself will beduplicated, but will share the underlying space.- Parameters:
resizable- - specifies whether or not the buffer can be resized (replaced with a larger buffer)- Returns:
- a new bitset with the specified resize behavior
-
random
public static BufferBitSet random(int n, int size)
- Parameters:
n- - the number of bits to setsize- - bits are set within the range zero to size (exclusive)- Returns:
- a new bitset with n bits set randomly in the range zero to size (exclusive)
- Throws:
java.lang.IllegalArgumentException- ifsize < 0java.lang.IllegalArgumentException- ifn < 0 || n > size
-
random
public static BufferBitSet random(int n, int size, java.util.Random random)
- Parameters:
n- - the number of bits to setsize- - bits are set within the range zero to size (exclusive)random- - the random number generator- Returns:
- a new bitset with n bits set randomly in the range zero to size (exclusive)
- Throws:
java.lang.IllegalArgumentException- ifsize < 0java.lang.IllegalArgumentException- ifn < 0 || n > size
-
toByteArray
public byte[] toByteArray()
Returns a new byte array containing all the bits in this bit set.- Returns:
- a new byte array containing all the bits in this bit set.
-
toBitSet
public java.util.BitSet toBitSet()
Returns a newBitSetcontaining all of the bits in thisBufferBitSet.- Returns:
- a new
BitSetcontaining all of the bits in thisBufferBitSet.
-
writeTo
public void writeTo(java.nio.channels.WritableByteChannel channel) throws java.io.IOExceptionWrite this bitset to the specifiedWritableByteChannel. Equivalent towriteTo(channel, 0, length())- Parameters:
channel- - the channel to write to- Throws:
java.io.IOException- if some I/O error occurs
-
writeTo
public void writeTo(java.nio.channels.WritableByteChannel channel, int fromIndex, int toIndex) throws java.io.IOExceptionWrite a range from this bitset to the specifiedWritableByteChannel. This method will write a 5-byte header followed by the bytes which store the bits in the specified range.- Parameters:
channel- - the channel to write tofromIndex- - index of the first bit to writetoIndex- - index after the last bit to write- Throws:
java.io.IOException- if some I/O error occursjava.lang.IndexOutOfBoundsException- iffromIndexis negative, ortoIndexis negative, orfromIndexis larger thantoIndex
-
readFrom
public static BufferBitSet readFrom(java.nio.channels.ReadableByteChannel channel) throws java.io.IOException
Read a bitset from the specifiedReadableByteChannel. The bitset must have been previously written with one of thewriteTomethods.- Parameters:
channel- - the channel to read from- Returns:
- a non-resizable bitset from the specified channel
- Throws:
java.io.IOException- if some I/O error occurs
-
get
public boolean get(int bitIndex)
Returns the value of the bit with the specified index. The value istrueif the bit with the indexbitIndexis currently set in this bitset; otherwise, the result isfalse.- Parameters:
bitIndex- the bit index- Returns:
- the value of the bit with the specified index
- Throws:
java.lang.IndexOutOfBoundsException- if the specified index is negative
-
get
public BufferBitSet get(int fromIndex, int toIndex)
Returns a newBufferBitSetcomposed of bits from this bitset fromfromIndex(inclusive) totoIndex(exclusive).The resulting bitset will always be stored in newly allocated space, and will have the same resizable settings as this bitset.
- Parameters:
fromIndex- - index of the first bit to includetoIndex- - index after the last bit to include- Returns:
- a new bitset from a range of this bitset
- Throws:
java.lang.IndexOutOfBoundsException- iffromIndexis negative, ortoIndexis negative, orfromIndexis larger thantoIndex
-
set
public void set(int bitIndex)
Sets the bit at the specified index totrue.- Parameters:
bitIndex- a bit index- Throws:
java.lang.IndexOutOfBoundsException- if the specified index is negative
-
set
public void set(int bitIndex, boolean value)Sets the bit at the specified index to the specified value.- Parameters:
bitIndex- a bit indexvalue- a boolean value to set- Throws:
java.lang.IndexOutOfBoundsException- if the specified index is negative
-
set
public void set(int fromIndex, int toIndex)Sets the bits from the specifiedfromIndex(inclusive) to the specifiedtoIndex(exclusive) totrue.- Parameters:
fromIndex- index of the first bit to be settoIndex- index after the last bit to be set- Throws:
java.lang.IndexOutOfBoundsException- iffromIndexis negative, ortoIndexis negative, orfromIndexis larger thantoIndex
-
set
public void set(int fromIndex, int toIndex, boolean value)Sets the bits from the specifiedfromIndex(inclusive) to the specifiedtoIndex(exclusive) to the specified value.- Parameters:
fromIndex- index of the first bit to be settoIndex- index after the last bit to be setvalue- value to set the selected bits to- Throws:
java.lang.IndexOutOfBoundsException- iffromIndexis negative, ortoIndexis negative, orfromIndexis larger thantoIndex
-
flip
public void flip(int bitIndex)
Sets the bit at the specified index to the complement of its current value.- Parameters:
bitIndex- the index of the bit to flip- Throws:
java.lang.IndexOutOfBoundsException- if the specified index is negative
-
flip
public void flip(int fromIndex, int toIndex)Sets each bit from the specifiedfromIndex(inclusive) to the specifiedtoIndex(exclusive) to the complement of its current value.- Parameters:
fromIndex- index of the first bit to fliptoIndex- index after the last bit to flip- Throws:
java.lang.IndexOutOfBoundsException- iffromIndexis negative, ortoIndexis negative, orfromIndexis larger thantoIndex
-
clear
public void clear(int bitIndex)
Sets the bit specified by the index tofalse.- Parameters:
bitIndex- the index of the bit to be cleared- Throws:
java.lang.IndexOutOfBoundsException- if the specified index is negative
-
clear
public void clear(int fromIndex, int toIndex)Sets the bits from the specifiedfromIndex(inclusive) to the specifiedtoIndex(exclusive) tofalse.- Parameters:
fromIndex- index of the first bit to be clearedtoIndex- index after the last bit to be cleared- Throws:
java.lang.IndexOutOfBoundsException- iffromIndexis negative, ortoIndexis negative, orfromIndexis larger thantoIndex
-
nextSetBit
public int nextSetBit(int fromIndex)
Returns the index of the first bit that is set totruethat occurs on or after the specified starting index. If no such bit exists then-1is returned.- Parameters:
fromIndex- the index to start checking from (inclusive)- Returns:
- the index of the next set bit, or
-1if there is no such bit - Throws:
java.lang.IndexOutOfBoundsException- if the specified index is negative
-
nextClearBit
public int nextClearBit(int fromIndex)
Returns the index of the first bit that is set tofalsethat occurs on or after the specified starting index.- Parameters:
fromIndex- the index to start checking from (inclusive)- Returns:
- the index of the next clear bit
- Throws:
java.lang.IndexOutOfBoundsException- if the specified index is negative
-
previousSetBit
public int previousSetBit(int fromIndex)
Returns the index of the nearest bit that is set totruethat occurs on or before the specified starting index. If no such bit exists, or if-1is given as the starting index, then-1is returned.- Parameters:
fromIndex- the index to start checking from (inclusive)- Returns:
- the index of the previous set bit, or
-1if there is no such bit - Throws:
java.lang.IndexOutOfBoundsException- if the specified index is less than-1
-
previousClearBit
public int previousClearBit(int fromIndex)
Returns the index of the nearest bit that is set tofalsethat occurs on or before the specified starting index. If no such bit exists, or if-1is given as the starting index, then-1is returned.- Parameters:
fromIndex- the index to start checking from (inclusive)- Returns:
- the index of the previous clear bit, or
-1if there is no such bit - Throws:
java.lang.IndexOutOfBoundsException- if the specified index is less than-1
-
lastSetBit
public int lastSetBit()
Returns the index of the highest set bit in the bitset, or -1 if the bitset contains no set bits.- Returns:
- the index of the highest set bit in the bitset, or -1 if the bitset contains no set bits.
-
and
public void and(BufferBitSet set)
Performs a logical AND of this target bitset with the argument bitset. This bitset is modified so that each bit in it has the valuetrueif and only if it both initially had the valuetrueand the corresponding bit in the bitset argument also had the valuetrue.- Parameters:
set- - aBufferBitSet
-
or
public void or(BufferBitSet set)
Performs a logical OR of this bitset with the bitset argument. This bitset is modified so that a bit in it has the valuetrueif and only if it either already had the valuetrueor the corresponding bit in the bitset argument has the valuetrue.- Parameters:
set- - aBufferBitSet
-
xor
public void xor(BufferBitSet set)
Performs a logical XOR of this bitset with the bitset argument. This bitset is modified so that a bit in it has the valuetrueif and only if one of the following statements holds:- The bit initially has the value
true, and the corresponding bit in the argument has the valuefalse. - The bit initially has the value
false, and the corresponding bit in the argument has the valuetrue.
- Parameters:
set- - aBufferBitSet
- The bit initially has the value
-
andNot
public void andNot(BufferBitSet set)
Clears all of the bits in this bitset whose corresponding bit is set in the specified bitset.- Parameters:
set- - theBufferBitSetwith which to mask this bitset
-
shiftRight
public BufferBitSet shiftRight(int offset)
Returns a copy of this bitset with each bit shifted right byoffset. The resulting bitset will always be stored in newly allocated space, and will have the same resizable setting as this bitset.- Parameters:
offset- - number of bits to shift by- Returns:
- a new bitset with shifted right by
offset - Throws:
java.lang.IllegalArgumentException- if offset is negativejava.lang.IllegalStateException- if the shifted size exceeds the maximum addressable size (2^31-1)
-
toString
public java.lang.String toString()
Returns a string representation of thisBufferBitSetequivalent to the representation of aSortedSetcontaining the indices of the bits which are set in this bitset.- Overrides:
toStringin classjava.lang.Object
-
size
public int size()
Returns the number of bits of space actually in use by thisBufferBitSetto represent bit values. The maximum element that can be set without resizing issize()-1- Returns:
- the number of bits of space currently in this bit set
-
isEmpty
public boolean isEmpty()
Returns true if thisBufferBitSetcontains no bits that are set totrue.- Returns:
- boolean indicating whether this bitset is empty
-
cardinality
public int cardinality()
Returns the number of bits set totruein thisBufferBitSet.- Returns:
- the number of bits set to
truein thisBufferBitSet
-
cardinality
public int cardinality(int fromIndex, int toIndex)Returns the number of bits set to true within the given range.- Parameters:
fromIndex- - index of the first bit in the rangetoIndex- - index after the last bit in the range- Returns:
- the number of bits set to true within the given range.
- Throws:
java.lang.IndexOutOfBoundsException- iffromIndexis negative, ortoIndexis negative, orfromIndexis larger thantoIndex
-
hashCode
public int hashCode()
Returns the hashcode value for this bitset. The hashcode depends only on which bits are set within thisBufferBitSet.Hashcode is computed using formula from
Arrays.hashCode(byte[])- Overrides:
hashCodein classjava.lang.Object- Returns:
- the hashcode value for this bitset
-
equals
public boolean equals(java.lang.Object obj)
Compares this object against the specified object. The result istrueif and only if the argument is notnulland is aBufferBitsetobject that has exactly the same set of bits set totrueas this bit set. That is, for every nonnegativeintindexk,((BitBufferSet) obj).get(k) == this.get(k)
must be true. The current sizes of the two bit sets are not compared.- Overrides:
equalsin classjava.lang.Object- Parameters:
obj- the object to compare with- Returns:
trueif the objects are the same;falseotherwise
-
clone
public java.lang.Object clone()
Cloning this bitset produces a new bitset that is equal to it.- Overrides:
clonein classjava.lang.Object- Returns:
- another bitset that has exactly the same bits set to
trueas this one
-
copy
public BufferBitSet copy()
- Returns:
- another bitset that has exactly the same bits set to
trueas this one
-
-