Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
Iterator.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 "Iterator.hpp"
17
18#include "Histogram.hpp"
19
20#include <cmath>
21
22namespace toolbox {
23inline namespace hdr {
24using namespace std;
25
27: h_{h}
28, counts_index_{-1}
29, total_count_{h.total_count()}
30, count_{0}
31, cumulative_count_{0}
32, value_{0}
33, highest_equivalent_value_{0}
34, value_iterated_from_{0}
35, value_iterated_to_{0}
36{
37}
38
39Iterator::~Iterator() = default;
40
42{
43 bool result = move_next();
44 if (result) {
46 }
47 return result;
48}
49
54
59
64
67{
68 if (counts_index_ >= h_.counts_len()) {
69 return false;
70 }
71 return peek_next_value_from_index() > reporting_level_upper_bound;
72}
73
75{
76 if (!has_next() || counts_index_ >= h_.counts_len()) {
77 return false;
78 }
79 move_next();
80 return true;
81}
82
100
102{
103 value_iterated_from_ = value_iterated_to_;
104 value_iterated_to_ = new_value_iterated_to;
105}
106
107PercentileIterator::PercentileIterator(const Histogram& h, int32_t ticks_per_half_distance) noexcept
108: Iterator{h}
109, seen_last_value_{false}
110, ticks_per_half_distance_{ticks_per_half_distance}
111, percentile_to_iterate_to_{0.0}
112, percentile_{0.0}
113{
114}
115
117{
118 if (!has_next()) {
119 if (seen_last_value_) {
120 return false;
121 }
122 seen_last_value_ = true;
123 percentile_ = 100.0;
124 return true;
125 }
126
127 if (counts_index_ == -1 && !basic_next()) {
128 return false;
129 }
130 do {
131 const double current_percentile{cumulative_count_ * 100.0 / h_.total_count()};
132 if (count_ != 0 && percentile_to_iterate_to_ <= current_percentile) {
134
135 percentile_ = percentile_to_iterate_to_;
136 const int64_t temp = (log(100.0 / (100.0 - percentile_to_iterate_to_)) / log(2)) + 1.0;
137 const int64_t half_distance = pow(2, static_cast<double>(temp));
138 const int64_t percentile_reporting_ticks{ticks_per_half_distance_ * half_distance};
139 percentile_to_iterate_to_ += 100.0 / percentile_reporting_ticks;
140
141 return true;
142 }
143 } while (basic_next());
144
145 return true;
146}
147
152
154
159
161{
162 while (basic_next()) {
163 if (count_ != 0) {
166 return true;
167 }
168 }
169 return false;
170}
171
174, value_units_per_bucket_{value_units_per_bucket}
175, next_value_reporting_level_{value_units_per_bucket}
176, next_value_reporting_level_lowest_equivalent_{h.lowest_equivalent_value(value_units_per_bucket)}
177{
178}
179
181{
183
184 if (has_next()
186 next_value_reporting_level_lowest_equivalent_)) {
187 do {
188 if (value_ >= next_value_reporting_level_lowest_equivalent_) {
189 update_iterated_values(next_value_reporting_level_);
190
191 next_value_reporting_level_ += value_units_per_bucket_;
192 next_value_reporting_level_lowest_equivalent_
193 = h_.lowest_equivalent_value(next_value_reporting_level_);
194
195 return true;
196 }
197 if (!move_next()) {
198 return true;
199 }
201 } while (true);
202 }
203 return false;
204}
205
207 double log_base) noexcept
209, log_base_{log_base}
210, next_value_reporting_level_{value_units_first_bucket}
211, next_value_reporting_level_lowest_equivalent_{h.lowest_equivalent_value(value_units_first_bucket)}
212{
213}
214
216{
218
219 if (has_next()
221 next_value_reporting_level_lowest_equivalent_)) {
222 do {
223 if (value_ >= next_value_reporting_level_lowest_equivalent_) {
224 update_iterated_values(next_value_reporting_level_);
225
226 next_value_reporting_level_ *= log_base_;
227 next_value_reporting_level_lowest_equivalent_
228 = h_.lowest_equivalent_value(next_value_reporting_level_);
229
230 return true;
231 }
232 if (!move_next()) {
233 return true;
234 }
236 } while (true);
237 }
238 return false;
239}
240
241} // namespace hdr
242} // namespace toolbox
CountAddedIterator is a recorded value iterator.
Definition Iterator.hpp:113
std::int64_t count_added_in_this_iteration_step_
Definition Iterator.hpp:132
CountAddedIterator(const CountAddedIterator &)=delete
A High Dynamic Range (HDR) Histogram.
Definition Histogram.hpp:64
std::int64_t total_count() const noexcept
Definition Histogram.hpp:85
std::int64_t value_at_index(std::int32_t index) const noexcept
std::int64_t highest_equivalent_value(std::int64_t value) const noexcept
std::int64_t counts_get_normalised(std::int32_t index) const noexcept
std::int64_t median_equivalent_value(std::int64_t value) const noexcept
std::int64_t lowest_equivalent_value(std::int64_t value) const noexcept
std::int32_t counts_len() const noexcept
Definition Histogram.hpp:86
Iterator is the base iterator for all iterator types.
Definition Iterator.hpp:28
std::int64_t median_equivalent_value_
Definition Iterator.hpp:81
void update_iterated_values(std::int64_t new_value_iterated_to) noexcept
Definition Iterator.cpp:101
std::int64_t cumulative_count_
Definition Iterator.hpp:77
std::int32_t counts_index_
Raw index into the counts array.
Definition Iterator.hpp:73
bool basic_next() noexcept
Definition Iterator.cpp:74
std::int64_t total_count_
Snapshot of the length at the time the iterator is created.
Definition Iterator.hpp:75
const Histogram & h_
Definition Iterator.hpp:71
std::int64_t count_
Definition Iterator.hpp:76
std::int64_t highest_equivalent_value_
Definition Iterator.hpp:79
bool has_next() const noexcept
Definition Iterator.cpp:55
bool has_buckets() const noexcept
Definition Iterator.cpp:50
std::int64_t value_
Definition Iterator.hpp:78
std::int64_t lowest_equivalent_value_
Definition Iterator.hpp:80
std::int64_t peek_next_value_from_index() const noexcept
Definition Iterator.cpp:60
virtual bool do_next() noexcept
Definition Iterator.cpp:41
Iterator(const Histogram &h) noexcept
Definition Iterator.cpp:26
bool move_next() noexcept
Definition Iterator.cpp:83
bool next_value_greater_than_reporting_level_upper_bound(std::int64_t reporting_level_upper_bound) const noexcept
Definition Iterator.cpp:65
LinearIterator(const Histogram &h, std::int64_t value_units_per_bucket) noexcept
bool do_next() noexcept override
Definition Iterator.cpp:180
LogIterator(const Histogram &h, std::int64_t value_units_first_bucket, double log_base) noexcept
bool do_next() noexcept override
Definition Iterator.cpp:215
PercentileIterator(const Histogram &h, std::int32_t ticks_per_half_distance) noexcept
bool do_next() noexcept override
Definition Iterator.cpp:116
bool do_next() noexcept override
Definition Iterator.cpp:160
RecordedIterator(const Histogram &h)
Definition Iterator.cpp:155
STL namespace.
constexpr auto bind() noexcept
Definition Slot.hpp:92