From 1f7c3aeeb4d0b56318205b2dc2f09c0c2e0f2586 Mon Sep 17 00:00:00 2001 From: Paul Loyd Date: Wed, 10 Apr 2024 10:20:42 +0200 Subject: [PATCH] Fix `DDSketch::length()` to count also the negative store --- src/ddsketch.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/ddsketch.rs b/src/ddsketch.rs index 8133aa8..bca9912 100644 --- a/src/ddsketch.rs +++ b/src/ddsketch.rs @@ -161,7 +161,7 @@ impl DDSketch { /// Returns the length of the underlying `Store`. This is mainly only useful for understanding /// how much the sketch has grown given the inserted values. pub fn length(&self) -> usize { - self.store.length() as usize + self.store.length() as usize + self.negative_store.length() as usize } /// Merge the contents of another sketch into this one. The sketch that is merged into this one @@ -352,4 +352,22 @@ mod tests { assert!(dd.quantile(0.5).unwrap().is_some()); assert!(dd.quantile(0.75).unwrap().is_some()); } + + #[test] + fn test_length() { + let mut dd = DDSketch::default(); + assert_eq!(dd.length(), 0); + + dd.add(1.0); + assert_eq!(dd.length(), 128); + dd.add(2.0); + dd.add(3.0); + assert_eq!(dd.length(), 128); + + dd.add(-1.0); + assert_eq!(dd.length(), 256); + dd.add(-2.0); + dd.add(-3.0); + assert_eq!(dd.length(), 256); + } }