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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
//! A sparse set.

use std::ops::{Deref, DerefMut};
use std::slice;

/// An implementation of a sparse set.
///
/// A sparse set is a specialized data structure for representing a set of integers.
/// It can be useful in some very narrow and specific cases, namely when the universe of possible
/// values is very large but used very sparingly and the set is iterated often or cleared often.
///
/// In this implement the SparseSet can hold an arbitrary value for every integer (key) in the set.
///
/// # Example
///
/// ```
/// use sparseset::SparseSet;
/// let mut set = SparseSet::with_capacity(128);
/// set.insert(42, 3);
/// set.insert(77, 5);
/// set.insert(23, 8);
///
/// assert_eq!(*set.get(42).unwrap(), 3);
///
/// set.remove(42);
/// assert!(!set.get(42).is_some());
///
/// for entry in set {
///     println!("- {} => {}", entry.key(), entry.value);
/// }
/// ```
///
/// # Performance
///
/// Note that SparseSet is *incredibly* inefficient in terms of space. The O(1) insertion time
/// assumes space for the element is already allocated.  Otherwise, a large key may require a
/// massive reallocation, with no direct relation to the number of elements in the collection.
/// SparseSet should only be seriously considered for small keys.
///
/// ## Runtime complexity
///
/// See how the runtime complexity of SparseSet compares to Hash and Btree maps:
///
/// |           | get       | insert   | remove   | iterate | clear        |
/// |-----------|-----------|----------|----------|---------|--------------|
/// | SparseSet | O(1)      | O(1)*    | O(1)     | O(n)    | O(1) / O(n)* |
/// | HashMap   | O(1)~     | O(1)~*   | O(1)~    | N/A     | N/A          |
/// | BTreeMap  | O(log n)  | O(log n) | O(log n) | N/A     | N/A          |
///
/// * Clear is O(1) on simple types and O(n) on types whom implements Drop.
/// * Iterating is really efficient, its iterating over a dense array. In fact, its even possible
/// to get an (even mutable) slice of the entries in the set.
///
/// See http://research.swtch.com/sparse for more details.
pub struct SparseSet<T> {
    dense: Vec<Entry<T>>,
    sparse: Vec<usize>,
}

/// An entry in the sparse set.
/// You can retrieve a slice (possibly mutable) of [Entry] from the SparseSet.
pub struct Entry<T> {
    key: usize,

    /// The value stored in the entry. A reference to it is returned by value() and value_mut(), as
    /// well as get() and get_mut() directly from SparseSet. The field can be used without going
    /// trough the accessors functions since it is public.
    pub value: T,
}

impl<T> Entry<T> {
    /// Read-only access to the entry's key.
    pub fn key(&self) -> usize {
        self.key
    }

    /// Returns the value. Mainly for symmetry with key() since the value is public anyway.
    pub fn value(&self) -> &T {
        &self.value
    }

    /// Returns the value, mutable. Mainly for symmetry with key() since the value is public
    /// anyway.
    pub fn value_mut(&mut self) -> &mut T {
        &mut self.value
    }
}

impl<T> SparseSet<T> {
    /// Creates a SparseSet with the given capacity.
    pub fn with_capacity(size: usize) -> Self {
        let mut sparse = Vec::with_capacity(size);
        unsafe { sparse.set_len(size) }
        SparseSet {
            dense: Vec::with_capacity(size),
            sparse: sparse,
        }
    }

    pub fn len(&self) -> usize {
        self.dense.len()
    }
    pub fn capacity(&self) -> usize {
        self.sparse.len()
    }

    /// Clears the SparseSet in O(1) for simple T and O(n) if T implements Drop.
    pub fn clear(&mut self) {
        self.dense.clear();
    }

    fn dense_idx(&self, key: usize) -> Option<usize> {
        let dense_idx = self.sparse[key];
        if dense_idx < self.len() {
            let entry = &self.dense[dense_idx];
            if entry.key == key {
                return Some(dense_idx);
            }
        }
        None
    }

    /// Returns a reference to the value corresponding to the given key in O(1).
    pub fn get(&self, key: usize) -> Option<&T> {
        if let Some(dense_idx) = self.dense_idx(key) {
            Some(&self.dense[dense_idx].value)
        } else {
            None
        }
    }

