diff options
author | Erich Schubert <erich@debian.org> | 2012-12-14 20:45:15 +0100 |
---|---|---|
committer | Andrej Shadura <andrewsh@debian.org> | 2019-03-09 22:30:35 +0000 |
commit | 357b2761a2c0ded8cad5e4d3c1e667b7639ff7a6 (patch) | |
tree | 3dd8947bb70a67c221adc3cd4359ba1d385e2f3c /src/de/lmu/ifi/dbs/elki/data/SparseFloatVector.java | |
parent | 4343785ebed9d4145f417d86d581f18a0d31e4ac (diff) | |
parent | b7b404fd7a726774d442562d11659d7b5368cdb9 (diff) |
Import Debian changes 0.5.5-1
elki (0.5.5-1) unstable; urgency=low
* New upstream release: 0.5.5 interim release.
Diffstat (limited to 'src/de/lmu/ifi/dbs/elki/data/SparseFloatVector.java')
-rw-r--r-- | src/de/lmu/ifi/dbs/elki/data/SparseFloatVector.java | 189 |
1 files changed, 103 insertions, 86 deletions
diff --git a/src/de/lmu/ifi/dbs/elki/data/SparseFloatVector.java b/src/de/lmu/ifi/dbs/elki/data/SparseFloatVector.java index 36a4e171..de3e7293 100644 --- a/src/de/lmu/ifi/dbs/elki/data/SparseFloatVector.java +++ b/src/de/lmu/ifi/dbs/elki/data/SparseFloatVector.java @@ -33,8 +33,8 @@ import gnu.trove.map.hash.TIntFloatHashMap; import java.util.Arrays; import java.util.BitSet; -import de.lmu.ifi.dbs.elki.datasource.parser.SparseNumberVectorLabelParser; import de.lmu.ifi.dbs.elki.math.linearalgebra.Vector; +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; import de.lmu.ifi.dbs.elki.utilities.optionhandling.AbstractParameterizer; @@ -50,21 +50,21 @@ import de.lmu.ifi.dbs.elki.utilities.optionhandling.AbstractParameterizer; * @author Arthur Zimek */ // TODO: implement ByteArraySerializer<SparseFloatVector> -public class SparseFloatVector extends AbstractNumberVector<SparseFloatVector, Float> implements SparseNumberVector<SparseFloatVector, Float> { +public class SparseFloatVector extends AbstractNumberVector<Float> implements SparseNumberVector<Float> { /** - * Static instance + * Static instance. */ - public static final SparseFloatVector STATIC = new SparseFloatVector(new int[0], new float[0], -1); + public static final SparseFloatVector.Factory FACTORY = new SparseFloatVector.Factory(); /** - * Indexes of values + * Indexes of values. */ - private int[] indexes; + private final int[] indexes; /** - * Stored values + * Stored values. */ - private float[] values; + private final float[] values; /** * The dimensionality of this feature vector. @@ -96,7 +96,7 @@ public class SparseFloatVector extends AbstractNumberVector<SparseFloatVector, F * zero is bigger than the given dimensionality) */ public SparseFloatVector(TIntFloatMap values, int dimensionality) throws IllegalArgumentException { - if(values.size() > dimensionality) { + if (values.size() > dimensionality) { throw new IllegalArgumentException("values.size() > dimensionality!"); } @@ -105,21 +105,21 @@ public class SparseFloatVector extends AbstractNumberVector<SparseFloatVector, F // Import and sort the indexes { TIntFloatIterator iter = values.iterator(); - for(int i = 0; iter.hasNext(); i++) { - this.indexes[i] = iter.key(); + for (int i = 0; iter.hasNext(); i++) { iter.advance(); + this.indexes[i] = iter.key(); } Arrays.sort(this.indexes); } // Import the values accordingly { - for(int i = 0; i < values.size(); i++) { + for (int i = 0; i < values.size(); i++) { this.values[i] = values.get(this.indexes[i]); } } this.dimensionality = dimensionality; final int maxdim = getMaxDim(); - if(maxdim > dimensionality) { + if (maxdim > dimensionality) { throw new IllegalArgumentException("Given dimensionality " + dimensionality + " is too small w.r.t. the given values (occurring maximum: " + maxdim + ")."); } } @@ -130,10 +130,9 @@ public class SparseFloatVector extends AbstractNumberVector<SparseFloatVector, F * @return the maximum dimensionality seen */ private int getMaxDim() { - if(this.indexes.length == 0) { + if (this.indexes.length == 0) { return 0; - } - else { + } else { return this.indexes[this.indexes.length - 1]; } } @@ -153,8 +152,8 @@ public class SparseFloatVector extends AbstractNumberVector<SparseFloatVector, F // Count the number of non-zero entries int size = 0; { - for(int i = 0; i < values.length; i++) { - if(values[i] != 0.0f) { + for (int i = 0; i < values.length; i++) { + if (values[i] != 0.0f) { size++; } } @@ -165,9 +164,9 @@ public class SparseFloatVector extends AbstractNumberVector<SparseFloatVector, F // Copy the values { int pos = 0; - for(int i = 0; i < values.length; i++) { + for (int i = 0; i < values.length; i++) { float value = values[i]; - if(value != 0.0f) { + if (value != 0.0f) { this.indexes[pos] = i + 1; this.values[pos] = value; pos++; @@ -193,19 +192,19 @@ public class SparseFloatVector extends AbstractNumberVector<SparseFloatVector, F @Override public void setDimensionality(int dimensionality) throws IllegalArgumentException { final int maxdim = getMaxDim(); - if(maxdim > dimensionality) { + if (maxdim > dimensionality) { throw new IllegalArgumentException("Given dimensionality " + dimensionality + " is too small w.r.t. the given values (occurring maximum: " + maxdim + ")."); } this.dimensionality = dimensionality; } @Override + @Deprecated public Float getValue(int dimension) { int pos = Arrays.binarySearch(this.indexes, dimension); - if(pos >= 0) { + if (pos >= 0) { return values[pos]; - } - else { + } else { return 0.0f; } } @@ -213,10 +212,9 @@ public class SparseFloatVector extends AbstractNumberVector<SparseFloatVector, F @Override public double doubleValue(int dimension) { int pos = Arrays.binarySearch(this.indexes, dimension); - if(pos >= 0) { + if (pos >= 0) { return values[pos]; - } - else { + } else { return 0.0; } } @@ -224,24 +222,23 @@ public class SparseFloatVector extends AbstractNumberVector<SparseFloatVector, F @Override public long longValue(int dimension) { int pos = Arrays.binarySearch(this.indexes, dimension); - if(pos >= 0) { + if (pos >= 0) { return (long) values[pos]; - } - else { + } else { return 0; } } @Override public Vector getColumnVector() { - double[] values = getValues(); - return new Vector(values); + return new Vector(getValues()); } /** * <p> * Provides a String representation of this SparseFloatVector as suitable for - * {@link SparseNumberVectorLabelParser}. + * {@link de.lmu.ifi.dbs.elki.datasource.parser.SparseNumberVectorLabelParser} + * . * </p> * * <p> @@ -263,7 +260,7 @@ public class SparseFloatVector extends AbstractNumberVector<SparseFloatVector, F public String toString() { StringBuilder featureLine = new StringBuilder(); featureLine.append(this.indexes.length); - for(int i = 0; i < this.indexes.length; i++) { + for (int i = 0; i < this.indexes.length; i++) { featureLine.append(ATTRIBUTE_SEPARATOR); featureLine.append(this.indexes[i]); featureLine.append(ATTRIBUTE_SEPARATOR); @@ -279,78 +276,98 @@ public class SparseFloatVector extends AbstractNumberVector<SparseFloatVector, F * @return an array consisting of the values of this feature vector */ private double[] getValues() { - double[] values = new double[dimensionality]; - for(int i = 0; i < indexes.length; i++) { - values[this.indexes[i]] = this.values[i]; + double[] vals = new double[dimensionality]; + for (int i = 0; i < indexes.length; i++) { + vals[this.indexes[i]] = this.values[i]; } - return values; + return vals; } - @Override - public <A> SparseFloatVector newFeatureVector(A array, ArrayAdapter<Float, A> adapter) { - int dim = adapter.size(array); - float[] values = new float[dim]; - for(int i = 0; i < dim; i++) { - values[i] = adapter.get(array, i); + /** + * Factory class. + * + * @author Erich Schubert + * + * @apiviz.has SparseFloatVector + */ + public static class Factory extends AbstractNumberVector.Factory<SparseFloatVector, Float> implements SparseNumberVector.Factory<SparseFloatVector, Float> { + @Override + public <A> SparseFloatVector newFeatureVector(A array, ArrayAdapter<Float, A> adapter) { + int dim = adapter.size(array); + float[] values = new float[dim]; + for (int i = 0; i < dim; i++) { + values[i] = adapter.get(array, i); + } + // TODO: inefficient + return new SparseFloatVector(values); } - // TODO: inefficient - return new SparseFloatVector(values); - } - @Override - public <A> SparseFloatVector newNumberVector(A array, NumberArrayAdapter<?, A> adapter) { - int dim = adapter.size(array); - float[] values = new float[dim]; - for(int i = 0; i < dim; i++) { - values[i] = adapter.getFloat(array, i); + @Override + public <A> SparseFloatVector newNumberVector(A array, NumberArrayAdapter<?, ? super A> adapter) { + int dim = adapter.size(array); + float[] values = new float[dim]; + for (int i = 0; i < dim; i++) { + values[i] = adapter.getFloat(array, i); + } + // TODO: inefficient + return new SparseFloatVector(values); } - // TODO: inefficient - return new SparseFloatVector(values); - } - @Override - public SparseFloatVector newNumberVector(TIntDoubleMap dvalues, int maxdim) { - int[] indexes = new int[dvalues.size()]; - float[] values = new float[dvalues.size()]; - // Import and sort the indexes - TIntDoubleIterator iter = dvalues.iterator(); - for(int i = 0; iter.hasNext(); i++) { - iter.advance(); - indexes[i] = iter.key(); + @Override + public SparseFloatVector newNumberVector(TIntDoubleMap dvalues, int maxdim) { + int[] indexes = new int[dvalues.size()]; + float[] values = new float[dvalues.size()]; + // Import and sort the indexes + TIntDoubleIterator iter = dvalues.iterator(); + for (int i = 0; iter.hasNext(); i++) { + iter.advance(); + indexes[i] = iter.key(); + } + Arrays.sort(indexes); + // Import the values accordingly + for (int i = 0; i < dvalues.size(); i++) { + values[i] = (float) dvalues.get(indexes[i]); + } + return new SparseFloatVector(indexes, values, maxdim); } - Arrays.sort(indexes); - // Import the values accordingly - for(int i = 0; i < dvalues.size(); i++) { - values[i] = (float) dvalues.get(indexes[i]); + + @Override + public ByteBufferSerializer<SparseFloatVector> getDefaultSerializer() { + // FIXME: add a serializer + return null; + } + + @Override + public Class<? super SparseFloatVector> getRestrictionClass() { + return SparseFloatVector.class; + } + + /** + * Parameterization class. + * + * @author Erich Schubert + * + * @apiviz.exclude + */ + public static class Parameterizer extends AbstractParameterizer { + @Override + protected SparseFloatVector.Factory makeInstance() { + return FACTORY; + } } - return new SparseFloatVector(indexes, values, maxdim); } @Override public BitSet getNotNullMask() { BitSet b = new BitSet(); - for(int key : indexes) { + for (int key : indexes) { b.set(key); } return b; } /** - * Parameterization class - * - * @author Erich Schubert - * - * @apiviz.exclude - */ - public static class Parameterizer extends AbstractParameterizer { - @Override - protected SparseFloatVector makeInstance() { - return STATIC; - } - } - - /** * Empty map. */ public static final TIntFloatMap EMPTYMAP = new TUnmodifiableIntFloatMap(new TIntFloatHashMap()); -}
\ No newline at end of file +} |