Use shrink_to_fit to reduce DefMap sizes

This commit is contained in:
Jonas Schievink 2021-04-03 23:45:27 +02:00
parent b78f1a0a4d
commit d1bce6070d
4 changed files with 48 additions and 1 deletions

View file

@ -194,6 +194,29 @@ impl<T> Arena<T> {
self.data.iter().enumerate().map(|(idx, value)| (Idx::from_raw(RawIdx(idx as u32)), value))
}
/// Returns an iterator over the arenas mutable elements.
///
/// ```
/// let mut arena = la_arena::Arena::new();
/// let idx1 = arena.alloc(20);
///
/// assert_eq!(arena[idx1], 20);
///
/// let mut iterator = arena.iter_mut();
/// *iterator.next().unwrap().1 = 10;
/// drop(iterator);
///
/// assert_eq!(arena[idx1], 10);
/// ```
pub fn iter_mut(
&mut self,
) -> impl Iterator<Item = (Idx<T>, &mut T)> + ExactSizeIterator + DoubleEndedIterator {
self.data
.iter_mut()
.enumerate()
.map(|(idx, value)| (Idx::from_raw(RawIdx(idx as u32)), value))
}
/// Reallocates the arena to make it take up as little space as possible.
pub fn shrink_to_fit(&mut self) {
self.data.shrink_to_fit();