xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
include
xrpl
basics
DecayingSample.h
1
#pragma once
2
3
#include <
chrono
>
4
#include <
cmath
>
5
#include <
cstddef
>
6
7
namespace
xrpl
{
8
13
template
<
int
Window,
typename
Clock>
14
class
DecayingSample
15
{
16
public
:
17
using
value_type
= Clock::duration::rep;
18
using
time_point
= Clock::time_point;
19
20
DecayingSample
() =
delete
;
21
25
explicit
DecayingSample
(
time_point
now) :
value_
(
value_type
()),
when_
(now)
26
{
27
}
28
33
value_type
34
add
(
value_type
value
,
time_point
now)
35
{
36
decay
(now);
37
value_
+=
value
;
38
return
value_
/ Window;
39
}
40
45
value_type
46
value
(
time_point
now)
47
{
48
decay
(now);
49
return
value_
/ Window;
50
}
51
52
private
:
53
// Apply exponential decay based on the specified time.
54
void
55
decay
(
time_point
now)
56
{
57
if
(now ==
when_
)
58
return
;
59
60
if
(
value_
!=
value_type
())
61
{
62
std::size_t
elapsed =
63
std::chrono::duration_cast<std::chrono::seconds>
(now -
when_
).count();
64
65
// A span larger than four times the window decays the
66
// value to an insignificant amount so just reset it.
67
//
68
if
(elapsed > 4 * Window)
69
{
70
value_
=
value_type
();
71
}
72
else
73
{
74
for
(; elapsed > 0; --elapsed)
75
{
76
value_
-= (
value_
+ Window - 1) / Window;
77
}
78
}
79
}
80
81
when_
= now;
82
}
83
84
// Current value in exponential units
85
value_type
value_
;
86
87
// Last time the aging function was applied
88
time_point
when_
;
89
};
90
91
//------------------------------------------------------------------------------
92
97
template
<
int
HalfLife,
class
Clock>
98
class
DecayWindow
99
{
100
public
:
101
using
time_point
= Clock::time_point;
102
103
explicit
DecayWindow
(
time_point
now) :
when_
(now)
104
{
105
}
106
107
void
108
add
(
double
value
,
time_point
now)
109
{
110
decay
(now);
111
value_
+=
value
;
112
}
113
114
double
115
value
(
time_point
now)
116
{
117
decay
(now);
118
return
value_
/ HalfLife;
119
}
120
121
private
:
122
static_assert
(HalfLife > 0,
"half life must be positive"
);
123
124
void
125
decay
(
time_point
now)
126
{
127
if
(now <=
when_
)
128
return
;
129
using namespace
std::chrono
;
130
auto
const
elapsed =
duration<double>
(now -
when_
).
count
();
131
value_
*=
std::pow
(2.0, -elapsed / HalfLife);
132
when_
= now;
133
}
134
135
double
value_
{0};
136
time_point
when_
;
137
};
138
139
}
// namespace xrpl
chrono
xrpl::DecayWindow::when_
time_point when_
Definition
DecayingSample.h:136
xrpl::DecayWindow::time_point
Clock::time_point time_point
Definition
DecayingSample.h:101
xrpl::DecayWindow::value_
double value_
Definition
DecayingSample.h:135
xrpl::DecayWindow::decay
void decay(time_point now)
Definition
DecayingSample.h:125
xrpl::DecayWindow::add
void add(double value, time_point now)
Definition
DecayingSample.h:108
xrpl::DecayWindow::value
double value(time_point now)
Definition
DecayingSample.h:115
xrpl::DecayWindow::DecayWindow
DecayWindow(time_point now)
Definition
DecayingSample.h:103
xrpl::DecayingSample::DecayingSample
DecayingSample()=delete
xrpl::DecayingSample::value
value_type value(time_point now)
Retrieve the current value in normalized units.
Definition
DecayingSample.h:46
xrpl::DecayingSample::value_type
Clock::duration::rep value_type
Definition
DecayingSample.h:17
xrpl::DecayingSample::when_
time_point when_
Definition
DecayingSample.h:88
xrpl::DecayingSample::value_
value_type value_
Definition
DecayingSample.h:85
xrpl::DecayingSample::DecayingSample
DecayingSample(time_point now)
Definition
DecayingSample.h:25
xrpl::DecayingSample::add
value_type add(value_type value, time_point now)
Add a new sample.
Definition
DecayingSample.h:34
xrpl::DecayingSample::time_point
Clock::time_point time_point
Definition
DecayingSample.h:18
xrpl::DecayingSample::decay
void decay(time_point now)
Definition
DecayingSample.h:55
cmath
std::chrono::duration::count
T count(T... args)
cstddef
std::chrono::duration_cast
T duration_cast(T... args)
std::chrono::duration
std::chrono
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
std::pow
T pow(T... args)
std::size_t
Generated by
1.17.0