#![allow(non_camel_case_types)]
use ffi;
use hasher::FastHash;
pub struct Hash64_1;
impl FastHash for Hash64_1 {
type Hash = u64;
type Seed = u32;
#[inline(always)]
fn hash_with_seed<T: AsRef<[u8]>>(bytes: T, seed: u32) -> u64 {
let mut hash = 0_u64;
unsafe {
ffi::metrohash64_1(
bytes.as_ref().as_ptr() as *const u8,
bytes.as_ref().len() as u64,
seed,
&mut hash as *mut u64 as *mut u8,
);
}
hash
}
}
impl_hasher!(
#[doc = r#"
# Example
```
use std::hash::Hasher;
use fasthash::{metro::Hasher64_1, FastHasher};
let mut h = Hasher64_1::new();
h.write(b"hello");
assert_eq!(h.finish(), 15663805623366682943);
h.write(b"world");
assert_eq!(h.finish(), 4615394705531318333);
```
"#]
Hasher64_1,
Hash64_1
);
pub struct Hash64_2;
impl FastHash for Hash64_2 {
type Hash = u64;
type Seed = u32;
#[inline(always)]
fn hash_with_seed<T: AsRef<[u8]>>(bytes: T, seed: u32) -> u64 {
let mut hash = 0_u64;
unsafe {
ffi::metrohash64_2(
bytes.as_ref().as_ptr() as *const u8,
bytes.as_ref().len() as u64,
seed,
&mut hash as *mut u64 as *mut u8,
);
}
hash
}
}
impl_hasher!(
#[doc = r#"
# Example
```
use std::hash::Hasher;
use fasthash::{metro::Hasher64_2, FastHasher};
let mut h = Hasher64_2::new();
h.write(b"hello");
assert_eq!(h.finish(), 12352443828090181231);
h.write(b"world");
assert_eq!(h.finish(), 13816693401637061492);
```
"#]
Hasher64_2,
Hash64_2
);
pub struct Hash128_1;
impl FastHash for Hash128_1 {
type Hash = u128;
type Seed = u32;
#[inline(always)]
fn hash_with_seed<T: AsRef<[u8]>>(bytes: T, seed: u32) -> u128 {
let mut hash = 0;
unsafe {
ffi::metrohash128_1(
bytes.as_ref().as_ptr() as *const u8,
bytes.as_ref().len() as u64,
seed,
&mut hash as *mut u128 as *mut u8,
);
}
hash
}
}
impl_hasher_ext!(
#[doc = r#"
# Example
```
use std::hash::Hasher;
use fasthash::{metro::Hasher128_1, FastHasher, HasherExt};
let mut h = Hasher128_1::new();
h.write(b"hello");
assert_eq!(h.finish_ext(), 62770881785623818170589043281119530380);
h.write(b"world");
assert_eq!(h.finish_ext(), 168124756093089300765778527570074281113);
```
"#]
Hasher128_1,
Hash128_1
);
pub struct Hash128_2;
impl FastHash for Hash128_2 {
type Hash = u128;
type Seed = u32;
#[inline(always)]
fn hash_with_seed<T: AsRef<[u8]>>(bytes: T, seed: u32) -> u128 {
let mut hash = 0;
unsafe {
ffi::metrohash128_2(
bytes.as_ref().as_ptr() as *const u8,
bytes.as_ref().len() as u64,
seed,
&mut hash as *mut u128 as *mut u8,
);
}
hash
}
}
impl_hasher_ext!(
#[doc = r#"
# Example
```
use std::hash::Hasher;
use fasthash::{metro::Hasher128_2, FastHasher, HasherExt};
let mut h = Hasher128_2::new();
h.write(b"hello");
assert_eq!(h.finish_ext(), 159488125173835797791070285137966695505);
h.write(b"world");
assert_eq!(h.finish_ext(), 296295343271043311657399689121923046467);
```
"#]
Hasher128_2,
Hash128_2
);
#[cfg(any(feature = "sse42", target_feature = "sse4.2"))]
pub mod crc {
use crate::FastHash;
pub struct Hash64_1;
impl FastHash for Hash64_1 {
type Hash = u64;
type Seed = u32;
#[inline(always)]
fn hash_with_seed<T: AsRef<[u8]>>(bytes: T, seed: u32) -> u64 {
let mut hash = 0_u64;
unsafe {
ffi::metrohash64crc_1(
bytes.as_ref().as_ptr() as *const u8,
bytes.as_ref().len() as u64,
seed,
&mut hash as *mut u64 as *mut u8,
);
}
hash
}
}
impl_hasher!(
#[doc = r#"
# Example
```
use std::hash::Hasher;
use fasthash::{metro::crc::Hasher64_1, FastHasher};
let mut h = Hasher64_1::new();
h.write(b"hello");
assert_eq!(h.finish(), 6455825309044375053);
h.write(b"world");
assert_eq!(h.finish(), 15512397028293617890);
```
"#]
Hasher64_1,
Hash64_1
);
pub struct Hash64_2;
impl FastHash for Hash64_2 {
type Hash = u64;
type Seed = u32;
#[inline(always)]
fn hash_with_seed<T: AsRef<[u8]>>(bytes: T, seed: u32) -> u64 {
let mut hash = 0_u64;
unsafe {
ffi::metrohash64crc_2(
bytes.as_ref().as_ptr() as *const u8,
bytes.as_ref().len() as u64,
seed,
&mut hash as *mut u64 as *mut u8,
);
}
hash
}
}
impl_hasher!(
#[doc = r#"
# Example
```
use std::hash::Hasher;
use fasthash::{metro::crc::Hasher64_2, FastHasher};
let mut h = Hasher64_2::new();
h.write(b"hello");
assert_eq!(h.finish(), 6093890398749886132);
h.write(b"world");
assert_eq!(h.finish(), 11309399771810154329);
```
"#]
Hasher64_2,
Hash64_2
);
pub struct Hash128_1;
impl FastHash for Hash128_1 {
type Hash = u128;
type Seed = u32;
#[inline(always)]
fn hash_with_seed<T: AsRef<[u8]>>(bytes: T, seed: u32) -> u128 {
let mut hash = 0;
unsafe {
ffi::metrohash128crc_1(
bytes.as_ref().as_ptr() as *const u8,
bytes.as_ref().len() as u64,
seed,
&mut hash as *mut u128 as *mut u8,
);
}
hash
}
}
impl_hasher_ext!(
#[doc = r#"
# Example
```
use std::hash::Hasher;
use fasthash::{metro::crc::Hasher128_1, FastHasher, HasherExt};
let mut h = Hasher128_1::new();
h.write(b"hello");
assert_eq!(h.finish_ext(), 305698986830952061993175238670398112766);
h.write(b"world");
assert_eq!(h.finish_ext(), 330807979290440384643858402038145360287);
```
"#]
Hasher128_1,
Hash128_1
);
pub struct Hash128_2;
impl FastHash for Hash128_2 {
type Hash = u128;
type Seed = u32;
#[inline(always)]
fn hash_with_seed<T: AsRef<[u8]>>(bytes: T, seed: u32) -> u128 {
let mut hash = 0;
unsafe {
ffi::metrohash128crc_2(
bytes.as_ref().as_ptr() as *const u8,
bytes.as_ref().len() as u64,
seed,
&mut hash as *mut u128 as *mut u8,
);
}
hash
}
}
impl_hasher_ext!(
#[doc = r#"
# Example
```
use std::hash::Hasher;
use fasthash::{metro::crc::Hasher128_2, FastHasher, HasherExt};
let mut h = Hasher128_2::new();
h.write(b"hello");
assert_eq!(h.finish_ext(), 72185604606880289212099011688929773703);
h.write(b"world");
assert_eq!(h.finish_ext(), 332348429832512530891646387991260171468);
```
"#]
Hasher128_2,
Hash128_2
);
}
cfg_if! {
if #[cfg(any(feature = "sse42", target_feature = "sse4.2"))] {
#[inline(always)]
pub fn hash64<T: AsRef<[u8]>>(v: T) -> u64 {
crc::Hash64_1::hash(v)
}
#[inline(always)]
pub fn hash64_with_seed<T: AsRef<[u8]>>(v: T, seed: u32) -> u64 {
crc::Hash64_1::hash_with_seed(v, seed)
}
#[inline(always)]
pub fn hash128<T: AsRef<[u8]>>(v: T) -> u128 {
crc::Hash128_1::hash(v)
}
#[inline(always)]
pub fn hash128_with_seed<T: AsRef<[u8]>>(v: T, seed: u32) -> u128 {
crc::Hash128_1::hash_with_seed(v, seed)
}
} else {
#[inline(always)]
pub fn hash64<T: AsRef<[u8]>>(v: T) -> u64 {
Hash64_1::hash(v)
}
#[inline(always)]
pub fn hash64_with_seed<T: AsRef<[u8]>>(v: T, seed: u32) -> u64 {
Hash64_1::hash_with_seed(v, seed)
}
#[inline(always)]
pub fn hash128<T: AsRef<[u8]>>(v: T) -> u128 {
Hash128_1::hash(v)
}
#[inline(always)]
pub fn hash128_with_seed<T: AsRef<[u8]>>(v: T, seed: u32) -> u128 {
Hash128_1::hash_with_seed(v, seed)
}
}
}