tisonkun commented on code in PR #71:
URL: https://github.com/apache/datasketches-rust/pull/71#discussion_r2700729507
##########
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);
Review Comment:
Maybe a more practical example? Ditto `halve`
@mrcroxx how can `halve` and `decay` practically be helpful?
--
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]