#[cfg(feature = "with_ahash")]
pub mod a_hash {
pub use ahash::{AHasher as Hasher, RandomState as Builder};
}
pub mod sip_hash {
pub use std::collections::hash_map::{DefaultHasher as Hasher, RandomState as Builder};
}
pub mod p_hash {
use std::hash::{BuildHasher as StdBuilderExt, Hasher as StdHasherExt};
#[derive(Clone, Debug, Default)]
pub struct Builder {}
impl Builder {
pub fn new() -> Self {
Self {}
}
}
impl StdBuilderExt for Builder {
type Hasher = Hasher;
fn build_hasher(&self) -> Hasher {
Hasher { buf: [0; 8] }
}
}
pub struct Hasher {
buf: [u8; 8],
}
impl Hasher {
#[cfg(debug_assertions)]
#[inline(always)]
fn test_bytes(bytes: &[u8]) {
if bytes.len() != 8 {
panic!(
"[illegal] p-hash-er \
called with non-`u64` argument ({} bytes, expected {})",
bytes.len(),
8
)
}
}
#[cfg(not(debug_assertions))]
#[inline(always)]
fn test_bytes(_: &[u8]) {}
}
impl StdHasherExt for Hasher {
fn finish(&self) -> u64 {
let block: u64 = u64::from_ne_bytes(self.buf);
block.wrapping_mul(0xDA5DF7A7BD02F2C7u64)
}
fn write(&mut self, bytes: &[u8]) {
Self::test_bytes(bytes);
self.buf[..8].clone_from_slice(&bytes[..8])
}
}
}
pub mod id_hash {
use std::hash::{BuildHasher as StdBuilderExt, Hasher as StdHasherExt};
#[derive(Clone, Debug, Default)]
pub struct Builder {}
impl Builder {
pub fn new() -> Self {
Self {}
}
}
impl StdBuilderExt for Builder {
type Hasher = Hasher;
fn build_hasher(&self) -> Hasher {
Hasher { buf: [0; 8] }
}
}
pub struct Hasher {
buf: [u8; 8],
}
impl Hasher {
#[cfg(debug_assertions)]
#[inline(always)]
fn test_bytes(bytes: &[u8]) {
if bytes.len() != 8 {
panic!(
"[illegal] p-hash-er \
called with non-`u64` argument ({} bytes, expected {})",
bytes.len(),
8
)
}
}
#[cfg(not(debug_assertions))]
#[inline(always)]
fn test_bytes(_: &[u8]) {}
}
impl StdHasherExt for Hasher {
fn finish(&self) -> u64 {
u64::from_ne_bytes(self.buf)
}
fn write(&mut self, bytes: &[u8]) {
Self::test_bytes(bytes);
self.buf[..8].clone_from_slice(&bytes[..8])
}
}
}