MCPcopy Create free account
hub / github.com/BuildCraft/BuildCraft / AverageInt

Class AverageInt

common/buildcraft/lib/misc/data/AverageInt.java:7–53  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5package buildcraft.lib.misc.data;
6
7public class AverageInt {
8 private int[] data;
9 private int pos, precise;
10 private int averageRaw, tickValue;
11
12 public AverageInt(int precise) {
13 this.precise = precise;
14 clear();
15 }
16
17 public void clear() {
18 this.data = new int[precise];
19 this.pos = 0;
20 }
21
22 public double getAverage() {
23 return (double) averageRaw / precise;
24 }
25
26 public void tick(int value) {
27 internalTick(tickValue + value);
28 tickValue = 0;
29 }
30
31 public void tick() {
32 internalTick(tickValue);
33 tickValue = 0;
34 }
35
36 private void internalTick(int value) {
37 pos = ++pos % precise;
38 int oldValue = data[pos];
39 data[pos] = value;
40 if (pos == 0) {
41 averageRaw = 0;
42 for (int iValue : data) {
43 averageRaw += iValue;
44 }
45 } else {
46 averageRaw = averageRaw - oldValue + value;
47 }
48 }
49
50 public void push(int value) {
51 tickValue += value;
52 }
53}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected