com.javanut.pronghorn.util.ma.MAvgRollerLong Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pronghorn-pipes Show documentation
Show all versions of pronghorn-pipes Show documentation
Ring buffer based queuing utility for applications that require high performance and/or a small
footprint. Well suited for embedded and stream based processing.
package com.javanut.pronghorn.util.ma;
import java.util.Arrays;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Keeps N values and a running mean at all times for doubles. As new samples are added
* the oldest values is thrown out and returned to the caller.
*
* @author Nathan Tippy
*
*/
public class MAvgRollerLong {
public long[] elements;
public int position;
public long distance; //public to avoid method call
private double runningMean;
private final int size;
private final double factor;
final static Logger logger = LoggerFactory.getLogger(MAvgRollerDouble.class);
public MAvgRollerLong(int size) {
this.size = size;
this.factor = 1/(double)size;
this.elements = new long[size];
this.position = 0;
this.distance = 0-size;//start negative size its ready for use when its zero or greater
this.runningMean = 0;
assert(distance<0);
}
public void reset() {
Arrays.fill(this.elements,0L);
this.position = 0;
this.distance = 0-size;//start negative size its ready for use when its zero or greater
this.runningMean = 0;
}
/**
* zero returns the last item added.
* one returns the item before that
*
* @param index
*/
public static double getElementAt(MAvgRollerLong roller, int index) {
index++;//need one more because roll is already pointing at the next spot
return roller.elements[index <= roller.position ? roller.position-index : roller.size+(roller.position-index) ];
}
public static int getCount(MAvgRollerLong roller) {
return (int) (!isValid(roller) ? roller.size+roller.distance : roller.size);
}
public static boolean isValid(MAvgRollerLong roller) {
return roller.distance>=0;
}
public static double peek(MAvgRollerLong roller){
return roller.elements[roller.position];//next one tossed out
}
/*
* Take this new sample and return the one that has been thrown out of the set
*/
public static final long roll(MAvgRollerLong roller, long curValue) {
if (roller.size == 0 ) {
return curValue;
}
long result = roller.elements[roller.position];
roller.elements[roller.position] = curValue;
if (++roller.position==roller.size) {
roller.position=0;
}
++roller.distance;
roller.runningMean += ((curValue-result)*roller.factor);
return result;
}
public static void print(MAvgRollerLong roller) {
int i = 0;
while (i
© 2015 - 2025 Weber Informatics LLC | Privacy Policy