rippled
test
csf
Histogram.h
1
//------------------------------------------------------------------------------
2
/*
3
This file is part of rippled: https://github.com/ripple/rippled
4
Copyright (c) 2012-2017 Ripple Labs Inc
5
6
Permission to use, copy, modify, and/or distribute this software for any
7
purpose with or without fee is hereby granted, provided that the above
8
copyright notice and this permission notice appear in all copies.
9
10
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
*/
18
//==============================================================================
19
#ifndef RIPPLE_TEST_CSF_HISTOGRAM_H_INCLUDED
20
#define RIPPLE_TEST_CSF_HISTOGRAM_H_INCLUDED
21
22
#include <
algorithm
>
23
#include <
chrono
>
24
#include <
map
>
25
26
namespace
ripple
{
27
namespace
test {
28
namespace
csf {
29
41
template
<
class
T,
class
Compare = std::less<T>>
42
class
Histogram
43
{
44
// TODO: Consider logarithimic bins around expected median if this becomes
45
// unscaleable
46
std::map<T, std::size_t, Compare>
counts_
;
47
std::size_t
samples
= 0;
48
49
public
:
51
void
52
insert
(T
const
& s)
53
{
54
++
counts_
[s];
55
++
samples
;
56
}
57
59
std::size_t
60
size
()
const
61
{
62
return
samples
;
63
}
64
66
std::size_t
67
numBins
()
const
68
{
69
return
counts_
.size();
70
}
71
73
T
74
minValue
()
const
75
{
76
return
counts_
.empty() ? T{} :
counts_
.begin()->first;
77
}
78
80
T
81
maxValue
()
const
82
{
83
return
counts_
.empty() ? T{} :
counts_
.rbegin()->first;
84
}
85
87
T
88
avg
()
const
89
{
90
T tmp{};
91
if
(
samples
== 0)
92
return
tmp;
93
// Since counts are sorted, shouldn't need to worry much about numerical
94
// error
95
for
(
auto
const
& [bin, count] :
counts_
)
96
{
97
tmp += bin * count;
98
}
99
return
tmp /
samples
;
100
}
101
108
T
109
percentile
(
float
p)
const
110
{
111
assert(p >= 0 && p <= 1);
112
std::size_t
pos =
std::round
(p *
samples
);
113
114
if
(
counts_
.empty())
115
return
T{};
116
117
auto
it =
counts_
.begin();
118
std::size_t
cumsum = it->second;
119
while
(it !=
counts_
.end() && cumsum < pos)
120
{
121
++it;
122
cumsum += it->second;
123
}
124
return
it->first;
125
}
126
};
127
128
}
// namespace csf
129
}
// namespace test
130
}
// namespace ripple
131
132
#endif
ripple::test::csf::Histogram::percentile
T percentile(float p) const
Calculate the given percentile of the distribution.
Definition:
Histogram.h:109
ripple::test::csf::Histogram::maxValue
T maxValue() const
Maximum observed value.
Definition:
Histogram.h:81
ripple::test::csf::Histogram::samples
std::size_t samples
Definition:
Histogram.h:47
algorithm
ripple::test::csf::Histogram::insert
void insert(T const &s)
Insert an sample.
Definition:
Histogram.h:52
ripple::test::csf::Histogram::counts_
std::map< T, std::size_t, Compare > counts_
Definition:
Histogram.h:46
ripple::test::csf::Histogram::minValue
T minValue() const
Minimum observed value.
Definition:
Histogram.h:74
chrono
map
ripple::test::csf::Histogram::size
std::size_t size() const
The number of samples.
Definition:
Histogram.h:60
ripple::test::csf::Histogram
Basic histogram.
Definition:
Histogram.h:42
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition:
RCLCensorshipDetector.h:29
std::round
T round(T... args)
ripple::test::csf::Histogram::numBins
std::size_t numBins() const
The number of distinct samples (bins)
Definition:
Histogram.h:67
std::size_t
ripple::test::csf::Histogram::avg
T avg() const
Histogram average.
Definition:
Histogram.h:88
Generated by
1.8.17