package de.lmu.ifi.dbs.elki.datasource.filter;
/*
 This file is part of ELKI:
 Environment for Developing KDD-Applications Supported by Index-Structures

 Copyright (C) 2012
 Ludwig-Maximilians-Universität München
 Lehr- und Forschungseinheit für Datenbanksysteme
 ELKI Development Team

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import de.lmu.ifi.dbs.elki.data.SparseFloatVector;
import de.lmu.ifi.dbs.elki.data.type.SimpleTypeInformation;
import de.lmu.ifi.dbs.elki.data.type.TypeUtil;
import de.lmu.ifi.dbs.elki.data.type.VectorFieldTypeInformation;

/**
 * Class that turns sparse float vectors into a proper vector field, by setting
 * the maximum dimensionality for each vector.
 * 
 * @author Erich Schubert
 */
public class SparseVectorFieldFilter extends AbstractConversionFilter<SparseFloatVector, SparseFloatVector> {
  /**
   * Maximum dimension
   */
  int maxdim = -1;

  /**
   * Constructor.
   */
  public SparseVectorFieldFilter() {
    super();
  }

  @Override
  protected boolean prepareStart(SimpleTypeInformation<SparseFloatVector> in) {
    return true;
  }

  @Override
  protected void prepareProcessInstance(SparseFloatVector obj) {
    maxdim = Math.max(maxdim, obj.getDimensionality());
  }

  @Override
  protected SparseFloatVector filterSingleObject(SparseFloatVector obj) {
    assert(maxdim > 0);
    obj.setDimensionality(maxdim);
    return obj;
  }

  @Override
  protected SimpleTypeInformation<? super SparseFloatVector> getInputTypeRestriction() {
    return TypeUtil.SPARSE_VECTOR_VARIABLE_LENGTH;
  }

  @Override
  protected SimpleTypeInformation<? super SparseFloatVector> convertedType(SimpleTypeInformation<SparseFloatVector> in) {
    return new VectorFieldTypeInformation<SparseFloatVector>(SparseFloatVector.class, maxdim, SparseFloatVector.STATIC);
  }
}
@@ -40,16 +40,17 @@
 filter;%0A
+%0A
 /*%0A This
@@ -930,16 +930,49 @@
 %3E.%0A */%0A%0A
+import java.lang.reflect.Field;%0A%0A
 import d
@@ -997,29 +997,30 @@
 .data.Sparse
-Float
+Number
 Vector;%0Aimpo
@@ -1186,16 +1186,64 @@
 rmation;
+%0Aimport de.lmu.ifi.dbs.elki.logging.LoggingUtil;
 %0A%0A/**%0A *
@@ -1441,78 +1441,82 @@
 lter
- extends AbstractConversionFilter%3CSparseFloatVector, SparseFloatVector
+%3CV extends SparseNumberVector%3CV, ?%3E%3E extends AbstractConversionFilter%3CV, V
 %3E %7B%0A
@@ -1722,33 +1722,17 @@
 rmation%3C
-SparseFloatVector
+V
 %3E in) %7B%0A
@@ -1805,33 +1805,17 @@
 nstance(
-SparseFloatVector
+V
  obj) %7B%0A
@@ -1899,33 +1899,17 @@
 otected 
-SparseFloatVector
+V
  filterS
@@ -1920,33 +1920,17 @@
 eObject(
-SparseFloatVector
+V
  obj) %7B%0A
@@ -1939,16 +1939,17 @@
   assert
+ 
 (maxdim 
@@ -2064,33 +2064,17 @@
 ? super 
-SparseFloatVector
+V
 %3E getInp
@@ -2147,24 +2147,57 @@
 ENGTH;%0A  %7D%0A%0A
+  @SuppressWarnings(%22unchecked%22)%0A
   @Override%0A
@@ -2238,33 +2238,17 @@
 ? super 
-SparseFloatVector
+V
 %3E conver
@@ -2281,117 +2281,408 @@
 ion%3C
-SparseFloatVector%3E in) %7B%0A    return new VectorFieldTypeInformation%3CSparseFloatVector%3E(SparseFloatVector.c
+V%3E in) %7B%0A    V factory = null;%0A    // FIXME: hack. Add factories to simple type information, too?%0A    try %7B%0A      Field f = in.getRestrictionClass().getField(%22STATIC%22);%0A      factory = (V) f.get(null);%0A    %7D%0A    catch(Exception e) %7B%0A      LoggingUtil.warning(%22Cannot determine factory for type %22 + in.getRestrictionClass());%0A    %7D%0A    return new VectorFieldTypeInformation%3CV%3E(in.getRestrictionC
 lass
+()
 , ma
@@ -2691,32 +2691,15 @@
 im, 
-SparseFloatVector.STATIC
+factory
 );%0A 

Support SparseDoubleVector, too.