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 0776aecaebe9e641da71f9de5c9e43a5f3159892 Author: tison <[email protected]> AuthorDate: Thu May 7 08:27:04 2026 +0800 rework Canonical value wrapper Signed-off-by: tison <[email protected]> --- datasketches/src/hash_value/canonical.rs | 126 +++++++++++++++++++++ .../src/{hash_value.rs => hash_value/mod.rs} | 22 +++- datasketches/src/theta/sketch.rs | 8 +- 3 files changed, 150 insertions(+), 6 deletions(-) diff --git a/datasketches/src/hash_value/canonical.rs b/datasketches/src/hash_value/canonical.rs new file mode 100644 index 0000000..c8b3dbe --- /dev/null +++ b/datasketches/src/hash_value/canonical.rs @@ -0,0 +1,126 @@ +use std::fmt; +use std::hash::Hash; +use std::hash::Hasher; +use std::ops::Deref; +use std::ops::DerefMut; + +/// A value wrapper that uses a canonical hashing strategy for its inner value. +/// +/// See the [module level documentation](super) for more. +#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq)] +pub struct Canonical<T>(T); + +impl<T> Canonical<T> { + /// Get the value out. + pub fn into_inner(self) -> T { + self.0 + } +} + +impl<T> Deref for Canonical<T> { + type Target = T; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl<T> DerefMut for Canonical<T> { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} + +impl<T: fmt::Debug> fmt::Debug for Canonical<T> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Debug::fmt(&self.0, f) + } +} + +impl<T: fmt::Display> fmt::Display for Canonical<T> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Display::fmt(&self.0, f) + } +} + +macro_rules! impl_canonical_value { + ($t:ty, $ctor:ident, |$v:ident| $canonical:expr) => { + impl From<$t> for Canonical<$t> { + fn from(value: $t) -> Self { + $ctor(value) + } + } + + impl Hash for Canonical<$t> { + fn hash<H: Hasher>(&self, state: &mut H) { + let $v = &self.0; + let canonical = $canonical; + Hash::hash(&canonical, state); + } + } + }; +} + +/// Create a canonical hashable value from a `f32` value. +/// +/// This canonical mapping ensures that the same hash value is produced for the same numeric +/// value, regardless of the IEEE754 representation. For example, `canonical_f32(0.0f32)` will +/// produce the same hash value as `canonical_f32(-0.0f32)`. Furthermore, `canonical_f32(5.0f32)` +/// will produce the same hash value as `canonical_f64(5.0f64)`. +/// +/// # Examples +/// +/// ``` +/// # use datasketches::hash_value::calculate_hash; +/// # use datasketches::hash_value::canonical_f32; +/// # use datasketches::hash_value::canonical_f64; +/// assert_eq!( +/// calculate_hash(canonical_f32(0.0)), +/// calculate_hash(canonical_f32(-0.0)) +/// ); +/// assert_eq!( +/// calculate_hash(canonical_f32(5.0)), +/// calculate_hash(canonical_f64(5.0)) +/// ); +/// ``` +pub fn canonical_f32(x: f32) -> Canonical<f32> { + Canonical(x) +} + +/// Create a canonical hashable value from a `f64` value. +/// +/// This canonical mapping ensures that the same hash value is produced for the same numeric +/// value, regardless of the IEEE754 representation. For example, `canonical_f64(0.0f64)` will +/// produce the same hash value as `canonical_f64(-0.0f64)`. Furthermore, `canonical_f64(5.0f64)` +/// will produce the same hash value as `canonical_f32(5.0f32)`. +/// +/// +/// # Examples +/// +/// ``` +/// # use datasketches::hash_value::calculate_hash; +/// # use datasketches::hash_value::canonical_f32; +/// # use datasketches::hash_value::canonical_f64; +/// assert_eq!( +/// calculate_hash(canonical_f64(0.0)), +/// calculate_hash(canonical_f64(-0.0)) +/// ); +/// assert_eq!( +/// calculate_hash(canonical_f32(5.0)), +/// calculate_hash(canonical_f64(5.0)) +/// ); +/// ``` +pub fn canonical_f64(x: f64) -> Canonical<f64> { + Canonical(x) +} + +impl_canonical_value!(f32, canonical_f32, |v| canonical_f64(*v as f64)); +impl_canonical_value!(f64, canonical_f64, |v| if v.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. + (v + 0.0).to_bits() +}); diff --git a/datasketches/src/hash_value.rs b/datasketches/src/hash_value/mod.rs similarity index 80% rename from datasketches/src/hash_value.rs rename to datasketches/src/hash_value/mod.rs index f45eae0..d8e54a3 100644 --- a/datasketches/src/hash_value.rs +++ b/datasketches/src/hash_value/mod.rs @@ -1,6 +1,22 @@ //! HashValue -use std::hash::{Hash, Hasher}; +mod canonical; + +use std::hash::Hash; +use std::hash::Hasher; + +pub use self::canonical::Canonical; +pub use self::canonical::canonical_f32; +pub use self::canonical::canonical_f64; + +#[doc(hidden)] // for doctest +pub fn calculate_hash<T: Hash>(t: T) -> u64 { + use std::hash::DefaultHasher; + + let mut s = DefaultHasher::new(); + t.hash(&mut s); + s.finish() +} /// HashValue #[derive(Eq, PartialEq, Clone, Copy, Debug)] @@ -54,6 +70,8 @@ impl HashValue<i64> { /// /// This is compatible with datasketches-cpp's behavior. pub fn canonical_i8(value: i8) -> Self { - HashValue { value: value as i64 } + HashValue { + value: value as i64, + } } } diff --git a/datasketches/src/theta/sketch.rs b/datasketches/src/theta/sketch.rs index 63ecd09..b2c8b80 100644 --- a/datasketches/src/theta/sketch.rs +++ b/datasketches/src/theta/sketch.rs @@ -73,7 +73,7 @@ pub trait ThetaSketchView: private::Sealed { fn is_empty(&self) -> bool; /// Returns an iterator over retained hash values. - fn iter<'a>(&'a self) -> impl Iterator<Item = u64> + 'a; + fn iter(&self) -> impl Iterator<Item = u64> + '_; /// Returns number of retained hash values. fn num_retained(&self) -> usize; @@ -113,8 +113,8 @@ impl ThetaSketch { /// # Examples /// /// ``` - /// use datasketches::theta::ThetaSketch; /// use datasketches::hash_value::HashValue; + /// use datasketches::theta::ThetaSketch; /// /// let mut sketch = ThetaSketch::builder().build(); /// sketch.update("apple"); @@ -329,7 +329,7 @@ impl ThetaSketchView for ThetaSketch { ThetaSketch::is_empty(self) } - fn iter<'a>(&'a self) -> impl Iterator<Item = u64> + 'a { + fn iter(&self) -> impl Iterator<Item = u64> + '_ { ThetaSketch::iter(self) } @@ -904,7 +904,7 @@ impl ThetaSketchView for CompactThetaSketch { CompactThetaSketch::is_empty(self) } - fn iter<'a>(&'a self) -> impl Iterator<Item = u64> + 'a { + fn iter(&self) -> impl Iterator<Item = u64> + '_ { CompactThetaSketch::iter(self) } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
