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 043410d4f8455144e9962197cd6adc4bdcf413d8 Author: tison <[email protected]> AuthorDate: Wed May 6 23:48:26 2026 +0800 refactor: use HashValue for all Hash inputs Signed-off-by: tison <[email protected]> --- datasketches/src/hash_value.rs | 46 ++++++++++++++++++++++++++++++++++++++++++ datasketches/src/lib.rs | 1 + 2 files changed, 47 insertions(+) diff --git a/datasketches/src/hash_value.rs b/datasketches/src/hash_value.rs new file mode 100644 index 0000000..1b1439e --- /dev/null +++ b/datasketches/src/hash_value.rs @@ -0,0 +1,46 @@ +//! HashValue + +use std::hash::{Hash, Hasher}; + +/// 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) + } +} diff --git a/datasketches/src/lib.rs b/datasketches/src/lib.rs index 02dc692..33ea2f8 100644 --- a/datasketches/src/lib.rs +++ b/datasketches/src/lib.rs @@ -37,6 +37,7 @@ pub mod countmin; pub mod cpc; pub mod error; pub mod frequencies; +pub mod hash_value; pub mod hll; pub mod tdigest; pub mod theta; --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
