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)

sclass RollingAverage {
  int windowSize = 50;
  int recalcEvery = 1000; // to counter floating point errors
  new LinkedList<Double> list;
  double sum;
  int n;
  
  *() {}
  *(int *windowSize) {}
  
  synchronized void add(double d) {
    if (++n >= recalcEvery) {
      n = 0;
      sum = doubleSum(list);
    }
    
    if (l(list) >= windowSize)
      sum -= popFirst(list);
      
    list.add(d);
    sum += d;
  }
  
  synchronized double get() {
    ret doubleRatio(sum, l(list));
  }
}

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: 205 / 240
Version history: 1 change(s)
Referenced in: [show references]