Package blbutil
Class BitArray
java.lang.Object
blbutil.BitArray
Interface BitArray
represents a mutable sequence of bits
with a fixed length.
Instances of BitArray
are not thread-safe.
-
Constructor Summary
ConstructorsConstructorDescriptionBitArray
(int size) Constructs aBitArray
instance with the specifiedsize
and having all bits set to 0 (unset).BitArray
(long[] values, int size) Constructs aBitArray
instance from the specified values.Constructs a newBitArray
instance with the same sequence of bits and the same size as the specifiedBitArray
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Clears all bits.void
clear
(int index) Clears the specified bit.void
Replaced the specified bits in thisBitlist
with the corresponding bits in the specifiedBitArray
.boolean
Returnstrue
if thisBitlist
and the specifiedBitArray
have identical sequences of bits for the specified indices, and returnsfalse
otherwise.static boolean
Returnstrue
if the specifiedBitArray
objects represent identical bit sequences having the same size, and returnsfalse
otherwise.boolean
get
(int index) Returns the specified bit as aboolean
value.int
getAsInt
(int index) Returns the specified bit as aint
value.int
hash
(int from, int to) Returns a hash code for the specified bits in thisBitlist
static int
longHashCode
(long value) restrict
(int from, int to) Returns a newBitArray
of size(from - to)
that is a copy of the specified bit indices of thisBitArray
.void
set
(int index) Sets the specified bit.int
size()
Returns the number of bits in thisBitArray
.static void
Swaps the specified bits of the two specifiedBitlist
objects.long[]
Returns thisBitArray
as along
array.toString()
Returns a string representation of thisBitArray
.
-
Constructor Details
-
BitArray
public BitArray(int size) Constructs aBitArray
instance with the specifiedsize
and having all bits set to 0 (unset).- Parameters:
size
- the number of bits- Throws:
IllegalArgumentException
- ifsize < 0
-
BitArray
public BitArray(long[] values, int size) Constructs aBitArray
instance from the specified values.- Parameters:
values
- a sequence of bitssize
- the number of bits- Throws:
IllegalArgumentException
- ifsize < 0
IllegalArgumentException
- ifvalues.length != (size + Long.SIZE - 1) / Long.SIZE
NullPointerException
- ifvalues == null
-
BitArray
Constructs a newBitArray
instance with the same sequence of bits and the same size as the specifiedBitArray
.- Parameters:
bitList
- a sequence of bits to be copied- Throws:
NullPointerException
- ifbitList == null
-
-
Method Details
-
size
public int size()Returns the number of bits in thisBitArray
.- Returns:
- the number of bits in this
BitArray
-
get
public boolean get(int index) Returns the specified bit as aboolean
value. A 1 bit returnstrue
and a 0 bit returnsfalse
.- Parameters:
index
- a bit index- Returns:
- the specified bit as a
boolean
value. - Throws:
IndexOutOfBoundsException
- ifindex < 0 || index >= this.size()
-
getAsInt
public int getAsInt(int index) Returns the specified bit as aint
value.- Parameters:
index
- a bit index- Returns:
- the specified bit as a
int
value. - Throws:
IndexOutOfBoundsException
- ifindex < 0 || index >= this.size()
-
set
public void set(int index) Sets the specified bit.- Parameters:
index
- a bit index- Throws:
IndexOutOfBoundsException
- ifindex < 0 || index >= this.size()
-
clear
public void clear(int index) Clears the specified bit.- Parameters:
index
- a bit index- Throws:
IndexOutOfBoundsException
- ifindex < 0 || index >= this.size()
-
clear
public void clear()Clears all bits. -
restrict
Returns a newBitArray
of size(from - to)
that is a copy of the specified bit indices of thisBitArray
.- 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 thisBitArray
- Throws:
IndexOutOfBoundsException
- iffrom < 0 || from > to || to > this.size
-
copyFrom
Replaced the specified bits in thisBitlist
with the corresponding bits in the specifiedBitArray
.- Parameters:
src
- theBitArray
to be copied fromfrom
- the first bit to be copied (inclusive)to
- the last bit to be copied (exclusive)- Throws:
IndexOutOfBoundsException
- iffrom < 0 || from > to || to > this.size || to > src.size()
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:
IndexOutOfBoundsException
- iffrom < 0 || from > to || to > this.size
-
longHashCode
public static int longHashCode(long value) -
swapBits
Swaps the specified bits of the two specifiedBitlist
objects.- Parameters:
a
- the firstBitArray
b
- the secondBitArray
from
- the first bit to be copied (inclusive)to
- the last bit to be copied (exclusive)- Throws:
IllegalArgumentException
- ifs.size() != b.size()
IndexOutOfBoundsException
- iffrom < 0 || from > to || to > a.size()
NullPointerException
- ifa == null || b == null
-
equal
Returnstrue
if thisBitlist
and the specifiedBitArray
have identical sequences of bits for the specified indices, and returnsfalse
otherwise. Returnstrue
if(from == to) && (0 <= from) && (from < other.size())
.- Parameters:
other
- theBitArray
to be compared withthis
for equality.from
- the first bit to be compared (inclusive)to
- the last bit to be compared (exclusive)- Returns:
true
if thisBitlist
and the specifiedBitArray
have identical sequences of bits for the specified indices.- Throws:
IndexOutOfBoundsException
- iffrom < 0 || from > to || to > this.size || to > other.size()
NullPointerException
- ifother == null
-
toLongArray
public long[] toLongArray()Returns thisBitArray
as along
array.- Returns:
- this
BitArray
as along
array
-
toString
Returns a string representation of thisBitArray
. The exact details of the representation are unspecified and subject to change. -
equals
Returnstrue
if the specifiedBitArray
objects represent identical bit sequences having the same size, and returnsfalse
otherwise.- Parameters:
a
- a sequence of long valuesb
- a sequence of long values- Returns:
true
if the specifiedBitArray
objects represent identical bit sequences having the same size- Throws:
NullPointerException
- ifa == null || b == null
-