Skip to content

Commit 6f99560

Browse files
committed
len -> distinct_values, count -> len
This is a better match for what "len" normally means. For example, `HashMap::len` doesn't return the number of buckets, but the number of *values*. It also matches better the intuition that `len` and `is_empty` are related (whereas `count` and `is_empty` being related is less obvious).
1 parent f22e8dd commit 6f99560

File tree

11 files changed

+75
-67
lines changed

11 files changed

+75
-67
lines changed

benches/serialization.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ fn do_serialize_bench<S>(
171171
S: TestOnlyHypotheticalSerializerInterface,
172172
{
173173
let mut h = Histogram::<u64>::new_with_bounds(low, high, digits).unwrap();
174-
let random_counts = (fraction_of_counts_len * h.len() as f64) as usize;
174+
let random_counts = (fraction_of_counts_len * h.distinct_values() as f64) as usize;
175175
let mut vec = Vec::with_capacity(random_counts);
176176

177177
let range = Range::new(low, high);
@@ -199,7 +199,7 @@ fn do_deserialize_bench<S>(
199199
S: TestOnlyHypotheticalSerializerInterface,
200200
{
201201
let mut h = Histogram::<u64>::new_with_bounds(low, high, digits).unwrap();
202-
let random_counts = (fraction_of_counts_len * h.len() as f64) as usize;
202+
let random_counts = (fraction_of_counts_len * h.distinct_values() as f64) as usize;
203203
let mut vec = Vec::with_capacity(random_counts);
204204

205205
let range = Range::new(low, high);

examples/cli.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,13 @@ fn quantiles<R: BufRead, W: Write>(
212212
"StdDeviation",
213213
hist.stdev(),
214214
)?;
215-
write_extra_data(&mut writer, "Max", hist.max(), "Total count", hist.count())?;
215+
write_extra_data(&mut writer, "Max", hist.max(), "Total count", hist.len())?;
216216
write_extra_data(
217217
&mut writer,
218218
"Buckets",
219219
hist.buckets(),
220220
"SubBuckets",
221-
hist.len(),
221+
hist.distinct_values(),
222222
)?;
223223

224224
Ok(())

src/iterators/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ where
180180
// unless we have ended.
181181
while !self.ended {
182182
// have we reached the end?
183-
if self.current_index == self.hist.len() {
183+
if self.current_index == self.hist.distinct_values() {
184184
self.ended = true;
185185
return None;
186186
}
@@ -194,7 +194,7 @@ where
194194
}
195195
} else {
196196
// nope -- alright, let's keep iterating
197-
assert!(self.current_index < self.hist.len());
197+
assert!(self.current_index < self.hist.distinct_values());
198198

199199
if self.fresh {
200200
// at a new index, and not past the max, so there's nonzero counts to add
@@ -218,7 +218,7 @@ where
218218
self.total_count_to_index,
219219
self.count_at_index,
220220
) {
221-
let quantile = self.total_count_to_index as f64 / self.hist.count() as f64;
221+
let quantile = self.total_count_to_index as f64 / self.hist.len() as f64;
222222
let val = IterationValue {
223223
value_iterated_to: metadata.value_iterated_to.unwrap_or_else(|| {
224224
self.hist

src/iterators/quantile.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl<'a, T: 'a + Counter> PickyIterator<T> for Iter<'a, T> {
4242

4343
// This calculation, combined with the `quantile * count` in `value_at_quantile`, tends
4444
// to produce a count_at_quantile that is 1 ulp wrong. That's just the way IEEE754 works.
45-
let current_quantile = running_total as f64 / self.hist.count() as f64;
45+
let current_quantile = running_total as f64 / self.hist.len() as f64;
4646
if current_quantile < self.quantile_to_iterate_to {
4747
return None;
4848
}

src/lib.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ impl<T: Counter> Histogram<T> {
284284
// ********************************************************************************************
285285

286286
/// Get the current number of distinct values that can be represented in the histogram.
287-
pub fn len(&self) -> usize {
287+
pub fn distinct_values(&self) -> usize {
288288
self.counts.len()
289289
}
290290

@@ -304,10 +304,16 @@ impl<T: Counter> Histogram<T> {
304304
}
305305

306306
/// Get the total number of samples recorded.
307+
#[deprecated(since = "6.0.0", note = "use `len` instead")]
307308
pub fn count(&self) -> u64 {
308309
self.total_count
309310
}
310311

312+
/// Get the total number of samples recorded.
313+
pub fn len(&self) -> u64 {
314+
self.total_count
315+
}
316+
311317
/// Returns true if this histogram has no recorded values.
312318
pub fn is_empty(&self) -> bool {
313319
self.total_count == 0
@@ -372,7 +378,9 @@ impl<T: Counter> Histogram<T> {
372378

373379
/// Get the index of the last histogram bin.
374380
fn last_index(&self) -> usize {
375-
self.len().checked_sub(1).expect("Empty counts array?")
381+
self.distinct_values()
382+
.checked_sub(1)
383+
.expect("Empty counts array?")
376384
}
377385

378386
// ********************************************************************************************
@@ -452,7 +460,7 @@ impl<T: Counter> Histogram<T> {
452460
// Counts arrays are of the same length and meaning,
453461
// so we can just iterate and add directly:
454462
let mut observed_other_total_count: u64 = 0;
455-
for i in 0..source.len() {
463+
for i in 0..source.distinct_values() {
456464
let other_count = source
457465
.count_at_index(i)
458466
.expect("iterating inside source length");
@@ -562,7 +570,7 @@ impl<T: Counter> Histogram<T> {
562570
// If total_count is at the max value, it may have saturated, so we must restat
563571
let mut needs_restat = self.total_count == u64::max_value();
564572

565-
for i in 0..subtrahend.len() {
573+
for i in 0..subtrahend.distinct_values() {
566574
let other_count = subtrahend
567575
.count_at_index(i)
568576
.expect("index inside subtrahend len must exist");
@@ -599,7 +607,7 @@ impl<T: Counter> Histogram<T> {
599607
}
600608

601609
if needs_restat {
602-
let l = self.len();
610+
let l = self.distinct_values();
603611
self.restat(l);
604612
}
605613

@@ -785,7 +793,7 @@ impl<T: Counter> Histogram<T> {
785793
// h.start_time = source.start_time;
786794
// h.end_time = source.end_time;
787795
h.auto_resize = source.auto_resize;
788-
h.counts.resize(source.len(), T::zero());
796+
h.counts.resize(source.distinct_values(), T::zero());
789797
h
790798
}
791799

src/tests/subtract.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn assert_min_max_count<T: Counter, B: Borrow<Histogram<T>>>(hist: B) {
1111
let mut min = None;
1212
let mut max = None;
1313
let mut total = 0;
14-
for i in 0..h.len() {
14+
for i in 0..h.distinct_values() {
1515
let value = h.value_for(i);
1616
let count = h.count_at(value);
1717
if count == T::zero() {
@@ -28,7 +28,7 @@ fn assert_min_max_count<T: Counter, B: Borrow<Histogram<T>>>(hist: B) {
2828

2929
assert_eq!(min, h.min());
3030
assert_eq!(max, h.max());
31-
assert_eq!(total, h.count());
31+
assert_eq!(total, h.len());
3232
}
3333

3434
#[test]
@@ -44,17 +44,17 @@ fn subtract_after_add() {
4444
h1.add(&h2).unwrap();
4545
assert_eq!(h1.count_at(TEST_VALUE_LEVEL), 2);
4646
assert_eq!(h1.count_at(1000 * TEST_VALUE_LEVEL), 2);
47-
assert_eq!(h1.count(), 4);
47+
assert_eq!(h1.len(), 4);
4848

4949
h1 += &h2;
5050
assert_eq!(h1.count_at(TEST_VALUE_LEVEL), 3);
5151
assert_eq!(h1.count_at(1000 * TEST_VALUE_LEVEL), 3);
52-
assert_eq!(h1.count(), 6);
52+
assert_eq!(h1.len(), 6);
5353

5454
h1.subtract(&h2).unwrap();
5555
assert_eq!(h1.count_at(TEST_VALUE_LEVEL), 2);
5656
assert_eq!(h1.count_at(1000 * TEST_VALUE_LEVEL), 2);
57-
assert_eq!(h1.count(), 4);
57+
assert_eq!(h1.len(), 4);
5858

5959
assert_min_max_count(h1);
6060
assert_min_max_count(h2);
@@ -69,13 +69,13 @@ fn subtract_to_zero_counts() {
6969

7070
assert_eq!(h1.count_at(TEST_VALUE_LEVEL), 1);
7171
assert_eq!(h1.count_at(1000 * TEST_VALUE_LEVEL), 1);
72-
assert_eq!(h1.count(), 2);
72+
assert_eq!(h1.len(), 2);
7373

7474
let clone = h1.clone();
7575
h1.subtract(&clone).unwrap();
7676
assert_eq!(h1.count_at(TEST_VALUE_LEVEL), 0);
7777
assert_eq!(h1.count_at(1000 * TEST_VALUE_LEVEL), 0);
78-
assert_eq!(h1.count(), 0);
78+
assert_eq!(h1.len(), 0);
7979

8080
assert_min_max_count(h1);
8181
}
@@ -141,14 +141,14 @@ fn subtract_values_inside_minuend_range_works() {
141141
assert_eq!(big.count_at(TEST_VALUE_LEVEL), 3);
142142
assert_eq!(big.count_at(1000 * TEST_VALUE_LEVEL), 3);
143143
assert_eq!(big.count_at(2 * max), 3); // overflow smaller hist...
144-
assert_eq!(big.count(), 9);
144+
assert_eq!(big.len(), 9);
145145

146146
// Subtracting the smaller histogram from the bigger one should work:
147147
big -= &h1;
148148
assert_eq!(big.count_at(TEST_VALUE_LEVEL), 2);
149149
assert_eq!(big.count_at(1000 * TEST_VALUE_LEVEL), 2);
150150
assert_eq!(big.count_at(2 * max), 3); // overflow smaller hist...
151-
assert_eq!(big.count(), 7);
151+
assert_eq!(big.len(), 7);
152152

153153
assert_min_max_count(h1);
154154
assert_min_max_count(big);
@@ -172,7 +172,7 @@ fn subtract_values_strictly_inside_minuend_range_yields_same_min_max_no_restat()
172172

173173
assert_eq!(1, h1.min());
174174
assert_eq!(1000, h1.max());
175-
assert_eq!(2, h1.count());
175+
assert_eq!(2, h1.len());
176176

177177
assert_min_max_count(h1);
178178
assert_min_max_count(h2);
@@ -340,7 +340,7 @@ fn subtract_values_minuend_saturated_total_recalculates_saturated() {
340340
assert_eq!(1, h1.min());
341341
assert_eq!(1000, h1.max());
342342
// still saturated
343-
assert_eq!(u64::max_value(), h1.count());
343+
assert_eq!(u64::max_value(), h1.len());
344344

345345
assert_min_max_count(h1);
346346
assert_min_max_count(h2);
@@ -368,7 +368,7 @@ fn subtract_values_minuend_saturated_total_recalculates_not_saturated() {
368368
assert_eq!(1, h1.min());
369369
assert_eq!(1000, h1.max());
370370
// not saturated
371-
assert_eq!(u64::max_value() / 16 * 15, h1.count());
371+
assert_eq!(u64::max_value() / 16 * 15, h1.len());
372372

373373
assert_min_max_count(h1);
374374
assert_min_max_count(h2);

tests/auto_resize.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ fn histogram_autosizing_edges() {
99
let mut histogram = Histogram::<u64>::new(3).unwrap();
1010
histogram += (1_u64 << 62) - 1;
1111
assert_eq!(histogram.buckets(), 52);
12-
assert_eq!(histogram.len(), 54272);
12+
assert_eq!(histogram.distinct_values(), 54272);
1313
histogram += u64::max_value();
1414
assert_eq!(histogram.buckets(), 54);
1515
// unit magnitude = floor(log_2 (1 / 2)) = 0
1616
// sub bucket count magnitude = floor(log_2 (2 * 10^3)) = 10
1717
// sub bucket half count mag = 9
1818
// sub bucket count = 2^(sbhcm + 1) = 2^9 = 1024
1919
// total array size = (54 + 1) * (sub bucket count / 2) = 56320
20-
assert_eq!(histogram.len(), 56320);
20+
assert_eq!(histogram.distinct_values(), 56320);
2121
}
2222

2323
#[test]
@@ -27,7 +27,7 @@ fn histogram_autosizing() {
2727
histogram += 1_u64 << i;
2828
}
2929
assert_eq!(histogram.buckets(), 53);
30-
assert_eq!(histogram.len(), 55296);
30+
assert_eq!(histogram.distinct_values(), 55296);
3131
}
3232

3333
#[test]

tests/data_access.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ fn scaling_equivalence() {
8787
} = load_histograms();
8888

8989
assert_near!(hist.mean() * SCALEF as f64, scaled_hist.mean(), 0.000001);
90-
assert_eq!(hist.count(), scaled_hist.count());
90+
assert_eq!(hist.len(), scaled_hist.len());
9191

9292
let expected_99th = hist.value_at_quantile(0.99) * 512;
9393
let scaled_99th = scaled_hist.value_at_quantile(0.99);
@@ -100,7 +100,7 @@ fn scaling_equivalence() {
100100
// averages should be equivalent
101101
assert_near!(hist.mean() * SCALEF as f64, scaled_hist.mean(), 0.000001);
102102
// total count should be the same
103-
assert_eq!(hist.count(), scaled_hist.count());
103+
assert_eq!(hist.len(), scaled_hist.len());
104104
// 99%'iles should be equivalent
105105
assert_eq!(
106106
scaled_hist.highest_equivalent(hist.value_at_quantile(0.99) * 512),
@@ -117,7 +117,7 @@ fn scaling_equivalence() {
117117
// averages should be equivalent
118118
assert_near!(post.mean() * SCALEF as f64, scaled_post.mean(), 0.000001);
119119
// total count should be the same
120-
assert_eq!(post.count(), scaled_post.count());
120+
assert_eq!(post.len(), scaled_post.len());
121121
// 99%'iles should be equivalent
122122
assert_eq!(
123123
post.lowest_equivalent(post.value_at_quantile(0.99)) * SCALEF,
@@ -134,8 +134,8 @@ fn scaling_equivalence() {
134134
fn total_count() {
135135
let Loaded { hist, raw, .. } = load_histograms();
136136

137-
assert_eq!(raw.count(), 10001);
138-
assert_eq!(hist.count(), 20000);
137+
assert_eq!(raw.len(), 10001);
138+
assert_eq!(hist.len(), 20000);
139139
}
140140

141141
#[test]
@@ -488,7 +488,7 @@ fn iter_all() {
488488
// TODO: also test total count and total value once the iterator exposes this
489489
num += 1;
490490
}
491-
assert_eq!(num, hist.len());
491+
assert_eq!(num, hist.distinct_values());
492492

493493
num = 0;
494494
let mut total_added_counts = 0;
@@ -505,7 +505,7 @@ fn iter_all() {
505505
total_added_counts += v.count_since_last_iteration();
506506
num += 1;
507507
}
508-
assert_eq!(num, hist.len());
508+
assert_eq!(num, hist.distinct_values());
509509
assert_eq!(total_added_counts, 20000);
510510
}
511511

@@ -528,16 +528,16 @@ fn value_duplication() {
528528
let histogram1 = hist.clone();
529529

530530
let mut num = 0;
531-
let mut ranges = Vec::with_capacity(histogram1.len());
532-
let mut counts = Vec::with_capacity(histogram1.len());
531+
let mut ranges = Vec::with_capacity(histogram1.distinct_values());
532+
let mut counts = Vec::with_capacity(histogram1.distinct_values());
533533
for v in histogram1.iter_all() {
534534
if v.count_since_last_iteration() > 0 {
535535
ranges.push(v.value_iterated_to());
536536
counts.push(v.count_since_last_iteration());
537537
}
538538
num += 1;
539539
}
540-
assert_eq!(num, histogram1.len());
540+
assert_eq!(num, histogram1.distinct_values());
541541

542542
let mut histogram2 = Histogram::new_with_max(TRACKABLE_MAX, SIGFIG).unwrap();
543543
for i in 0..ranges.len() {
@@ -564,5 +564,5 @@ fn total_count_exceeds_bucket_type() {
564564
h.record(100_000).unwrap();
565565
}
566566

567-
assert_eq!(400, h.count());
567+
assert_eq!(400, h.len());
568568
}

0 commit comments

Comments
 (0)