Package blbutil
Class BitList
- java.lang.Object
-
- blbutil.BitList
-
public class BitList extends java.lang.ObjectInterface
BitListrepresents a mutable sequence of bits with a fixed length.Instances of
BitListare not thread-safe.
-
-
Constructor Summary
Constructors Constructor Description BitList(int size)Constructs aBitListinstance with the specifiedsizeand having all bits set to 0 (unset).BitList(long[] values, int size)Constructs aBitListinstance from the specified values.BitList(BitList bitList)Constructs a newBitListinstance with the same sequence of bits and the same size as the specifiedBitList.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Clears all bits.voidclear(int index)Clears the specified bit.voidcopyFrom(BitList src, int from, int to)Replaced the specified bits in thisBitlistwith the corresponding bits in the specifiedBitList.booleanequal(BitList other, int from, int to)Returnstrueif thisBitlistand the specifiedBitListhave identical sequences of bits for the specified indices, and returnsfalseotherwise.static booleanequals(BitList a, BitList b)Returnstrueif the specifiedBitListobjects represent identical bit sequences having the same size, and returnsfalseotherwise.booleanget(int index)Returns the specified bit as abooleanvalue.intgetAsInt(int index)Returns the specified bit as aintvalue.inthash(int from, int to)Returns a hash code for the specified bits in thisBitlistBitListrestrict(int from, int to)Returns a newBitListof size(from - to)that is a copy of the specified bit indices of thisBitList.voidset(int index)Sets the specified bit.intsize()Returns the number of bits in thisBitList.static voidswapBits(BitList a, BitList b, int from, int to)Swaps the specified bits of the two specifiedBitlistobjects.long[]toLongArray()Returns thisBitListas alongarray.java.lang.StringtoString()Returns a string representation of thisBitList.
-
-
-
Constructor Detail
-
BitList
public BitList(int size)
Constructs aBitListinstance with the specifiedsizeand having all bits set to 0 (unset).- Parameters:
size- the number of bits- Throws:
java.lang.IllegalArgumentException- ifsize < 0
-
BitList
public BitList(long[] values, int size)Constructs aBitListinstance from the specified values.- Parameters:
values- a sequence of bitssize- the number of bits- Throws:
java.lang.IllegalArgumentException- ifsize < 0java.lang.IllegalArgumentException- ifvalues.length != (size + Long.SIZE - 1) / Long.SIZEjava.lang.NullPointerException- ifvalues == null
-
BitList
public BitList(BitList bitList)
Constructs a newBitListinstance with the same sequence of bits and the same size as the specifiedBitList.- Parameters:
bitList- a sequence of bits to be copied- Throws:
java.lang.NullPointerException- ifbitList == null
-
-
Method Detail
-
size
public int size()
Returns the number of bits in thisBitList.- Returns:
- the number of bits in this
BitList
-
get
public boolean get(int index)
Returns the specified bit as abooleanvalue. A 1 bit returnstrueand a 0 bit returnsfalse.- Parameters:
index- a bit index- Returns:
- the specified bit as a
booleanvalue. - Throws:
java.lang.IndexOutOfBoundsException- ifindex < 0 || index >= this.size()
-
getAsInt
public int getAsInt(int index)
Returns the specified bit as aintvalue.- Parameters:
index- a bit index- Returns:
- the specified bit as a
intvalue. - Throws:
java.lang.IndexOutOfBoundsException- ifindex < 0 || index >= this.size()
-
set
public void set(int index)
Sets the specified bit.- Parameters:
index- a bit index- Throws:
java.lang.IndexOutOfBoundsException- ifindex < 0 || index >= this.size()
-
clear
public void clear(int index)
Clears the specified bit.- Parameters:
index- a bit index- Throws:
java.lang.IndexOutOfBoundsException- ifindex < 0 || index >= this.size()
-
clear
public void clear()
Clears all bits.
-
restrict
public BitList restrict(int from, int to)
Returns a newBitListof size(from - to)that is a copy of the specified bit indices of thisBitList.- Parameters:
from- the first bit to be copied (inclusive)to- the last bit to be copied (exclusive)- Returns:
- a new
BitListof size(from - to)that is a copy of the specified bit indices of thisBitList - Throws:
java.lang.IndexOutOfBoundsException- iffrom < 0 || from > to || to > this.size
-
copyFrom
public void copyFrom(BitList src, int from, int to)
Replaced the specified bits in thisBitlistwith the corresponding bits in the specifiedBitList.- Parameters:
src- theBitListto be copied fromfrom- the first bit to be copied (inclusive)to- the last bit to be copied (exclusive)- Throws:
java.lang.IndexOutOfBoundsException- iffrom < 0 || from > to || to > this.size || to > src.size()java.lang.NullPointerException- ifsrc == null
-
hash
public int hash(int from, int to)Returns a hash code for the specified bits in thisBitlist- 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- iffrom < 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 specifiedBitlistobjects.- Parameters:
a- the firstBitListb- the secondBitListfrom- the first bit to be copied (inclusive)to- the last bit to be copied (exclusive)- Throws:
java.lang.IllegalArgumentException- ifs.size() != b.size()java.lang.IndexOutOfBoundsException- iffrom < 0 || from > to || to > a.size()java.lang.NullPointerException- ifa == null || b == null
-
equal
public boolean equal(BitList other, int from, int to)
Returnstrueif thisBitlistand the specifiedBitListhave identical sequences of bits for the specified indices, and returnsfalseotherwise.- Parameters:
other- theBitListto be compared withthisfor equality.from- the first bit to be compared (inclusive)to- the last bit to be compared (exclusive)- Returns:
trueif thisBitlistand the specifiedBitListhave identical sequences of bits for the specified indices.- Throws:
java.lang.IndexOutOfBoundsException- iffrom < 0 || from > to || to > this.size || to > other.size()java.lang.NullPointerException- ifother == null
-
toLongArray
public long[] toLongArray()
Returns thisBitListas alongarray.- Returns:
- this
BitListas alongarray
-
toString
public java.lang.String toString()
Returns a string representation of thisBitList. The exact details of the representation are unspecified and subject to change.- Overrides:
toStringin classjava.lang.Object- Returns:
- a string representation of this
BitList.
-
equals
public static boolean equals(BitList a, BitList b)
Returnstrueif the specifiedBitListobjects represent identical bit sequences having the same size, and returnsfalseotherwise.- Parameters:
a- a sequence of long valuesb- a sequence of long values- Returns:
trueif the specifiedBitListobjects represent identical bit sequences having the same size- Throws:
java.lang.NullPointerException- ifa == null || b == null
-
-