84 lines
1.8 KiB
C++
84 lines
1.8 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 MOVINGMINMAXTS_H
|
||
#define MOVINGMINMAXTS_H
|
||
|
||
#include <vector>
|
||
#include "../data/Timestamp.h"
|
||
#include <algorithm>
|
||
|
||
template <typename T> class MovingMinMaxTS {
|
||
|
||
private:
|
||
|
||
/** timestamp -> value combination */
|
||
struct Entry {
|
||
Timestamp ts;
|
||
T value;
|
||
Entry(const Timestamp ts, const T& value) : ts(ts), value(value) {;}
|
||
};
|
||
|
||
/** the regional window to use */
|
||
Timestamp window;
|
||
|
||
/** the value history for the window-size */
|
||
std::vector<Entry> history;
|
||
|
||
/** current sum */
|
||
T sum;
|
||
|
||
public:
|
||
|
||
|
||
/** ctor with the window-size to use */
|
||
MovingMinMaxTS(const Timestamp window, const T zeroElement) : window(window), sum(zeroElement) {
|
||
|
||
}
|
||
|
||
/** add a new entry */
|
||
void add(const Timestamp ts, const T& data) {
|
||
|
||
// append to history
|
||
history.push_back(Entry(ts, data));
|
||
|
||
// remove too-old history entries
|
||
const Timestamp oldest = ts - window;
|
||
while(history.front().ts < oldest) {
|
||
|
||
// remove from history
|
||
history.erase(history.begin());
|
||
|
||
}
|
||
|
||
}
|
||
|
||
/** get the current min element */
|
||
T getMin() const {
|
||
auto comp = [] (const Entry& e1, const Entry& e2) {return e1.value < e2.value;};
|
||
auto it = std::min_element(history.begin(), history.end(), comp);
|
||
return it->value;
|
||
}
|
||
|
||
/** get the current max element */
|
||
T getMax() const {
|
||
auto comp = [] (const Entry& e1, const Entry& e2) {return e1.value < e2.value;};
|
||
auto it = std::max_element(history.begin(), history.end(), comp);
|
||
return it->value;
|
||
}
|
||
|
||
T getRange() const {
|
||
return getMax() - getMin();
|
||
}
|
||
|
||
};
|
||
|
||
#endif // MOVINGMINMAXTS_H
|