use crate::core::counter::Counter;
use crate::iterators::{HistogramIterator, PickMetadata, PickyIterator};
use crate::Histogram;
pub struct Iter<'a, T: 'a + Counter> {
hist: &'a Histogram<T>,
value_units_per_bucket: u64,
current_step_highest_value_reporting_level: u64,
current_step_lowest_value_reporting_level: u64,
}
impl<'a, T: 'a + Counter> Iter<'a, T> {
pub fn new(
hist: &'a Histogram<T>,
value_units_per_bucket: u64,
) -> HistogramIterator<'a, T, Iter<'a, T>> {
assert!(
value_units_per_bucket > 0,
"value_units_per_bucket must be > 0"
);
let new_lowest = hist.lowest_equivalent(value_units_per_bucket - 1);
HistogramIterator::new(
hist,
Iter {
hist,
value_units_per_bucket,
current_step_highest_value_reporting_level: value_units_per_bucket - 1,
current_step_lowest_value_reporting_level: new_lowest,
},
)
}
}
impl<'a, T: 'a + Counter> PickyIterator<T> for Iter<'a, T> {
fn pick(&mut self, index: usize, _: u64, _: T) -> Option<PickMetadata> {
let val = self.hist.value_for(index);
if val >= self.current_step_lowest_value_reporting_level || index == self.hist.last_index()
{
let metadata =
PickMetadata::new(None, Some(self.current_step_highest_value_reporting_level));
self.current_step_highest_value_reporting_level += self.value_units_per_bucket;
self.current_step_lowest_value_reporting_level = self
.hist
.lowest_equivalent(self.current_step_highest_value_reporting_level);
Some(metadata)
} else {
None
}
}
fn more(&mut self, index_to_pick: usize) -> bool {
let next_index = index_to_pick.checked_add(1).expect("usize overflow");
self.current_step_highest_value_reporting_level < self.hist.value_for(next_index)
}
}