From bcf871bf44f5dd123b4ad9fa4cab497437b9298c Mon Sep 17 00:00:00 2001 From: Glease <4586901+Glease@users.noreply.github.com> Date: Sat, 12 Mar 2022 16:23:06 +0800 Subject: fix pss gui & make average IO actually an average (#157) --- .../gtPlusPlus/core/util/MovingAverageLong.java | 53 ++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/main/java/gtPlusPlus/core/util/MovingAverageLong.java (limited to 'src/main/java/gtPlusPlus/core') 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; + } +} -- cgit