1#![doc = include_str!("../README.md")]
2
3pub type HashMap<K, V> = std::collections::HashMap<K, V, RandomState>;
4pub trait HashMapExt {
5 fn new() -> Self;
6 fn with_capacity(capacity: usize) -> Self;
7}
8impl<K, V> HashMapExt for HashMap<K, V> {
9 fn new() -> Self {
10 HashMap::with_hasher(RandomState::new())
11 }
12 fn with_capacity(capacity: usize) -> Self {
13 HashMap::with_capacity_and_hasher(capacity, RandomState::new())
14 }
15}
16
17pub type HashSet<T> = std::collections::HashSet<T, RandomState>;
18pub trait HashSetExt {
19 fn new() -> Self;
20 fn with_capacity(capacity: usize) -> Self;
21}
22impl<T> HashSetExt for HashSet<T> {
23 fn new() -> Self {
24 HashSet::with_hasher(RandomState::new())
25 }
26 fn with_capacity(capacity: usize) -> Self {
27 HashSet::with_capacity_and_hasher(capacity, RandomState::new())
28 }
29}
30
31pub struct MuseHasher(u64);
32impl core::hash::Hasher for MuseHasher {
33 #[inline(always)]
34 fn finish(&self) -> u64 {
35 self.0
36 }
37
38 #[inline]
39 fn write(&mut self, bytes: &[u8]) {
40 self.0 = museair::bfast::hash(bytes, self.0);
41 }
42}
43
44#[derive(Debug, Clone, Copy)]
45pub struct RandomState(u64);
46impl RandomState {
47 pub fn new() -> Self {
48 use std::cell::Cell;
49 thread_local!(static THREAD_UNIQUE_SEED: Cell<u64> = const { Cell::new(1123) });
50
51 let mut unique = THREAD_UNIQUE_SEED.get();
52 unique ^= &unique as *const _ as u64;
53 unique = museair::bfast::hash(b"laqubikh", unique);
54 THREAD_UNIQUE_SEED.set(unique);
55
56 Self(unique)
57 }
58}
59impl core::hash::BuildHasher for RandomState {
60 type Hasher = MuseHasher;
61 #[inline(always)]
62 fn build_hasher(&self) -> Self::Hasher {
63 MuseHasher(self.0)
64 }
65}
66
67#[derive(Debug, Clone, Copy)]
68pub struct FixedState(u64);
69impl FixedState {
70 #[inline(always)]
71 pub fn with_seed(seed: u64) -> Self {
72 Self(seed)
73 }
74}
75impl core::hash::BuildHasher for FixedState {
76 type Hasher = MuseHasher;
77 #[inline(always)]
78 fn build_hasher(&self) -> Self::Hasher {
79 MuseHasher(self.0)
80 }
81}