package yi.stat.hist;

import java.util.*;

/**
 * HashBasedHist implements a hash based histogram which is suitable for sparse
 * histogram
 */
public class HashBasedHist extends HashMap{
  /**
   * How many data has been put into the histogram
   */
  int m_total = 0;

  /**
   * The minimum data in the histogram
   */
  int m_minimum;

  /**
   * The maximum data in the histogram
   */
  int m_maximum;

  public void put(int data) {
    put(data, 1);
  }

  public void put(int data, int times) {
    int count = get(data);
    put(new Integer(data), new Integer(count + times));

    // statistics information
    if (m_total == 0) m_minimum = m_maximum = data;
    m_total += times;
    if (data < m_minimum) m_minimum = data;
    if (data > m_maximum) m_maximum = data;
  }

  public int getTotal() {
    return m_total;
  }

  public int getMaximum() {
    return m_maximum;
  }

  public int getMinimum() {
    return m_minimum;
  }

  public int get(int data) {
    Integer key = new Integer(data);
    return containsKey(key)? ((Integer)get(key)).intValue() : 0;
  }
}