package yi.ds;

/**
 * Class ArrayCumulator is to provide a simplified functions of double array (or
 * vector cumulation.  For example, caculate the CIE Lab means of color pixels.
 * Each cumulation element is a double vector instead of just a double.
 */
public class DoubleArrayCumulator implements java.io.Serializable{
  /**
   * Save the summaries
   */
  double[] m_sum = null;
  /**
   * The number of the data
   */
  int count = 0;
  /**
   * The number of the data dimension
   */
  int m_dataLen;

  public DoubleArrayCumulator() {
  }

  public DoubleArrayCumulator(double[] data) {
    super();
    add(data);
  }

  public void add(double[] data) {
    if (m_sum == null) {
      m_dataLen = data.length;
      m_sum = (double[])data.clone();
    }
    else {
      for (int i=0; i<data.length; i++) m_sum[i] += data[i];
    }
    count ++;
  }

  public double[] getSummaries() {
    return m_sum;
  }

  public double[] getMeans() {
    double[] means = new double[m_dataLen];
    for (int i=0; i<m_dataLen; i++) {
      means[i] = m_sum[i]/count;
    }
    return means;
  }

  public int getCount() {
    return count;
  }

}