Package blbutil

Class BitList


  • public class BitList
    extends java.lang.Object

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

    Instances of BitList are not thread-safe.

    • Constructor Summary

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

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Clears all bits.
      void clear​(int index)
      Clears the specified bit.
      void copyFrom​(BitList src, int from, int to)
      Replaced the specified bits in this Bitlist with the corresponding bits in the specified BitList.
      boolean equal​(BitList other, int from, int to)
      Returns true if this Bitlist and the specified BitList have identical sequences of bits for the specified indices, and returns false otherwise.
      static boolean equals​(BitList a, BitList b)
      Returns true if the specified BitList 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
      BitList restrict​(int from, int to)
      Returns a new BitList of size (from - to) that is a copy of the specified bit indices of this BitList.
      void set​(int index)
      Sets the specified bit.
      int size()
      Returns the number of bits in this BitList.
      static void swapBits​(BitList a, BitList b, int from, int to)
      Swaps the specified bits of the two specified Bitlist objects.
      long[] toLongArray()
      Returns this BitList as a long array.
      java.lang.String toString()
      Returns a string representation of this BitList.
      • Methods inherited from class java.lang.Object

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

      • BitList

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

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

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

      • size

        public int size()
        Returns the number of bits in this BitList.
        Returns:
        the number of bits in this BitList
      • 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:
        java.lang.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:
        java.lang.IndexOutOfBoundsException - if index < 0 || index >= this.size()
      • set

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

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

        public void clear()
        Clears all bits.
      • restrict

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

        public void copyFrom​(BitList src,
                             int from,
                             int to)
        Replaced the specified bits in this Bitlist with the corresponding bits in the specified BitList.
        Parameters:
        src - the BitList to be copied from
        from - the first bit to be copied (inclusive)
        to - the last bit to be copied (exclusive)
        Throws:
        java.lang.IndexOutOfBoundsException - if from < 0 || from > to || to > this.size || to > src.size()
        java.lang.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:
        java.lang.IndexOutOfBoundsException - if from < 0 || from > to || to > this.size
      • swapBits

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

        public boolean equal​(BitList other,
                             int from,
                             int to)
        Returns true if this Bitlist and the specified BitList have identical sequences of bits for the specified indices, and returns false otherwise.
        Parameters:
        other - the BitList 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 BitList have identical sequences of bits for the specified indices.
        Throws:
        java.lang.IndexOutOfBoundsException - if from < 0 || from > to || to > this.size || to > other.size()
        java.lang.NullPointerException - if other == null
      • toLongArray

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

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

        public static boolean equals​(BitList a,
                                     BitList b)
        Returns true if the specified BitList 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 BitList objects represent identical bit sequences having the same size
        Throws:
        java.lang.NullPointerException - if a == null || b == null