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
//! A reimplementation of [`slotmap`](https://docs.rs/slotmap/) in terms
//! of the generic arenas in [`base`](crate::base)
//!
//! The module structure here is identical to [`crate::base`], and
//! you can look there for detailed documentation about the types.
//! Each implementation of `SlotMap` will have all the methods from the
//! corrosponding `Arena`, and those that take or produce generic keys
//! will instead take/produce a `Key`.
//!
//! In each module, you'll find an `SlotMap` newtype (with one public field),
//! a `VacantEntry` newtype (again with one public field). These are thin
//! wrappers around their generic counterparts. Their only serve the purpose
//! of making error messages easier to parse, and use a default `Key`.
//! You will also find a vareity of type aliases for various iterators, and
//! for the default `Key` type for ease of use.
//!
//! If you want to access the raw backing `Arena`/`VacantEntry`, you still can,
//! it is the only public field of each slotmap/vacant entry.

macro_rules! imp_slot_map {
    (
        new $($const:ident)?: $new:expr,
        slots: $slots:ident,
        ($($value:ty)?)
    ) => {
        /// a slot map
        #[derive(Debug, Clone)]
        #[repr(transparent)]
        pub struct SlotMap<T>(pub Arena<T, ()>);

        /// a vacant entry into [`SlotMap`]
        pub struct VacantEntry<'a, T>(pub imp::VacantEntry<'a, T, ()>);

        /// The key for [`SlotMap`]
        pub type Key = $crate::Key<usize>;

        /// Returned from [`SlotMap::entries`]
        pub type Entries<'a, T> = imp::Entries<'a, T, (), DefaultVersion, usize>;
        /// Returned from [`SlotMap::entries_mut`]
        pub type EntriesMut<'a, T> = imp::EntriesMut<'a, T, (), DefaultVersion, usize>;
        /// Returned from [`SlotMap::into_entries`]
        pub type IntoEntries<T> = imp::IntoEntries<T, (), DefaultVersion, usize>;

        impl<T> VacantEntry<'_, T> {
            /// see [`VacantEntry::key`](imp::VacantEntry::key)
            pub fn key(&self) -> usize { self.0.key() }

            /// see [`VacantEntry::insert`](imp::VacantEntry::insert)
            pub fn insert(self, value: T) -> usize { self.0.insert(value) }
        }

        impl<T> Default for SlotMap<T> {
            fn default() -> Self { Self::new() }
        }

        impl<T> SlotMap<T> {
            /// Create a new slab
            pub $($const)? fn new() -> Self { Self($new) }
            /// see [`Arena::is_empty`](imp::Arena::is_empty)
            pub fn is_empty(&self) -> bool { self.0.is_empty() }
            /// see [`Arena::len`](imp::Arena::is_empty)
            pub fn len(&self) -> usize { self.0.len() }
            /// see [`Arena::capacity`](imp::Arena::capacity)
            pub fn capacity(&self) -> usize { self.0.capacity() }
            /// see [`Arena::reserve`](imp::Arena::reserve)
            pub fn reserve(&mut self, additional: usize) { self.0.reserve(additional) }
            /// see [`Arena::clear`](imp::Arena::reserve)
            pub fn clear(&mut self) { self.0.clear(); }
            /// see [`Arena::vacant_entry`](imp::Arena::vacant_entry)
            pub fn vacant_entry(&mut self) -> VacantEntry<'_, T> { VacantEntry(self.0.vacant_entry()) }
            /// see [`Arena::insert`](imp::Arena::insert)
            pub fn insert(&mut self, value: T) -> Key { self.0.insert(value) }
            /// see [`Arena::contains`](imp::Arena::contains)
            pub fn contains(&self, key: Key) -> bool { self.0.contains(key) }
            /// see [`Arena::remove`](imp::Arena::remove)
            pub fn remove(&mut self, key: Key) -> T { self.0.remove(key) }
            /// see [`Arena::try_remove`](imp::Arena::try_remove)
            pub fn try_remove(&mut self, key: Key) -> Option<T> { self.0.try_remove(key) }
            /// see [`Arena::delete`](imp::Arena::delete)
            pub fn delete(&mut self, key: Key) -> bool { self.0.delete(key) }
            /// see [`Arena::get`](imp::Arena::get)
            pub fn get(&self, key: Key) -> Option<&T> { self.0.get(key) }
            /// see [`Arena::get_mut`](imp::Arena::get_mut)
            pub fn get_mut(&mut self, key: Key) -> Option<&mut T> { self.0.get_mut(key) }
            /// see [`Arena::get_unchecked`](imp::Arena::get_unchecked)
            #[allow(clippy::missing_safety_doc)]
            pub unsafe fn get_unchecked(&self, index: usize) -> &T { self.0.get_unchecked(index) }
            /// see [`Arena::get_unchecked_mut`](imp::Arena::get_unchecked_mut)
            #[allow(clippy::missing_safety_doc)]
            pub unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T { self.0.get_unchecked_mut(index) }
            /// see [`Arena::delete_all`](imp::Arena::delete_all)
            pub fn delete_all(&mut self) { self.0.delete_all() }
            /// see [`Arena::retain`](imp::Arena::retain)
            pub fn retain<F: FnMut(&mut T) -> bool>(&mut self, f: F) { self.0.retain(f) }
            /// see [`Arena::keys`](imp::Arena::keys)
            pub fn keys(&self) -> Keys<'_ $(, $value)?> { self.0.keys() }
            /// see [`Arena::iter`](imp::Arena::iter)
            pub fn iter(&self) -> Iter<'_, T> { self.0.iter() }
            /// see [`Arena::iter_mut`](imp::Arena::iter_mut)
            pub fn iter_mut(&mut self) -> IterMut<'_, T> { self.0.iter_mut() }
            /// see [`Arena::drain`](imp::Arena::drain)
            pub fn drain(&mut self) -> Drain<'_, T> { self.0.drain() }
            /// see [`Arena::drain_filter`](imp::Arena::drain_filter)
            pub fn drain_filter<F: FnMut(&mut T) -> bool>(&mut self, filter: F) -> DrainFilter<'_, T, F> { self.0.drain_filter(filter) }
            /// see [`Arena::entries`](imp::Arena::entries)
            pub fn entries(&self) -> Entries<'_, T> { self.0.entries() }
            /// see [`Arena::entries_mut`](imp::Arena::entries_mut)
            pub fn entries_mut(&mut self) -> EntriesMut<'_, T> { self.0.entries_mut() }
            /// see [`Arena::into_entries`](imp::Arena::into_entries)
            pub fn into_entries(self) -> IntoEntries<T> { self.0.into_entries() }
        }

        impl<T> IntoIterator for SlotMap<T> {
            type IntoIter = IntoIter<T>;
            type Item = T;

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

        impl<T> Index<Key> for SlotMap<T> {
            type Output = T;

            fn index(&self, key: Key) -> &Self::Output { &self.0[key] }
        }

        impl<T> IndexMut<Key> for SlotMap<T> {
            fn index_mut(&mut self, key: Key) -> &mut Self::Output { &mut self.0[key] }
        }
    };
}

