| 1 | /// A convenience alias for creating a hash map with an FNV hasher. |
| 2 | pub(crate) type HashMap<K, V> = |
| 3 | std::collections::HashMap<K, V, std::hash::BuildHasherDefault<Hasher>>; |
| 4 | |
| 5 | /// A hasher that implements the Fowler–Noll–Vo (FNV) hash. |
| 6 | pub(crate) struct Hasher(u64); |
| 7 | |
| 8 | impl Hasher { |
| 9 | const OFFSET_BASIS: u64 = 0xcbf29ce484222325; |
| 10 | const PRIME: u64 = 0x100000001b3; |
| 11 | } |
| 12 | |
| 13 | impl Default for Hasher { |
| 14 | fn default() -> Hasher { |
| 15 | Hasher(Hasher::OFFSET_BASIS) |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | impl std::hash::Hasher for Hasher { |
| 20 | fn finish(&self) -> u64 { |
| 21 | self.0 |
| 22 | } |
| 23 | |
| 24 | fn write(&mut self, bytes: &[u8]) { |
| 25 | for &byte: u8 in bytes.iter() { |
| 26 | self.0 = self.0 ^ u64::from(byte); |
| 27 | self.0 = self.0.wrapping_mul(Hasher::PRIME); |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |