Class LongArrayBitVector
- All Implemented Interfaces:
BitVector,BigList<Boolean>,BooleanBigList,BooleanCollection,BooleanIterable,BooleanStack,Size64,Stack<Boolean>,Serializable,Cloneable,Comparable<BigList<? extends Boolean>>,Iterable<Boolean>,Collection<Boolean>,RandomAccess
public class LongArrayBitVector extends AbstractBitVector implements Cloneable, Serializable
The main goal of this class is to be fast and flexible. It implements a lightweight, fast, open,
optimized, reuse-oriented version of bit vectors. Instances of this class represent a bit vector
using an array of longs that is enlarged as needed when new entries are created (using
LongArrays.grow(long[], int, int)), but is never
made smaller (even on a clear()). Use trim() for that purpose.
Besides usual methods for setting and getting bits, this class provides views that make
it possible to access comfortably the bit vector in different ways: for instance,
asLongBigList(int) provide access as a list of longs, whereas AbstractBitVector.asLongSet()
provides access in setwise form.
When enlarging the underlying array (e.g., for append(long, int) operations or add
operations on the big list view), or when invoking
ensureCapacity(long), this class calls LongArrays.grow(long[], int, int), which
could enlarge the array more than expected. On the contrary, length(long) (and the
corresponding method in the big list view) sizes the underlying
array in an exact manner.
Bit numbering follows the right-to-left convention: bit k (counted from the right) of word w is bit 64w + k of the overall bit vector.
If CHECKS is true at compile time, boundary checks for all bit operations will be
compiled in. For maximum speed, you may want to recompile this class with CHECKS set to
false. CHECKS is public, so you can check from your code whether you're being provided a
version with checks or not.
Warning: A few optional methods have still to be implemented (e.g., adding an element at an arbitrary position using the list view).
Warning: In some cases, you might want to cache locally the result of
bits() to speed up computations on immutable bit vectors (this is what happens, for
instance, in static
ranking structures). This class, however, does its own serialisation of the bit vector: as a
result, all cached references to the result of bits() must be marked as transient and
rebuilt at deserialisation time, or you will end up saving the bits twice.
- See Also:
- Serialized Form
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classLongArrayBitVector.LongBigListViewA list-of-integers view of a bit vector.Nested classes/interfaces inherited from class it.unimi.dsi.bits.AbstractBitVector
AbstractBitVector.LongSetView, AbstractBitVector.SubBitVectorNested classes/interfaces inherited from class it.unimi.dsi.fastutil.booleans.AbstractBooleanBigList
AbstractBooleanBigList.BooleanSubList -
Field Summary
Fields Modifier and Type Field Description static longALL_ONESprotected long[]bitsThe backing array of this vector.static intBITS_PER_WORDstatic booleanCHECKSWhether this class has been compiled with index checks or not.static intLAST_BITstatic longLAST_BIT_MASKprotected longlengthThe number of bits in this vector.static intLOG2_BITS_PER_WORDstatic intWORD_MASK -
Constructor Summary
Constructors Modifier Constructor Description protectedLongArrayBitVector(long capacity) -
Method Summary
Modifier and Type Method Description voidadd(long index, boolean value)BitVectorand(BitVector v)Performs a logical and between this bit vector and another one, leaving the result in this vector.LongArrayBitVectorappend(long value, int width)Appends the less significant bits of a long integer to this bit vector.LongBigListasLongBigList(int width)Returns a view of this bit vector as a list of nonnegative integers of specified width.protected static intbit(long index)Returns the inside-word index of the bit that would hold the bit of specified index.long[]bits()Returns the bits in this bit vector as an array of longs, not to be modified.voidclear()Sets the size of this bit vector to 0.voidclear(long index)Clears a bit in this bit vector (optional operation).LongArrayBitVectorclone()Returns a cloned copy of this bit vector.LongArrayBitVectorcopy()Returns a copy of this bit vector.LongArrayBitVectorcopy(long from, long to)Returns a copy of a part of this bit vector.static LongArrayBitVectorcopy(BitVector bv)Returns a copy of the given bit vector.longcount()Counts the number of bits set to true in this bit vector.LongArrayBitVectorensureCapacity(long numBits)Ensures that this bit vector can hold the specified number of bits.booleanequals(LongArrayBitVector v)booleanequals(LongArrayBitVector v, long start, long end)booleanequals(Object o)LongArrayBitVectorfast()Returns this bit vector.voidfill(boolean value)Sets all bits this bit vector to the given boolean value (optional operation).voidfill(long from, long to, boolean value)Fills a range of bits in this bit vector (optional operation).voidflip()Flips all bits in this bit vector (optional operation).voidflip(long from, long to)Flips a range of bits in this bit vector (optional operation).booleangetBoolean(long index)static LongArrayBitVectorgetInstance()Creates a new empty bit vector.static LongArrayBitVectorgetInstance(long capacity)Creates a new empty bit vector of given capacity.longgetLong(long from, long to)Returns the specified bit range as a long.inthashCode()Returns a hash code for this bit vector.longlength()Returns the number of bits in this bit vector.LongArrayBitVectorlength(long newLength)Sets the number of bits in this bit vector.longlongestCommonPrefixLength(BitVector v)Returns the length of the greatest common prefix between this and the specified vector.longlongestCommonPrefixLength(LongArrayBitVector v)protected static longmask(long index)Returns a mask having a 1 exactly at the bitbit(index).longnextOne(long index)Returns the position of the first bit set at of after the given position.longnextZero(long index)Returns the position of the first bit unset after the given position.protected static intnumWords(long size)Returns the number of words that are necessary to hold the given number of bits.static LongArrayBitVectorof(int... bit)Creates a new bit vector with given bits.static LongArrayBitVectorofLength(long length)Creates a new empty bit vector of given length.BitVectoror(BitVector v)Performs a logical or between this bit vector and another one, leaving the result in this vector.longpreviousOne(long index)Returns the position of the first bit set strictly before the given position.longpreviousZero(long index)Returns the position of the first bit unset before or at the given position.booleanremoveBoolean(long index)LongArrayBitVectorreplace(BitVector bv)Replaces the content of this bit vector with another bit vector.LongArrayBitVectorreplace(LongArrayBitVector bv)voidset(long index)Sets a bit in this bit vector (optional operation).booleanset(long index, boolean value)booleantrim()Reduces as must as possible the size of the backing array.protected static intword(long index)Return the index of the word that holds a bit of specified index.static LongArrayBitVectorwrap(long[] array)Wraps the given array of longs in a bit vector.static LongArrayBitVectorwrap(long[] array, long size)Wraps the given array of longs in a bit vector for the given number of bits.BitVectorxor(BitVector v)Performs a logical xor between this bit vector and another one, leaving the result in this vector.Methods inherited from class it.unimi.dsi.bits.AbstractBitVector
add, add, add, add, append, asLongSet, clear, compareTo, compareTo, ensureIndex, ensureRestrictedIndex, equals, fill, fill, firstOne, firstZero, flip, flip, getBoolean, getInt, isPrefix, isProperPrefix, lastOne, lastZero, removeBoolean, set, set, set, size, size, size64, subVector, subVector, toStringMethods inherited from class it.unimi.dsi.fastutil.booleans.AbstractBooleanBigList
add, addAll, addAll, addAll, addAll, addAll, addAll, addElements, addElements, contains, get, getElements, indexOf, indexOf, iterator, lastIndexOf, lastIndexOf, listIterator, listIterator, peek, peekBoolean, pop, popBoolean, push, push, rem, remove, removeElements, set, subList, top, topBooleanMethods inherited from class it.unimi.dsi.fastutil.booleans.AbstractBooleanCollection
add, contains, containsAll, remove, removeAll, retainAll, toArray, toBooleanArray, toBooleanArrayMethods inherited from class java.util.AbstractCollection
containsAll, isEmpty, removeAll, retainAll, toArray, toArrayMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface it.unimi.dsi.fastutil.booleans.BooleanBigList
add, addAll, addAll, addAll, addElements, addElements, get, getElements, indexOf, indexOf, iterator, lastIndexOf, lastIndexOf, listIterator, listIterator, remove, removeElements, set, subListMethods inherited from interface it.unimi.dsi.fastutil.booleans.BooleanCollection
add, addAll, contains, contains, containsAll, rem, remove, removeAll, retainAll, toArray, toBooleanArray, toBooleanArrayMethods inherited from interface java.util.Collection
addAll, containsAll, isEmpty, parallelStream, removeAll, removeIf, retainAll, spliterator, stream, toArray, toArray, toArray
-
Field Details
-
LOG2_BITS_PER_WORD
public static final int LOG2_BITS_PER_WORD- See Also:
- Constant Field Values
-
BITS_PER_WORD
public static final int BITS_PER_WORD- See Also:
- Constant Field Values
-
WORD_MASK
public static final int WORD_MASK- See Also:
- Constant Field Values
-
LAST_BIT
public static final int LAST_BIT- See Also:
- Constant Field Values
-
ALL_ONES
public static final long ALL_ONES- See Also:
- Constant Field Values
-
LAST_BIT_MASK
public static final long LAST_BIT_MASK- See Also:
- Constant Field Values
-
CHECKS
public static final boolean CHECKSWhether this class has been compiled with index checks or not.- See Also:
- Constant Field Values
-
length
protected long lengthThe number of bits in this vector. -
bits
protected transient long[] bitsThe backing array of this vector. Bit 0 of the first element contains bit 0 of the bit vector, bit 1 of the second element contains bitBITS_PER_WORDof the bit vector and so on.
-
-
Constructor Details
-
LongArrayBitVector
protected LongArrayBitVector(long capacity)
-
-
Method Details
-
numWords
protected static final int numWords(long size)Returns the number of words that are necessary to hold the given number of bits.- Parameters:
size- a number of bits.- Returns:
- the number of words that are necessary to hold the given number of bits.
-
word
protected static final int word(long index)Return the index of the word that holds a bit of specified index.- Parameters:
index- the index of a bit, or -1.- Returns:
- the index of the word that holds the bit of given index, or -1 if
indexis -1.
-
bit
protected static final int bit(long index)Returns the inside-word index of the bit that would hold the bit of specified index.Note that bit 0 is positioned in word 0, index 0, bit 1 in word 0, index 1, …, bit
BITS_PER_WORDin word 0, index 0, bitBITS_PER_WORD+ 1 in word 1, index 1, and so on.- Parameters:
index- the index of a bit.- Returns:
- the inside-word index of the bit that would hold the bit of specified index.
-
mask
protected static final long mask(long index)Returns a mask having a 1 exactly at the bitbit(index).- Parameters:
index- the index of a bit- Returns:
- a mask having a 1 exactly at the bit
bit(index).
-
getInstance
Creates a new empty bit vector of given capacity. The resulting vector will be able to containcapacitybits without reallocations of the backing array.Note that this constructor creates an empty bit vector. If you want a cleared bit vector of a specified size, please use the
ofLength(long)factory method.- Parameters:
capacity- the capacity (in bits) of the new bit vector.- Returns:
- a new bit vector of given capacity.
-
getInstance
Creates a new empty bit vector. No allocation is actually performed.- Returns:
- a new bit vector with no capacity.
-
ofLength
Creates a new empty bit vector of given length.- Parameters:
length- the size (in bits) of the new bit vector.
-
of
Creates a new bit vector with given bits.- Parameters:
bit- a list of bits that will be set in the newly created bit vector.
-
bits
public long[] bits()Description copied from interface:BitVectorReturns the bits in this bit vector as an array of longs, not to be modified.- Specified by:
bitsin interfaceBitVector- Overrides:
bitsin classAbstractBitVector- Returns:
- an array of longs whose first
BitVector.length()bits contain the bits of this bit vector. The array cannot be modified.
-
length
public long length()Description copied from interface:BitVectorReturns the number of bits in this bit vector.If the number of bits in this vector is smaller than or equal to
Integer.MAX_VALUE, this method is semantically equivalent toList.size(). In any case, this method is semantically equivalent toSize64.size64(), but it is prefererred. -
ensureCapacity
Ensures that this bit vector can hold the specified number of bits.This method uses
LongArrays.grow(long[], int, int)to ensure that there is enough space for the given number of bits. As a consequence, the actual length of the long array allocated might be larger than expected.- Parameters:
numBits- the number of bits that this vector must be able to contain.- Returns:
- this bit vector.
-
length
Description copied from interface:BitVectorSets the number of bits in this bit vector.It is expected that this method will try to allocate exactly the necessary space.
If the argument fits an integer, this method has the same side effects of
BooleanList.size(int). In any case, this method has the same side effects ofBigList.size(long), but it is preferred, as it has the advantage of returning this bit vector, thus making it possible to chain methods.- Specified by:
lengthin interfaceBitVector- Overrides:
lengthin classAbstractBitVector- Returns:
- this bit vector.
-
fill
public void fill(boolean value)Description copied from interface:BitVectorSets all bits this bit vector to the given boolean value (optional operation).- Specified by:
fillin interfaceBitVector- Overrides:
fillin classAbstractBitVector- Parameters:
value- the value (true or false).
-
fill
public void fill(long from, long to, boolean value)Description copied from interface:BitVectorFills a range of bits in this bit vector (optional operation).- Specified by:
fillin interfaceBitVector- Overrides:
fillin classAbstractBitVector- Parameters:
from- the first index (inclusive).to- the last index (not inclusive).value- the value (true or false).
-
flip
public void flip()Description copied from interface:BitVectorFlips all bits in this bit vector (optional operation).- Specified by:
flipin interfaceBitVector- Overrides:
flipin classAbstractBitVector
-
flip
public void flip(long from, long to)Description copied from interface:BitVectorFlips a range of bits in this bit vector (optional operation).- Specified by:
flipin interfaceBitVector- Overrides:
flipin classAbstractBitVector- Parameters:
from- the first index (inclusive).to- the last index (not inclusive).
-
trim
public boolean trim()Reduces as must as possible the size of the backing array.- Returns:
- true if some trimming was actually necessary.
-
clear
public void clear()Sets the size of this bit vector to 0.Note that this method does not try to reallocate that backing array. If you want to force that behaviour, call
trim()afterwards.- Specified by:
clearin interfaceCollection<Boolean>- Overrides:
clearin classAbstractBitVector
-
copy
Description copied from interface:BitVectorReturns a copy of a part of this bit vector.- Specified by:
copyin interfaceBitVector- Overrides:
copyin classAbstractBitVector- Parameters:
from- the starting bit, inclusive.to- the ending bit, not inclusive.- Returns:
- a copy of the part of this bit vector going from bit
from(inclusive) to bitto(not inclusive)
-
copy
Description copied from interface:BitVectorReturns a copy of this bit vector.- Specified by:
copyin interfaceBitVector- Overrides:
copyin classAbstractBitVector- Returns:
- a copy of this bit vector.
-
fast
Returns this bit vector.- Specified by:
fastin interfaceBitVector- Overrides:
fastin classAbstractBitVector- Returns:
- this bit vector.
-
copy
Returns a copy of the given bit vector.This method uses
BitVector.getLong(long, long)onLong.SIZEboundaries to copy at high speed.- Parameters:
bv- a bit vector.- Returns:
- an instance of this class containing a copy of the given vector.
-
getBoolean
public boolean getBoolean(long index)- Specified by:
getBooleanin interfaceBooleanBigList
-
set
public boolean set(long index, boolean value)- Specified by:
setin interfaceBooleanBigList- Overrides:
setin classAbstractBitVector
-
set
public void set(long index)Description copied from interface:BitVectorSets a bit in this bit vector (optional operation).- Specified by:
setin interfaceBitVector- Overrides:
setin classAbstractBitVector- Parameters:
index- the index of a bit.
-
clear
public void clear(long index)Description copied from interface:BitVectorClears a bit in this bit vector (optional operation).- Specified by:
clearin interfaceBitVector- Overrides:
clearin classAbstractBitVector- Parameters:
index- the index of a bit.
-
add
public void add(long index, boolean value)- Specified by:
addin interfaceBooleanBigList- Overrides:
addin classAbstractBitVector
-
removeBoolean
public boolean removeBoolean(long index)- Specified by:
removeBooleanin interfaceBooleanBigList- Overrides:
removeBooleanin classAbstractBitVector
-
append
Description copied from interface:BitVectorAppends the less significant bits of a long integer to this bit vector.- Specified by:
appendin interfaceBitVector- Overrides:
appendin classAbstractBitVector- Parameters:
value- a value to be appendedwidth- the number of less significant bits to be added to this bit vector.- Returns:
- this bit vector.
-
getLong
public long getLong(long from, long to)Description copied from interface:BitVectorReturns the specified bit range as a long.Note that bit 0 of the returned long will be bit
fromof this bit vector.Implementations are invited to provide high-speed implementations for the case in which
fromis a multiple ofLong.SIZEandtoisfrom+Long.SIZE(or less, in case the vector length is exceeded). This behaviour make it possible to implement high-speed hashing, copies, etc.- Specified by:
getLongin interfaceBitVector- Overrides:
getLongin classAbstractBitVector- Parameters:
from- the starting bit (inclusive).to- the ending bit (exclusive).- Returns:
- the long value contained in the specified bits.
-
count
public long count()Description copied from interface:BitVectorCounts the number of bits set to true in this bit vector.- Specified by:
countin interfaceBitVector- Overrides:
countin classAbstractBitVector- Returns:
- the number of bits set to true in this bit vector.
-
nextOne
public long nextOne(long index)Description copied from interface:BitVectorReturns the position of the first bit set at of after the given position.- Specified by:
nextOnein interfaceBitVector- Overrides:
nextOnein classAbstractBitVector- Returns:
- the position of the first bit set at or after position
index, or -1 if no such bit exists.
-
previousOne
public long previousOne(long index)Description copied from interface:BitVectorReturns the position of the first bit set strictly before the given position.- Specified by:
previousOnein interfaceBitVector- Overrides:
previousOnein classAbstractBitVector- Returns:
- the position of the first bit set strictly before position
index, or -1 if no such bit exists.
-
nextZero
public long nextZero(long index)Description copied from interface:BitVectorReturns the position of the first bit unset after the given position.- Specified by:
nextZeroin interfaceBitVector- Overrides:
nextZeroin classAbstractBitVector- Returns:
- the first bit unset after position
index(inclusive), or -1 if no such bit exists.
-
previousZero
public long previousZero(long index)Description copied from interface:BitVectorReturns the position of the first bit unset before or at the given position.- Specified by:
previousZeroin interfaceBitVector- Overrides:
previousZeroin classAbstractBitVector- Returns:
- the first bit unset before or at the given position, or -1 if no such bit exists.
-
longestCommonPrefixLength
Description copied from interface:BitVectorReturns the length of the greatest common prefix between this and the specified vector.- Specified by:
longestCommonPrefixLengthin interfaceBitVector- Overrides:
longestCommonPrefixLengthin classAbstractBitVector- Parameters:
v- a bit vector.- Returns:
- the length of the greatest common prefix.
-
longestCommonPrefixLength
-
and
Description copied from interface:BitVectorPerforms a logical and between this bit vector and another one, leaving the result in this vector.- Specified by:
andin interfaceBitVector- Overrides:
andin classAbstractBitVector- Parameters:
v- a bit vector.- Returns:
- this bit vector.
-
or
Description copied from interface:BitVectorPerforms a logical or between this bit vector and another one, leaving the result in this vector.- Specified by:
orin interfaceBitVector- Overrides:
orin classAbstractBitVector- Parameters:
v- a bit vector.- Returns:
- this bit vector.
-
xor
Description copied from interface:BitVectorPerforms a logical xor between this bit vector and another one, leaving the result in this vector.- Specified by:
xorin interfaceBitVector- Overrides:
xorin classAbstractBitVector- Parameters:
v- a bit vector.- Returns:
- this bit vector.
-
wrap
Wraps the given array of longs in a bit vector for the given number of bits.Note that all bits in
arraybeyond that of indexsizemust be unset, or an exception will be thrown.- Parameters:
array- an array of longs.size- the number of bits of the newly created bit vector.- Returns:
- a bit vector of size
sizeusingarrayas backing array.
-
wrap
Wraps the given array of longs in a bit vector.- Parameters:
array- an array of longs.- Returns:
- a bit vector of size
array.length * Long.SIZEusingarrayas backing array.
-
clone
Returns a cloned copy of this bit vector.This method is functionally equivalent to
copy(), except thatcopy()trims the backing array.- Overrides:
clonein classObject- Returns:
- a copy of this bit vector.
- Throws:
CloneNotSupportedException
-
replace
-
replace
Description copied from interface:BitVectorReplaces the content of this bit vector with another bit vector.- Specified by:
replacein interfaceBitVector- Overrides:
replacein classAbstractBitVector- Parameters:
bv- a bit vector.- Returns:
- this bit vector.
-
hashCode
public int hashCode()Description copied from interface:BitVectorReturns a hash code for this bit vector.Hash codes for bit vectors are defined as follows:
final long length = length(); long fullLength = length - length % Long.SIZE; long h = 0x9e3779b97f4a7c13L ^ length; for(long i = 0; i < fullLength; i += Long.SIZE) h ^= (h << 5) + getLong(i, i + Long.SIZE) + (h >>> 2); if (length != fullLength) h ^= (h << 5) + getLong(fullLength, length) + (h >>> 2); (int)((h >>> 32) ^ h);
The last value is the hash code of the bit vector. This hashing is based on shift-add-xor hashing (M.V. Ramakrishna and Justin Zobel, “Performance in practice of string hashing functions”, Proc. of the Fifth International Conference on Database Systems for Advanced Applications, 1997, pages 215−223).
The returned value is not a high-quality hash such as Jenkins's, but it can be computed very quickly; in any case, 32 bits are too few for a high-quality hash to be used in large-scale applications.
Important: all bit vector implementations are required to return the value defined here. The simplest way to obtain this result is to subclass
AbstractBitVector.- Specified by:
hashCodein interfaceBitVector- Specified by:
hashCodein interfaceCollection<Boolean>- Overrides:
hashCodein classAbstractBitVector- Returns:
- a hash code for this bit vector.
-
equals
- Specified by:
equalsin interfaceCollection<Boolean>- Overrides:
equalsin classAbstractBitVector
-
equals
-
equals
-
asLongBigList
Description copied from interface:BitVectorReturns a view of this bit vector as a list of nonnegative integers of specified width.More formally,
getLong(p)will return the nonnegative integer defined by the bits starting atp * width(bit 0, inclusive) and ending at(p + 1) * width(bitwidth− 1, exclusive).- Specified by:
asLongBigListin interfaceBitVector- Overrides:
asLongBigListin classAbstractBitVector
-