tisonkun commented on code in PR #293:
URL: https://github.com/apache/datasketches-rust/pull/293#discussion_r4625826704


##########
datasketches/src/kll/sketch.rs:
##########
@@ -143,6 +143,72 @@ impl<T: Clone + Ord> KllSketch<T> {
         self.internal_update(item);
     }
 
+    /// Updates the sketch with an item repeated `weight` times.
+    ///
+    /// The result is equivalent to calling [`update`](Self::update) `weight` 
times, at a cost
+    /// that grows with the logarithm of `weight` rather than with `weight` 
itself. A weight of
+    /// zero is a no-op.
+    ///
+    /// # Panics
+    ///
+    /// Panics without modifying the sketch if the stream weight would exceed 
[`u64::MAX`].
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// # use datasketches::common::SearchCriteria;
+    /// # use datasketches::kll::KllSketch;
+    /// let mut sketch = KllSketch::<i64>::new(200).unwrap();
+    /// sketch.update_with_weight(1, 3);
+    /// sketch.update_with_weight(2, 1_000_000);
+    /// assert_eq!(sketch.n(), 1_000_003);
+    /// assert_eq!(
+    ///     sketch.rank(&1, SearchCriteria::Inclusive).unwrap(),
+    ///     3.0 / 1_000_003.0
+    /// );
+    /// ```
+    pub fn update_with_weight(&mut self, item: T, weight: u64) {
+        if weight == 0 {

Review Comment:
   Documented in d59d8c0 on `KllSketch::update_with_weight`: a weight of zero 
is a no-op (matching `CountMinSketch` and `FrequentItemsSketch`), with an 
explicit note that DataSketches Java and DataSketches C++ reject zero weights.



##########
datasketches/src/kll/sketch.rs:
##########
@@ -143,6 +143,72 @@ impl<T: Clone + Ord> KllSketch<T> {
         self.internal_update(item);
     }
 
+    /// Updates the sketch with an item repeated `weight` times.
+    ///
+    /// The result is equivalent to calling [`update`](Self::update) `weight` 
times, at a cost
+    /// that grows with the logarithm of `weight` rather than with `weight` 
itself. A weight of
+    /// zero is a no-op.
+    ///
+    /// # Panics
+    ///
+    /// Panics without modifying the sketch if the stream weight would exceed 
[`u64::MAX`].
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// # use datasketches::common::SearchCriteria;
+    /// # use datasketches::kll::KllSketch;
+    /// let mut sketch = KllSketch::<i64>::new(200).unwrap();
+    /// sketch.update_with_weight(1, 3);
+    /// sketch.update_with_weight(2, 1_000_000);
+    /// assert_eq!(sketch.n(), 1_000_003);
+    /// assert_eq!(
+    ///     sketch.rank(&1, SearchCriteria::Inclusive).unwrap(),
+    ///     3.0 / 1_000_003.0
+    /// );
+    /// ```
+    pub fn update_with_weight(&mut self, item: T, weight: u64) {
+        if weight == 0 {
+            return;
+        }
+        let final_n = self
+            .n
+            .checked_add(weight)
+            .expect("total stream weight overflow");
+        if weight < (self.capacity - self.num_retained) as u64 {

Review Comment:
   Updated in d59d8c0 to use `weight <= free_capacity as u64`, keeping updates 
up to full level-zero capacity (`total_weight() == k`) in exact mode.



##########
tests-integration/tests/kll_test/update.rs:
##########
@@ -0,0 +1,148 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::panic::AssertUnwindSafe;
+use std::panic::catch_unwind;
+
+use datasketches::common::SearchCriteria;
+use datasketches::kll::KllSketch;
+
+const DEFAULT_K: u16 = 200;
+const RANK_EPS_FOR_K_200: f64 = 0.0133;
+
+#[test]
+fn zero_weight_is_a_no_op() {
+    let mut sketch = KllSketch::<i64>::new(DEFAULT_K).unwrap();
+    sketch.update_with_weight(1, 0);
+    assert!(sketch.is_empty());
+    assert_eq!(sketch.min_item(), None);
+}
+
+#[test]
+fn weighted_update_matches_repeated_updates_in_exact_mode() {

Review Comment:
   Covered in 7de1af0: 
`exact_mode_and_capacity_boundary_match_repeated_updates` fills the remaining 
capacity with `weight == free_capacity` (`120 + 80 == 200`), asserts exact mode 
and byte-identical serialization against 200 plain updates, and then verifies 
that one more unit update transitions into estimation mode.



-- 
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]

Reply via email to