Class BitSetView
java.lang.Object
express.mvp.roray.utils.memory.BitSetView
A reusable, zero-allocation flyweight for performing bit-level operations over a MemorySegment.
This provides a BitSet-like API for off-heap memory without heap allocation. Bits are indexed from 0, where bit 0 is the LSB of byte 0.
Thread Safety: Not thread-safe. Use one instance per thread.
Zero-GC: All operations avoid heap allocation.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionlongbyteSize()Returns the size of the bit array in bytes.longReturns the number of bits set to 1 (population count).voidclear(long bitIndex) Clears the bit at the specified index (sets it to 0).voidclearAll()Clears all bits in the bit array (sets all to 0).voidflip(long bitIndex) Flips the bit at the specified index (0 becomes 1, 1 becomes 0).booleanget(long bitIndex) Returns the value of the bit at the specified index.booleanisEmpty()Checks if this BitSetView is empty (no bits set to 1).booleanisValid()Checks if this view has been wrapped around valid data.longnextClearBit(long fromIndex) Returns the index of the next bit that is set to 0, starting from the specified index (inclusive).longnextSetBit(long fromIndex) Returns the index of the next bit that is set to 1, starting from the specified index (inclusive).longoffset()Returns the offset within the segment where the bit array starts.segment()Returns the underlying MemorySegment this view is wrapping.voidset(long bitIndex) Sets the bit at the specified index to 1.voidset(long bitIndex, boolean value) Sets the bit at the specified index to the specified value.voidsetAll()Sets all bits in the bit array (sets all to 1).longsize()Returns the number of bits that can be stored in this bit array.voidwrap(MemorySegment segment, long offset, long size) Wraps a segment slice for bit operations.
-
Constructor Details
-
BitSetView
public BitSetView()
-
-
Method Details
-
wrap
Wraps a segment slice for bit operations.- Parameters:
segment- The backing memory segment.offset- The offset within the segment where the bit array starts.size- The size of the bit array in bytes.
-
segment
Returns the underlying MemorySegment this view is wrapping.- Returns:
- The MemorySegment, or null if not wrapped.
-
offset
public long offset()Returns the offset within the segment where the bit array starts.- Returns:
- The byte offset.
-
byteSize
public long byteSize()Returns the size of the bit array in bytes.- Returns:
- The byte size.
-
isValid
public boolean isValid()Checks if this view has been wrapped around valid data.- Returns:
- true if wrapped with a non-null segment, false otherwise.
-
set
public void set(long bitIndex) Sets the bit at the specified index to 1.- Parameters:
bitIndex- The index of the bit to set (0-based).- Throws:
IndexOutOfBoundsException- if bitIndex is out of range.
-
set
public void set(long bitIndex, boolean value) Sets the bit at the specified index to the specified value.- Parameters:
bitIndex- The index of the bit to set (0-based).value- true to set the bit, false to clear it.- Throws:
IndexOutOfBoundsException- if bitIndex is out of range.
-
clear
public void clear(long bitIndex) Clears the bit at the specified index (sets it to 0).- Parameters:
bitIndex- The index of the bit to clear (0-based).- Throws:
IndexOutOfBoundsException- if bitIndex is out of range.
-
clearAll
public void clearAll()Clears all bits in the bit array (sets all to 0). -
setAll
public void setAll()Sets all bits in the bit array (sets all to 1). -
get
public boolean get(long bitIndex) Returns the value of the bit at the specified index.- Parameters:
bitIndex- The index of the bit to get (0-based).- Returns:
- true if the bit is set, false otherwise.
- Throws:
IndexOutOfBoundsException- if bitIndex is out of range.
-
flip
public void flip(long bitIndex) Flips the bit at the specified index (0 becomes 1, 1 becomes 0).- Parameters:
bitIndex- The index of the bit to flip (0-based).- Throws:
IndexOutOfBoundsException- if bitIndex is out of range.
-
cardinality
public long cardinality()Returns the number of bits set to 1 (population count).- Returns:
- The cardinality (number of 1 bits).
-
nextSetBit
public long nextSetBit(long fromIndex) Returns the index of the next bit that is set to 1, starting from the specified index (inclusive).- Parameters:
fromIndex- The index to start searching from (0-based).- Returns:
- The index of the next set bit, or -1 if no set bit is found.
- Throws:
IndexOutOfBoundsException- if fromIndex is negative.
-
nextClearBit
public long nextClearBit(long fromIndex) Returns the index of the next bit that is set to 0, starting from the specified index (inclusive).- Parameters:
fromIndex- The index to start searching from (0-based).- Returns:
- The index of the next clear bit, or -1 if no clear bit is found.
- Throws:
IndexOutOfBoundsException- if fromIndex is negative.
-
size
public long size()Returns the number of bits that can be stored in this bit array.- Returns:
- The capacity in bits (size * 8).
-
isEmpty
public boolean isEmpty()Checks if this BitSetView is empty (no bits set to 1).- Returns:
- true if all bits are 0, false otherwise.
-