-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathhash.rs
104 lines (86 loc) · 2.37 KB
/
hash.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Simplified implementation of the fast Rust C
// hash algorithm, also used by Firefox.
// https://github.com/rust-lang/rustc-hash
use std::collections::{HashMap, HashSet};
use std::hash::{BuildHasher, Hasher};
pub type FastSet<T> = HashSet<T, BuildFxHasher>;
pub type FastMap<K, V> = HashMap<K, V, BuildFxHasher>;
pub struct FastSetBuilder;
pub struct FastMapBuilder;
impl FastSetBuilder {
pub fn empty<T>() -> FastSet<T> {
HashSet::with_hasher(BuildFxHasher)
}
pub fn with_capacity<T>(capacity: usize) -> FastSet<T> {
HashSet::with_capacity_and_hasher(capacity, BuildFxHasher)
}
}
impl FastMapBuilder {
pub fn empty<K, V>() -> FastMap<K, V> {
HashMap::with_hasher(BuildFxHasher)
}
pub fn with_capacity<K, V>(capacity: usize) -> FastMap<K, V> {
HashMap::with_capacity_and_hasher(capacity, BuildFxHasher)
}
}
#[derive(Clone, Copy, Default)]
pub struct BuildFxHasher;
impl BuildHasher for BuildFxHasher {
type Hasher = FxHasher;
fn build_hasher(&self) -> Self::Hasher {
FxHasher { state: 0 }
}
}
pub struct FxHasher {
state: u64,
}
impl FxHasher {
#[inline]
fn hash(&mut self, i: u64) {
self.state = (self.state.rotate_left(5) ^ i).wrapping_mul(0x517cc1b727220a95);
}
}
impl Hasher for FxHasher {
#[inline]
fn write(&mut self, mut bytes: &[u8]) {
while bytes.len() >= 8 {
self.hash(u64::from_ne_bytes(bytes[..8].try_into().unwrap()));
bytes = &bytes[8..];
}
if bytes.len() >= 4 {
self.hash(u32::from_ne_bytes(bytes[..4].try_into().unwrap()) as u64);
bytes = &bytes[4..];
}
if bytes.len() >= 2 {
self.hash(u16::from_ne_bytes(bytes[..2].try_into().unwrap()) as u64);
bytes = &bytes[2..];
}
if !bytes.is_empty() {
self.hash(bytes[0] as u64);
}
}
#[inline]
fn write_u8(&mut self, i: u8) {
self.hash(i as u64);
}
#[inline]
fn write_u16(&mut self, i: u16) {
self.hash(i as u64);
}
#[inline]
fn write_u32(&mut self, i: u32) {
self.hash(i as u64);
}
#[inline]
fn write_u64(&mut self, i: u64) {
self.hash(i);
}
#[inline]
fn write_usize(&mut self, i: usize) {
self.hash(i as u64);
}
#[inline]
fn finish(&self) -> u64 {
self.state
}
}