This is an automated email from the ASF dual-hosted git repository. tisonkun pushed a commit to branch hashable in repository https://gitbox.apache.org/repos/asf/datasketches-rust.git
commit a253bede488a68814c262acb37e62ac4f8aab310 Author: tison <[email protected]> AuthorDate: Thu May 7 00:25:40 2026 +0800 adapt Signed-off-by: tison <[email protected]> --- datasketches/src/common/mod.rs | 13 ---------- datasketches/src/cpc/sketch.rs | 36 +++++++++++++++------------ datasketches/src/hll/mod.rs | 8 ++++-- datasketches/src/hll/sketch.rs | 6 ++++- datasketches/src/theta/sketch.rs | 43 ++++++++------------------------- datasketches/tests/theta_sketch_test.rs | 9 ++++--- 6 files changed, 47 insertions(+), 68 deletions(-) diff --git a/datasketches/src/common/mod.rs b/datasketches/src/common/mod.rs index a43373a..36b734f 100644 --- a/datasketches/src/common/mod.rs +++ b/datasketches/src/common/mod.rs @@ -26,16 +26,3 @@ pub use self::resize::ResizeFactor; // private to datasketches crate pub(crate) mod binomial_bounds; pub(crate) mod inv_pow2_table; - -/// Canonicalize double value for compatibility with Java -pub(crate) fn canonical_double(value: f64) -> u64 { - if value.is_nan() { - // Java's Double.doubleToLongBits() NaN value - 0x7ff8000000000000u64 - } else { - // -0.0 + 0.0 == +0.0 under IEEE754 roundTiesToEven rounding mode, - // which Rust guarantees. Thus, by adding a positive zero we - // canonicalize signed zero without any branches in one instruction. - (value + 0.0).to_bits() - } -} diff --git a/datasketches/src/cpc/sketch.rs b/datasketches/src/cpc/sketch.rs index 534f0a0..9970aa3 100644 --- a/datasketches/src/cpc/sketch.rs +++ b/datasketches/src/cpc/sketch.rs @@ -24,7 +24,6 @@ use crate::codec::assert::ensure_serial_version_is; use crate::codec::assert::insufficient_data; use crate::codec::family::Family; use crate::common::NumStdDev; -use crate::common::canonical_double; use crate::common::inv_pow2_table::INVERSE_POWERS_OF_2; use crate::cpc::DEFAULT_LG_K; use crate::cpc::Flavor; @@ -170,9 +169,28 @@ impl CpcSketch { self.num_coupons == 0 } - /// Update the sketch with a hashable value. + /// Update the sketch with a hash value. /// - /// For `f32`/`f64` values, use `update_f32`/`update_f64` instead. + /// You may use `HashValue` for compatibility purpose. Read the + /// [module level documentation of `hash_value`](crate::hash_value) + /// for more details. + /// + /// # Examples + /// + /// ```rust + /// use datasketches::cpc::CpcSketch; + /// use datasketches::hash_value::HashValue; + /// + /// let mut sketch = CpcSketch::with_seed(11, 123); + /// sketch.update(1); + /// sketch.update(2); + /// sketch.update(3); + /// + /// let mut sketch = CpcSketch::with_seed(11, 123); + /// sketch.update(HashValue::canonical_f64(1.5)); + /// sketch.update(HashValue::canonical_f64(2.5)); + /// sketch.update(HashValue::canonical_f64(3.5)); + /// ``` pub fn update<T: Hash>(&mut self, value: T) { let mut hasher = MurmurHash3X64128::with_seed(self.seed); value.hash(&mut hasher); @@ -191,18 +209,6 @@ impl CpcSketch { self.row_col_update(row_col); } - /// Update the sketch with a f64 value. - pub fn update_f64(&mut self, value: f64) { - // Canonicalize double for compatibility with Java - let canonical = canonical_double(value); - self.update(canonical); - } - - /// Update the sketch with a f32 value. - pub fn update_f32(&mut self, value: f32) { - self.update_f64(value as f64); - } - pub(super) fn flavor(&self) -> Flavor { determine_flavor(self.lg_k, self.num_coupons) } diff --git a/datasketches/src/hll/mod.rs b/datasketches/src/hll/mod.rs index 1a3c2e3..3088540 100644 --- a/datasketches/src/hll/mod.rs +++ b/datasketches/src/hll/mod.rs @@ -204,14 +204,18 @@ impl Coupon { self.0 } - /// Compute the HLL coupon for a hashable value. + /// Compute the HLL coupon for a hash value. + /// + /// You may use `HashValue` for compatibility purpose. Read the + /// [module level documentation of `hash_value`](crate::hash_value) + /// for more details. /// /// Hashes `v` using MurmurHash3 128-bit and packs the result into a coupon: /// the low 26 bits of the low hash word become the slot index, and the /// leading-zero count of the high hash word (capped at 62, then plus one) /// becomes the 6-bit register value. #[inline(always)] - pub fn from_hash(v: impl Hash) -> Self { + pub fn from_hash<T: Hash>(v: T) -> Self { let mut hasher = MurmurHash3X64128::default(); v.hash(&mut hasher); let (lo, hi) = hasher.finish128(); diff --git a/datasketches/src/hll/sketch.rs b/datasketches/src/hll/sketch.rs index 5214d46..1b95aba 100644 --- a/datasketches/src/hll/sketch.rs +++ b/datasketches/src/hll/sketch.rs @@ -158,9 +158,13 @@ impl HllSketch { /// Update the sketch with a value. /// - /// Accepts any type that implements [`Hash`]. The value is hashed and converted to + /// Accepts any type that implements [`Hash`]. The value is hashed and converted to /// an internal coupon, which is then inserted into the sketch. /// + /// You may use `HashValue` for compatibility purpose. Read the + /// [module level documentation of `hash_value`](crate::hash_value) + /// for more details. + /// /// If you need to insert the same logical value into multiple sketches, consider /// pre-computing the coupon with [`Coupon::from_hash`] and calling /// [`update_with_coupon`](Self::update_with_coupon) on each sketch to avoid diff --git a/datasketches/src/theta/sketch.rs b/datasketches/src/theta/sketch.rs index fdba5fe..63ecd09 100644 --- a/datasketches/src/theta/sketch.rs +++ b/datasketches/src/theta/sketch.rs @@ -30,7 +30,6 @@ use crate::codec::family::Family; use crate::common::NumStdDev; use crate::common::ResizeFactor; use crate::common::binomial_bounds; -use crate::common::canonical_double; use crate::error::Error; use crate::hash::DEFAULT_UPDATE_SEED; use crate::hash::compute_seed_hash; @@ -105,50 +104,28 @@ impl ThetaSketch { ThetaSketchBuilder::default() } - /// Update the sketch with a hashable value. + /// Update the sketch with a hash value. /// - /// For `f32`/`f64` values, use `update_f32`/`update_f64` instead. + /// You may use `HashValue` for compatibility purpose. Read the + /// [module level documentation of `hash_value`](crate::hash_value) + /// for more details. /// /// # Examples /// /// ``` - /// # use datasketches::theta::ThetaSketch; - /// let mut sketch = ThetaSketch::builder().build(); - /// sketch.update("apple"); - /// assert!(sketch.estimate() >= 1.0); - /// ``` - pub fn update<T: Hash>(&mut self, value: T) { - self.table.try_insert(value); - } - - /// Update the sketch with a f64 value. - /// - /// # Examples + /// use datasketches::theta::ThetaSketch; + /// use datasketches::hash_value::HashValue; /// - /// ``` - /// # use datasketches::theta::ThetaSketch; /// let mut sketch = ThetaSketch::builder().build(); - /// sketch.update_f64(1.0); + /// sketch.update("apple"); /// assert!(sketch.estimate() >= 1.0); - /// ``` - pub fn update_f64(&mut self, value: f64) { - // Canonicalize double for compatibility with Java - let canonical = canonical_double(value); - self.update(canonical); - } - - /// Update the sketch with a f32 value. /// - /// # Examples - /// - /// ``` - /// # use datasketches::theta::ThetaSketch; /// let mut sketch = ThetaSketch::builder().build(); - /// sketch.update_f32(1.0); + /// sketch.update(HashValue::canonical_f64(3.5)); /// assert!(sketch.estimate() >= 1.0); /// ``` - pub fn update_f32(&mut self, value: f32) { - self.update_f64(value as f64); + pub fn update<T: Hash>(&mut self, value: T) { + self.table.try_insert(value); } /// Return cardinality estimate diff --git a/datasketches/tests/theta_sketch_test.rs b/datasketches/tests/theta_sketch_test.rs index 73045cd..ca9b5c3 100644 --- a/datasketches/tests/theta_sketch_test.rs +++ b/datasketches/tests/theta_sketch_test.rs @@ -16,6 +16,7 @@ // under the License. use datasketches::common::NumStdDev; +use datasketches::hash_value::HashValue; use datasketches::theta::ThetaSketch; #[test] @@ -39,10 +40,10 @@ fn test_update_various_types() { sketch.update("string"); sketch.update(42i64); sketch.update(42u64); - sketch.update_f64(3.15); - sketch.update_f64(3.15); - sketch.update_f32(3.15); - sketch.update_f32(3.15); + sketch.update(HashValue::canonical_f64(3.15)); + sketch.update(HashValue::canonical_f64(3.15)); + sketch.update(HashValue::canonical_f32(3.15)); + sketch.update(HashValue::canonical_f32(3.15)); sketch.update([1u8, 2, 3]); assert!(!sketch.is_empty()); --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
