54 lines
893 B
C++
54 lines
893 B
C++
/*
|
||
* © Copyright 2014 – Urheberrechtshinweis
|
||
* Alle Rechte vorbehalten / All Rights Reserved
|
||
*
|
||
* Programmcode ist urheberrechtlich geschuetzt.
|
||
* Das Urheberrecht liegt, soweit nicht ausdruecklich anders gekennzeichnet, bei Frank Ebner.
|
||
* Keine Verwendung ohne explizite Genehmigung.
|
||
* (vgl. § 106 ff UrhG / § 97 UrhG)
|
||
*/
|
||
|
||
#ifndef AVERAGE_H
|
||
#define AVERAGE_H
|
||
|
||
#include "../../Assertions.h"
|
||
|
||
namespace Stats {
|
||
|
||
template <typename Scalar> class Average {
|
||
|
||
private:
|
||
|
||
int cnt;
|
||
Scalar sum;
|
||
|
||
public:
|
||
|
||
/** ctor */
|
||
Average() : cnt(0), sum() {
|
||
;
|
||
}
|
||
|
||
/** contains a valid average? */
|
||
bool isValid() const {
|
||
return cnt > 0;
|
||
}
|
||
|
||
/** add a new value */
|
||
void add(const Scalar val) {
|
||
sum += val;
|
||
++cnt;
|
||
}
|
||
|
||
/** get the current value */
|
||
Scalar get() const {
|
||
Assert::isNot0(cnt, "add() values first!");
|
||
return sum / cnt;
|
||
}
|
||
|
||
};
|
||
|
||
}
|
||
|
||
#endif // AVERAGE_H
|