Class AbstractBitVector
- java.lang.Object
-
- java.util.AbstractCollection<java.lang.Boolean>
-
- it.unimi.dsi.fastutil.booleans.AbstractBooleanCollection
-
- it.unimi.dsi.fastutil.booleans.AbstractBooleanBigList
-
- it.unimi.dsi.bits.AbstractBitVector
-
- All Implemented Interfaces:
BitVector,it.unimi.dsi.fastutil.BigList<java.lang.Boolean>,it.unimi.dsi.fastutil.booleans.BooleanBigList,it.unimi.dsi.fastutil.booleans.BooleanCollection,it.unimi.dsi.fastutil.booleans.BooleanIterable,it.unimi.dsi.fastutil.booleans.BooleanStack,it.unimi.dsi.fastutil.Size64,it.unimi.dsi.fastutil.Stack<java.lang.Boolean>,java.lang.Comparable<it.unimi.dsi.fastutil.BigList<? extends java.lang.Boolean>>,java.lang.Iterable<java.lang.Boolean>,java.util.Collection<java.lang.Boolean>,java.util.RandomAccess
- Direct Known Subclasses:
AbstractBitVector.SubBitVector,BooleanListBitVector,LongArrayBitVector,LongBigArrayBitVector
public abstract class AbstractBitVector extends it.unimi.dsi.fastutil.booleans.AbstractBooleanBigList implements BitVector
An abstract implementation of aBitVector.This abstract implementation provides almost all methods: you have to provide just
BooleanList.getBoolean(int)andBitVector.length(). No attributes are defined.Note that the integer-set view provided by
asLongSet()is not cached: if you want to cache the result of the first call, you must do your own caching.Warning: this class has several optimised methods that assume that
getLong(long, long)is implemented efficiently when its arguments are multiples ofLong.SIZE(see, e.g., the implementation ofcompareTo(BitVector)andlongestCommonPrefixLength(BitVector)). If you want speed up the processing of your ownBitVectorimplementations, just implementgetLong(long, long)so that it is fast under the above conditions.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classAbstractBitVector.LongBigListViewA list-of-integers view of a bit vector.static classAbstractBitVector.LongSetViewA view of a bit vector as a sorted set of long integers.static classAbstractBitVector.SubBitVectorA subvector of a given bit vector, specified by an initial and a final bit.
-
Constructor Summary
Constructors Constructor Description AbstractBitVector()
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description booleanadd(boolean value)voidadd(int value)Adds a bit with specified value at the end of this bit vector.voidadd(int index, boolean value)voidadd(long index, boolean value)voidadd(long index, int value)Adds a bit with specified integer value at the specified index (optional operation).BitVectorand(BitVector v)Performs a logical and between this bit vector and another one, leaving the result in this vector.BitVectorappend(long value, int k)Appends the less significant bits of a long integer to this bit vector.BitVectorappend(BitVector bv)Appends another bit vector to this bit vector.it.unimi.dsi.fastutil.longs.LongBigListasLongBigList(int width)Returns a view of this bit vector as a list of nonnegative integers of specified width.it.unimi.dsi.fastutil.longs.LongSortedSetasLongSet()Returns a view of this bit vector as a sorted set of long integers.long[]bits()Returns the bits in this bit vector as an array of longs, not to be modified.voidclear()voidclear(int index)voidclear(long index)Clears a bit in this bit vector (optional operation).intcompareTo(BitVector v)Compares lexicographically this bit vector with another bit vector.intcompareTo(it.unimi.dsi.fastutil.BigList<? extends java.lang.Boolean> list)BitVectorcopy()Returns a copy of this bit vector.BitVectorcopy(long from, long to)Returns a copy of a part of this bit vector.longcount()Counts the number of bits set to true in this bit vector.protected voidensureIndex(long index)protected voidensureRestrictedIndex(long index)booleanequals(BitVector v, long start, long end)Checks for equality with a segment of another vector.booleanequals(java.lang.Object o)BitVectorfast()Returns an instance ofLongArrayBitVectorcontaining a copy of this bit vector.voidfill(boolean value)Sets all bits this bit vector to the given boolean value (optional operation).voidfill(int value)Sets all bits this bit vector to the given integer value (optional operation).voidfill(long from, long to, boolean value)Fills a range of bits in this bit vector (optional operation).voidfill(long from, long to, int value)Clears a range of bits in this bit vector (optional operation).longfirstOne()Returns the position of the first bit set in this bit vector.longfirstZero()Returns the position of the first bit unset in this bit vector.voidflip()Flips all bits in this bit vector (optional operation).voidflip(int index)voidflip(long index)Flips a bit in this bit vector (optional operation).voidflip(long from, long to)Flips a range of bits in this bit vector (optional operation).booleangetBoolean(int index)intgetInt(long index)Returns the value of the specified bit as an integer.longgetLong(long from, long to)Returns the specified bit range as a long.inthashCode()Returns a hash code for this bit vector.booleanisPrefix(BitVector v)Returns true if this bit vector is a prefix of the specified bit vector.booleanisProperPrefix(BitVector v)Returns true if this bit vector is a proper prefix of the specified bit vector.longlastOne()Returns the position of the last bit set in this bit vector.longlastZero()Returns the position of the last bit unset in this bit vector.BitVectorlength(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 bit vector.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.BitVectoror(BitVector v)Performs a logical or between this bit vector and another one, leaving the result in this bit 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(int index)booleanremoveBoolean(long index)BitVectorreplace(BitVector bv)Replaces the content of this bit vector with another bit vector.voidset(int index)booleanset(int index, boolean value)voidset(long index)Sets a bit in this bit vector (optional operation).booleanset(long index, boolean value)voidset(long index, int value)Sets the value of the specified bit as an integer (optional operation).intsize()Deprecated.voidsize(long newSize)longsize64()BitVectorsubVector(long from)Returns a subvector view specified by initial index and running up to the end of this bit vector.BitVectorsubVector(long from, long to)Returns a subvector view specified by initial and final index.java.lang.StringtoString()Returns a string representation of this vector.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.fastutil.booleans.AbstractBooleanBigList
add, addAll, addAll, addAll, addAll, addElements, addElements, contains, forEach, get, getElements, indexOf, indexOf, iterator, lastIndexOf, lastIndexOf, listIterator, listIterator, peek, peekBoolean, pop, popBoolean, push, push, rem, remove, removeElements, set, setElements, subList, top, topBoolean
-
Methods inherited from class it.unimi.dsi.fastutil.booleans.AbstractBooleanCollection
add, contains, containsAll, containsAll, remove, removeAll, removeAll, retainAll, retainAll, toArray, toBooleanArray, toBooleanArray
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface it.unimi.dsi.fastutil.booleans.BooleanBigList
add, addAll, addAll, addAll, addAll, addAll, addElements, addElements, get, getBoolean, getElements, getElements, indexOf, indexOf, iterator, lastIndexOf, lastIndexOf, listIterator, listIterator, remove, removeElements, set, setElements, setElements, setElements, spliterator, subList
-
Methods inherited from interface it.unimi.dsi.fastutil.booleans.BooleanCollection
add, addAll, contains, contains, containsAll, rem, remove, removeAll, removeIf, removeIf, retainAll, toArray, toBooleanArray, toBooleanArray
-
-
-
-
Method Detail
-
ensureRestrictedIndex
protected void ensureRestrictedIndex(long index)
- Overrides:
ensureRestrictedIndexin classit.unimi.dsi.fastutil.booleans.AbstractBooleanBigList
-
ensureIndex
protected void ensureIndex(long index)
- Overrides:
ensureIndexin classit.unimi.dsi.fastutil.booleans.AbstractBooleanBigList
-
set
public void set(int index)
- Implementation Specification:
- This implementation delegates to
set(long, boolean).
-
clear
public void clear(int index)
- Implementation Specification:
- This implementation delegates to
set(long, boolean).
-
flip
public void flip(int index)
- Implementation Specification:
- This implementation delegates to
flip(long).
-
set
public void set(long index)
Description copied from interface:BitVectorSets a bit in this bit vector (optional operation).
-
clear
public void clear(long index)
Description copied from interface:BitVectorClears a bit in this bit vector (optional operation).
-
flip
public void flip(long index)
Description copied from interface:BitVectorFlips a bit in this bit vector (optional operation).
-
fill
public void fill(boolean value)
Description copied from interface:BitVectorSets all bits this bit vector to the given boolean value (optional operation).
-
fill
public void fill(int value)
Description copied from interface:BitVectorSets all bits this bit vector to the given integer value (optional operation).
-
flip
public void flip()
Description copied from interface:BitVectorFlips all bits in this bit vector (optional operation).
-
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).
-
fill
public void fill(long from, long to, int value)Description copied from interface:BitVectorClears a range of bits in this bit vector (optional operation).
-
flip
public void flip(long from, long to)Description copied from interface:BitVectorFlips a range of bits in this bit vector (optional operation).
-
getInt
public int getInt(long index)
Description copied from interface:BitVectorReturns the value of the specified bit as an integer.This method is a useful synonym for
BooleanBigList.getBoolean(long).
-
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.
-
getBoolean
public boolean getBoolean(int index)
- Implementation Specification:
- This implementation delegates to
BooleanBigList.getBoolean(long).
-
removeBoolean
public boolean removeBoolean(int index)
- Implementation Specification:
- This implementation delegates to
removeBoolean(long).
-
set
public boolean set(int index, boolean value)- Implementation Specification:
- This implementation delegates to
set(long, boolean).
-
add
public void add(int index, boolean value)- Implementation Specification:
- This implementation delegates to
add(long, boolean).
-
removeBoolean
public boolean removeBoolean(long index)
- Specified by:
removeBooleanin interfaceit.unimi.dsi.fastutil.booleans.BooleanBigList- Overrides:
removeBooleanin classit.unimi.dsi.fastutil.booleans.AbstractBooleanBigList
-
set
public boolean set(long index, boolean value)- Specified by:
setin interfaceit.unimi.dsi.fastutil.booleans.BooleanBigList- Overrides:
setin classit.unimi.dsi.fastutil.booleans.AbstractBooleanBigList
-
add
public void add(long index, boolean value)- Specified by:
addin interfaceit.unimi.dsi.fastutil.booleans.BooleanBigList- Overrides:
addin classit.unimi.dsi.fastutil.booleans.AbstractBooleanBigList
-
set
public void set(long index, int value)Description copied from interface:BitVectorSets the value of the specified bit as an integer (optional operation).This method is a useful synonym for
BooleanBigList.set(long, boolean).
-
add
public void add(long index, int value)Description copied from interface:BitVectorAdds a bit with specified integer value at the specified index (optional operation).This method is a useful synonym for
BooleanBigList.add(long, boolean).
-
add
public boolean add(boolean value)
- Specified by:
addin interfaceit.unimi.dsi.fastutil.booleans.BooleanCollection- Overrides:
addin classit.unimi.dsi.fastutil.booleans.AbstractBooleanBigList
-
add
public void add(int value)
Description copied from interface:BitVectorAdds a bit with specified value at the end of this bit vector.This method is a useful synonym for
BooleanList.add(boolean).
-
append
public BitVector append(long value, int k)
Description copied from interface:BitVectorAppends the less significant bits of a long integer to this bit vector.
-
append
public BitVector append(BitVector bv)
Description copied from interface:BitVectorAppends another bit vector to this bit vector.
-
copy
public BitVector copy()
Description copied from interface:BitVectorReturns a copy of this bit vector.
-
copy
public BitVector copy(long from, long to)
Description copied from interface:BitVectorReturns a copy of a part of this bit vector.
-
fast
public BitVector fast()
Returns an instance ofLongArrayBitVectorcontaining a copy of this bit vector.- Specified by:
fastin interfaceBitVector- Returns:
- an instance of
LongArrayBitVectorcontaining a copy of this bit vector.
-
count
public long count()
Description copied from interface:BitVectorCounts the number of bits set to true in this bit vector.
-
firstOne
public long firstOne()
Description copied from interface:BitVectorReturns the position of the first bit set in this bit vector.
-
lastOne
public long lastOne()
Description copied from interface:BitVectorReturns the position of the last bit set in this bit vector.
-
firstZero
public long firstZero()
Description copied from interface:BitVectorReturns the position of the first bit unset in this bit vector.
-
lastZero
public long lastZero()
Description copied from interface:BitVectorReturns the position of the last bit unset 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.
-
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- Parameters:
index- a bit position.- 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.
-
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- Parameters:
index- a bit position.- Returns:
- the first bit unset before or at the given position, or -1 if no such bit exists.
-
longestCommonPrefixLength
public long longestCommonPrefixLength(BitVector v)
Description copied from interface:BitVectorReturns the length of the greatest common prefix between this and the specified bit vector.- Specified by:
longestCommonPrefixLengthin interfaceBitVector- Parameters:
v- a bit vector.- Returns:
- the length of the greatest common prefix.
-
isPrefix
public boolean isPrefix(BitVector v)
Description copied from interface:BitVectorReturns true if this bit vector is a prefix of the specified bit vector.
-
isProperPrefix
public boolean isProperPrefix(BitVector v)
Description copied from interface:BitVectorReturns true if this bit vector is a proper prefix of the specified bit vector.- Specified by:
isProperPrefixin interfaceBitVector- Parameters:
v- a bit vector.- Returns:
- true if this bit vector is a proper prefix of
v(i.e., it is a prefix but not equal).
-
and
public BitVector and(BitVector v)
Description copied from interface:BitVectorPerforms a logical and between this bit vector and another one, leaving the result in this vector.
-
or
public BitVector or(BitVector v)
Description copied from interface:BitVectorPerforms a logical or between this bit vector and another one, leaving the result in this bit vector.
-
xor
public BitVector xor(BitVector v)
Description copied from interface:BitVectorPerforms a logical xor between this bit vector and another one, leaving the result in this vector.
-
size
@Deprecated public int size()
Deprecated.Description copied from interface:BitVector- Specified by:
sizein interfaceit.unimi.dsi.fastutil.BigList<java.lang.Boolean>- Specified by:
sizein interfaceBitVector- Specified by:
sizein interfacejava.util.Collection<java.lang.Boolean>- Specified by:
sizein interfaceit.unimi.dsi.fastutil.Size64- Overrides:
sizein classit.unimi.dsi.fastutil.booleans.AbstractBooleanBigList
-
size64
public long size64()
- Specified by:
size64in interfaceit.unimi.dsi.fastutil.Size64- Implementation Specification:
- This implementation just returns
BitVector.length().
-
clear
public void clear()
- Specified by:
clearin interfacejava.util.Collection<java.lang.Boolean>- Overrides:
clearin classit.unimi.dsi.fastutil.booleans.AbstractBooleanBigList
-
replace
public BitVector replace(BitVector bv)
Replaces the content of this bit vector with another bit vector.
-
equals
public boolean equals(java.lang.Object o)
- Specified by:
equalsin interfacejava.util.Collection<java.lang.Boolean>- Overrides:
equalsin classit.unimi.dsi.fastutil.booleans.AbstractBooleanBigList
-
equals
public boolean equals(BitVector v, long start, long end)
Description copied from interface:BitVectorChecks for equality with a segment of another 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 & -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.
-
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- Returns:
- an array of longs whose first
BitVector.length()bits contain the bits of this bit vector. The array cannot be modified.
-
length
public BitVector length(long newLength)
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.
-
size
public void size(long newSize)
- Specified by:
sizein interfaceit.unimi.dsi.fastutil.BigList<java.lang.Boolean>- Overrides:
sizein classit.unimi.dsi.fastutil.booleans.AbstractBooleanBigList
-
asLongSet
public it.unimi.dsi.fastutil.longs.LongSortedSet asLongSet()
Description copied from interface:BitVectorReturns a view of this bit vector as a sorted set of long integers.More formally, this bit vector is infinitely extended to the left with zeros (e.g., all bits beyond
BitVector.length(long)are considered zeroes). The resulting infinite string is interpreted as the characteristic function of a set of integers.Note that, in particular, the resulting string representation is exactly that of a
BitSet.
-
asLongBigList
public it.unimi.dsi.fastutil.longs.LongBigList asLongBigList(int width)
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- Parameters:
width- a bit width.- Returns:
- a view of this bit vector as a list of nonnegative integers of specified width.
-
subVector
public BitVector subVector(long from, long to)
Description copied from interface:BitVectorReturns a subvector view specified by initial and final index.The object returned by this method is a bit vector representing a view of this bit vector restricted to the given indices. Changes to the subvector will be reflected in the main vector.
-
subVector
public BitVector subVector(long from)
Description copied from interface:BitVectorReturns a subvector view specified by initial index and running up to the end of this bit vector.- Specified by:
subVectorin interfaceBitVector- Parameters:
from- the first index (inclusive).- Returns:
- a subvector view specified by initial index and running up to the end of this bit vector.
- See Also:
BitVector.subVector(long, long)
-
compareTo
public int compareTo(it.unimi.dsi.fastutil.BigList<? extends java.lang.Boolean> list)
- Specified by:
compareToin interfacejava.lang.Comparable<it.unimi.dsi.fastutil.BigList<? extends java.lang.Boolean>>- Overrides:
compareToin classit.unimi.dsi.fastutil.booleans.AbstractBooleanBigList
-
compareTo
public int compareTo(BitVector v)
Compares lexicographically this bit vector with another bit vector.- Parameters:
v- another bit vector.- Returns:
- -1, 0, or 1, depending on the lexicographical order of the two vectors.
- Implementation Notes:
- This method compares the two bit vectors by extracting a 64-bit word at a time using
BitVector.getLong(long, long).
-
toString
public java.lang.String toString()
Returns a string representation of this vector.Note that this string representation shows the bit of index 0 at the leftmost position.
- Overrides:
toStringin classit.unimi.dsi.fastutil.booleans.AbstractBooleanBigList- Returns:
- a string representation of this vector, with the bit of index 0 on the left.
-
-