diff options
| author | Glease <4586901+Glease@users.noreply.github.com> | 2022-03-12 16:23:06 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-03-12 09:23:06 +0100 |
| commit | bcf871bf44f5dd123b4ad9fa4cab497437b9298c (patch) | |
| tree | e3f210ad841fa4e98edabc49b28c235b36d0a519 /src/main/java/gtPlusPlus/core | |
| parent | 58e94b7daa0d73e71c97eb78325064bfa91f10f8 (diff) | |
| download | GT5-Unofficial-bcf871bf44f5dd123b4ad9fa4cab497437b9298c.tar.gz GT5-Unofficial-bcf871bf44f5dd123b4ad9fa4cab497437b9298c.tar.bz2 GT5-Unofficial-bcf871bf44f5dd123b4ad9fa4cab497437b9298c.zip | |
fix pss gui & make average IO actually an average (#157)
Diffstat (limited to 'src/main/java/gtPlusPlus/core')
| -rw-r--r-- | src/main/java/gtPlusPlus/core/util/MovingAverageLong.java | 53 |
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; + } +} |
