hdrhistogram/iterators/
recorded.rs

1use crate::core::counter::Counter;
2use crate::iterators::{HistogramIterator, PickMetadata, PickyIterator};
3use crate::Histogram;
4
5/// An iterator that will yield only bins with at least one sample.
6pub struct Iter {
7    visited: Option<usize>,
8}
9
10impl Iter {
11    /// Construct a new sampled iterator. See `Histogram::iter_recorded` for details.
12    pub fn new<T: Counter>(hist: &Histogram<T>) -> HistogramIterator<T, Iter> {
13        HistogramIterator::new(hist, Iter { visited: None })
14    }
15}
16
17impl<T: Counter> PickyIterator<T> for Iter {
18    fn pick(&mut self, index: usize, _: u64, count_at_index: T) -> Option<PickMetadata> {
19        if count_at_index != T::zero() && self.visited.map(|i| i != index).unwrap_or(true) {
20            self.visited = Some(index);
21            return Some(PickMetadata::new(None, None));
22        }
23        None
24    }
25
26    fn more(&mut self, _: usize) -> bool {
27        // more() is really more_with_zero_counts(), but here as this is the
28        // record-picker, we never visit empty bins, and thus there will never
29        // be `more()`
30        //
31        // If we yield a record, by definition the current bin cannot be empty,
32        // and as we iterate over record, once the last one is yielded, there
33        // can't any more bins to yield.
34        false
35    }
36}