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 c19e11cd11dc7b30c20964d33e6ce36a747615fd Author: tison <[email protected]> AuthorDate: Thu May 7 09:19:13 2026 +0800 fixup Signed-off-by: tison <[email protected]> --- datasketches/src/cpc/sketch.rs | 13 ++++--- datasketches/src/hash_value/canonical.rs | 15 ++------- datasketches/src/hash_value/mod.rs | 58 -------------------------------- datasketches/src/hll/mod.rs | 5 ++- datasketches/src/hll/sketch.rs | 10 ++++-- datasketches/src/theta/sketch.rs | 9 +++-- datasketches/tests/theta_sketch_test.rs | 10 +++--- 7 files changed, 27 insertions(+), 93 deletions(-) diff --git a/datasketches/src/cpc/sketch.rs b/datasketches/src/cpc/sketch.rs index 9970aa3..028e57b 100644 --- a/datasketches/src/cpc/sketch.rs +++ b/datasketches/src/cpc/sketch.rs @@ -171,15 +171,14 @@ impl CpcSketch { /// Update the sketch with a hash value. /// - /// You may use `HashValue` for compatibility purpose. Read the - /// [module level documentation of `hash_value`](crate::hash_value) - /// for more details. + /// You may use [`hash_value`](crate::hash_value) for generating canonical hash values, and/or + /// being compatible with other datasketches implementations. /// /// # Examples /// /// ```rust /// use datasketches::cpc::CpcSketch; - /// use datasketches::hash_value::HashValue; + /// use datasketches::hash_value; /// /// let mut sketch = CpcSketch::with_seed(11, 123); /// sketch.update(1); @@ -187,9 +186,9 @@ impl CpcSketch { /// 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)); + /// sketch.update(hash_value::canonical_f64(1.5)); + /// sketch.update(hash_value::canonical_f64(2.5)); + /// sketch.update(hash_value::canonical_f64(3.5)); /// ``` pub fn update<T: Hash>(&mut self, value: T) { let mut hasher = MurmurHash3X64128::with_seed(self.seed); diff --git a/datasketches/src/hash_value/canonical.rs b/datasketches/src/hash_value/canonical.rs index 97b809f..2966056 100644 --- a/datasketches/src/hash_value/canonical.rs +++ b/datasketches/src/hash_value/canonical.rs @@ -303,14 +303,8 @@ pub fn canonical_u32(v: u32) -> Canonical<u32> { /// ``` /// # use datasketches::hash_value::calculate_hash; /// # use datasketches::hash_value::canonical_i64; -/// assert_eq!( -/// calculate_hash(canonical_i64(42)), -/// calculate_hash(42i64) -/// ); -/// assert_eq!( -/// calculate_hash(canonical_i64(-1)), -/// calculate_hash(-1i64) -/// ); +/// assert_eq!(calculate_hash(canonical_i64(42)), calculate_hash(42i64)); +/// assert_eq!(calculate_hash(canonical_i64(-1)), calculate_hash(-1i64)); /// ``` pub fn canonical_i64(v: i64) -> Canonical<i64> { Canonical(v) @@ -326,10 +320,7 @@ pub fn canonical_i64(v: i64) -> Canonical<i64> { /// ``` /// # use datasketches::hash_value::calculate_hash; /// # use datasketches::hash_value::canonical_u64; -/// assert_eq!( -/// calculate_hash(canonical_u64(42)), -/// calculate_hash(42u64) -/// ); +/// assert_eq!(calculate_hash(canonical_u64(42)), calculate_hash(42u64)); /// assert_eq!( /// calculate_hash(canonical_u64(u64::MAX)), /// calculate_hash(u64::MAX) diff --git a/datasketches/src/hash_value/mod.rs b/datasketches/src/hash_value/mod.rs index d4d88e1..2d908da 100644 --- a/datasketches/src/hash_value/mod.rs +++ b/datasketches/src/hash_value/mod.rs @@ -29,61 +29,3 @@ pub fn calculate_hash<T: Hash>(t: T) -> u64 { t.hash(&mut s); s.finish() } - -/// HashValue -#[derive(Eq, PartialEq, Clone, Copy, Debug)] -pub struct HashValue<T> { - value: T, -} - -impl<T: Hash> Hash for HashValue<T> { - fn hash<H: Hasher>(&self, state: &mut H) { - self.value.hash(state); - } -} - -impl<T: Hash> HashValue<T> { - /// Create a `HashValue` from a hashable value. - pub fn from_hash(value: T) -> Self { - HashValue { value } - } -} - -impl HashValue<u64> { - /// Create a `HashValue` from a `f64` value using the canonical mapping. - #[inline(always)] - pub fn canonical_f64(value: f64) -> Self { - HashValue { - value: 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() - }, - } - } - - /// Create a `HashValue` from a `f32` value using the canonical mapping. - #[inline(always)] - pub fn canonical_f32(value: f32) -> Self { - HashValue::canonical_f64(value as f64) - } -} - -impl HashValue<i64> { - /// Create a `HashValue` from an `i8` value by casting the value to `i64`. - /// - /// This canonical mapping ensures that the same hash value is produced for the same numeric - /// value, regardless of the original type. For example, `HashValue::canonical_i8(42)` will - /// produce the same hash value as `HashValue::from_hash(42i64)`. - /// - /// This is compatible with datasketches-cpp's behavior. - pub fn canonical_i8(value: i8) -> Self { - HashValue { - value: value as i64, - } - } -} diff --git a/datasketches/src/hll/mod.rs b/datasketches/src/hll/mod.rs index 3088540..c83142a 100644 --- a/datasketches/src/hll/mod.rs +++ b/datasketches/src/hll/mod.rs @@ -206,9 +206,8 @@ impl Coupon { /// 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. + /// You may use [`hash_value`](crate::hash_value) for generating canonical hash values, and/or + /// being compatible with other datasketches implementations. /// /// 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 diff --git a/datasketches/src/hll/sketch.rs b/datasketches/src/hll/sketch.rs index 1b95aba..98e65e9 100644 --- a/datasketches/src/hll/sketch.rs +++ b/datasketches/src/hll/sketch.rs @@ -161,9 +161,8 @@ impl HllSketch { /// 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. + /// You may use [`hash_value`](crate::hash_value) for generating canonical hash values, and/or + /// being compatible with other datasketches implementations. /// /// If you need to insert the same logical value into multiple sketches, consider /// pre-computing the coupon with [`Coupon::from_hash`] and calling @@ -175,9 +174,14 @@ impl HllSketch { /// ``` /// # use datasketches::hll::HllSketch; /// # use datasketches::hll::HllType; + /// # use datasketches::hash_value; /// let mut sketch = HllSketch::new(10, HllType::Hll8); /// sketch.update("apple"); /// assert!(sketch.estimate() >= 1.0); + /// + /// let mut sketch = HllSketch::new(10, HllType::Hll8); + /// sketch.update(hash_value::canonical_str("apple")); + /// assert!(sketch.estimate() >= 1.0); /// ``` pub fn update<T: Hash>(&mut self, value: T) { self.update_with_coupon(Coupon::from_hash(value)); diff --git a/datasketches/src/theta/sketch.rs b/datasketches/src/theta/sketch.rs index b2c8b80..ff6fbaf 100644 --- a/datasketches/src/theta/sketch.rs +++ b/datasketches/src/theta/sketch.rs @@ -106,14 +106,13 @@ impl ThetaSketch { /// Update the sketch with a hash value. /// - /// You may use `HashValue` for compatibility purpose. Read the - /// [module level documentation of `hash_value`](crate::hash_value) - /// for more details. + /// You may use [`hash_value`](crate::hash_value) for generating canonical hash values, and/or + /// being compatible with other datasketches implementations. /// /// # Examples /// /// ``` - /// use datasketches::hash_value::HashValue; + /// use datasketches::hash_value; /// use datasketches::theta::ThetaSketch; /// /// let mut sketch = ThetaSketch::builder().build(); @@ -121,7 +120,7 @@ impl ThetaSketch { /// assert!(sketch.estimate() >= 1.0); /// /// let mut sketch = ThetaSketch::builder().build(); - /// sketch.update(HashValue::canonical_f64(3.5)); + /// sketch.update(hash_value::canonical_str("apple")); /// assert!(sketch.estimate() >= 1.0); /// ``` pub fn update<T: Hash>(&mut self, value: T) { diff --git a/datasketches/tests/theta_sketch_test.rs b/datasketches/tests/theta_sketch_test.rs index ca9b5c3..e2d959a 100644 --- a/datasketches/tests/theta_sketch_test.rs +++ b/datasketches/tests/theta_sketch_test.rs @@ -16,7 +16,7 @@ // under the License. use datasketches::common::NumStdDev; -use datasketches::hash_value::HashValue; +use datasketches::hash_value; use datasketches::theta::ThetaSketch; #[test] @@ -40,10 +40,10 @@ fn test_update_various_types() { sketch.update("string"); sketch.update(42i64); sketch.update(42u64); - 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(hash_value::canonical_f64(3.15)); + sketch.update(hash_value::canonical_f64(3.15)); + sketch.update(hash_value::canonical_f32(3.15)); + sketch.update(hash_value::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]
