Skip to content

Commit 6de1b9b

Browse files
authored
Fix #124 Record iteration when recording only zeros (#125)
As pointed in the comment in the corresponding issues, we want to ensure we trigger the picker logic at least once to keep the invariant of the loop, that is to say that the picking logic has been executed at least once for last_picked_index, which is not the case for zero when there are only zeros. To do so we use and Option, and initialize to None, to make sure the pick logic is ran at least once. I added a test that collect the record and check for their count. Fixes #124
1 parent bdfad24 commit 6de1b9b

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/iterators/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub struct HistogramIterator<'a, T: 'a + Counter, P: PickyIterator<T>> {
7474
count_since_last_iteration: u64,
7575
count_at_index: T,
7676
current_index: usize,
77-
last_picked_index: usize,
77+
last_picked_index: Option<usize>,
7878
max_value_index: usize,
7979
fresh: bool,
8080
ended: bool,
@@ -152,7 +152,7 @@ impl<'a, T: Counter, P: PickyIterator<T>> HistogramIterator<'a, T, P> {
152152
count_since_last_iteration: 0,
153153
count_at_index: T::zero(),
154154
current_index: 0,
155-
last_picked_index: 0,
155+
last_picked_index: None,
156156
max_value_index: h.index_for(h.max()).expect("Either 0 or an existing index"),
157157
picker,
158158
fresh: true,
@@ -186,7 +186,7 @@ where
186186
}
187187

188188
// Have we already picked the index with the last non-zero count in the histogram?
189-
if self.last_picked_index >= self.max_value_index {
189+
if self.last_picked_index >= Some(self.max_value_index) {
190190
// is the picker done?
191191
if !self.picker.more(self.current_index) {
192192
self.ended = true;
@@ -243,7 +243,7 @@ where
243243
// step multiple times without advancing the index.
244244

245245
self.count_since_last_iteration = 0;
246-
self.last_picked_index = self.current_index;
246+
self.last_picked_index = Some(self.current_index);
247247
return Some(val);
248248
}
249249

tests/histogram.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,3 +560,10 @@ fn subtract_underflow_guarded_by_per_value_count_check() {
560560
h.subtract(h2).unwrap_err()
561561
);
562562
}
563+
564+
#[test]
565+
fn recorded_only_zeros() {
566+
let mut h = Histogram::<u64>::new(1).unwrap();
567+
h += 0;
568+
assert_eq!(h.iter_recorded().count(), 1);
569+
}

0 commit comments

Comments
 (0)