tisonkun commented on code in PR #71:
URL: https://github.com/apache/datasketches-rust/pull/71#discussion_r2700729047
##########
datasketches/src/countmin/sketch.rs:
##########
@@ -180,6 +180,51 @@ impl CountMinSketch {
}
}
+ /// Divides every counter by two, truncating toward zero.
+ ///
+ /// Useful for exponential decay where counts represent recent activity.
+ ///
+ /// # Examples
+ ///
+ /// ```rust
+ /// # use datasketches::countmin::CountMinSketch;
+ /// let mut sketch = CountMinSketch::new(4, 128);
+ /// sketch.update_with_weight("apple", 3);
+ /// sketch.halve();
+ /// assert!(sketch.estimate("apple") >= 1);
+ /// ```
+ pub fn halve(&mut self) {
+ self.counts.iter_mut().for_each(|c| *c /= 2);
+ self.total_weight /= 2;
+ }
+
+ /// Multiplies every counter by `decay` and truncates back into `i64`.
+ ///
+ /// Values are truncated toward zero after multiplication; choose `decay`
in `(0, 1]`.
+ /// The total weight is scaled by the same factor to keep bounds
consistent.
+ ///
+ /// # Examples
+ ///
+ /// ```rust
+ /// # use datasketches::countmin::CountMinSketch;
+ /// let mut sketch = CountMinSketch::new(4, 128);
+ /// sketch.update_with_weight("apple", 3);
+ /// sketch.decay(0.5);
+ /// assert!(sketch.estimate("apple") >= 1);
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// Panics if `decay` is not finite or is outside `(0, 1]`.
+ pub fn decay(&mut self, decay: f64) {
+ assert!(decay.is_finite(), "decay must be finite");
+ assert!(decay > 0.0 && decay <= 1.0, "decay must be within (0, 1]");
Review Comment:
```suggestion
assert!(decay > 0.0 && decay <= 1.0, "decay must be within (0, 1]");
```
Infinite or NaN values cannot pass this assertion already.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]