Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
Utility.cpp
Go to the documentation of this file.
1// The Reactive C++ Toolbox.
2// Copyright (C) 2021 Reactive Markets Limited
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16#include "Utility.hpp"
17
18#include "Histogram.hpp"
19#include "Iterator.hpp"
20
21#include <cmath>
22
23namespace toolbox {
24inline namespace hdr {
25using namespace std;
26namespace {
27int64_t get_count_at_percentile(const Histogram& h, double percentile) noexcept
28{
29 if (percentile > 100.0) {
30 percentile = 100.0;
31 }
32 const int count_at_percentile = (percentile * h.total_count() / 100) + 0.5;
33 return std::max<int64_t>(count_at_percentile, 1);
34}
35} // namespace
36
37int64_t min(const Histogram& h) noexcept
38{
39 return h.min();
40}
41
42int64_t max(const Histogram& h) noexcept
43{
44 return h.max();
45}
46
47int64_t value_at_percentile(const Histogram& h, double percentile) noexcept
48{
50
51 int64_t total{0};
52 Iterator iter{h};
53 while (iter.next()) {
54 total += iter.count();
56 return h.highest_equivalent_value(iter.value());
57 }
58 }
59 return 0;
60}
61
62double mean(const Histogram& h) noexcept
63{
64 const auto total_count = h.total_count();
65
66 int64_t total{0};
67 Iterator iter{h};
68 while (iter.next()) {
69 if (iter.count() != 0) {
70 total += iter.count() * h.median_equivalent_value(iter.value());
71 }
72 }
73 return double(total) / total_count;
74}
75
76double stddev(const Histogram& h) noexcept
77{
78 const int64_t total_count{h.total_count()};
79 const double mean_val{mean(h)};
80
81 double geometric_dev_total{0.0};
82 Iterator iter{h};
83 while (iter.next()) {
84 if (iter.count() != 0) {
85 const double dev{h.median_equivalent_value(iter.value()) - mean_val};
87 }
88 }
89 return sqrt(geometric_dev_total / total_count);
90}
91
92} // namespace hdr
93} // namespace toolbox
A High Dynamic Range (HDR) Histogram.
Definition Histogram.hpp:64
Iterator is the base iterator for all iterator types.
Definition Iterator.hpp:28
std::int64_t count() const noexcept
Value directly from array for the current counts_index.
Definition Iterator.hpp:45
STL namespace.
int64_t min(const Histogram &h) noexcept
Definition Utility.cpp:37
double mean(const Histogram &h) noexcept
Definition Utility.cpp:62
int64_t value_at_percentile(const Histogram &h, double percentile) noexcept
Definition Utility.cpp:47
int64_t max(const Histogram &h) noexcept
Definition Utility.cpp:42
double stddev(const Histogram &h) noexcept
Definition Utility.cpp:76
constexpr auto bind() noexcept
Definition Slot.hpp:92