1use std::fmt;
2use std::ops::Deref;
3use std::slice;
4
5#[derive(Clone)]
16pub struct SparseSet {
17 dense: Vec<usize>,
20 sparse: Box<[usize]>,
25}
26
27impl SparseSet {
28 pub fn new(size: usize) -> SparseSet {
29 SparseSet {
30 dense: Vec::with_capacity(size),
31 sparse: vec![0; size].into_boxed_slice(),
32 }
33 }
34
35 pub fn len(&self) -> usize {
36 self.dense.len()
37 }
38
39 pub fn is_empty(&self) -> bool {
40 self.dense.is_empty()
41 }
42
43 pub fn capacity(&self) -> usize {
44 self.dense.capacity()
45 }
46
47 pub fn insert(&mut self, value: usize) {
48 let i = self.len();
49 assert!(i < self.capacity());
50 self.dense.push(value);
51 self.sparse[value] = i;
52 }
53
54 pub fn contains(&self, value: usize) -> bool {
55 let i = self.sparse[value];
56 self.dense.get(i) == Some(&value)
57 }
58
59 pub fn clear(&mut self) {
60 self.dense.clear();
61 }
62}
63
64impl fmt::Debug for SparseSet {
65 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66 write!(f, "SparseSet({:?})", self.dense)
67 }
68}
69
70impl Deref for SparseSet {
71 type Target = [usize];
72
73 fn deref(&self) -> &Self::Target {
74 &self.dense
75 }
76}
77
78impl<'a> IntoIterator for &'a SparseSet {
79 type Item = &'a usize;
80 type IntoIter = slice::Iter<'a, usize>;
81 fn into_iter(self) -> Self::IntoIter {
82 self.iter()
83 }
84}