rust_css_parser/utils/
fx.rs1use std::hash::{BuildHasherDefault, Hasher};
5
6const SEED: u64 = 0x51_7c_c1_b7_27_22_0a_95;
7
8#[derive(Default, Clone, Copy)]
10pub struct FxHasher {
11 hash: u64,
12}
13
14impl FxHasher {
15 #[inline]
16 fn add(&mut self, word: u64) {
17 self.hash = (self.hash.rotate_left(5) ^ word).wrapping_mul(SEED);
18 }
19}
20
21impl Hasher for FxHasher {
22 #[inline]
23 fn write(&mut self, bytes: &[u8]) {
24 let mut chunks = bytes.chunks_exact(8);
25 for chunk in &mut chunks {
26 self.add(u64::from_le_bytes(chunk.try_into().unwrap()));
27 }
28 let rest = chunks.remainder();
29 if !rest.is_empty() {
30 let mut buffer = [0u8; 8];
31 buffer[..rest.len()].copy_from_slice(rest);
32 self.add(u64::from_le_bytes(buffer));
33 }
34 }
35
36 #[inline]
37 fn write_u8(&mut self, i: u8) {
38 self.add(i as u64);
39 }
40
41 #[inline]
42 fn write_u16(&mut self, i: u16) {
43 self.add(i as u64);
44 }
45
46 #[inline]
47 fn write_u32(&mut self, i: u32) {
48 self.add(i as u64);
49 }
50
51 #[inline]
52 fn write_i32(&mut self, i: i32) {
53 self.add(i as u32 as u64);
54 }
55
56 #[inline]
57 fn write_u64(&mut self, i: u64) {
58 self.add(i);
59 }
60
61 #[inline]
62 fn write_usize(&mut self, i: usize) {
63 self.add(i as u64);
64 }
65
66 #[inline]
67 fn finish(&self) -> u64 {
68 self.hash
69 }
70}
71
72pub type FxBuildHasher = BuildHasherDefault<FxHasher>;
73pub type HashMap<K, V> = std::collections::HashMap<K, V, FxBuildHasher>;
74pub type HashSet<K> = std::collections::HashSet<K, FxBuildHasher>;