rustymath_core/
hashing.rs1use std::hash::{BuildHasher, Hash, Hasher};
3
4#[derive(Clone, Default, Debug)]
5pub struct Hashing(foldhash::fast::FixedState);
6
7impl BuildHasher for Hashing {
8 type Hasher = foldhash::fast::FoldHasher<'static>;
9 #[inline(always)]
10 fn build_hasher(&self) -> Self::Hasher { self.0.build_hasher() }
11 #[inline(always)]
12 fn hash_one<T: Hash>(&self, x: T) -> u64 {
13 let mut h = self.build_hasher();
14 x.hash(&mut h);
15 h.finish()
16 }
17}
18
19pub type HashMap<K, V> = hashbrown::HashMap<K, V, Hashing>;
20pub type HashSet<K> = hashbrown::HashSet<K, Hashing>;
21
22pub trait HashMapExt {
24 fn new() -> Self;
25 fn with_capacity(capacity: usize) -> Self;
26}
27impl<K, V> HashMapExt for HashMap<K, V> {
28 fn new() -> Self { Self::with_hasher(Hashing::default()) }
29 fn with_capacity(capacity: usize) -> Self { Self::with_capacity_and_hasher(capacity, Hashing::default()) }
30}
31
32pub trait HashSetExt {
34 fn new() -> Self;
35 fn with_capacity(capacity: usize) -> Self;
36}
37impl<K> HashSetExt for HashSet<K> {
38 fn new() -> Self { Self::with_hasher(Hashing::default()) }
39 fn with_capacity(capacity: usize) -> Self { Self::with_capacity_and_hasher(capacity, Hashing::default()) }
40}