/// a dense slot_map
///
/// see [`base::dense`](crate::base::dense) for details
pub mod dense {
    use core::ops::{Index, IndexMut};

    use crate::{
        base::dense::{self as imp, Arena},
        version::DefaultVersion,
    };

    /// Returned from [`SlotMap::iter`]
    pub type Iter<'a, T> = core::slice::Iter<'a, T>;
    /// Returned from [`SlotMap::iter_mut`]
    pub type IterMut<'a, T> = core::slice::IterMut<'a, T>;
    /// Returned from [`SlotMap::into_iter`]
    pub type IntoIter<T> = std::vec::IntoIter<T>;

    /// Returned from [`SlotMap::drain`]
    pub type Drain<'a, T> = imp::Drain<'a, T, (), DefaultVersion>;
    /// Returned from [`SlotMap::drain_filter`]
    pub type DrainFilter<'a, T, F> = imp::DrainFilter<'a, T, (), DefaultVersion, F>;

    /// Returned from [`SlotMap::keys`]
    pub type Keys<'a> = imp::Keys<'a, (), DefaultVersion, Key>;

    imp_slot_map! {
        new: Arena::with_ident(()),
        slots: len,
        ()
    }
}

/// a hop slot_map
///
/// see [`base::hop`](crate::base::hop) for details
pub mod hop {
    use core::ops::{Index, IndexMut};

    use crate::{
        base::hop::{self as imp, Arena},
        version::DefaultVersion,
    };

    /// Returned from [`SlotMap::iter`]
    pub type Iter<'a, T> = imp::Iter<'a, T, DefaultVersion>;
    /// Returned from [`SlotMap::iter_mut`]
    pub type IterMut<'a, T> = imp::IterMut<'a, T, DefaultVersion>;
    /// Returned from [`SlotMap::into_iter`]
    pub type IntoIter<T> = imp::IntoIter<T, DefaultVersion>;

    /// Returned from [`SlotMap::drain`]
    pub type Drain<'a, T> = imp::Drain<'a, T, DefaultVersion>;
    /// Returned from [`SlotMap::drain_filter`]
    pub type DrainFilter<'a, T, F> = imp::DrainFilter<'a, T, DefaultVersion, F>;

    /// Returned from [`SlotMap::keys`]
    pub type Keys<'a, T> = imp::Keys<'a, T, (), DefaultVersion, Key>;

    imp_slot_map! {
        new: Arena::with_ident(()),
        slots: len,
        (T)
    }
}

/// a sparse slot_map
///
/// see [`base::sparse`](crate::base::sparse) for details
pub mod sparse {
    use core::ops::{Index, IndexMut};

    use crate::{
        base::sparse::{self as imp, Arena},
        version::DefaultVersion,
    };

    /// Returned from [`SlotMap::iter`]
    pub type Iter<'a, T> = imp::Iter<'a, T, DefaultVersion>;
    /// Returned from [`SlotMap::iter_mut`]
    pub type IterMut<'a, T> = imp::IterMut<'a, T, DefaultVersion>;
    /// Returned from [`SlotMap::into_iter`]
    pub type IntoIter<T> = imp::IntoIter<T, DefaultVersion>;

    /// Returned from [`SlotMap::drain`]
    pub type Drain<'a, T> = imp::Drain<'a, T, DefaultVersion>;
    /// Returned from [`SlotMap::drain_filter`]
    pub type DrainFilter<'a, T, F> = imp::DrainFilter<'a, T, DefaultVersion, F>;

    /// Returned from [`SlotMap::keys`]
    pub type Keys<'a, T> = imp::Keys<'a, T, (), DefaultVersion, Key>;

    imp_slot_map! {
        new const: Arena::INIT,
        slots: slots,
        (T)
    }
}