/
mod.rs
76 lines (64 loc) · 1.58 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
extern crate xxhash;
extern crate core;
use self::core::kinds::Sized;
#[inline]
pub fn displace(f1: u32, f2: u32, d1: u32, d2: u32) -> u32 {
d2 + f1 * d1 + f2
}
#[inline]
fn split(hash: u64) -> (u32, u32, u32) {
const BITS: uint = 21;
const MASK: u64 = (1 << BITS) - 1;
((hash & MASK) as u32,
((hash >> BITS) & MASK) as u32,
((hash >> (2 * BITS)) & MASK) as u32)
}
/// A trait implemented by types which can be used in PHF data structures
pub trait PhfHash for Sized? {
/// Hashes the value of `self`, factoring in a seed
fn phf_hash(&self, seed: u64) -> (u32, u32, u32);
}
impl<'a> PhfHash for &'a str {
#[inline]
fn phf_hash(&self, seed: u64) -> (u32, u32, u32) {
split(xxhash::hash_with_seed(seed, self))
}
}
impl<'a> PhfHash for &'a [u8] {
#[inline]
fn phf_hash(&self, seed: u64) -> (u32, u32, u32) {
split(xxhash::oneshot(*self, seed))
}
}
impl PhfHash for str {
#[inline]
fn phf_hash(&self, seed: u64) -> (u32, u32, u32) {
split(xxhash::hash_with_seed(seed, &self))
}
}
impl PhfHash for [u8] {
#[inline]
fn phf_hash(&self, seed: u64) -> (u32, u32, u32) {
split(xxhash::oneshot(self, seed))
}
}
macro_rules! sip_impl(
($t:ty) => (
impl PhfHash for $t {
#[inline]
fn phf_hash(&self, seed: u64) -> (u32, u32, u32) {
split(xxhash::hash_with_seed(seed, self))
}
}
)
)
sip_impl!(u8)
sip_impl!(i8)
sip_impl!(u16)
sip_impl!(i16)
sip_impl!(u32)
sip_impl!(i32)
sip_impl!(u64)
sip_impl!(i64)
sip_impl!(char)
sip_impl!(bool)