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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
use crate::map::SecondaryMap;
use crate::EntityRef;
use alloc::vec::Vec;
use core::mem;
use core::slice;
use core::u32;
#[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize};
pub trait SparseMapValue<K> {
fn key(&self) -> K;
}
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
pub struct SparseMap<K, V>
where
K: EntityRef,
V: SparseMapValue<K>,
{
sparse: SecondaryMap<K, u32>,
dense: Vec<V>,
}
impl<K, V> SparseMap<K, V>
where
K: EntityRef,
V: SparseMapValue<K>,
{
pub fn new() -> Self {
Self {
sparse: SecondaryMap::new(),
dense: Vec::new(),
}
}
pub fn len(&self) -> usize {
self.dense.len()
}
pub fn is_empty(&self) -> bool {
self.dense.is_empty()
}
pub fn clear(&mut self) {
self.dense.clear();
}
pub fn get(&self, key: K) -> Option<&V> {
if let Some(idx) = self.sparse.get(key).cloned() {
if let Some(entry) = self.dense.get(idx as usize) {
if entry.key() == key {
return Some(entry);
}
}
}
None
}
pub fn get_mut(&mut self, key: K) -> Option<&mut V> {
if let Some(idx) = self.sparse.get(key).cloned() {
if let Some(entry) = self.dense.get_mut(idx as usize) {
if entry.key() == key {
return Some(entry);
}
}
}
None
}
fn index(&self, key: K) -> Option<usize> {
if let Some(idx) = self.sparse.get(key).cloned() {
let idx = idx as usize;
if let Some(entry) = self.dense.get(idx) {
if entry.key() == key {
return Some(idx);
}
}
}
None
}
pub fn contains_key(&self, key: K) -> bool {
self.get(key).is_some()
}
pub fn insert(&mut self, value: V) -> Option<V> {
let key = value.key();
if let Some(entry) = self.get_mut(key) {
return Some(mem::replace(entry, value));
}
let idx = self.dense.len();
debug_assert!(idx <= u32::MAX as usize, "SparseMap overflow");
self.dense.push(value);
self.sparse[key] = idx as u32;
None
}
pub fn remove(&mut self, key: K) -> Option<V> {
if let Some(idx) = self.index(key) {
let back = self.dense.pop().unwrap();
if idx == self.dense.len() {
return Some(back);
}
self.sparse[back.key()] = idx as u32;
return Some(mem::replace(&mut self.dense[idx], back));
}
None
}
pub fn pop(&mut self) -> Option<V> {
self.dense.pop()
}
pub fn values(&self) -> slice::Iter<V> {
self.dense.iter()
}
pub fn as_slice(&self) -> &[V] {
self.dense.as_slice()
}
}
impl<'a, K, V> IntoIterator for &'a SparseMap<K, V>
where
K: EntityRef,
V: SparseMapValue<K>,
{
type Item = &'a V;
type IntoIter = slice::Iter<'a, V>;
fn into_iter(self) -> Self::IntoIter {
self.values()
}
}
impl<T> SparseMapValue<T> for T
where
T: EntityRef,
{
fn key(&self) -> Self {
*self
}
}
pub type SparseSet<T> = SparseMap<T, T>;
#[cfg(test)]
mod tests {
use super::*;
use crate::EntityRef;
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Inst(u32);
entity_impl!(Inst, "inst");
#[derive(PartialEq, Eq, Debug)]
struct Obj(Inst, &'static str);
impl SparseMapValue<Inst> for Obj {
fn key(&self) -> Inst {
self.0
}
}
#[test]
fn empty_immutable_map() {
let i1 = Inst::new(1);
let map: SparseMap<Inst, Obj> = SparseMap::new();
assert!(map.is_empty());
assert_eq!(map.len(), 0);
assert_eq!(map.get(i1), None);
assert_eq!(map.values().count(), 0);
}
#[test]
fn single_entry() {
let i0 = Inst::new(0);
let i1 = Inst::new(1);
let i2 = Inst::new(2);
let mut map = SparseMap::new();
assert!(map.is_empty());
assert_eq!(map.len(), 0);
assert_eq!(map.get(i1), None);
assert_eq!(map.get_mut(i1), None);
assert_eq!(map.remove(i1), None);
assert_eq!(map.insert(Obj(i1, "hi")), None);
assert!(!map.is_empty());
assert_eq!(map.len(), 1);
assert_eq!(map.get(i0), None);
assert_eq!(map.get(i1), Some(&Obj(i1, "hi")));
assert_eq!(map.get(i2), None);
assert_eq!(map.get_mut(i0), None);
assert_eq!(map.get_mut(i1), Some(&mut Obj(i1, "hi")));
assert_eq!(map.get_mut(i2), None);
assert_eq!(map.remove(i0), None);
assert_eq!(map.remove(i2), None);
assert_eq!(map.remove(i1), Some(Obj(i1, "hi")));
assert_eq!(map.len(), 0);
assert_eq!(map.get(i1), None);
assert_eq!(map.get_mut(i1), None);
assert_eq!(map.remove(i0), None);
assert_eq!(map.remove(i1), None);
assert_eq!(map.remove(i2), None);
}
#[test]
fn multiple_entries() {
let i0 = Inst::new(0);
let i1 = Inst::new(1);
let i2 = Inst::new(2);
let i3 = Inst::new(3);
let mut map = SparseMap::new();
assert_eq!(map.insert(Obj(i2, "foo")), None);
assert_eq!(map.insert(Obj(i1, "bar")), None);
assert_eq!(map.insert(Obj(i0, "baz")), None);
assert_eq!(
map.values().map(|obj| obj.1).collect::<Vec<_>>(),
["foo", "bar", "baz"]
);
assert_eq!(map.len(), 3);
assert_eq!(map.get(i0), Some(&Obj(i0, "baz")));
assert_eq!(map.get(i1), Some(&Obj(i1, "bar")));
assert_eq!(map.get(i2), Some(&Obj(i2, "foo")));
assert_eq!(map.get(i3), None);
assert_eq!(map.remove(i1), Some(Obj(i1, "bar")));
assert_eq!(map.len(), 2);
assert_eq!(map.get(i0), Some(&Obj(i0, "baz")));
assert_eq!(map.get(i1), None);
assert_eq!(map.get(i2), Some(&Obj(i2, "foo")));
assert_eq!(map.get(i3), None);
assert_eq!(map.insert(Obj(i1, "barbar")), None);
assert_eq!(map.len(), 3);
assert_eq!(map.get(i0), Some(&Obj(i0, "baz")));
assert_eq!(map.get(i1), Some(&Obj(i1, "barbar")));
assert_eq!(map.get(i2), Some(&Obj(i2, "foo")));
assert_eq!(map.get(i3), None);
assert_eq!(map.insert(Obj(i0, "bazbaz")), Some(Obj(i0, "baz")));
assert_eq!(map.len(), 3);
assert_eq!(map.get(i0), Some(&Obj(i0, "bazbaz")));
assert_eq!(map.get(i1), Some(&Obj(i1, "barbar")));
assert_eq!(map.get(i2), Some(&Obj(i2, "foo")));
assert_eq!(map.get(i3), None);
let mut v = Vec::new();
for i in &map {
v.push(i.1);
}
assert_eq!(v.len(), map.len());
}
#[test]
fn entity_set() {
let i0 = Inst::new(0);
let i1 = Inst::new(1);
let mut set = SparseSet::new();
assert_eq!(set.insert(i0), None);
assert_eq!(set.insert(i0), Some(i0));
assert_eq!(set.insert(i1), None);
assert_eq!(set.get(i0), Some(&i0));
assert_eq!(set.get(i1), Some(&i1));
}
}