use std::hash::{Hash, Hasher};
use twox_hash::XxHash64;
pub(crate) struct HashGenerator {
hash1: u64,
hash2: u64,
}
impl HashGenerator {
#[inline]
pub fn new<T: Hash>(item: &T) -> Self {
let hash1 = Self::hash_with_seed(item, 0);
let hash2 = Self::hash_with_seed(item, 1);
let hash2 = if hash2 == 0 { 1 } else { hash2 };
Self { hash1, hash2 }
}
#[inline]
pub fn nth(&self, i: usize) -> u64 {
self.hash1.wrapping_add((i as u64).wrapping_mul(self.hash2))
}
#[inline]
fn hash_with_seed<T: Hash>(item: &T, seed: u64) -> u64 {
let mut hasher = XxHash64::with_seed(seed);
item.hash(&mut hasher);
hasher.finish()
}
}