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 an array of longs that is enlarged as needed when new entries
are created (by dividing the current length by the golden ratio), 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) size 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.
Modifier and Type | Class and Description |
---|---|
protected static class |
LongArrayBitVector.LongBigListView
A list-of-integers view of a bit vector.
|
AbstractBitVector.LongSetView, AbstractBitVector.SubBitVector
Modifier and Type | Field and Description |
---|---|
static long |
ALL_ONES |
protected long[] |
bits
The backing array of this vector.
|
static int |
BITS_PER_WORD |
static boolean |
CHECKS
Whether this class has been compiled with index checks or not.
|
static int |
LAST_BIT |
static long |
LAST_BIT_MASK |
protected long |
length
The number of bits in this vector.
|
static int |
LOG2_BITS_PER_WORD |
static int |
WORD_MASK |
Modifier | Constructor and Description |
---|---|
protected |
LongArrayBitVector(long capacity) |
Modifier and Type | Method and Description |
---|---|
void |
add(long index,
boolean value)
Adds a bit with specified value at the specified index (optional operation).
|
BitVector |
and(BitVector v)
Performs a logical and between this bit vector and another one, leaving the result in this vector.
|
LongArrayBitVector |
append(long value,
int width)
Appends the less significant bits of a long integer to this bit vector.
|
LongBigList |
asLongBigList(int width)
Returns a view of this bit vector as a list of nonnegative integers of specified width.
|
protected static int |
bit(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.
|
void |
clear()
Sets the size of this bit vector to 0.
|
void |
clear(long index)
Clears a bit in this bit vector (optional operation).
|
LongArrayBitVector |
clone()
Returns a cloned copy of this bit vector.
|
LongArrayBitVector |
copy()
Returns a copy of this bit vector.
|
static LongArrayBitVector |
copy(BitVector bv)
Returns a copy of the given bit vector.
|
LongArrayBitVector |
copy(long from,
long to)
Returns a copy of a part of this bit vector.
|
long |
count()
Counts the number of bits set to true in this bit vector.
|
LongArrayBitVector |
ensureCapacity(long numBits)
Ensures that this bit vector can hold the specified number of bits.
|
boolean |
equals(LongArrayBitVector v) |
boolean |
equals(Object o) |
LongArrayBitVector |
fast()
Returns this bit vector.
|
void |
fill(boolean value)
Sets all bits this bit vector to the given boolean value (optional operation).
|
void |
fill(long from,
long to,
boolean value)
Fills a range of bits in this bit vector (optional operation).
|
void |
flip()
Flips all bits in this bit vector (optional operation).
|
boolean |
getBoolean(long index)
Returns the value of the specified bit.
|
static LongArrayBitVector |
getInstance()
Creates a new empty bit vector.
|
static LongArrayBitVector |
getInstance(long capacity)
Creates a new empty bit vector of given capacity.
|
long |
getLong(long from,
long to)
Returns the specified bit range as a long.
|
int |
hashCode()
Returns a hash code for this bit vector.
|
long |
length()
Returns the number of bits in this bit vector.
|
LongArrayBitVector |
length(long newLength)
Sets the number of bits in this bit vector.
|
long |
longestCommonPrefixLength(BitVector v)
Returns the length of the greatest common prefix between this and the specified vector.
|
long |
longestCommonPrefixLength(LongArrayBitVector v) |
protected static long |
mask(long index)
Returns a mask having a 1 exactly at the bit
bit(index) . |
long |
nextOne(long index)
Returns the position of the first bit set after the given position.
|
long |
nextZero(long index)
Returns the position of the first bit unset after the given position.
|
protected static int |
numWords(long size)
Returns the number of words that are necessary to hold the given number of bits.
|
static LongArrayBitVector |
of(int... bit)
Creates a new bit vector with given bits.
|
static LongArrayBitVector |
ofLength(long length)
Creates a new empty bit vector of given length.
|
BitVector |
or(BitVector v)
Performs a logical or between this bit vector and another one, leaving the result in this vector.
|
long |
previousOne(long index)
Returns the position of the first bit set before or at the given position.
|
long |
previousZero(long index)
Returns the position of the first bit unset before or at the given position.
|
boolean |
removeBoolean(long index)
Removes a bit with specified index (optional operation).
|
LongArrayBitVector |
replace(BitVector bv)
Replaces the content of this bit vector with another bit vector.
|
LongArrayBitVector |
replace(LongArrayBitVector bv) |
void |
set(long index)
Sets a bit in this bit vector (optional operation).
|
boolean |
set(long index,
boolean value)
Sets the value of the specified bit (optional operation).
|
boolean |
trim()
Reduces as must as possible the size of the backing array.
|
protected static int |
word(long index)
Return the index of the word that holds a bit of specified index.
|
static LongArrayBitVector |
wrap(long[] array)
Wraps the given array of longs in a bit vector.
|
static LongArrayBitVector |
wrap(long[] array,
long size)
Wraps the given array of longs in a bit vector for the given number of bits.
|
BitVector |
xor(BitVector v)
Performs a logical xor between this bit vector and another one, leaving the result in this vector.
|
add, add, add, add, append, asLongSet, clear, compareTo, compareTo, ensureIndex, ensureRestrictedIndex, fill, fill, firstOne, firstZero, flip, flip, flip, getBoolean, getInt, lastOne, lastZero, removeBoolean, set, set, set, size, size, subList, subVector, subVector, toString
add, addAll, addAll, addAll, addAll, addAll, addAll, addElements, addElements, booleanListIterator, booleanListIterator, booleanSubList, contains, ensureIndex, ensureRestrictedIndex, get, getElements, indexOf, indexOf, iterator, lastIndexOf, lastIndexOf, listIterator, listIterator, peek, peekBoolean, pop, popBoolean, push, push, rem, remove, remove, removeElements, set, top, topBoolean
add, booleanIterator, contains, containsAll, containsAll, isEmpty, rem, removeAll, removeAll, retainAll, retainAll, toArray, toArray, toArray, toBooleanArray, toBooleanArray
finalize, getClass, notify, notifyAll, wait, wait, wait
addAll, addAll, addAll, addElements, addElements, booleanListIterator, booleanListIterator, booleanSubList, getElements, indexOf, iterator, lastIndexOf, listIterator, listIterator, removeElements
add, add, addAll, addAll, contains, containsAll, get, indexOf, isEmpty, lastIndexOf, remove, remove, removeAll, retainAll, set, toArray, toArray
public static final int LOG2_BITS_PER_WORD
public static final int BITS_PER_WORD
public static final int WORD_MASK
public static final int LAST_BIT
public static final long ALL_ONES
public static final long LAST_BIT_MASK
public static final boolean CHECKS
protected long length
protected transient long[] bits
BITS_PER_WORD
of the bit vector and so on.protected static final int numWords(long size)
size
- a number of bits.protected static final int word(long index)
index
- the index of a bit, or -1.index
is -1.protected static final int bit(long index)
Note that bit 0 is positioned in word 0, index 0, bit 1 in word 0, index 1, …,
bit BITS_PER_WORD
in word 0, index 0, bit BITS_PER_WORD
+ 1 in word 1, index 1,
and so on.
index
- the index of a bit.protected static final long mask(long index)
bit(index)
.index
- the index of a bitbit(index)
.public static LongArrayBitVector getInstance(long capacity)
capacity
bits 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.
capacity
- the capacity (in bits) of the new bit vector.public static LongArrayBitVector getInstance()
public static LongArrayBitVector ofLength(long length)
length
- the size (in bits) of the new bit vector.public static LongArrayBitVector of(int... bit)
bit
- a list of bits that will be set in the newly created bit vector.public long[] bits()
BitVector
bits
in interface BitVector
bits
in class AbstractBitVector
BitVector.length()
bits contain the bits of
this bit vector. The array cannot be modified.public long length()
BitVector
If the number of bits in this vector is smaller than or equal to Integer.MAX_VALUE
, this
method is semantically equivalent to List.size()
.
public LongArrayBitVector ensureCapacity(long numBits)
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.
numBits
- the number of bits that this vector must be able to contain.public LongArrayBitVector length(long newLength)
BitVector
It is expected that this method will try to allocate exactly the necessary space.
If the number of bits in this vector is smaller than
or equal to Integer.MAX_VALUE
, this
method is semantically equivalent to BooleanList.size(int)
.
length
in interface BitVector
length
in class AbstractBitVector
public void fill(boolean value)
BitVector
fill
in interface BitVector
fill
in class AbstractBitVector
public void fill(long from, long to, boolean value)
BitVector
fill
in interface BitVector
fill
in class AbstractBitVector
from
- the first index (inclusive).to
- the last index (not inclusive).value
- the value (true or false).public void flip()
BitVector
flip
in interface BitVector
flip
in class AbstractBitVector
public boolean trim()
public void clear()
Note that this method does not try to reallocate that backing array.
If you want to force that behaviour, call trim()
afterwards.
clear
in interface Collection<Boolean>
clear
in interface List<Boolean>
clear
in class AbstractBitVector
public LongArrayBitVector copy(long from, long to)
BitVector
copy
in interface BitVector
copy
in class AbstractBitVector
from
- the starting bit, inclusive.to
- the ending bit, not inclusive.from
(inclusive) to bit to
(not inclusive)public LongArrayBitVector copy()
BitVector
copy
in interface BitVector
copy
in class AbstractBitVector
public LongArrayBitVector fast()
fast
in interface BitVector
fast
in class AbstractBitVector
public static LongArrayBitVector copy(BitVector bv)
This method uses BitVector.getLong(long, long)
on Long.SIZE
boundaries to copy at high speed.
bv
- a bit vector.public boolean getBoolean(long index)
BitVector
This method is semantically equivalent to BooleanList.getBoolean(int)
,
but it gives access to long indices.
getBoolean
in interface BitVector
index
- the index of a bit.public boolean set(long index, boolean value)
BitVector
This method is semantically equivalent to BooleanList.set(int,boolean)
,
but it gives access to long indices.
set
in interface BitVector
set
in class AbstractBitVector
index
- the index of a bit.value
- the new value.public void set(long index)
BitVector
set
in interface BitVector
set
in class AbstractBitVector
index
- the index of a bit.public void clear(long index)
BitVector
clear
in interface BitVector
clear
in class AbstractBitVector
index
- the index of a bit.public void add(long index, boolean value)
BitVector
This method is semantically equivalent to BooleanList.add(int,boolean)
,
but it gives access to long indices.
add
in interface BitVector
add
in class AbstractBitVector
index
- the index of a bit.value
- the value that will be inserted at position index
.public boolean removeBoolean(long index)
BitVector
This method is semantically equivalent to BooleanList.removeBoolean(int)
,
but it gives access to long indices.
removeBoolean
in interface BitVector
removeBoolean
in class AbstractBitVector
index
- the index of a bit.public LongArrayBitVector append(long value, int width)
BitVector
append
in interface BitVector
append
in class AbstractBitVector
value
- a value to be appendedwidth
- the number of less significant bits to be added to this bit vector.public long getLong(long from, long to)
BitVector
Note that bit 0 of the returned long will be bit from
of this bit vector.
Implementations are invited to provide high-speed implementations for
the case in which from
is a multiple of Long.SIZE
and to
is from
+ Long.SIZE
(or less,
in case the vector length is exceeded). This behaviour make it possible to
implement high-speed hashing, copies, etc.
getLong
in interface BitVector
getLong
in class AbstractBitVector
from
- the starting bit (inclusive).to
- the ending bit (exclusive).public long count()
BitVector
count
in interface BitVector
count
in class AbstractBitVector
public long nextOne(long index)
BitVector
nextOne
in interface BitVector
nextOne
in class AbstractBitVector
index
(inclusive), or -1 if no such bit exists.public long previousOne(long index)
BitVector
previousOne
in interface BitVector
previousOne
in class AbstractBitVector
public long nextZero(long index)
BitVector
nextZero
in interface BitVector
nextZero
in class AbstractBitVector
index
(inclusive), or -1 if no such bit exists.public long previousZero(long index)
BitVector
previousZero
in interface BitVector
previousZero
in class AbstractBitVector
public long longestCommonPrefixLength(BitVector v)
BitVector
longestCommonPrefixLength
in interface BitVector
longestCommonPrefixLength
in class AbstractBitVector
v
- a bit vector.public long longestCommonPrefixLength(LongArrayBitVector v)
public BitVector and(BitVector v)
BitVector
and
in interface BitVector
and
in class AbstractBitVector
v
- a bit vector.public BitVector or(BitVector v)
BitVector
or
in interface BitVector
or
in class AbstractBitVector
v
- a bit vector.public BitVector xor(BitVector v)
BitVector
xor
in interface BitVector
xor
in class AbstractBitVector
v
- a bit vector.public static LongArrayBitVector wrap(long[] array, long size)
Note that all bits in array
beyond that of index
size
must be unset, or an exception will be thrown.
array
- an array of longs.size
- the number of bits of the newly created bit vector.size
using array
as backing array.public static LongArrayBitVector wrap(long[] array)
array
- an array of longs.array.length * Long.SIZE
using array
as backing array.public LongArrayBitVector clone() throws CloneNotSupportedException
This method is functionally equivalent to copy()
,
except that copy()
trims the backing array.
clone
in class Object
CloneNotSupportedException
public LongArrayBitVector replace(LongArrayBitVector bv)
public LongArrayBitVector replace(BitVector bv)
BitVector
replace
in interface BitVector
replace
in class AbstractBitVector
bv
- a bit vector.public boolean equals(Object o)
equals
in interface Collection<Boolean>
equals
in interface List<Boolean>
equals
in class AbstractBitVector
public boolean equals(LongArrayBitVector v)
public int hashCode()
BitVector
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
.
hashCode
in interface BitVector
hashCode
in interface Collection<Boolean>
hashCode
in interface List<Boolean>
hashCode
in class AbstractBitVector
public LongBigList asLongBigList(int width)
BitVector
More formally, getLong(p)
will return
the nonnegative integer defined by the bits starting at p * width
(bit 0, inclusive)
and ending at (p + 1) * width
(bit width
− 1, exclusive).
asLongBigList
in interface BitVector
asLongBigList
in class AbstractBitVector
Copyright © 2006–2019 SYSTAP, LLC DBA Blazegraph. All rights reserved.