//package Adaptivealgorithm;

import java.util.LinkedList;


public class Histogram {
public int numbins;
public double max, min;
public double first,step;
	
public LinkedList <Double> hist = new LinkedList();
public Histogram(double min, double max, int numbin)	
{
	this.numbins = numbin;
	this.max = max;
	this.min = min;
	
	this.step = (this.max - this.min)/(double)numbins;
	
	this.first = min + (step/2.0);
	
	for(int i=0;i<numbins;i++)
	{
		Double temp = new Double(0);
		hist.add(temp);
	}
}

public void addelement(double val)
{
	int index = (int)(((double)numbins*val)/(this.max - this.min));
	if(index == numbins)
	{
		index --;
	}
	
	else if(index > numbins)
		return;
	
	
	System.out.println("Index set "+index+" "+val+" Max "+max+" Min "+min+" numbins "+numbins
			+" first "+this.first+" step "+this.step);
	Double gettemp= hist.get(index);
	double value = gettemp.doubleValue();
	value += 1.0;
	Double addval = new Double(value);
	hist.set(index, addval);
	
}


public LinkedList <Bar> distribution()
{
	LinkedList <Bar> distribution = new LinkedList();

    double total  = 0;
    for(int i=0;i<hist.size();i++)
    {
            Double val = hist.get(i);
            total += val.doubleValue();
    }

    for(int i=0;i<hist.size();i++)
    {
      System.out.println("Val "+hist.get(i)/total+" "+hist.size()+" "+numbins);
      Bar value = new Bar(first+i*step,hist.get(i).doubleValue()/total);
      distribution.add(value);
    }

    return distribution;
}

public LinkedList <Bar> distributionlessthanapoint(int rechargeper)
{
	LinkedList <Bar> distribution = new LinkedList();
	int index = 0;
	double total =0.0;
	
	for(int i=0;i<hist.size();i++)
	{
		total += (hist.get(i)).doubleValue();
		if(first+i*step > rechargeper)
		{
			index = i;
			break;
		}
	}
	System.out.println("Total "+total);
	for(int i=0;i<=index;i++)
	{
		  Bar value = new Bar(first+i*step,hist.get(i).doubleValue()/total);
		  System.out.println("Dist "+(first+i*step)+" "+hist.get(i).doubleValue());
	      distribution.add(value);	
	}
	
	return distribution;
}
	
}