    /// Returns a mutable reference to the value corresponding to the given key in O(1).
    pub fn get_mut(&mut self, key: usize) -> Option<&mut T> {
        if let Some(dense_idx) = self.dense_idx(key) {
            Some(&mut self.dense[dense_idx].value)
        } else {
            None
        }
    }

    /// Test if the given key is contained in the set in O(1).
    pub fn contains(&self, key: usize) -> bool {
        self.dense_idx(key).is_some()
    }

    /// Insert in the set a value for the given key in O(1).
    ///
    /// * returns true if the key was set.
    /// * returns false if the key was already set.
    ///
    /// If the key was already set, the previous value is overridden.
    pub fn insert(&mut self, key: usize, value: T) -> bool {
        assert!(
            key < self.capacity(),
            "key ({}) must be under capacity ({})",
            key,
            self.capacity()
        );
        if let Some(stored_value) = self.get_mut(key) {
            *stored_value = value;
            return false;
        }
        let n = self.dense.len();
        self.dense.push(Entry {
            key: key,
            value: value,
        });
        self.sparse[key] = n;
        true
    }

    /// Removes the given key in O(1).
    /// Returns the removed value or None if key not found.
    pub fn remove(&mut self, key: usize) -> Option<T> {
        if self.contains(key) {
            let dense_idx = self.sparse[key];
            let r = self.dense.swap_remove(dense_idx).value;
            if dense_idx < self.len() {
                let swapped_entry = &self.dense[dense_idx];
                self.sparse[swapped_entry.key] = dense_idx;
            }
            // not strictly necessary, just nice to
            // restrict any future contains(key) to one test.
            self.sparse[key] = self.capacity();
            Some(r)
        } else {
            None
        }
    }
}

/// Deref to a slice.
impl<T> Deref for SparseSet<T> {
    type Target = [Entry<T>];

    fn deref(&self) -> &Self::Target {
        &self.dense[..]
    }
}

/// Deref to a mutable slice.
impl<T> DerefMut for SparseSet<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.dense[..]
    }
}

/// Move into an interator, consuming the SparseSet.
impl<T> IntoIterator for SparseSet<T> {
    type Item = Entry<T>;
    type IntoIter = std::vec::IntoIter<Self::Item>;

    fn into_iter(self) -> Self::IntoIter {
        self.dense.into_iter()
    }
}

/// An interator over the elements of the SparseSet.
impl<'a, T> IntoIterator for &'a SparseSet<T> {
    type Item = &'a Entry<T>;
    type IntoIter = slice::Iter<'a, Entry<T>>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

/// An interator over mutable elements of the SparseSet.
impl<'a, T> IntoIterator for &'a mut SparseSet<T> {
    type Item = &'a mut Entry<T>;
    type IntoIter = slice::IterMut<'a, Entry<T>>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter_mut()
    }
}

#[test]
fn create() {
    let s = SparseSet::<()>::with_capacity(42);
    assert_eq!(s.len(), 0);
    assert_eq!(s.capacity(), 42);
}

#[test]
fn insert() {
    let mut s = SparseSet::with_capacity(42);
    s.insert(3, ());
    assert_eq!(s.len(), 1);
    assert_eq!(s.capacity(), 42);
    s.insert(3, ());
    assert_eq!(s.len(), 1);
    assert_eq!(s.capacity(), 42);
}

#[test]
fn clear() {
    let mut s = SparseSet::with_capacity(42);
    s.insert(3, ());
    assert_eq!(s.len(), 1);
    assert_eq!(s.capacity(), 42);
    s.clear();
    assert_eq!(s.len(), 0);
    assert_eq!(s.capacity(), 42);
}

#[test]
fn contains() {
    let mut s = SparseSet::with_capacity(5);
    s.insert(3, ());
    s.insert(1, ());
    assert_eq!(s.len(), 2);
    assert_eq!(s.capacity(), 5);

    assert!(s.contains(0) == false);
    assert!(s.contains(1) == true);
    assert!(s.contains(2) == false);
    assert!(s.contains(3) == true);
    assert!(s.contains(4) == false);
}

