1use std::hash::{Hash, Hasher};
2
3use foldhash::SharedSeed;
4
5pub type PlRandomState = foldhash::quality::RandomState;
6pub type PlSeedableRandomStateQuality = foldhash::quality::SeedableRandomState;
7pub type PlRandomStateQuality = foldhash::quality::RandomState;
8pub type PlFixedStateQuality = foldhash::quality::FixedState;
9
10pub type PlHashMap<K, V> = hashbrown::HashMap<K, V, PlRandomState>;
11pub type PlHashSet<V> = hashbrown::HashSet<V, PlRandomState>;
12pub type PlIndexMap<K, V> = indexmap::IndexMap<K, V, PlRandomState>;
13pub type PlIndexSet<K> = indexmap::IndexSet<K, PlRandomState>;
14
15#[derive(Default)]
17pub struct ScratchHashMap<K, V>(PlHashMap<K, V>);
18
19impl<K, V> ScratchHashMap<K, V> {
20 pub fn get(&mut self) -> &mut PlHashMap<K, V> {
22 self.0.clear();
23 &mut self.0
24 }
25}
26
27#[derive(Default)]
29pub struct ScratchHashSet<K>(PlHashSet<K>);
30
31impl<K> ScratchHashSet<K> {
32 pub fn get(&mut self) -> &mut PlHashSet<K> {
34 self.0.clear();
35 &mut self.0
36 }
37}
38
39#[derive(Default)]
40pub struct ScratchIndexSet<K>(PlIndexSet<K>);
41
42impl<K> ScratchIndexSet<K> {
43 pub fn get(&mut self) -> &mut PlIndexSet<K> {
45 self.0.clear();
46 &mut self.0
47 }
48}
49
50#[derive(Default)]
51pub struct ScratchIndexMap<K, V>(PlIndexMap<K, V>);
52
53impl<K, V> ScratchIndexMap<K, V> {
54 pub fn get(&mut self) -> &mut PlIndexMap<K, V> {
56 self.0.clear();
57 &mut self.0
58 }
59}
60
61pub trait SeedableFromU64SeedExt {
62 fn seed_from_u64(seed: u64) -> Self;
63}
64
65impl SeedableFromU64SeedExt for PlSeedableRandomStateQuality {
66 fn seed_from_u64(seed: u64) -> Self {
67 PlSeedableRandomStateQuality::with_seed(seed, SharedSeed::global_fixed())
68 }
69}
70
71pub trait InitHashMaps {
72 type HashMap;
73
74 fn new() -> Self::HashMap;
75
76 fn with_capacity(capacity: usize) -> Self::HashMap;
77}
78
79impl<K, V> InitHashMaps for PlHashMap<K, V> {
80 type HashMap = Self;
81
82 fn new() -> Self::HashMap {
83 Self::with_capacity_and_hasher(0, Default::default())
84 }
85
86 fn with_capacity(capacity: usize) -> Self {
87 Self::with_capacity_and_hasher(capacity, Default::default())
88 }
89}
90impl<K> InitHashMaps for PlHashSet<K> {
91 type HashMap = Self;
92
93 fn new() -> Self::HashMap {
94 Self::with_capacity_and_hasher(0, Default::default())
95 }
96
97 fn with_capacity(capacity: usize) -> Self {
98 Self::with_capacity_and_hasher(capacity, Default::default())
99 }
100}
101
102impl<K> InitHashMaps for PlIndexSet<K> {
103 type HashMap = Self;
104
105 fn new() -> Self::HashMap {
106 Self::with_capacity_and_hasher(0, Default::default())
107 }
108
109 fn with_capacity(capacity: usize) -> Self::HashMap {
110 Self::with_capacity_and_hasher(capacity, Default::default())
111 }
112}
113
114impl<K, V> InitHashMaps for PlIndexMap<K, V> {
115 type HashMap = Self;
116
117 fn new() -> Self::HashMap {
118 Self::with_capacity_and_hasher(0, Default::default())
119 }
120
121 fn with_capacity(capacity: usize) -> Self::HashMap {
122 Self::with_capacity_and_hasher(capacity, Default::default())
123 }
124}
125
126#[derive(Debug, Clone)]
127#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
128#[cfg_attr(feature = "dsl-schema", derive(schemars::JsonSchema))]
129#[repr(transparent)]
130pub struct PlIndexMapHashable<K: Eq + Hash, V>(pub PlIndexMap<K, V>);
131
132impl<K: Eq + Hash + PartialEq, V: PartialEq> PartialEq for PlIndexMapHashable<K, V> {
133 fn eq(&self, other: &Self) -> bool {
134 PartialEq::eq(&self.0, &other.0)
135 }
136}
137
138impl<K: Eq + Hash + PartialEq, V: PartialEq> Eq for PlIndexMapHashable<K, V> {}
139
140impl<K: Eq + Hash, V> Hash for PlIndexMapHashable<K, V> {
141 fn hash<H: Hasher>(&self, state: &mut H) {
142 self.keys().len().hash(state);
143 for key in self.keys() {
144 key.hash(state);
145 }
146 }
147}
148
149impl<K: Eq + Hash, V> std::ops::Deref for PlIndexMapHashable<K, V> {
150 type Target = PlIndexMap<K, V>;
151
152 fn deref(&self) -> &Self::Target {
153 &self.0
154 }
155}
156
157impl<K: Eq + Hash, V> std::ops::DerefMut for PlIndexMapHashable<K, V> {
158 fn deref_mut(&mut self) -> &mut Self::Target {
159 &mut self.0
160 }
161}