1macro_rules! imp_slot_map {
21 (
22 new $($const:ident)?: $new:expr,
23 slots: $slots:ident,
24 ($($value:ty)?)
25 ) => {
26 #[derive(Debug, Clone)]
28 #[repr(transparent)]
29 pub struct SlotMap<T>(pub Arena<T, ()>);
30
31 pub struct VacantEntry<'a, T>(pub imp::VacantEntry<'a, T, ()>);
33
34 pub type Key = $crate::Key<usize>;
36
37 pub type Entries<'a, T> = imp::Entries<'a, T, (), DefaultVersion, usize>;
39 pub type EntriesMut<'a, T> = imp::EntriesMut<'a, T, (), DefaultVersion, usize>;
41 pub type IntoEntries<T> = imp::IntoEntries<T, (), DefaultVersion, usize>;
43
44 impl<T> VacantEntry<'_, T> {
45 pub fn key(&self) -> usize { self.0.key() }
47
48 pub fn insert(self, value: T) -> usize { self.0.insert(value) }
50 }
51
52 impl<T> Default for SlotMap<T> {
53 fn default() -> Self { Self::new() }
54 }
55
56 impl<T> SlotMap<T> {
57 pub $($const)? fn new() -> Self { Self($new) }
59 pub fn is_empty(&self) -> bool { self.0.is_empty() }
61 pub fn len(&self) -> usize { self.0.len() }
63 pub fn capacity(&self) -> usize { self.0.capacity() }
65 pub fn reserve(&mut self, additional: usize) { self.0.reserve(additional) }
67 pub fn clear(&mut self) { self.0.clear(); }
69 pub fn vacant_entry(&mut self) -> VacantEntry<'_, T> { VacantEntry(self.0.vacant_entry()) }
71 pub fn insert(&mut self, value: T) -> Key { self.0.insert(value) }
73 pub fn contains(&self, key: Key) -> bool { self.0.contains(key) }
75 pub fn remove(&mut self, key: Key) -> T { self.0.remove(key) }
77 pub fn try_remove(&mut self, key: Key) -> Option<T> { self.0.try_remove(key) }
79 pub fn delete(&mut self, key: Key) -> bool { self.0.delete(key) }
81 pub fn get(&self, key: Key) -> Option<&T> { self.0.get(key) }
83 pub fn get_mut(&mut self, key: Key) -> Option<&mut T> { self.0.get_mut(key) }
85 #[allow(clippy::missing_safety_doc)]
87 pub unsafe fn get_unchecked(&self, index: usize) -> &T { self.0.get_unchecked(index) }
88 #[allow(clippy::missing_safety_doc)]
90 pub unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T { self.0.get_unchecked_mut(index) }
91 pub fn delete_all(&mut self) { self.0.delete_all() }
93 pub fn retain<F: FnMut(&mut T) -> bool>(&mut self, f: F) { self.0.retain(f) }
95 pub fn keys(&self) -> Keys<'_ $(, $value)?> { self.0.keys() }
97 pub fn iter(&self) -> Iter<'_, T> { self.0.iter() }
99 pub fn iter_mut(&mut self) -> IterMut<'_, T> { self.0.iter_mut() }
101 pub fn drain(&mut self) -> Drain<'_, T> { self.0.drain() }
103 pub fn drain_filter<F: FnMut(&mut T) -> bool>(&mut self, filter: F) -> DrainFilter<'_, T, F> { self.0.drain_filter(filter) }
105 pub fn entries(&self) -> Entries<'_, T> { self.0.entries() }
107 pub fn entries_mut(&mut self) -> EntriesMut<'_, T> { self.0.entries_mut() }
109 pub fn into_entries(self) -> IntoEntries<T> { self.0.into_entries() }
111 }
112
113 impl<T> IntoIterator for SlotMap<T> {
114 type IntoIter = IntoIter<T>;
115 type Item = T;
116
117 fn into_iter(self) -> Self::IntoIter { self.0.into_iter() }
118 }
119
120 impl<T> Index<Key> for SlotMap<T> {
121 type Output = T;
122
123 fn index(&self, key: Key) -> &Self::Output { &self.0[key] }
124 }
125
126 impl<T> IndexMut<Key> for SlotMap<T> {
127 fn index_mut(&mut self, key: Key) -> &mut Self::Output { &mut self.0[key] }
128 }
129 };
130}
131
132pub mod dense {
136 use core::ops::{Index, IndexMut};
137
138 use crate::{
139 base::dense::{self as imp, Arena},
140 version::DefaultVersion,
141 };
142
143 pub type Iter<'a, T> = core::slice::Iter<'a, T>;
145 pub type IterMut<'a, T> = core::slice::IterMut<'a, T>;
147 pub type IntoIter<T> = std::vec::IntoIter<T>;
149
150 pub type Drain<'a, T> = imp::Drain<'a, T, (), DefaultVersion>;
152 pub type DrainFilter<'a, T, F> = imp::DrainFilter<'a, T, (), DefaultVersion, F>;
154
155 pub type Keys<'a> = imp::Keys<'a, (), DefaultVersion, Key>;
157
158 imp_slot_map! {
159 new: Arena::with_ident(()),
160 slots: len,
161 ()
162 }
163}
164
165pub mod hop {
169 use core::ops::{Index, IndexMut};
170
171 use crate::{
172 base::hop::{self as imp, Arena},
173 version::DefaultVersion,
174 };
175
176 pub type Iter<'a, T> = imp::Iter<'a, T, DefaultVersion>;
178 pub type IterMut<'a, T> = imp::IterMut<'a, T, DefaultVersion>;
180 pub type IntoIter<T> = imp::IntoIter<T, DefaultVersion>;
182
183 pub type Drain<'a, T> = imp::Drain<'a, T, DefaultVersion>;
185 pub type DrainFilter<'a, T, F> = imp::DrainFilter<'a, T, DefaultVersion, F>;
187
188 pub type Keys<'a, T> = imp::Keys<'a, T, (), DefaultVersion, Key>;
190
191 imp_slot_map! {
192 new: Arena::with_ident(()),
193 slots: len,
194 (T)
195 }
196}
197
198pub mod sparse {
202 use core::ops::{Index, IndexMut};
203
204 use crate::{
205 base::sparse::{self as imp, Arena},
206 version::DefaultVersion,
207 };
208
209 pub type Iter<'a, T> = imp::Iter<'a, T, DefaultVersion>;
211 pub type IterMut<'a, T> = imp::IterMut<'a, T, DefaultVersion>;
213 pub type IntoIter<T> = imp::IntoIter<T, DefaultVersion>;
215
216 pub type Drain<'a, T> = imp::Drain<'a, T, DefaultVersion>;
218 pub type DrainFilter<'a, T, F> = imp::DrainFilter<'a, T, DefaultVersion, F>;
220
221 pub type Keys<'a, T> = imp::Keys<'a, T, (), DefaultVersion, Key>;
223
224 imp_slot_map! {
225 new const: Arena::INIT,
226 slots: slots,
227 (T)
228 }
229}