package com.haxademic.core.data;

/**
 * Stores an array of values, intended to be the 
 * last [x] frames of data over time, allowing the 
 * developer to efficiently keep track of the last 
 * sampling of data from input, movement, or other
 * constantly-changing values that need to be 
 * instantly analyzed.  
 */
public class FloatBuffer {
	
	protected int size;
	protected int sampleIndex;
	protected float[] buffer;
	
	/**
	 * Build a FloatBuffer object at a fixed size.
	 * @param size	Number of indexes in the buffer array
	 */
	public FloatBuffer( int size ) {
		initBuffer( size );
	}
	
	protected void initBuffer( int size ) {
		this.size = size;
		sampleIndex = 0;
		buffer = new float[size];
		for( int i=0; i < size; i++) buffer[i] = 0;
	}
	
	/**
	 * Overwrite the oldest value in the buffer with a new value.
	 * @param update		The new float value
	 * @return				The FloatBuffer instance, for chaining
	 */
	public FloatBuffer update( float value ) {
		sampleIndex++;
		if(sampleIndex == size) sampleIndex = 0;
		buffer[sampleIndex] = value;
		return this;
	};
	
	/**
	 * Returns a sum of the buffer values
	 * @return		A sum of the values in the buffer
	 */
	public float sum() {
		float sum = 0;
		for( int i=0; i < size; i++ ) {
			sum += buffer[i];
		}
		return sum;
	};
	
	/**
	 * Returns a sum of the positive buffer values
	 * @return		A sum of the positive values in the buffer
	 */
	public float sumPos() {
		float sum = 0;
		for( int i=0; i < size; i++ ) {
			if(buffer[i] > 0) sum += buffer[i];
		}
		return sum;
	};
	
	/**
	 * Returns a sum of the negative buffer values
	 * @return		A sum of the positive values in the buffer
	 */
	public float sumNeg() {
		float sum = 0;
		for( int i=0; i < size; i++ ) {
			if(buffer[i] < 0) sum += buffer[i];
		}
		return sum;
	};
	
	/**
	 * Returns a sum of the numbers in the buffer, run through Math.abs()
	 * @return		A sum of the absolute positive values in the buffer
	 */
	public float absSum() {
		int absSum = 0;
		for( int i=0; i < size; i++) {
			absSum += Math.abs(buffer[i]);
		}
		return absSum;
	};
	
	/**
	 * Returns the most positive value in the buffer
	 * @return		The most positive value in the buffer
	 */
	public float max() {
		float max = buffer[0];
		for( int i=1; i < size; i++) {
			if( buffer[i] > max ) max = buffer[i];
		}
		return max;
	};

	/**
	 * Returns the most negative value in the buffer
	 * @return		The most negative value in the buffer
	 */
	public float min() {
		float min = buffer[0];
		for( int i=1; i < size; i++) {
			if( buffer[i] < min ) min = buffer[i];
		}
		return min;
	};

	/**
	 * Returns the most positive absolute value in the buffer
	 * @return		The most positive absolute value in the buffer
	 */
	public float absMax() {
		float max = Math.abs(buffer[0]);
		for( int i=1; i < size; i++) {
			if( Math.abs(buffer[i]) > max ) max = Math.abs(buffer[i]);
		}
		return max;
	};
	
	/**
	 * Returns the average value of the buffer
	 * @return		The average value of the buffer
	 */
	public float average() {
		return sum() / size;
	};
}

@@ -153,20 +153,8 @@
  to 
-efficiently 
 keep
@@ -167,19 +167,26 @@
  of the 
-las
+most recen
 t %0A * sa
@@ -281,28 +281,14 @@
  be 
-%0A * instantly analyz
+smooth
 ed. 
@@ -2220,33 +2220,33 @@
 %5D;%0A%09%09for( int i=
-1
+0
 ; i %3C size; i++)
@@ -2476,33 +2476,33 @@
 %5D;%0A%09%09for( int i=
-1
+0
 ; i %3C size; i++)
@@ -2775,9 +2775,9 @@
 t i=
-1
+0
 ; i 
@@ -3002,16 +3002,16 @@
 age() %7B%0A
-
 %09%09return
@@ -3029,10 +3029,568 @@
 ze;%0A%09%7D;%0A
+%09%0A%09/**%0A%09 * Returns first index of a needle value%0A%09 * @return%09%09The buffer index of needle%0A%09 */%0A%09public int indexOf(float needle) %7B%0A%09%09for( int i=0; i %3C size; i++) %7B%0A%09%09%09if( buffer%5Bi%5D == needle) return i;%0A%09%09%7D%0A%09%09return -1;%0A%09%7D;%0A%09%0A%09/**%0A%09 * Returns The a percentage of buffer values that aren't zero. Confidences decreases as zeros increase.%0A%09 * @return%09%09The percentage of buffer values that aren't zero%0A%09 */%0A%09public float confidence() %7B%0A%09%09float numZeros = 0;%0A%09%09for( int i=0; i %3C size; i++) %7B%0A%09%09%09if( buffer%5Bi%5D == 0 ) numZeros++;%0A%09%09%7D%0A%09%09return 1f - numZeros/size;%0A%09%7D;%0A
 %7D%0A

Add a couple more helper methods