Package elki.data
Class BitVector
- java.lang.Object
-
- elki.data.BitVector
-
- All Implemented Interfaces:
FeatureVector<java.lang.Number>,NumberVector,SparseFeatureVector<java.lang.Number>,SparseNumberVector,SpatialComparable
public class BitVector extends java.lang.Object implements SparseNumberVector
Vector using a dense bit set encoding, based onlong[]storage.- Since:
- 0.1
- Author:
- Arthur Zimek
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classBitVector.FactoryFactory for bit vectors.static classBitVector.ShortSerializerSerialization class for dense integer vectors with up toShort.MAX_VALUEdimensions, by using a short for storing the dimensionality.
-
Field Summary
Fields Modifier and Type Field Description private long[]bitsStoring the bits.private intdimensionalityDimensionality of this bit vector.static BitVector.FactoryFACTORYStatic instance.static ByteBufferSerializer<BitVector>SHORT_SERIALIZERSerializer for up to 2^15-1 dimensions.-
Fields inherited from interface elki.data.FeatureVector
TYPE
-
Fields inherited from interface elki.data.NumberVector
ATTRIBUTE_SEPARATOR, FIELD_1D, FIELD_2D
-
Fields inherited from interface elki.data.SparseNumberVector
FIELD, VARIABLE_LENGTH
-
-
Constructor Summary
Constructors Constructor Description BitVector(long[] bits, int dimensionality)Create a new BitVector corresponding to the specified bits and of the specified dimensionality.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidandOnto(long[] v)Combine onto v using the AND operation, i.e.booleanbooleanValue(int dimension)Get the value of a single bit.intcardinality()Compute the vector cardinality (uncached!)long[]cloneBits()Returns a copy of the bits currently set in this BitVector.booleancontains(long[] bitset)Returns whether this BitVector contains all bits that are set to true in the specified BitSet.doubledoubleValue(int dimension)Returns the value in the specified dimension as double.booleanequals(java.lang.Object obj)Indicates whether some other object is "equal to" this BitVector.intgetDimensionality()The dimensionality of the vector space where of this FeatureVector of V is an element.BitgetValue(int dimension)Deprecated.inthammingDistance(BitVector v2)Compute the Hamming distance of two bit vectors.inthashCode()booleanintersect(BitVector v2)Compute whether two vectors intersect.intintersectionSize(BitVector v2)Compute the vector intersection size.intiter()Iterator over non-zero features only, ascendingly.intiterAdvance(int iter)Advance the iterator to the next position.intiterDim(int iter)Get the dimension an iterator points to.doubleiterDoubleValue(int iter)Get the value of the iterators' current dimension.longiterLongValue(int iter)Get the value of the iterators' current dimension.intiterRetract(int iter)Retract the iterator to the next position.booleaniterValid(int iter)Test the iterator position for validity.doublejaccardSimilarity(BitVector v2)Compute the Jaccard similarity of two bit vectors.longlongValue(int dimension)Returns the value in the specified dimension as long.voidorOnto(long[] v)Combine onto v using the OR operation, i.e.voidsetDimensionality(int dimensionality)Update the vector space dimensionality.double[]toArray()Returns a Vector representing in one column andgetDimensionality()rows the values of this BitVector as double values.java.lang.StringtoString()Returns a String representation of this BitVector.intunionSize(BitVector v2)Compute the vector union size.voidxorOnto(long[] v)Combine onto v using the XOR operation, i.e.-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface elki.data.NumberVector
getMax, getMin
-
Methods inherited from interface elki.data.SparseNumberVector
byteValue, floatValue, intValue, iterByteValue, iterFloatValue, iterIntValue, iterShortValue, shortValue
-
-
-
-
Field Detail
-
FACTORY
public static final BitVector.Factory FACTORY
Static instance.
-
SHORT_SERIALIZER
public static final ByteBufferSerializer<BitVector> SHORT_SERIALIZER
Serializer for up to 2^15-1 dimensions.
-
bits
private final long[] bits
Storing the bits.
-
dimensionality
private int dimensionality
Dimensionality of this bit vector.
-
-
Method Detail
-
getDimensionality
public int getDimensionality()
Description copied from interface:FeatureVectorThe dimensionality of the vector space where of this FeatureVector of V is an element.- Specified by:
getDimensionalityin interfaceFeatureVector<java.lang.Number>- Specified by:
getDimensionalityin interfaceSpatialComparable- Returns:
- the number of dimensions of this FeatureVector of V
-
setDimensionality
public void setDimensionality(int dimensionality)
Description copied from interface:SparseNumberVectorUpdate the vector space dimensionality.- Specified by:
setDimensionalityin interfaceSparseNumberVector- Parameters:
dimensionality- New dimensionality
-
booleanValue
public boolean booleanValue(int dimension)
Get the value of a single bit.- Parameters:
dimension- Bit number to get- Returns:
truewhen set
-
getValue
@Deprecated public Bit getValue(int dimension)
Deprecated.Description copied from interface:FeatureVectorReturns the value in the specified dimension.- Specified by:
getValuein interfaceFeatureVector<java.lang.Number>- Specified by:
getValuein interfaceNumberVector- Parameters:
dimension- the desired dimension, where 0 ≤ dimension ≤this.getDimensionality()-1- Returns:
- the value in the specified dimension
-
doubleValue
public double doubleValue(int dimension)
Description copied from interface:NumberVectorReturns the value in the specified dimension as double.Note: this might seem redundant with respect to
getValue(dim).doubleValue(), but usually this is much more efficient due to boxing/unboxing cost.- Specified by:
doubleValuein interfaceNumberVector- Specified by:
doubleValuein interfaceSparseNumberVector- Parameters:
dimension- the desired dimension, where 0 ≤ dimension <this.getDimensionality()- Returns:
- the value in the specified dimension
-
longValue
public long longValue(int dimension)
Description copied from interface:NumberVectorReturns the value in the specified dimension as long.Note: this might seem redundant with respect to
getValue(dim).longValue(), but usually this is much more efficient due to boxing/unboxing cost.- Specified by:
longValuein interfaceNumberVector- Specified by:
longValuein interfaceSparseNumberVector- Parameters:
dimension- the desired dimension, where 0 ≤ dimension <this.getDimensionality()- Returns:
- the value in the specified dimension
-
iter
public int iter()
Description copied from interface:SparseNumberVectorIterator over non-zero features only, ascendingly.Note: depending on the underlying implementation, this may or may not be the dimension. Use
SparseFeatureVector.iterDim(int)to get the actual dimension. In fact, usually this will be the ith non-zero value, assuming an array representation.Think of this number as an iterator. For efficiency, it has a primitive type!
Intended usage:
for (int iter = v.iter(); v.iterValid(iter); iter = v.iterAdvance(iter)) { final int dim = v.iterDim(iter); final double val = v.iterDoubleValue(iter); // Do something. }- Specified by:
iterin interfaceSparseFeatureVector<java.lang.Number>- Specified by:
iterin interfaceSparseNumberVector- Returns:
- Identifier for the first non-zero dimension, not necessarily the dimension!
-
iterAdvance
public int iterAdvance(int iter)
Description copied from interface:SparseFeatureVectorAdvance the iterator to the next position.- Specified by:
iterAdvancein interfaceSparseFeatureVector<java.lang.Number>- Parameters:
iter- Previous iterator position- Returns:
- Next iterator position
-
iterRetract
public int iterRetract(int iter)
Description copied from interface:SparseFeatureVectorRetract the iterator to the next position.- Specified by:
iterRetractin interfaceSparseFeatureVector<java.lang.Number>- Parameters:
iter- Next iterator position- Returns:
- Previous iterator position
-
iterValid
public boolean iterValid(int iter)
Description copied from interface:SparseFeatureVectorTest the iterator position for validity.- Specified by:
iterValidin interfaceSparseFeatureVector<java.lang.Number>- Parameters:
iter- Iterator position- Returns:
truewhen it refers to a valid position.
-
iterDim
public int iterDim(int iter)
Description copied from interface:SparseFeatureVectorGet the dimension an iterator points to.- Specified by:
iterDimin interfaceSparseFeatureVector<java.lang.Number>- Parameters:
iter- Iterator position- Returns:
- Dimension the iterator refers to
-
iterDoubleValue
public double iterDoubleValue(int iter)
Description copied from interface:SparseNumberVectorGet the value of the iterators' current dimension.- Specified by:
iterDoubleValuein interfaceSparseNumberVector- Parameters:
iter- Iterator- Returns:
- Value at the current position
-
iterLongValue
public long iterLongValue(int iter)
Description copied from interface:SparseNumberVectorGet the value of the iterators' current dimension.- Specified by:
iterLongValuein interfaceSparseNumberVector- Parameters:
iter- Iterator- Returns:
- Value at the current position
-
toArray
public double[] toArray()
Returns a Vector representing in one column andgetDimensionality()rows the values of this BitVector as double values.- Specified by:
toArrayin interfaceNumberVector- Returns:
- a Matrix representing in one column and
getDimensionality()rows the values of this BitVector as double values - See Also:
NumberVector.toArray()
-
contains
public boolean contains(long[] bitset)
Returns whether this BitVector contains all bits that are set to true in the specified BitSet.- Parameters:
bitset- the bits to inspect in this BitVector- Returns:
- true if this BitVector contains all bits that are set to true in the specified BitSet, false otherwise
-
cloneBits
public long[] cloneBits()
Returns a copy of the bits currently set in this BitVector.- Returns:
- a copy of the bits currently set in this BitVector
-
cardinality
public int cardinality()
Compute the vector cardinality (uncached!)- Returns:
- Vector cardinality
-
jaccardSimilarity
public double jaccardSimilarity(BitVector v2)
Compute the Jaccard similarity of two bit vectors.- Parameters:
v2- Second bit vector- Returns:
- Jaccard similarity (intersection / union)
-
hammingDistance
public int hammingDistance(BitVector v2)
Compute the Hamming distance of two bit vectors.- Parameters:
v2- Second bit vector- Returns:
- Hamming distance (number of bits difference)
-
intersectionSize
public int intersectionSize(BitVector v2)
Compute the vector intersection size.- Parameters:
v2- Second bit vector- Returns:
- Intersection size (number of bits in both)
-
unionSize
public int unionSize(BitVector v2)
Compute the vector union size.- Parameters:
v2- Second bit vector- Returns:
- Intersection size (number of bits in both)
-
intersect
public boolean intersect(BitVector v2)
Compute whether two vectors intersect.- Parameters:
v2- Second bit vector- Returns:
trueif they intersect in at least one bit.
-
andOnto
public void andOnto(long[] v)
Combine onto v using the AND operation, i.e.v &= this.- Parameters:
v- Existing bit set of same length.
-
orOnto
public void orOnto(long[] v)
Combine onto v using the OR operation, i.e.v |= this.- Parameters:
v- Existing bit set of same length.
-
xorOnto
public void xorOnto(long[] v)
Combine onto v using the XOR operation, i.e.v ^= this.- Parameters:
v- Existing bit set of same length.
-
toString
public java.lang.String toString()
Returns a String representation of this BitVector. The representation is suitable to be parsed byBitVectorLabelParser.- Specified by:
toStringin interfaceFeatureVector<java.lang.Number>- Overrides:
toStringin classjava.lang.Object- Returns:
- a String representation of the FeatureVector of V
-
equals
public boolean equals(java.lang.Object obj)
Indicates whether some other object is "equal to" this BitVector. This BitVector is equal to the given object, if the object is a BitVector of same dimensionality and with identical bits set.- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
-