guanaqo
1.0.0-alpha.27
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>
10
namespace
guanaqo
{
11
12
/// Keep track of the maximum value over a specified horizon length.
13
/// @ingroup core
14
template
<
class
T>
15
class
MaxHistory
{
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
guanaqo::MaxHistory::buffer
std::vector< T > buffer
Definition
max-history.hpp:45
guanaqo::MaxHistory::full
bool full
Definition
max-history.hpp:46
guanaqo::MaxHistory::max_
T max_
Definition
max-history.hpp:48
guanaqo::MaxHistory::add
void add(T newt)
Definition
max-history.hpp:19
guanaqo::MaxHistory::max
const T & max() const
Definition
max-history.hpp:42
guanaqo::MaxHistory::MaxHistory
MaxHistory(size_t memory)
Definition
max-history.hpp:17
guanaqo::MaxHistory::it
decltype(buffer.begin()) it
Definition
max-history.hpp:47
guanaqo
Definition
blas-interface.hpp:9
include
guanaqo
max-history.hpp
Generated on
for guanaqo by
1.16.1