Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
Histogram.hpp
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#ifndef TOOLBOX_HDR_HISTOGRAM
16#define TOOLBOX_HDR_HISTOGRAM
17
18#include <toolbox/Config.h>
19
20#include <cstdint>
21#include <vector>
22
23namespace toolbox {
25inline namespace hdr {
39 BucketConfig(std::int64_t lowest_trackable_value, std::int64_t highest_trackable_value,
40 int significant_figures);
41 ~BucketConfig() noexcept = default;
42
43 // Copy.
44 BucketConfig(const BucketConfig&) noexcept = default;
45 BucketConfig& operator=(const BucketConfig&) noexcept = default;
46
47 // Move.
48 BucketConfig(BucketConfig&&) noexcept = default;
49 BucketConfig& operator=(BucketConfig&&) noexcept = default;
50
51 std::int64_t lowest_trackable_value;
52 std::int64_t highest_trackable_value;
53 std::int32_t significant_figures;
54 std::int32_t unit_magnitude;
55 std::int32_t sub_bucket_half_count_magnitude;
56 std::int32_t sub_bucket_count;
57 std::int32_t sub_bucket_half_count;
58 std::int64_t sub_bucket_mask;
59 std::int32_t bucket_count;
60 std::int32_t counts_len;
61};
62
65 public:
66 explicit Histogram(const BucketConfig& config);
67 Histogram(std::int64_t lowest_trackable_value, std::int64_t highest_trackable_value,
68 std::int32_t significant_figures);
69 ~Histogram() noexcept = default;
70
71 // Copy.
72 Histogram(const Histogram&) = default;
73 Histogram& operator=(const Histogram&) = default;
74
75 // Move.
76 Histogram(Histogram&&) noexcept = default;
77 Histogram& operator=(Histogram&&) noexcept = default;
78
79 std::int64_t lowest_trackable_value() const noexcept { return lowest_trackable_value_; }
80 std::int64_t highest_trackable_value() const noexcept { return highest_trackable_value_; }
81 std::int32_t significant_figures() const noexcept { return significant_figures_; }
82
83 std::int32_t sub_bucket_count() const noexcept { return sub_bucket_count_; }
84 std::int32_t bucket_count() const noexcept { return bucket_count_; }
85 std::int64_t total_count() const noexcept { return total_count_; }
86 std::int32_t counts_len() const noexcept { return static_cast<std::int32_t>(counts_.size()); }
87
89 std::int64_t min() const noexcept;
90
92 std::int64_t max() const noexcept;
93
101 bool values_are_equivalent(std::int64_t a, std::int64_t b) const noexcept;
102
110 std::int64_t lowest_equivalent_value(std::int64_t value) const noexcept;
111 std::int64_t highest_equivalent_value(std::int64_t value) const noexcept;
112
118 std::int64_t count_at_value(std::int64_t value) const noexcept;
119 std::int64_t count_at_index(std::int32_t index) const noexcept;
120 std::int64_t value_at_index(std::int32_t index) const noexcept;
121 std::int64_t size_of_equivalent_value_range(std::int64_t value) const noexcept;
122 std::int64_t next_non_equivalent_value(std::int64_t value) const noexcept;
123 std::int64_t median_equivalent_value(std::int64_t value) const noexcept;
124 std::int64_t counts_get_normalised(std::int32_t index) const noexcept;
125
130 void reset() noexcept;
131
138 bool record_value(std::int64_t value) noexcept;
139
147 bool record_values(std::int64_t value, std::int64_t count) noexcept;
148
149 private:
150 std::int32_t normalize_index(std::int32_t index) const noexcept;
151 std::int32_t get_bucket_index(std::int64_t value) const noexcept;
152 std::int32_t counts_index(std::int32_t bucket_index,
153 std::int32_t sub_bucket_index) const noexcept;
154 std::int32_t counts_index_for(std::int64_t value) const noexcept;
155 std::int64_t non_zero_min() const noexcept;
156
157 void counts_inc_normalised(std::int32_t index, std::int64_t value) noexcept;
158 void update_min_max(std::int64_t value) noexcept;
159
160 std::int64_t lowest_trackable_value_;
161 std::int64_t highest_trackable_value_;
162 std::int32_t significant_figures_;
163 std::int32_t unit_magnitude_;
164 std::int32_t sub_bucket_half_count_magnitude_;
165 std::int32_t sub_bucket_count_;
166 std::int32_t sub_bucket_half_count_;
167 std::int64_t sub_bucket_mask_;
168 std::int32_t bucket_count_;
169
170 std::int32_t normalizing_index_offset_;
171 std::int64_t min_value_;
172 std::int64_t max_value_;
173 std::int64_t total_count_;
174 std::vector<std::int64_t> counts_;
175};
176
177} // namespace hdr
178} // namespace toolbox
179
180#endif // TOOLBOX_HDR_HISTOGRAM
#define TOOLBOX_API
Definition Config.h:39
A High Dynamic Range (HDR) Histogram.
Definition Histogram.hpp:64
std::int32_t significant_figures() const noexcept
Definition Histogram.hpp:81
std::int64_t total_count() const noexcept
Definition Histogram.hpp:85
std::int32_t sub_bucket_count() const noexcept
Definition Histogram.hpp:83
std::int64_t highest_trackable_value() const noexcept
Definition Histogram.hpp:80
~Histogram() noexcept=default
Histogram(std::int64_t lowest_trackable_value, std::int64_t highest_trackable_value, std::int32_t significant_figures)
std::int32_t bucket_count() const noexcept
Definition Histogram.hpp:84
std::int32_t counts_len() const noexcept
Definition Histogram.hpp:86
STL namespace.
int64_t min(const Histogram &h) noexcept
Definition Utility.cpp:41
Bucket configuration.
Definition Histogram.hpp:27
BucketConfig(std::int64_t lowest_trackable_value, std::int64_t highest_trackable_value, int significant_figures)
~BucketConfig() noexcept=default