cc.redberry.core.utils
Class BitArray
- java.lang.Object
-
- cc.redberry.core.utils.BitArray
-
public class BitArray extends java.lang.ObjectThis class represents an "array of booleans" with many fast and useful methods. Consumes ~ 8 times less memory than array of booleans for big sizes. Has slightly different semantics than java's built inBitSetand also provides additional functionality likeloadValueFrom(BitArray, int, int, int)andcopyOfRange(int, int).
-
-
Constructor Summary
Constructors Constructor and Description BitArray(boolean[] array)Creates a bit array from array of booleansBitArray(int size)Creates an array with specified size.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description voidand(BitArray bitArray)Compound assignment and.BitArrayappend(BitArray other)intbitCount()Returns the number of 1 bits.voidclear(int i)Clears the specified bit (sets to 0)voidclearAll()Clears all bits of this bit arrayBitArrayclone()Returns a clone of this bit array.BitArraycopyOfRange(int from)BitArraycopyOfRange(int from, int to)Returns a new bit array, containing values from the specified rangebooleanequals(java.lang.Object o)booleanget(int i)Returns the state of specified bitint[]getBits()Returns an array with positions of all "1" bits.inthashCode()booleanintersects(BitArray bitArray)Returnstrueif there are 1 bits in the same positions.booleanisEmpty()Returns true if all bits in this array are in the 0 state.booleanisFull()Returns true if all bits in this array are in the 1 state.voidloadValueFrom(BitArray bitArray)Copy values from the array of the same sizevoidloadValueFrom(BitArray bitArray, int sourceOffset, int thisOffset, int length)Analog ofSystem.arraycopy(Object, int, Object, int, int), where src isbitArray.intnextBit(int position)Returns the next "1" bit from the specified position.intnextZeroBit(int position)Returns the next "0" bit from the specified position (inclusively).voidnot()Inverts all bits in this bit arrayvoidor(BitArray bitArray)voidset(int i)Sets the specified bit (sets to 1)voidset(int i, boolean value)Sets the value of specified bit to specified valuevoidsetAll()Sets all bits of this bit arrayvoidsetAll(int[] positions, boolean value)Sets values at specified positions to specified valuevoidsetAll(IntArrayList positions, boolean value)Sets values at specified positions to specified valueintsize()Returns the length of this bit arrayBitArraytimes(int times)java.lang.StringtoString()voidxor(BitArray bitArray)Compound assignment xor.
-
-
-
Field Detail
-
EMPTY
public static final BitArray EMPTY
-
-
Constructor Detail
-
BitArray
public BitArray(int size)
Creates an array with specified size. Initial state of all bits is 0 (cleared).- Parameters:
size-
-
BitArray
public BitArray(boolean[] array)
Creates a bit array from array of booleans- Parameters:
array- boolean array
-
-
Method Detail
-
and
public void and(BitArray bitArray)
Compound assignment and.- Parameters:
bitArray- rhs of and
-
or
public void or(BitArray bitArray)
- Parameters:
bitArray- rhs of or
-
xor
public void xor(BitArray bitArray)
Compound assignment xor.- Parameters:
bitArray- rhs of xor
-
not
public void not()
Inverts all bits in this bit array
-
bitCount
public int bitCount()
Returns the number of 1 bits.- Returns:
- number of 1 bits
-
clearAll
public void clearAll()
Clears all bits of this bit array
-
clone
public BitArray clone()
Returns a clone of this bit array.- Overrides:
clonein classjava.lang.Object- Returns:
- clone of this bit array
-
intersects
public boolean intersects(BitArray bitArray)
Returnstrueif there are 1 bits in the same positions. Equivalent to!this.and(other).isEmpty()- Parameters:
bitArray- other bit array- Returns:
trueif there are 1 bits in the same positions
-
loadValueFrom
public void loadValueFrom(BitArray bitArray)
Copy values from the array of the same size- Parameters:
bitArray- bit array to copy values from
-
get
public boolean get(int i)
Returns the state of specified bit- Parameters:
i- index- Returns:
trueif bit is set,falseif bit is cleared
-
set
public void set(int i)
Sets the specified bit (sets to 1)- Parameters:
i- index of bit
-
clear
public void clear(int i)
Clears the specified bit (sets to 0)- Parameters:
i- index of bit
-
set
public void set(int i, boolean value)Sets the value of specified bit to specified value- Parameters:
i- indexvalue- value
-
setAll
public void setAll(int[] positions, boolean value)Sets values at specified positions to specified value- Parameters:
positions- positionsvalue- value
-
setAll
public void setAll(IntArrayList positions, boolean value)
Sets values at specified positions to specified value- Parameters:
positions- positionsvalue- value
-
setAll
public void setAll()
Sets all bits of this bit array
-
size
public int size()
Returns the length of this bit array- Returns:
- length of this bit array
-
isFull
public boolean isFull()
Returns true if all bits in this array are in the 1 state.- Returns:
- true if all bits in this array are in the 1 state
-
isEmpty
public boolean isEmpty()
Returns true if all bits in this array are in the 0 state.- Returns:
- true if all bits in this array are in the 0 state
-
getBits
public int[] getBits()
Returns an array with positions of all "1" bits.- Returns:
- array with positions of all "1" bits
-
nextBit
public int nextBit(int position)
Returns the next "1" bit from the specified position.- Parameters:
position- initial position- Returns:
- position of the next "1" bit of -1 if all bits after position are 0
-
nextZeroBit
public int nextZeroBit(int position)
Returns the next "0" bit from the specified position (inclusively).- Parameters:
position- initial position- Returns:
- position of the next "0" bit of -1 if all bits after position are 0
-
copyOfRange
public BitArray copyOfRange(int from, int to)
Returns a new bit array, containing values from the specified range- Parameters:
from- lower bound of rangeto- upper bound of range- Returns:
- new bit array, containing values from the specified range
-
copyOfRange
public BitArray copyOfRange(int from)
-
loadValueFrom
public void loadValueFrom(BitArray bitArray, int sourceOffset, int thisOffset, int length)
Analog ofSystem.arraycopy(Object, int, Object, int, int), where src isbitArray.- Parameters:
bitArray- sourcesourceOffset- source offsetthisOffset- destination offsetlength- number of bits to copy
-
times
public BitArray times(int times)
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
-
DataMelt 3.0 © DataMelt by jWork.ORG