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 <
map
>
23
#include <
chrono
>
24
#include <
algorithm
>
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
public
:
50
void
51
insert
(T
const
& s)
52
{
53
++
counts_
[s];
54
++
samples
;
55
}
56
58
std::size_t
59
size
()
const
60
{
61
return
samples
;
62
}
63
65
std::size_t
66
numBins
()
const
67
{
68
return
counts_
.size();
69
}
70
72
T
73
minValue
()
const
74
{
75
return
counts_
.empty() ? T{} :
counts_
.begin()->first;
76
}
77
79
T
80
maxValue
()
const
81
{
82
return
counts_
.empty() ? T{} :
counts_
.rbegin()->first;
83
}
84
86
T
87
avg
()
const
88
{
89
T tmp{};
90
if
(
samples
== 0)
91
return
tmp;
92
// Since counts are sorted, shouldn't need to worry much about numerical
93
// error
94
for
(
auto
const
& [bin, count] :
counts_
)
95
{
96
tmp += bin * count;
97
}
98
return
tmp/
samples
;
99
}
100
107
T
108
percentile
(
float
p)
const
109
{
110
assert(p >= 0 && p <=1);
111
std::size_t
pos =
std::round
(p *
samples
);
112
113
if
(
counts_
.empty())
114
return
T{};
115
116
auto
it =
counts_
.begin();
117
std::size_t
cumsum = it->second;
118
while
(it !=
counts_
.end() && cumsum < pos)
119
{
120
++it;
121
cumsum += it->second;
122
}
123
return
it->first;
124
}
125
};
126
127
}
// namespace csf
128
}
// namespace test
129
}
// namespace ripple
130
131
#endif
ripple::test::csf::Histogram::percentile
T percentile(float p) const
Calculate the given percentile of the distribution.
Definition:
Histogram.h:108
ripple::test::csf::Histogram::maxValue
T maxValue() const
Maximum observed value.
Definition:
Histogram.h:80
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:51
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:73
chrono
map
ripple::test::csf::Histogram::size
std::size_t size() const
The number of samples.
Definition:
Histogram.h:59
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:66
std::size_t
ripple::test::csf::Histogram::avg
T avg() const
Histogram average.
Definition:
Histogram.h:87
Generated by
1.8.17