diff options
author | Erich Schubert <erich@debian.org> | 2013-10-29 20:02:37 +0100 |
---|---|---|
committer | Andrej Shadura <andrewsh@debian.org> | 2019-03-09 22:30:37 +0000 |
commit | ec7f409f6e795bbcc6f3c005687954e9475c600c (patch) | |
tree | fbf36c0ab791c556198b487ca40ae56ae5ab1ee5 /src/de/lmu/ifi/dbs/elki/data/SparseDoubleVector.java | |
parent | 974d4cf6d54cadc06258039f2cd0515cc34aeac6 (diff) | |
parent | 8300861dc4c62c5567a4e654976072f854217544 (diff) |
Import Debian changes 0.6.0~beta2-1
elki (0.6.0~beta2-1) unstable; urgency=low
* New upstream beta release.
* 3DPC extension is not yet included.
Diffstat (limited to 'src/de/lmu/ifi/dbs/elki/data/SparseDoubleVector.java')
-rw-r--r-- | src/de/lmu/ifi/dbs/elki/data/SparseDoubleVector.java | 66 |
1 files changed, 58 insertions, 8 deletions
diff --git a/src/de/lmu/ifi/dbs/elki/data/SparseDoubleVector.java b/src/de/lmu/ifi/dbs/elki/data/SparseDoubleVector.java index 1b07d5e9..110f3084 100644 --- a/src/de/lmu/ifi/dbs/elki/data/SparseDoubleVector.java +++ b/src/de/lmu/ifi/dbs/elki/data/SparseDoubleVector.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.data; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team @@ -28,10 +28,13 @@ import gnu.trove.iterator.TIntDoubleIterator; import gnu.trove.map.TIntDoubleMap; import gnu.trove.map.hash.TIntDoubleHashMap; +import java.io.IOException; +import java.nio.ByteBuffer; import java.util.Arrays; import java.util.BitSet; import de.lmu.ifi.dbs.elki.math.linearalgebra.Vector; +import de.lmu.ifi.dbs.elki.persistent.ByteArrayUtil; import de.lmu.ifi.dbs.elki.persistent.ByteBufferSerializer; import de.lmu.ifi.dbs.elki.utilities.datastructures.arraylike.ArrayAdapter; import de.lmu.ifi.dbs.elki.utilities.datastructures.arraylike.NumberArrayAdapter; @@ -47,7 +50,6 @@ import de.lmu.ifi.dbs.elki.utilities.optionhandling.AbstractParameterizer; * * @author Arthur Zimek */ -// TODO: implement ByteArraySerializer<SparseDoubleVector> public class SparseDoubleVector extends AbstractNumberVector<Double> implements SparseNumberVector<Double> { /** * Static instance. @@ -55,6 +57,11 @@ public class SparseDoubleVector extends AbstractNumberVector<Double> implements public static final SparseDoubleVector.Factory FACTORY = new SparseDoubleVector.Factory(); /** + * Serializer using varint encoding. + */ + public static final ByteBufferSerializer<SparseDoubleVector> VARIABLE_SERIALIZER = new VariableSerializer(); + + /** * Indexes of values. */ private final int[] indexes; @@ -165,7 +172,7 @@ public class SparseDoubleVector extends AbstractNumberVector<Double> implements for (int i = 0; i < values.length; i++) { double value = values[i]; if (value != 0.0f) { - this.indexes[pos] = i + 1; + this.indexes[pos] = i; this.values[pos] = value; pos++; } @@ -296,7 +303,7 @@ public class SparseDoubleVector extends AbstractNumberVector<Double> implements for (int i = 0; i < dim; i++) { values[i] = adapter.get(array, i); } - // TODO: inefficient + // TODO: improve efficiency return new SparseDoubleVector(values); } @@ -307,7 +314,7 @@ public class SparseDoubleVector extends AbstractNumberVector<Double> implements for (int i = 0; i < dim; i++) { values[i] = adapter.getDouble(array, i); } - // TODO: inefficient + // TODO: improve efficiency return new SparseDoubleVector(values); } @@ -318,10 +325,9 @@ public class SparseDoubleVector extends AbstractNumberVector<Double> implements @Override public ByteBufferSerializer<SparseDoubleVector> getDefaultSerializer() { - // FIXME: add a serializer - return null; + return VARIABLE_SERIALIZER; } - + @Override public Class<? super SparseDoubleVector> getRestrictionClass() { return SparseDoubleVector.class; @@ -355,4 +361,48 @@ public class SparseDoubleVector extends AbstractNumberVector<Double> implements * Empty map. */ public static final TIntDoubleMap EMPTYMAP = new TUnmodifiableIntDoubleMap(new TIntDoubleHashMap()); + + /** + * Serialization class using VarInt encodings. + * + * @author Erich Schubert + * + * @apiviz.uses SparseDoubleVector - - «serializes» + */ + public static class VariableSerializer implements ByteBufferSerializer<SparseDoubleVector> { + @Override + public SparseDoubleVector fromByteBuffer(ByteBuffer buffer) throws IOException { + final int dimensionality = ByteArrayUtil.readUnsignedVarint(buffer); + final int nonzero = ByteArrayUtil.readUnsignedVarint(buffer); + final int[] dims = new int[nonzero]; + final double[] values = new double[nonzero]; + for (int i = 0; i < nonzero; i++) { + dims[i] = ByteArrayUtil.readUnsignedVarint(buffer); + values[i] = buffer.getDouble(); + } + return new SparseDoubleVector(dims, values, dimensionality); + } + + @Override + public void toByteBuffer(ByteBuffer buffer, SparseDoubleVector vec) throws IOException { + ByteArrayUtil.writeUnsignedVarint(buffer, vec.dimensionality); + ByteArrayUtil.writeUnsignedVarint(buffer, vec.values.length); + for (int i = 0; i < vec.values.length; i++) { + ByteArrayUtil.writeUnsignedVarint(buffer, vec.indexes[i]); + buffer.putDouble(vec.values[i]); + } + } + + @Override + public int getByteSize(SparseDoubleVector vec) { + int sum = 0; + sum += ByteArrayUtil.getUnsignedVarintSize(vec.dimensionality); + sum += ByteArrayUtil.getUnsignedVarintSize(vec.values.length); + for (int d : vec.indexes) { + sum += ByteArrayUtil.getUnsignedVarintSize(d); + } + sum += vec.values.length * ByteArrayUtil.SIZE_DOUBLE; + return sum; + } + } } |