#![allow(non_camel_case_types)]
use std::os::raw::c_void;
use ffi;
use hasher::FastHash;
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
}
}
}
impl_hasher!(
#[doc = r#"
# Example
```
use std::hash::Hasher;
use fasthash::{murmur3::Hasher32, FastHasher};
let mut h = Hasher32::new();
h.write(b"hello");
assert_eq!(h.finish(), 613153351);
h.write(b"world");
assert_eq!(h.finish(), 2687965642);
```
"#]
Hasher32,
Hash32
);
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
}
}
}
impl_hasher_ext!(
#[doc = r#"
# Example
```
use std::hash::Hasher;
use fasthash::{murmur3::Hasher128_x86, FastHasher, HasherExt};
let mut h = Hasher128_x86::new();
h.write(b"hello");
assert_eq!(h.finish_ext(), 205839232668418009241864179939306390688);
h.write(b"world");
assert_eq!(h.finish_ext(), 83212725615010754952022132390053357814);
```
"#]
Hasher128_x86,
Hash128_x86
);
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
}
}
}
impl_hasher_ext!(
#[doc = r#"
# Example
```
use std::hash::Hasher;
use fasthash::{murmur3::Hasher128_x64, FastHasher, HasherExt};
let mut h = Hasher128_x64::new();
h.write(b"hello");
assert_eq!(h.finish_ext(), 121118445609844952839898260755277781762);
h.write(b"world");
assert_eq!(h.finish_ext(), 216280293825344914020777844322685271162);
```
"#]
Hasher128_x64,
Hash128_x64
);
#[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)
}
}