69 lines
1.2 KiB
C++
69 lines
1.2 KiB
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 MOVINGAVG_H
|
||
#define MOVINGAVG_H
|
||
|
||
#include <vector>
|
||
|
||
template <typename T> class MovingAVG {
|
||
|
||
private:
|
||
|
||
/** up to "size" elements */
|
||
std::vector<T> values;
|
||
|
||
/** track the current sum of the vector's values */
|
||
T curSum;
|
||
|
||
/** the number of elements to average */
|
||
int size;
|
||
|
||
public:
|
||
|
||
/** ctor */
|
||
MovingAVG(const int size) : curSum(), size(size) {;}
|
||
|
||
/** add a new value */
|
||
void add(const T val) {
|
||
|
||
// add new value
|
||
values.push_back(val);
|
||
curSum += val;
|
||
|
||
// too many values?
|
||
if ((int) values.size() > size) {
|
||
curSum -= values.front();
|
||
values.erase(values.begin());
|
||
}
|
||
|
||
}
|
||
|
||
/** get the current average */
|
||
T get() const {
|
||
return curSum / values.size();
|
||
}
|
||
|
||
|
||
/** get the number of used entries */
|
||
int getNumUsed() const {
|
||
return (int) values.size();
|
||
}
|
||
|
||
/** get number of entries to average */
|
||
int getSize() const {
|
||
return size;
|
||
}
|
||
|
||
|
||
};
|
||
|
||
#endif // MOVINGAVG_H
|