guanaqo develop
Utilities for scientific software
Loading...
Searching...
No Matches
max-history.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file
4/// @ingroup core
5/// Sliding-window maximum tracker.
6
7#include <algorithm>
8#include <cstddef>
9#include <vector>
10namespace guanaqo {
11
12/// Keep track of the maximum value over a specified horizon length.
13/// @ingroup core
14template <class T>
16 public:
17 MaxHistory(size_t memory) : buffer(std::vector<T>(memory)) {}
18
19 void add(T newt) {
20 if (full) {
21 T oldt = std::move(*it);
22 *it = std::move(newt);
23 if (*it > max_)
24 max_ = *it;
25 else if (oldt == max_)
26 max_ = *std::max_element(buffer.begin(), buffer.end());
27 ++it;
28 if (it == buffer.end())
29 it = buffer.begin();
30 } else {
31 if (it == buffer.begin() || newt > max_)
32 max_ = newt;
33 *it = std::move(newt);
34 ++it;
35 if (it == buffer.end()) {
36 it = buffer.begin();
37 full = true;
38 }
39 }
40 }
41
42 const T &max() const { return max_; }
43
44 private:
45 std::vector<T> buffer;
46 bool full = false;
47 decltype(buffer.begin()) it = buffer.begin();
48 T max_{};
49};
50
51} // namespace guanaqo
std::vector< T > buffer
const T & max() const
MaxHistory(size_t memory)
decltype(buffer.begin()) it