package yi.ds;

/**
 * Class ArrayCumulator is to provide a simplified functions of int array (or
 * vector cumulation.  For example, caculate the R,G,B means of color pixels.
 * Each cumulation element is an integer vector instead of just an integer.  The
 * class doesn't perform overflow examination.
 */
public class IntArrayCumulator {
  /**
   * Save the summaries
   */
  int[] m_sum = null;
  /**
   * The number of the data
   */
  int m_count = 0;
  /**
   * The number of the data dimension
   */
  int m_dataLen;

  public IntArrayCumulator() {
  }

  public IntArrayCumulator(int[] data) {
    super();
    add(data);
  }

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

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

  /**
   * Return the means of the int array.  If the count == 0, return 0s.
   * @return
   */
  public int[] getMeans() {
    int[] means = new int[m_dataLen];
    for (int i=0; i<m_dataLen; i++) {
      means[i] = m_count==0? 0: (int)Math.round((double)m_sum[i]/m_count);
    }
    return means;
  }

  public int getCount() {
    return m_count;
  }

}