Package blbutil

Class BitArray

java.lang.Object
blbutil.BitArray

public class BitArray extends Object

Interface BitArray represents a mutable sequence of bits with a fixed length.

Instances of BitArray are not thread-safe.

  • Constructor Summary

    Constructors
    Constructor
    Description
    BitArray(int size)
    Constructs a BitArray instance with the specified size and having all bits set to 0 (unset).
    BitArray(long[] values, int size)
    Constructs a BitArray instance from the specified values.
    BitArray(BitArray bitList)
    Constructs a new BitArray instance with the same sequence of bits and the same size as the specified BitArray.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Clears all bits.
    void
    clear(int index)
    Clears the specified bit.
    void
    copyFrom(BitArray src, int from, int to)
    Replaced the specified bits in this Bitlist with the corresponding bits in the specified BitArray.
    boolean
    equal(BitArray other, int from, int to)
    Returns true if this Bitlist and the specified BitArray have identical sequences of bits for the specified indices, and returns false otherwise.
    static boolean
    Returns true if the specified BitArray objects represent identical bit sequences having the same size, and returns false otherwise.
    boolean
    get(int index)
    Returns the specified bit as a boolean value.
    int
    getAsInt(int index)
    Returns the specified bit as a int value.
    int
    hash(int from, int to)
    Returns a hash code for the specified bits in this Bitlist
    static int
    longHashCode(long value)
     
    restrict(int from, int to)
    Returns a new BitArray of size (from - to) that is a copy of the specified bit indices of this BitArray.
    void
    set(int index)
    Sets the specified bit.
    int
    Returns the number of bits in this BitArray.
    static void
    swapBits(BitArray a, BitArray b, int from, int to)
    Swaps the specified bits of the two specified Bitlist objects.
    long[]
    Returns this BitArray as a long array.
    Returns a string representation of this BitArray.

    Methods inherited from class java.lang.Object

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

    • BitArray

      public BitArray(int size)
      Constructs a BitArray instance with the specified size and having all bits set to 0 (unset).
      Parameters:
      size - the number of bits
      Throws:
      IllegalArgumentException - if size < 0
    • BitArray

      public BitArray(long[] values, int size)
      Constructs a BitArray instance from the specified values.
      Parameters:
      values - a sequence of bits
      size - the number of bits
      Throws:
      IllegalArgumentException - if size < 0
      IllegalArgumentException - if values.length != (size + Long.SIZE - 1) / Long.SIZE
      NullPointerException - if values == null
    • BitArray

      public BitArray(BitArray bitList)
      Constructs a new BitArray instance with the same sequence of bits and the same size as the specified BitArray.
      Parameters:
      bitList - a sequence of bits to be copied
      Throws:
      NullPointerException - if bitList == null
  • Method Details

    • size

      public int size()
      Returns the number of bits in this BitArray.
      Returns:
      the number of bits in this BitArray
    • get

      public boolean get(int index)
      Returns the specified bit as a boolean value. A 1 bit returns true and a 0 bit returns false.
      Parameters:
      index - a bit index
      Returns:
      the specified bit as a boolean value.
      Throws:
      IndexOutOfBoundsException - if index < 0 || index >= this.size()
    • getAsInt

      public int getAsInt(int index)
      Returns the specified bit as a int value.
      Parameters:
      index - a bit index
      Returns:
      the specified bit as a int value.
      Throws:
      IndexOutOfBoundsException - if index < 0 || index >= this.size()
    • set

      public void set(int index)
      Sets the specified bit.
      Parameters:
      index - a bit index
      Throws:
      IndexOutOfBoundsException - if index < 0 || index >= this.size()
    • clear

      public void clear(int index)
      Clears the specified bit.
      Parameters:
      index - a bit index
      Throws:
      IndexOutOfBoundsException - if index < 0 || index >= this.size()
    • clear

      public void clear()
      Clears all bits.
    • restrict

      public BitArray restrict(int from, int to)
      Returns a new BitArray of size (from - to) that is a copy of the specified bit indices of this BitArray.
      Parameters:
      from - the first bit to be copied (inclusive)
      to - the last bit to be copied (exclusive)
      Returns:
      a new BitArray of size (from - to) that is a copy of the specified bit indices of this BitArray
      Throws:
      IndexOutOfBoundsException - if from < 0 || from > to || to > this.size
    • copyFrom

      public void copyFrom(BitArray src, int from, int to)
      Replaced the specified bits in this Bitlist with the corresponding bits in the specified BitArray.
      Parameters:
      src - the BitArray to be copied from
      from - the first bit to be copied (inclusive)
      to - the last bit to be copied (exclusive)
      Throws:
      IndexOutOfBoundsException - if from < 0 || from > to || to > this.size || to > src.size()
      NullPointerException - if src == null
    • hash

      public int hash(int from, int to)
      Returns a hash code for the specified bits in this Bitlist
      Parameters:
      from - the first bit (inclusive)
      to - the last bit (exclusive)
      Returns:
      a hash code for the specified bits in this Bitlist
      Throws:
      IndexOutOfBoundsException - if from < 0 || from > to || to > this.size
    • longHashCode

      public static int longHashCode(long value)
    • swapBits

      public static void swapBits(BitArray a, BitArray b, int from, int to)
      Swaps the specified bits of the two specified Bitlist objects.
      Parameters:
      a - the first BitArray
      b - the second BitArray
      from - the first bit to be copied (inclusive)
      to - the last bit to be copied (exclusive)
      Throws:
      IllegalArgumentException - if s.size() != b.size()
      IndexOutOfBoundsException - if from < 0 || from > to || to > a.size()
      NullPointerException - if a == null || b == null
    • equal

      public boolean equal(BitArray other, int from, int to)
      Returns true if this Bitlist and the specified BitArray have identical sequences of bits for the specified indices, and returns false otherwise. Returns true if (from == to) && (0 <= from) && (from < other.size()).
      Parameters:
      other - the BitArray to be compared with this for equality.
      from - the first bit to be compared (inclusive)
      to - the last bit to be compared (exclusive)
      Returns:
      true if this Bitlist and the specified BitArray have identical sequences of bits for the specified indices.
      Throws:
      IndexOutOfBoundsException - if from < 0 || from > to || to > this.size || to > other.size()
      NullPointerException - if other == null
    • toLongArray

      public long[] toLongArray()
      Returns this BitArray as a long array.
      Returns:
      this BitArray as a long array
    • toString

      public String toString()
      Returns a string representation of this BitArray. The exact details of the representation are unspecified and subject to change.
      Overrides:
      toString in class Object
      Returns:
      a string representation of this BitArray.
    • equals

      public static boolean equals(BitArray a, BitArray b)
      Returns true if the specified BitArray objects represent identical bit sequences having the same size, and returns false otherwise.
      Parameters:
      a - a sequence of long values
      b - a sequence of long values
      Returns:
      true if the specified BitArray objects represent identical bit sequences having the same size
      Throws:
      NullPointerException - if a == null || b == null