mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-04-06 07:46:08 +00:00
Metrics
This commit is contained in:
87
src/main/java/com/volmit/iris/util/AtomicAverage.java
Normal file
87
src/main/java/com/volmit/iris/util/AtomicAverage.java
Normal file
@@ -0,0 +1,87 @@
|
||||
package com.volmit.iris.util;
|
||||
|
||||
import com.google.common.util.concurrent.AtomicDoubleArray;
|
||||
|
||||
/**
|
||||
* Provides an incredibly fast averaging object. It swaps values from a sum
|
||||
* using an array. Averages do not use any form of looping. An average of 10,000
|
||||
* entries is the same speed as an average with 5 entries.
|
||||
*
|
||||
* @author cyberpwn
|
||||
*
|
||||
*/
|
||||
public class AtomicAverage {
|
||||
protected AtomicDoubleArray values;
|
||||
private double average;
|
||||
private double lastSum;
|
||||
private boolean dirty;
|
||||
protected int cursor;
|
||||
private boolean brandNew;
|
||||
|
||||
/**
|
||||
* Create an average holder
|
||||
*
|
||||
* @param size the size of entries to keep
|
||||
*/
|
||||
public AtomicAverage(int size) {
|
||||
values = new AtomicDoubleArray(size);
|
||||
DoubleArrayUtils.fill(values, 0);
|
||||
brandNew = true;
|
||||
average = 0;
|
||||
cursor = 0;
|
||||
lastSum = 0;
|
||||
dirty = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Put a value into the average (rolls over if full)
|
||||
*
|
||||
* @param i the value
|
||||
*/
|
||||
public void put(double i) {
|
||||
|
||||
dirty = true;
|
||||
|
||||
if(brandNew)
|
||||
{
|
||||
DoubleArrayUtils.fill(values, i);
|
||||
lastSum = size() * i;
|
||||
brandNew = false;
|
||||
return;
|
||||
}
|
||||
|
||||
double current = values.get(cursor);
|
||||
lastSum = (lastSum - current) + i;
|
||||
values.set(cursor, i);
|
||||
cursor = cursor + 1 < size() ? cursor + 1 : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current average
|
||||
*
|
||||
* @return the average
|
||||
*/
|
||||
public double getAverage() {
|
||||
if (dirty) {
|
||||
calculateAverage();
|
||||
return getAverage();
|
||||
}
|
||||
|
||||
return average;
|
||||
}
|
||||
|
||||
private void calculateAverage() {
|
||||
average = lastSum / (double) size();
|
||||
dirty = false;
|
||||
}
|
||||
|
||||
public int size()
|
||||
{
|
||||
return values.length();
|
||||
}
|
||||
|
||||
public boolean isDirty()
|
||||
{
|
||||
return dirty;
|
||||
}
|
||||
}
|
||||
102
src/main/java/com/volmit/iris/util/AtomicRollingSequence.java
Normal file
102
src/main/java/com/volmit/iris/util/AtomicRollingSequence.java
Normal file
@@ -0,0 +1,102 @@
|
||||
package com.volmit.iris.util;
|
||||
|
||||
public class AtomicRollingSequence extends AtomicAverage
|
||||
{
|
||||
private double median;
|
||||
private double max;
|
||||
private double min;
|
||||
private boolean dirtyMedian;
|
||||
private int dirtyExtremes;
|
||||
private boolean precision;
|
||||
|
||||
public AtomicRollingSequence(int size)
|
||||
{
|
||||
super(size);
|
||||
median = 0;
|
||||
min = 0;
|
||||
max = 0;
|
||||
setPrecision(false);
|
||||
}
|
||||
|
||||
public double addLast(int amt)
|
||||
{
|
||||
double f = 0;
|
||||
|
||||
for(int i = 0; i < Math.min(values.length(), amt); i++)
|
||||
{
|
||||
f += values.get(i);
|
||||
}
|
||||
|
||||
return f;
|
||||
}
|
||||
|
||||
public void setPrecision(boolean p)
|
||||
{
|
||||
this.precision = p;
|
||||
}
|
||||
|
||||
public boolean isPrecision()
|
||||
{
|
||||
return precision;
|
||||
}
|
||||
|
||||
public double getMin()
|
||||
{
|
||||
if(dirtyExtremes > (isPrecision() ? 0 : values.length()))
|
||||
{
|
||||
resetExtremes();
|
||||
}
|
||||
|
||||
return min;
|
||||
}
|
||||
|
||||
public double getMax()
|
||||
{
|
||||
if(dirtyExtremes > (isPrecision() ? 0 : values.length()))
|
||||
{
|
||||
resetExtremes();
|
||||
}
|
||||
|
||||
return max;
|
||||
}
|
||||
|
||||
public double getMedian()
|
||||
{
|
||||
if(dirtyMedian)
|
||||
{
|
||||
recalculateMedian();
|
||||
}
|
||||
|
||||
return median;
|
||||
}
|
||||
|
||||
private void recalculateMedian()
|
||||
{
|
||||
median = new KList<Double>().forceAdd(values).sort().middleValue();
|
||||
dirtyMedian = false;
|
||||
}
|
||||
|
||||
public void resetExtremes()
|
||||
{
|
||||
max = Integer.MIN_VALUE;
|
||||
min = Integer.MAX_VALUE;
|
||||
|
||||
for(int i = 0; i < values.length(); i++)
|
||||
{
|
||||
double v = values.get(i);
|
||||
max = M.max(max, v);
|
||||
min = M.min(min, v);
|
||||
}
|
||||
|
||||
dirtyExtremes = 0;
|
||||
}
|
||||
|
||||
public void put(double i)
|
||||
{
|
||||
super.put(i);
|
||||
dirtyMedian = true;
|
||||
dirtyExtremes++;
|
||||
max = M.max(max, i);
|
||||
min = M.min(min, i);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,10 @@
|
||||
package com.volmit.iris.util;
|
||||
|
||||
|
||||
import com.google.common.util.concurrent.AtomicDoubleArray;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class DoubleArrayUtils
|
||||
{
|
||||
public static void shiftRight(double[] values, double push)
|
||||
@@ -21,9 +25,15 @@ public class DoubleArrayUtils
|
||||
|
||||
public static void fill(double[] values, double value)
|
||||
{
|
||||
for(int i = 0; i < values.length; i++)
|
||||
Arrays.fill(values, value);
|
||||
}
|
||||
|
||||
public static void fill(AtomicDoubleArray values, double value)
|
||||
{
|
||||
for(int i = 0; i < values.length(); i++)
|
||||
{
|
||||
values[i] = value;
|
||||
values.set(i, value);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
package com.volmit.iris.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import com.google.common.util.concurrent.AtomicDoubleArray;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class KList<T> extends ArrayList<T> implements List<T>
|
||||
@@ -594,6 +591,17 @@ public class KList<T> extends ArrayList<T> implements List<T>
|
||||
return this;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public KList<T> forceAdd(AtomicDoubleArray values)
|
||||
{
|
||||
for(int i = 0; i < values.length(); i++)
|
||||
{
|
||||
add((T) ((Object)values.get(i)));
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public KList<T> forceAdd(float[] values)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user