Torc  0.1
torcmaths.h
Go to the documentation of this file.
1 #ifndef TORCMATHS_H
2 #define TORCMATHS_H
3 
4 // Qt
5 #include <QQueue>
6 
11 template <typename T> class TorcAverage
12 {
13  public:
14  explicit TorcAverage(int MaxCount = 0)
15  : m_average(0.0),
16  m_count(0),
17  m_maxCount(MaxCount),
18  m_values()
19  {
20  }
21 
22  double AddValue(T Value)
23  {
24  if (m_maxCount)
25  {
26  while (m_values.size() >= m_maxCount)
27  {
28  m_average = ((m_average * m_count) - m_values.dequeue()) / (m_count - 1);
29  m_count--;
30  }
31  m_values.enqueue(Value);
32  }
33  m_average = ((m_average * m_count) + Value) / (m_count + 1);
34  m_count++;
35  return m_average;
36  }
37 
38  double GetAverage(void)
39  {
40  return m_average;
41  }
42 
43  void Reset(void)
44  {
45  m_average = 0.0;
46  m_count = 0;
47  m_values.clear();
48  }
49 
50  private:
51  double m_average;
52  quint64 m_count;
53  int m_maxCount;
54  QQueue<T> m_values;
55 };
56 
57 #endif // TORCMATHS_H
double GetAverage(void)
Definition: torcmaths.h:38
TorcAverage(int MaxCount=0)
Definition: torcmaths.h:14
Compute a running average.
Definition: torcmaths.h:11
void Reset(void)
Definition: torcmaths.h:43
double AddValue(T Value)
Definition: torcmaths.h:22