use std::hash::{BuildHasher, Hash, Hasher};
#[cfg(all(not(feature = "fnv"), not(feature = "sip13"), not(feature = "wyhash")))]
use std::{hash::BuildHasherDefault, collections::hash_map::DefaultHasher};
#[cfg(feature = "sip13")]
#[allow(deprecated)]
use std::hash::SipHasher13;
pub trait BuildSeededHasher {
type Hasher: Hasher;
fn build_hasher(&self, seed: u32) -> Self::Hasher;
#[inline]
fn hash_one<T: Hash>(&self, x: T, seed: u32) -> u64 {
let mut h = self.build_hasher(seed);
x.hash(&mut h);
h.finish()
}
}
#[derive(Default, Copy, Clone)]
pub struct Seedable<BH: BuildHasher>(BH);
impl<BH: BuildHasher> BuildSeededHasher for Seedable<BH> {
type Hasher = BH::Hasher;
#[inline]
fn build_hasher(&self, seed: u32) -> Self::Hasher {
let mut result = self.0.build_hasher();
result.write_u32(seed);
result
}
}
#[cfg(feature = "sip13")]
#[derive(Default, Copy, Clone)]
pub struct BuildSip13;
#[cfg(feature = "sip13")]
#[allow(deprecated)]
impl BuildSeededHasher for BuildSip13 {
type Hasher = SipHasher13;
#[inline] fn build_hasher(&self, seed: u32) -> Self::Hasher {
Self::Hasher::new_with_keys(seed as u64, seed as u64)
}
}
#[cfg(feature = "wyhash")]
#[derive(Default, Copy, Clone)]
pub struct BuildWyHash;
#[cfg(feature = "wyhash")]
impl BuildSeededHasher for BuildWyHash {
type Hasher = wyhash::WyHash;
#[inline] fn build_hasher(&self, seed: u32) -> Self::Hasher {
Self::Hasher::with_seed(seed as u64)
}
}
#[cfg(feature = "fnv")]
impl BuildSeededHasher for fnv::FnvBuildHasher {
type Hasher = fnv::FnvHasher;
#[inline] fn build_hasher(&self, seed: u32) -> Self::Hasher {
Self::Hasher::with_key(seed as u64)
}
}
#[cfg(feature = "wyhash")]
pub type BuildDefaultSeededHasher = BuildWyHash;
#[cfg(all(feature = "sip13", not(feature = "wyhash")))]
pub type BuildDefaultSeededHasher = BuildSip13;
#[cfg(all(feature = "fnv", not(feature = "sip13"), not(feature = "wyhash")))]
pub type BuildDefaultSeededHasher = fnv::FnvBuildHasher;
#[cfg(all(not(feature = "fnv"), not(feature = "sip13"), not(feature = "wyhash")))]
pub type BuildDefaultSeededHasher = Seedable<BuildHasherDefault<DefaultHasher>>;