aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gtPlusPlus/core
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/gtPlusPlus/core')
-rw-r--r--src/main/java/gtPlusPlus/core/util/MovingAverageLong.java53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/main/java/gtPlusPlus/core/util/MovingAverageLong.java b/src/main/java/gtPlusPlus/core/util/MovingAverageLong.java
new file mode 100644
index 0000000000..f59aa20ecd
--- /dev/null
+++ b/src/main/java/gtPlusPlus/core/util/MovingAverageLong.java
@@ -0,0 +1,53 @@
+package gtPlusPlus.core.util;
+
+import net.minecraft.nbt.NBTTagCompound;
+
+import java.math.BigInteger;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.util.Arrays;
+
+public class MovingAverageLong {
+ private final long[] storage;
+ private int ptr;
+
+ public MovingAverageLong(int sampleSize) {
+ storage = new long[sampleSize];
+ }
+
+ public void set(long average) {
+ Arrays.fill(storage, average);
+ }
+
+ public void sample(long data) {
+ storage[ptr] = data;
+ ptr = (ptr + 1) % storage.length;
+ }
+
+ public long get() {
+ BigInteger result = BigInteger.ZERO;
+ for (long l : storage) {
+ result = result.add(BigInteger.valueOf(l));
+ }
+ return result.divide(BigInteger.valueOf(storage.length)).longValue();
+ }
+
+ public void write(NBTTagCompound tagCompound, String key) {
+ ByteBuffer buf = ByteBuffer.allocate(storage.length * Long.BYTES).order(ByteOrder.nativeOrder());
+ buf.asLongBuffer().put(storage);
+ tagCompound.setByteArray(key, buf.array());
+ }
+
+ /**
+ * if read failed, the internal states would not be changed.
+ * @return true if successful, false otherwise.
+ */
+ public boolean read(NBTTagCompound tagCompound, String key) {
+ ByteBuffer buf = ByteBuffer.wrap(tagCompound.getByteArray(key));
+ if (buf.remaining() != storage.length * Long.BYTES)
+ // not very good...
+ return false;
+ buf.asLongBuffer().get(storage);
+ return true;
+ }
+}