Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

27
LINES

< > BotCompany Repo | #1024553 // RollingAverage (memory-hungry version with LinkedList)

JavaX fragment (include)

1  
sclass RollingAverage {
2  
  int windowSize = 50;
3  
  int recalcEvery = 1000; // to counter floating point errors
4  
  new LinkedList<Double> list;
5  
  double sum;
6  
  int n;
7  
  
8  
  *() {}
9  
  *(int *windowSize) {}
10  
  
11  
  synchronized void add(double d) {
12  
    if (++n >= recalcEvery) {
13  
      n = 0;
14  
      sum = doubleSum(list);
15  
    }
16  
    
17  
    if (l(list) >= windowSize)
18  
      sum -= popFirst(list);
19  
      
20  
    list.add(d);
21  
    sum += d;
22  
  }
23  
  
24  
  synchronized double get() {
25  
    ret doubleRatio(sum, l(list));
26  
  }
27  
}

Author comment

Began life as a copy of #1019129

download  show line numbers  debug dex  old transpilations   

Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1024553
Snippet name: RollingAverage (memory-hungry version with LinkedList)
Eternal ID of this version: #1024553/2
Text MD5: 586f1e96687414e98d1be422649095a1
Author: stefan
Category: javax / maths
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-12-26 22:25:55
Source code size: 522 bytes / 27 lines
Pitched / IR pitched: No / No
Views / Downloads: 206 / 242
Version history: 1 change(s)
Referenced in: [show references]