#![allow(non_camel_case_types)]
use std::os::raw::c_void;
use crate::ffi;
use crate::hasher::FastHash;
#[derive(Clone, Default)]
pub struct Hash32;
impl FastHash for Hash32 {
type Hash = u32;
type Seed = u32;
#[inline(always)]
fn hash_with_seed<T: AsRef<[u8]>>(bytes: T, seed: u32) -> u32 {
unsafe {
let mut hash = 0_u32;
ffi::MurmurHash3_x86_32(
bytes.as_ref().as_ptr() as *const c_void,
bytes.as_ref().len() as i32,
seed,
&mut hash as *mut u32 as *mut c_void,
);
hash
}
}
}
trivial_hasher! {
Hasher32(Hash32) -> u32
}
#[derive(Clone, Default)]
pub struct Hash128_x86;
impl FastHash for Hash128_x86 {
type Hash = u128;
type Seed = u32;
#[inline(always)]
fn hash_with_seed<T: AsRef<[u8]>>(bytes: T, seed: u32) -> u128 {
unsafe {
let mut hash = 0;
ffi::MurmurHash3_x86_128(
bytes.as_ref().as_ptr() as *const c_void,
bytes.as_ref().len() as i32,
seed,
&mut hash as *mut u128 as *mut c_void,
);
hash
}
}
}
trivial_hasher! {
Hasher128_x86(Hash128_x86) -> u128
}
#[derive(Clone, Default)]
pub struct Hash128_x64;
impl FastHash for Hash128_x64 {
type Hash = u128;
type Seed = u32;
#[inline(always)]
fn hash_with_seed<T: AsRef<[u8]>>(bytes: T, seed: u32) -> u128 {
unsafe {
let mut hash = 0;
ffi::MurmurHash3_x64_128(
bytes.as_ref().as_ptr() as *const c_void,
bytes.as_ref().len() as i32,
seed,
&mut hash as *mut u128 as *mut c_void,
);
hash
}
}
}
trivial_hasher! {
Hasher128_x64(Hash128_x64) -> u128
}
#[inline(always)]
pub fn hash32<T: AsRef<[u8]>>(v: T) -> u32 {
Hash32::hash(v)
}
#[inline(always)]
pub fn hash32_with_seed<T: AsRef<[u8]>>(v: T, seed: u32) -> u32 {
Hash32::hash_with_seed(v, seed)
}
#[inline(always)]
pub fn hash128<T: AsRef<[u8]>>(v: T) -> u128 {
if cfg!(target_pointer_width = "64") {
Hash128_x64::hash(v)
} else {
Hash128_x86::hash(v)
}
}
#[inline(always)]
pub fn hash128_with_seed<T: AsRef<[u8]>>(v: T, seed: u32) -> u128 {
if cfg!(target_pointer_width = "64") {
Hash128_x64::hash_with_seed(v, seed)
} else {
Hash128_x86::hash_with_seed(v, seed)
}
}