//Source file: D:\\Projects\\src\\yi\\iu\\imgdb\\learn\\FeatureRepresentationList.java

package yi.iu.imgdb.learn;

import java.util.*;
import java.io.*;

/**
 * Linked List of FeatureRepresentation
 */
public class FeatureRepresentationList extends LinkedList
{
  public int featureDimension;

  static final long serialVersionUID = 250646837123208460L;

  public FeatureRepresentationList(int ft_dim)
  {
    super();
    featureDimension = ft_dim;
  }

  public FeatureRepresentationList(double[][] data, int ft_dim)
  {
    for (int i=0; i<data.length; i++) {
      addFeatureRepresentation(new FeatureRepresentation(data[i]));
    }
    featureDimension = ft_dim;
  }


  public void addFeatureRepresentation(FeatureRepresentation fr) {
    super.add(fr);
  }

  public double[][] as2DDoubleArray() {
    double[][] data = new double[size()][];
    int i_index = 0;
    for (Iterator i=iterator(); i.hasNext(); i_index++) {
      data[i_index] = ((FeatureRepresentation)i.next()).data;
    }
    return data;
  }

  /**
   * Select specified attributes for features.  Eg. select_index = [0,4,5]}
   * means to only use the 1st, 5th, and 6th attributes for "color" feature.
   * After this method is called, the data length will be shorted.
   * @param ft_attr_sel
   */
  public void selectAttributes(int[] select_index) {
    if (select_index == null || select_index.length == 0) return;

    // a new linked list to store the new feature vectors with only the selected
    // attributes
    LinkedList new_ft = new LinkedList();
    for (Iterator i=iterator(); i.hasNext();) {
      double[] vt = ((FeatureRepresentation)i.next()).data;
      double[] new_vt = new double[select_index.length];
      for (int j=0; j<select_index.length; j++) {
        new_vt[j] = vt[select_index[j]];
      }
      new_ft.add(new FeatureRepresentation(new_vt));
    }

    // remove the old data
    clear();

    // put the new data with only selected attributes
    addAll(new_ft);

    // change the dimension value of the feature
    this.featureDimension = select_index.length;
  }

  public String toString() {
    StringWriter str = new StringWriter();
    PrintWriter out = new PrintWriter(str);

    out.print("dim=" + featureDimension);
    out.print(",{");
    boolean first = true;
    for (Iterator i=iterator(); i.hasNext(); ) {
      if (first) {
        first = false;
      }
      else {
        out.print(",");
      }
      out.print(i.next());
    }
    out.print("}");
    return str.toString();
  }
}
