bloom-bitset 11.2.0

Zero-dependency probabilistic data structures: 512-bit cache-line-blocked Bloom filter, 4-bit saturating Counting Bloom filter, and HyperLogLog cardinality estimator.
Documentation
use super::*;

/// Finalizes a 64-bit integer with SplitMix64 bit-mixing.
///
/// # Arguments
///
/// - `u64` - Input word.
///
/// # Returns
///
/// - `u64` - Well-mixed 64-bit hash.
#[inline(always)]
pub(crate) fn mix64(mut x: u64) -> u64 {
    x = x.wrapping_add(MIX_INC);
    x = (x ^ (x >> 30)).wrapping_mul(MIX_M1);
    x = (x ^ (x >> 27)).wrapping_mul(MIX_M2);
    x ^ (x >> 31)
}

/// Computes one fast word-wide 64-bit hash of `bytes` for HyperLogLog insertion.
///
/// # Arguments
///
/// - `&[u8]` - Input byte slice.
///
/// # Returns
///
/// - `u64` - Well-mixed 64-bit digest.
#[inline(always)]
pub(crate) fn hash_one(bytes: &[u8]) -> u64 {
    let mut h1: u64 = FNV_OFFSET ^ (bytes.len() as u64);
    let (chunks, rem): (&[[u8; 8]], &[u8]) = bytes.as_chunks::<8>();
    for chunk in chunks {
        let w: u64 = u64::from_le_bytes(*chunk);
        h1 = (h1.rotate_left(19) ^ w).wrapping_mul(MIX_M1);
    }
    if !rem.is_empty() {
        let mut tail: u64 = 0;
        let mut shift: u32 = 0;
        for &b in rem {
            tail |= u64::from(b) << shift;
            shift += 8;
        }
        h1 = (h1.rotate_left(19) ^ tail).wrapping_mul(MIX_M1);
    }
    mix64(h1)
}

/// Computes two independent 64-bit hashes `(h1, h2)` of `bytes` using word-wide mixing.
///
/// # Arguments
///
/// - `&[u8]` - Input byte slice.
///
/// # Returns
///
/// - `(u64, u64)` - Two independent 64-bit digests for double-hashing and block selection.
#[inline(always)]
pub(crate) fn hash_pair(bytes: &[u8]) -> (u64, u64) {
    let mut h1: u64 = FNV_OFFSET ^ (bytes.len() as u64);
    let mut h2: u64 = FNV_OFFSET ^ SECOND_SEED;
    let (chunks, rem): (&[[u8; 8]], &[u8]) = bytes.as_chunks::<8>();
    for chunk in chunks {
        let w: u64 = u64::from_le_bytes(*chunk);
        h1 = (h1.rotate_left(19) ^ w).wrapping_mul(MIX_M1);
        h2 = (h2.rotate_left(23) ^ w.wrapping_mul(MIX_M2)).wrapping_mul(FNV_PRIME);
    }
    if !rem.is_empty() {
        let mut tail: u64 = 0;
        let mut shift: u32 = 0;
        for &b in rem {
            tail |= u64::from(b) << shift;
            shift += 8;
        }
        h1 = (h1.rotate_left(19) ^ tail).wrapping_mul(MIX_M1);
        h2 = (h2.rotate_left(23) ^ tail.wrapping_mul(MIX_M2)).wrapping_mul(FNV_PRIME);
    }
    (mix64(h1), mix64(h2))
}

/// Computes two independent 64-bit digests for any [`Hash`] key.
///
/// # Arguments
///
/// - `&K` - Key reference implementing [`Hash`].
///
/// # Returns
///
/// - `(u64, u64)` - Two independent 64-bit hashes.
#[inline(always)]
pub fn hash_key_pair<K>(key: &K) -> (u64, u64)
where
    K: Hash + ?Sized,
{
    let mut hasher: DefaultHasher = DefaultHasher::new();
    key.hash(&mut hasher);
    let raw: u64 = hasher.finish();
    (mix64(raw), mix64(raw ^ SECOND_SEED))
}