Class BitSetView

java.lang.Object
express.mvp.roray.utils.memory.BitSetView

public final class BitSetView extends Object
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
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    long
    Returns the size of the bit array in bytes.
    long
    Returns the number of bits set to 1 (population count).
    void
    clear(long bitIndex)
    Clears the bit at the specified index (sets it to 0).
    void
    Clears all bits in the bit array (sets all to 0).
    void
    flip(long bitIndex)
    Flips the bit at the specified index (0 becomes 1, 1 becomes 0).
    boolean
    get(long bitIndex)
    Returns the value of the bit at the specified index.
    boolean
    Checks if this BitSetView is empty (no bits set to 1).
    boolean
    Checks if this view has been wrapped around valid data.
    long
    nextClearBit(long fromIndex)
    Returns the index of the next bit that is set to 0, starting from the specified index (inclusive).
    long
    nextSetBit(long fromIndex)
    Returns the index of the next bit that is set to 1, starting from the specified index (inclusive).
    long
    Returns the offset within the segment where the bit array starts.
    Returns the underlying MemorySegment this view is wrapping.
    void
    set(long bitIndex)
    Sets the bit at the specified index to 1.
    void
    set(long bitIndex, boolean value)
    Sets the bit at the specified index to the specified value.
    void
    Sets all bits in the bit array (sets all to 1).
    long
    Returns the number of bits that can be stored in this bit array.
    void
    wrap(MemorySegment segment, long offset, long size)
    Wraps a segment slice for bit operations.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • BitSetView

      public BitSetView()
  • Method Details

    • wrap

      public void wrap(MemorySegment segment, long offset, long size)
      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

      public MemorySegment 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.