1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
use super::unionfind::UnionFind;
use hashbrown::raw::{Bucket, RawTable};
use std::hash::{Hash, Hasher};
use std::marker::PhantomData;
pub trait CtxEq<V1: ?Sized, V2: ?Sized> {
fn ctx_eq(&self, a: &V1, b: &V2, uf: &mut UnionFind) -> bool;
}
pub trait CtxHash<Value: ?Sized>: CtxEq<Value, Value> {
fn ctx_hash(&self, value: &Value, uf: &mut UnionFind) -> u64;
}
#[derive(Default)]
pub struct NullCtx;
impl<V: Eq + Hash> CtxEq<V, V> for NullCtx {
fn ctx_eq(&self, a: &V, b: &V, _: &mut UnionFind) -> bool {
a.eq(b)
}
}
impl<V: Eq + Hash> CtxHash<V> for NullCtx {
fn ctx_hash(&self, value: &V, _: &mut UnionFind) -> u64 {
let mut state = fxhash::FxHasher::default();
value.hash(&mut state);
state.finish()
}
}
struct BucketData<K, V> {
hash: u32,
k: K,
v: V,
}
pub struct CtxHashMap<K, V> {
raw: RawTable<BucketData<K, V>>,
}
impl<K, V> CtxHashMap<K, V> {
pub fn new() -> Self {
Self {
raw: RawTable::new(),
}
}
pub fn with_capacity(capacity: usize) -> Self {
Self {
raw: RawTable::with_capacity(capacity),
}
}
}
impl<K, V> CtxHashMap<K, V> {
pub fn insert<Ctx: CtxEq<K, K> + CtxHash<K>>(
&mut self,
k: K,
v: V,
ctx: &Ctx,
uf: &mut UnionFind,
) -> Option<V> {
let hash = ctx.ctx_hash(&k, uf) as u32;
match self.raw.find(hash as u64, |bucket| {
hash == bucket.hash && ctx.ctx_eq(&bucket.k, &k, uf)
}) {
Some(bucket) => {
let data = unsafe { bucket.as_mut() };
Some(std::mem::replace(&mut data.v, v))
}
None => {
let data = BucketData { hash, k, v };
self.raw
.insert_entry(hash as u64, data, |bucket| bucket.hash as u64);
None
}
}
}
pub fn get<'a, Q, Ctx: CtxEq<K, Q> + CtxHash<Q> + CtxHash<K>>(
&'a self,
k: &Q,
ctx: &Ctx,
uf: &mut UnionFind,
) -> Option<&'a V> {
let hash = ctx.ctx_hash(k, uf) as u32;
self.raw
.find(hash as u64, |bucket| {
hash == bucket.hash && ctx.ctx_eq(&bucket.k, k, uf)
})
.map(|bucket| {
let data = unsafe { bucket.as_ref() };
&data.v
})
}
#[inline(always)]
pub fn entry<'a, Ctx: CtxEq<K, K> + CtxHash<K>>(
&'a mut self,
k: K,
ctx: &'a Ctx,
uf: &mut UnionFind,
) -> Entry<'a, K, V> {
let hash = ctx.ctx_hash(&k, uf) as u32;
match self.raw.find(hash as u64, |bucket| {
hash == bucket.hash && ctx.ctx_eq(&bucket.k, &k, uf)
}) {
Some(bucket) => Entry::Occupied(OccupiedEntry {
bucket,
_phantom: PhantomData,
}),
None => Entry::Vacant(VacantEntry {
raw: &mut self.raw,
hash,
key: k,
}),
}
}
}
pub enum Entry<'a, K: 'a, V> {
Occupied(OccupiedEntry<'a, K, V>),
Vacant(VacantEntry<'a, K, V>),
}
pub struct OccupiedEntry<'a, K, V> {
bucket: Bucket<BucketData<K, V>>,
_phantom: PhantomData<&'a ()>,
}
impl<'a, K: 'a, V> OccupiedEntry<'a, K, V> {
pub fn get(&self) -> &'a V {
let bucket = unsafe { self.bucket.as_ref() };
&bucket.v
}
}
pub struct VacantEntry<'a, K, V> {
raw: &'a mut RawTable<BucketData<K, V>>,
hash: u32,
key: K,
}
impl<'a, K, V> VacantEntry<'a, K, V> {
pub fn insert(self, v: V) -> &'a V {
let bucket = self.raw.insert(
self.hash as u64,
BucketData {
hash: self.hash,
k: self.key,
v,
},
|bucket| bucket.hash as u64,
);
let data = unsafe { bucket.as_ref() };
&data.v
}
}
#[cfg(test)]
mod test {
use super::*;
use std::hash::Hash;
#[derive(Clone, Copy, Debug)]
struct Key {
index: u32,
}
struct Ctx {
vals: &'static [&'static str],
}
impl CtxEq<Key, Key> for Ctx {
fn ctx_eq(&self, a: &Key, b: &Key, _: &mut UnionFind) -> bool {
self.vals[a.index as usize].eq(self.vals[b.index as usize])
}
}
impl CtxHash<Key> for Ctx {
fn ctx_hash(&self, value: &Key, _: &mut UnionFind) -> u64 {
let mut state = fxhash::FxHasher::default();
self.vals[value.index as usize].hash(&mut state);
state.finish()
}
}
#[test]
fn test_basic() {
let ctx = Ctx {
vals: &["a", "b", "a"],
};
let mut uf = UnionFind::new();
let k0 = Key { index: 0 };
let k1 = Key { index: 1 };
let k2 = Key { index: 2 };
assert!(ctx.ctx_eq(&k0, &k2, &mut uf));
assert!(!ctx.ctx_eq(&k0, &k1, &mut uf));
assert!(!ctx.ctx_eq(&k2, &k1, &mut uf));
let mut map: CtxHashMap<Key, u64> = CtxHashMap::new();
assert_eq!(map.insert(k0, 42, &ctx, &mut uf), None);
assert_eq!(map.insert(k2, 84, &ctx, &mut uf), Some(42));
assert_eq!(map.get(&k1, &ctx, &mut uf), None);
assert_eq!(*map.get(&k0, &ctx, &mut uf).unwrap(), 84);
}
#[test]
fn test_entry() {
let mut ctx = Ctx {
vals: &["a", "b", "a"],
};
let mut uf = UnionFind::new();
let k0 = Key { index: 0 };
let k1 = Key { index: 1 };
let k2 = Key { index: 2 };
let mut map: CtxHashMap<Key, u64> = CtxHashMap::new();
match map.entry(k0, &mut ctx, &mut uf) {
Entry::Vacant(v) => {
v.insert(1);
}
_ => panic!(),
}
match map.entry(k1, &mut ctx, &mut uf) {
Entry::Vacant(_) => {}
Entry::Occupied(_) => panic!(),
}
match map.entry(k2, &mut ctx, &mut uf) {
Entry::Occupied(o) => {
assert_eq!(*o.get(), 1);
}
_ => panic!(),
}
}
}