package speiger.src.coreengine.utils.counters.averager;

public class RunningAvg {
	long[] slots;
	int offset;
	long total;
	
	public RunningAvg(int slotCount) {
		slots = new long[slotCount];
		offset = 0;
	}
	
	public void init(long value) {
		while(offset < slots.length) {
			slots[offset++] = value;
		}
		total += (slots.length - offset) * value;
	}
	
	public void add(long value) {
		total -= slots[offset % slots.length];
		slots[offset++ % slots.length] = value;
		offset %= slots.length;
		total += value;
	}
	
	public long avg() {
		return total / slots.length;
	}
	
	public void dampenForLowResTicker() {
		if(avg() > 10000000L) {
			for(int i = 0;i < slots.length;i++) {
				slots[i] *= 0.9F;
			}
			total *= 0.9F;
		}
	}
}