-
Notifications
You must be signed in to change notification settings - Fork 3
/
RunningAverage.h
83 lines (70 loc) · 1.96 KB
/
RunningAverage.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. See the enclosed file LICENSE for a copy or if
* that was not distributed with this file, You can obtain one at
* http://mozilla.org/MPL/2.0/.
*
* Copyright 2017 Max H. Gerlach
*
* */
#include <list>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
#pragma GCC diagnostic ignored "-Wshadow"
#include <boost/serialization/list.hpp>
#pragma GCC diagnostic pop
namespace RA {
template<typename Val>
class RunningAverage {
int sampleSize;
int samplesAdded;
std::list<Val> values; //deque was problematic when serialized in a debug build
Val runningAverage;
public:
RunningAverage(int sampleSize_);
void addValue(Val v);
Val get();
int getSamplesAdded();
private:
friend class boost::serialization::access;
RunningAverage() :
sampleSize(), samplesAdded(), values(),
runningAverage()
{
//private default constructor, just for serialization
}
template<class Archive>
void serialize(Archive& ar, const uint32_t version) {
(void)version;
ar & sampleSize & samplesAdded
& values & runningAverage;
}
};
template<typename Val>
RunningAverage<Val>::RunningAverage(int sampleSize_) :
sampleSize(sampleSize_), samplesAdded(0), values(),
runningAverage(0)
{
}
template<typename Val>
void RunningAverage<Val>::addValue(Val v) {
if (samplesAdded < sampleSize) {
values.push_back(v);
runningAverage += v / sampleSize;
} else {
runningAverage -= values.front() / sampleSize;
values.pop_front();
values.push_back(v);
runningAverage += v / sampleSize;
}
++samplesAdded;
}
template<typename Val>
Val RunningAverage<Val>::get() {
return runningAverage;
}
template<typename Val>
int RunningAverage<Val>::getSamplesAdded() {
return samplesAdded;
}
}
typedef RA::RunningAverage<double> RunningAverage;