Class NibbleArray

java.lang.Object
net.glowstone.util.NibbleArray

public final class NibbleArray extends Object
An array of nibbles (4-bit values) stored efficiently as a byte array of half the size.

The even indices are stored in the least significant nibble and the odd indices in the most significant bits. For example, [1 5 8 15] is stored as [0x51 0xf8].

  • Constructor Summary

    Constructors
    Constructor
    Description
    NibbleArray(byte... rawData)
    Construct a new NibbleArray using the given underlying bytes.
    NibbleArray(int size)
    Construct a new NibbleArray with the given size in nibbles.
    NibbleArray(int size, byte value)
    Construct a new NibbleArray with the given size in nibble, filled with the specified nibble value.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Get the size in bytes, one-half the size in nibbles.
    void
    fill(byte value)
    Fill the nibble array with the specified value.
    byte
    get(int index)
    Get the nibble at the given index.
    byte[]
    Get the raw bytes of this nibble array.
    void
    set(int index, byte value)
    Set the nibble at the given index to the given value.
    void
    setRawData(byte... source)
    Copies into the raw bytes of this nibble array from the given source.
    int
    Get the size in nibbles.
    Take a snapshot of this NibbleArray which will not reflect changes.

    Methods inherited from class java.lang.Object

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

    • NibbleArray

      public NibbleArray(int size)
      Construct a new NibbleArray with the given size in nibbles.
      Parameters:
      size - The number of nibbles in the array.
      Throws:
      IllegalArgumentException - If size is not positive and even.
    • NibbleArray

      public NibbleArray(int size, byte value)
      Construct a new NibbleArray with the given size in nibble, filled with the specified nibble value.
      Parameters:
      size - The number of nibbles in the array.
      value - The value to fill the array with.
      Throws:
      IllegalArgumentException - If size is not positive and even.
    • NibbleArray

      public NibbleArray(byte... rawData)
      Construct a new NibbleArray using the given underlying bytes. No copy is created.
      Parameters:
      rawData - The raw data to use.
  • Method Details

    • size

      public int size()
      Get the size in nibbles.
      Returns:
      The size in nibbles.
    • byteSize

      public int byteSize()
      Get the size in bytes, one-half the size in nibbles.
      Returns:
      The size in bytes.
    • get

      public byte get(int index)
      Get the nibble at the given index.
      Parameters:
      index - The nibble index.
      Returns:
      The value of the nibble at that index.
    • set

      public void set(int index, byte value)
      Set the nibble at the given index to the given value.
      Parameters:
      index - The nibble index.
      value - The new value to store.
    • fill

      public void fill(byte value)
      Fill the nibble array with the specified value.
      Parameters:
      value - The value nibble to fill with.
    • setRawData

      public void setRawData(byte... source)
      Copies into the raw bytes of this nibble array from the given source.
      Parameters:
      source - The array to copy from.
      Throws:
      IllegalArgumentException - If source is not the correct length.
    • snapshot

      public NibbleArray snapshot()
      Take a snapshot of this NibbleArray which will not reflect changes.
      Returns:
      The snapshot NibbleArray.
    • getRawData

      public byte[] getRawData()
      Get the raw bytes of this nibble array. Modifying the returned array will modify the internal representation of this nibble array.
      Returns:
      The raw bytes.