SlidingAverage.hh
Go to the documentation of this file.
1 #ifndef SLIDING_AVERAGE_HH_
2 #define SLIDING_AVERAGE_HH_
3 
4 /*
5  * Sliding Average
6  * Author: Thijs Miedema
7  * Description: Always up-to-date Sliding Average
8  * Date: July 2018
9 */
10 
11 #include <vector>
12 
13 namespace dune {
14 
15 template
16 <typename T>
18  public:
19  SlidingAverage(unsigned _length) : length(_length), pt(0), values(_length), current_sum(0) {};
20 
21  void add_value(T value){
22  current_sum -= values[pt];
23  current_sum += value;
24  values[pt] = value;
25  pt = (pt + 1) % length;
26  }
27 
28  T sum() {
29  return current_sum;
30  }
31 
32  T avg() {
33  return current_sum / (T) length;
34  }
35 
36  private:
37  const unsigned length;
38  unsigned pt;
39  std::vector<T> values;
41 };
42 
43 template class SlidingAverage<unsigned>;
44 template class SlidingAverage<int>;
45 template class SlidingAverage<double>;
46 
47 } // namespace dune
48 #endif /* SLIDING_AVERAGE_HH_ */
SlidingAverage(unsigned _length)
std::vector< T > values
const unsigned length
void add_value(T value)