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 b712ec6bb27f1fa25d3ce4d6ecf197da7ea602c5
Author: tison <[email protected]>
AuthorDate: Thu May 7 08:43:30 2026 +0800

    canonical integers
    
    Signed-off-by: tison <[email protected]>
---
 datasketches/src/hash_value/canonical.rs | 231 ++++++++++++++++++++++++++++++-
 datasketches/src/hash_value/mod.rs       |   8 ++
 2 files changed, 235 insertions(+), 4 deletions(-)

diff --git a/datasketches/src/hash_value/canonical.rs 
b/datasketches/src/hash_value/canonical.rs
index c8b3dbe..56edbcd 100644
--- a/datasketches/src/hash_value/canonical.rs
+++ b/datasketches/src/hash_value/canonical.rs
@@ -83,8 +83,8 @@ macro_rules! impl_canonical_value {
 ///     calculate_hash(canonical_f64(5.0))
 /// );
 /// ```
-pub fn canonical_f32(x: f32) -> Canonical<f32> {
-    Canonical(x)
+pub fn canonical_f32(v: f32) -> Canonical<f32> {
+    Canonical(v)
 }
 
 /// Create a canonical hashable value from a `f64` value.
@@ -110,8 +110,8 @@ pub fn canonical_f32(x: f32) -> Canonical<f32> {
 ///     calculate_hash(canonical_f64(5.0))
 /// );
 /// ```
-pub fn canonical_f64(x: f64) -> Canonical<f64> {
-    Canonical(x)
+pub fn canonical_f64(v: f64) -> Canonical<f64> {
+    Canonical(v)
 }
 
 impl_canonical_value!(f32, canonical_f32, |v| canonical_f64(*v as f64));
@@ -124,3 +124,226 @@ impl_canonical_value!(f64, canonical_f64, |v| if 
v.is_nan() {
     // canonicalize signed zero without any branches in one instruction.
     (v + 0.0).to_bits()
 });
+
+/// Create a canonical hashable value from an `i8` value.
+///
+/// Integer canonicalization is compatible with datasketches-cpp. Values 
narrower than 64 bits are
+/// converted to signed 64-bit values before hashing. Unsigned narrow values 
are first interpreted
+/// as the signed integer of the same width, then sign-extended to `i64`.
+///
+/// This means `255u8` canonicalizes like `-1i8`, not like `255i64`.
+///
+/// # Examples
+///
+/// ```
+/// # use datasketches::hash_value::calculate_hash;
+/// # use datasketches::hash_value::{canonical_i8, canonical_i64, 
canonical_u8};
+/// assert_eq!(
+///     calculate_hash(canonical_i8(-1)),
+///     calculate_hash(canonical_u8(255))
+/// );
+/// assert_eq!(
+///     calculate_hash(canonical_i8(42)),
+///     calculate_hash(canonical_i64(42))
+/// );
+/// assert_ne!(
+///     calculate_hash(canonical_u8(128)),
+///     calculate_hash(canonical_i64(128))
+/// );
+/// ```
+pub fn canonical_i8(v: i8) -> Canonical<i8> {
+    Canonical(v)
+}
+
+/// Create a canonical hashable value from a `u8` value.
+///
+/// Integer canonicalization is compatible with datasketches-cpp. Values 
narrower than 64 bits are
+/// converted to signed 64-bit values before hashing. Unsigned narrow values 
are first interpreted
+/// as the signed integer of the same width, then sign-extended to `i64`.
+///
+/// This means `255u8` canonicalizes like `-1i8`, not like `255i64`.
+///
+/// # Examples
+///
+/// ```
+/// # use datasketches::hash_value::calculate_hash;
+/// # use datasketches::hash_value::{canonical_i8, canonical_i64, 
canonical_u8};
+/// assert_eq!(
+///     calculate_hash(canonical_u8(255)),
+///     calculate_hash(canonical_i8(-1))
+/// );
+/// assert_eq!(
+///     calculate_hash(canonical_u8(42)),
+///     calculate_hash(canonical_i64(42))
+/// );
+/// assert_ne!(
+///     calculate_hash(canonical_u8(128)),
+///     calculate_hash(canonical_i64(128))
+/// );
+/// ```
+pub fn canonical_u8(v: u8) -> Canonical<u8> {
+    Canonical(v)
+}
+
+/// Create a canonical hashable value from an `i16` value.
+///
+/// Integer canonicalization is compatible with datasketches-cpp. Values 
narrower than 64 bits are
+/// converted to signed 64-bit values before hashing. Unsigned narrow values 
are first interpreted
+/// as the signed integer of the same width, then sign-extended to `i64`.
+///
+/// # Examples
+///
+/// ```
+/// # use datasketches::hash_value::calculate_hash;
+/// # use datasketches::hash_value::{canonical_i16, canonical_i64};
+/// assert_eq!(
+///     calculate_hash(canonical_i16(42)),
+///     calculate_hash(canonical_i64(42))
+/// );
+/// assert_eq!(
+///     calculate_hash(canonical_i16(-1)),
+///     calculate_hash(canonical_i64(-1))
+/// );
+/// ```
+pub fn canonical_i16(v: i16) -> Canonical<i16> {
+    Canonical(v)
+}
+
+/// Create a canonical hashable value from a `u16` value.
+///
+/// Integer canonicalization is compatible with datasketches-cpp. Values 
narrower than 64 bits are
+/// converted to signed 64-bit values before hashing. Unsigned narrow values 
are first interpreted
+/// as the signed integer of the same width, then sign-extended to `i64`.
+///
+/// This means `65535u16` canonicalizes like `-1i16`, not like `65535i64`.
+///
+/// # Examples
+///
+/// ```
+/// # use datasketches::hash_value::calculate_hash;
+/// # use datasketches::hash_value::{canonical_i16, canonical_i64, 
canonical_u16};
+/// assert_eq!(
+///     calculate_hash(canonical_u16(65535)),
+///     calculate_hash(canonical_i16(-1))
+/// );
+/// assert_eq!(
+///     calculate_hash(canonical_u16(42)),
+///     calculate_hash(canonical_i64(42))
+/// );
+/// assert_ne!(
+///     calculate_hash(canonical_u16(32768)),
+///     calculate_hash(canonical_i64(32768))
+/// );
+/// ```
+pub fn canonical_u16(v: u16) -> Canonical<u16> {
+    Canonical(v)
+}
+
+/// Create a canonical hashable value from an `i32` value.
+///
+/// Integer canonicalization is compatible with datasketches-cpp. Values 
narrower than 64 bits are
+/// converted to signed 64-bit values before hashing. Unsigned narrow values 
are first interpreted
+/// as the signed integer of the same width, then sign-extended to `i64`.
+///
+/// # Examples
+///
+/// ```
+/// # use datasketches::hash_value::calculate_hash;
+/// # use datasketches::hash_value::{canonical_i32, canonical_i64};
+/// assert_eq!(
+///     calculate_hash(canonical_i32(42)),
+///     calculate_hash(canonical_i64(42))
+/// );
+/// assert_eq!(
+///     calculate_hash(canonical_i32(-1)),
+///     calculate_hash(canonical_i64(-1))
+/// );
+/// ```
+pub fn canonical_i32(v: i32) -> Canonical<i32> {
+    Canonical(v)
+}
+
+/// Create a canonical hashable value from a `u32` value.
+///
+/// Integer canonicalization is compatible with datasketches-cpp. Values 
narrower than 64 bits are
+/// converted to signed 64-bit values before hashing. Unsigned narrow values 
are first interpreted
+/// as the signed integer of the same width, then sign-extended to `i64`.
+///
+/// This means `4294967295u32` canonicalizes like `-1i32`, not like 
`4294967295i64`.
+///
+/// # Examples
+///
+/// ```
+/// # use datasketches::hash_value::calculate_hash;
+/// # use datasketches::hash_value::{canonical_i32, canonical_i64, 
canonical_u32};
+/// assert_eq!(
+///     calculate_hash(canonical_u32(4294967295)),
+///     calculate_hash(canonical_i32(-1))
+/// );
+/// assert_eq!(
+///     calculate_hash(canonical_u32(42)),
+///     calculate_hash(canonical_i64(42))
+/// );
+/// assert_ne!(
+///     calculate_hash(canonical_u32(2147483648)),
+///     calculate_hash(canonical_i64(2147483648))
+/// );
+/// ```
+pub fn canonical_u32(v: u32) -> Canonical<u32> {
+    Canonical(v)
+}
+
+/// Create a canonical hashable value from an `i64` value.
+///
+/// Integer canonicalization is compatible with datasketches-cpp. Signed 
64-bit values are hashed
+/// as themselves.
+///
+/// # Examples
+///
+/// ```
+/// # 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)
+/// );
+/// ```
+pub fn canonical_i64(v: i64) -> Canonical<i64> {
+    Canonical(v)
+}
+
+/// Create a canonical hashable value from a `u64` value.
+///
+/// Integer canonicalization is compatible with datasketches-cpp. Unsigned 
64-bit values are hashed
+/// as themselves.
+///
+/// # Examples
+///
+/// ```
+/// # 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(u64::MAX)),
+///     calculate_hash(u64::MAX)
+/// );
+/// ```
+pub fn canonical_u64(v: u64) -> Canonical<u64> {
+    Canonical(v)
+}
+
+impl_canonical_value!(i8, canonical_i8, |v| *v as i64);
+impl_canonical_value!(u8, canonical_u8, |v| (*v as i8) as i64);
+impl_canonical_value!(i16, canonical_i16, |v| *v as i64);
+impl_canonical_value!(u16, canonical_u16, |v| (*v as i16) as i64);
+impl_canonical_value!(i32, canonical_i32, |v| *v as i64);
+impl_canonical_value!(u32, canonical_u32, |v| (*v as i32) as i64);
+impl_canonical_value!(i64, canonical_i64, |v| *v);
+impl_canonical_value!(u64, canonical_u64, |v| *v);
diff --git a/datasketches/src/hash_value/mod.rs 
b/datasketches/src/hash_value/mod.rs
index d8e54a3..4e26e31 100644
--- a/datasketches/src/hash_value/mod.rs
+++ b/datasketches/src/hash_value/mod.rs
@@ -8,6 +8,14 @@ use std::hash::Hasher;
 pub use self::canonical::Canonical;
 pub use self::canonical::canonical_f32;
 pub use self::canonical::canonical_f64;
+pub use self::canonical::canonical_i8;
+pub use self::canonical::canonical_i16;
+pub use self::canonical::canonical_i32;
+pub use self::canonical::canonical_i64;
+pub use self::canonical::canonical_u8;
+pub use self::canonical::canonical_u16;
+pub use self::canonical::canonical_u32;
+pub use self::canonical::canonical_u64;
 
 #[doc(hidden)] // for doctest
 pub fn calculate_hash<T: Hash>(t: T) -> u64 {


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to