79 lines
1.5 KiB
C++
Executable File
79 lines
1.5 KiB
C++
Executable File
/*
|
||
* © 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 HISTORYTS_H
|
||
#define HISTORYTS_H
|
||
|
||
#include <vector>
|
||
#include "Timestamp.h"
|
||
#include <algorithm>
|
||
|
||
/**
|
||
* keep the history of values for a given amount of time
|
||
*/
|
||
template <typename T> class HistoryTS {
|
||
|
||
private:
|
||
|
||
/** timestamp -> value combination */
|
||
struct Entry {
|
||
Timestamp ts;
|
||
T value;
|
||
Entry(const Timestamp ts, const T& value) : ts(ts), value(value) {;}
|
||
};
|
||
|
||
/** the time-window to keep */
|
||
Timestamp window;
|
||
|
||
/** the value history for the window-size */
|
||
std::vector<Entry> history;
|
||
|
||
public:
|
||
|
||
|
||
/** ctor with the time-window to keep */
|
||
HistoryTS(const Timestamp window) : window(window) {
|
||
|
||
}
|
||
|
||
/** 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 most recent entry */
|
||
T getMostRecent() const {
|
||
return history.back().value;
|
||
}
|
||
|
||
/** get the oldest entry available */
|
||
T getOldest() const {
|
||
return history.front().value;
|
||
}
|
||
|
||
|
||
|
||
};
|
||
|
||
|
||
#endif // HISTORYTS_H
|