superbit/simhash/
sim_hasher.rs1use siphasher::sip::SipHasher;
2use siphasher::sip128::{Hasher128, SipHasher as SipHasher128};
3use std::hash::{Hash, Hasher};
4use xxhash_rust::xxh3::Xxh3;
5
6pub trait SimHasher: Sized {
7 type T;
8 fn hash<U>(&self, item: &U) -> Self::T
9 where
10 Self: Sized,
11 U: Hash;
12}
13
14pub struct SimSipHasher64 {
15 key1: u64,
16 key2: u64,
17}
18
19impl SimSipHasher64 {
20 pub fn new(key1: u64, key2: u64) -> Self {
21 SimSipHasher64 { key1, key2 }
22 }
23}
24
25impl SimHasher for SimSipHasher64 {
26 type T = u64;
27
28 fn hash<U>(&self, item: &U) -> Self::T
29 where
30 Self: Sized,
31 U: Hash,
32 {
33 let mut sip = SipHasher::new_with_keys(self.key1, self.key2);
34 item.hash(&mut sip);
35 sip.finish()
36 }
37}
38
39pub struct SimSipHasher128 {
40 key1: u64,
41 key2: u64,
42}
43
44impl SimSipHasher128 {
45 pub fn new(key1: u64, key2: u64) -> Self {
46 SimSipHasher128 { key1, key2 }
47 }
48}
49
50impl SimHasher for SimSipHasher128 {
51 type T = u128;
52
53 fn hash<U>(&self, item: &U) -> Self::T
54 where
55 Self: Sized,
56 U: Hash,
57 {
58 let mut sip = SipHasher128::new_with_keys(self.key1, self.key2);
59 item.hash(&mut sip);
60 sip.finish128().as_u128()
61 }
62}
63
64pub struct Xxh3Hasher64;
65
66impl Xxh3Hasher64 {
67 #[inline]
68 pub fn new() -> Self {
69 Self
70 }
71}
72
73impl SimHasher for Xxh3Hasher64 {
74 type T = u64;
75
76 #[inline]
77 fn hash<U>(&self, item: &U) -> Self::T
78 where
79 U: Hash,
80 {
81 let mut h = Xxh3::new();
82 item.hash(&mut h);
83 h.finish()
84 }
85}
86
87pub struct Xxh3Hasher128; impl Xxh3Hasher128 {
90 pub fn new() -> Self {
91 Self
92 }
93}
94
95impl SimHasher for Xxh3Hasher128 {
96 type T = u128;
97
98 #[inline]
99 fn hash<U>(&self, item: &U) -> Self::T
100 where
101 U: Hash,
102 {
103 let mut h = Xxh3::new(); item.hash(&mut h);
105 h.digest128() }
107}