#[test]
fn remove() {
    let mut s = SparseSet::with_capacity(5);
    s.insert(3, ());
    s.insert(1, ());
    assert_eq!(s.len(), 2);
    assert_eq!(s.capacity(), 5);

    assert!(s.contains(1));
    assert!(s.contains(3));

    s.remove(3);
    assert_eq!(s.len(), 1);
    assert_eq!(s.capacity(), 5);

    assert!(s.contains(1));
    assert!(s.contains(3) == false);

    s.remove(3);
    assert_eq!(s.len(), 1);
    assert_eq!(s.capacity(), 5);

    s.remove(1);
    assert_eq!(s.len(), 0);
    assert_eq!(s.capacity(), 5);

    assert!(s.contains(1) == false);
    assert!(s.contains(3) == false);
}

#[test]
fn remove2() {
    let mut s = SparseSet::with_capacity(20_000);

    for i in 1..=10_000 {
        s.insert(i, 42);
    }

    for i in 1..=10_000 {
        s.remove(i);
    }
}

#[test]
fn remove3() {
    let mut s = SparseSet::with_capacity(100);
    s.insert(3, 42);
    s.insert(2, 42);
    s.insert(1, 42);

    s.remove(3);
    s.remove(2);
    s.remove(1);
}

#[test]
fn iter() {
    let mut s = SparseSet::with_capacity(5);
    s.insert(3, ());
    s.insert(1, ());
    s.insert(2, ());
    s.remove(1);
    assert_eq!(s.len(), 2);

    let mut i = 0;
    let mut total = 0;
    for entry in &s {
        i += 1;
        total += entry.key();
    }
    assert_eq!(i, 2);
    assert_eq!(total, 5);
    assert_eq!(s.len(), 2);
}

#[test]
fn iter_mut() {
    let mut s = SparseSet::with_capacity(5);
    s.insert(1, 11);
    s.insert(2, 22);
    s.remove(1);
    s.insert(4, 44);
    assert_eq!(s.len(), 2);

    let mut i = 0;
    let mut total = 0;
    for entry in &mut s {
        i += 1;
        total += entry.value;
        entry.value += 1;
    }
    assert_eq!(i, 2);
    assert_eq!(total, 22 + 44);

    let mut i = 0;
    let mut total = 0;
    for entry in &s {
        i += 1;
        total += entry.value;
    }
    assert_eq!(i, 2);
    assert_eq!(total, 23 + 45);
}

#[test]
fn into_iter() {
    let mut s = SparseSet::with_capacity(5);
    s.insert(3, ());
    s.insert(1, ());
    s.insert(2, ());
    assert_eq!(s.len(), 3);

    let mut i = 0;
    let mut total = 0;
    for entry in s {
        i += 1;
        total += entry.key();
    }
    assert_eq!(i, 3);
    assert_eq!(total, 3 + 1 + 2);
}

#[test]
fn slice() {
    let mut s = SparseSet::with_capacity(5);
    s.insert(3, ());
    s.insert(1, ());
    s.insert(2, ());

    {
        let the_slice: &[Entry<()>] = &s;
        let mut i = 0;
        let mut total: usize = 0;
        for entry in the_slice {
            i += 1;
            total += entry.key();
        }
        assert_eq!(i, 3);
        assert_eq!(total, 3 + 1 + 2);
    }

    s.insert(0, ());

    {
        let the_slice: &mut [Entry<()>] = &mut s;
        let mut i = 0;
        let mut total: usize = 0;
        for entry in the_slice {
            i += 1;
            total += entry.key();
            entry.value = ();
        }
        assert_eq!(i, 4);
        assert_eq!(total, 3 + 1 + 2);
    }
}

#[test]
fn get() {
    let mut s = SparseSet::with_capacity(5);
    s.insert(1, 11);
    s.insert(2, 22);
    {
        let v = s.get(2);
        assert!(v.is_some());
        let g = s.get(1);
        assert!(g.is_some());
    }
    {
        let v = s.get_mut(2);
        assert!(v.is_some());
        let v = v.unwrap();
        *v += 1;
    }
    assert_eq!(*s.get(1).unwrap(), 11);
    assert_eq!(*s.get(2).unwrap(), 23);
}

#[test]
fn slice_indexing() {
    let mut s = SparseSet::with_capacity(5);
    s.insert(2, ());
    s.insert(1, ());
    let e = &s[0];
    assert_eq!(e.key(), 2);
}