Struct ra_ap_la_arena::Arena
source · pub struct Arena<T> { /* private fields */ }
Expand description
Yet another index-based arena.
Implementations§
source§impl<T> Arena<T>
impl<T> Arena<T>
sourcepub const fn new() -> Arena<T>
pub const fn new() -> Arena<T>
Creates a new empty arena.
let arena: la_arena::Arena<i32> = la_arena::Arena::new();
assert!(arena.is_empty());
sourcepub fn with_capacity(capacity: usize) -> Arena<T>
pub fn with_capacity(capacity: usize) -> Arena<T>
Create a new empty arena with specific capacity.
let arena: la_arena::Arena<i32> = la_arena::Arena::with_capacity(42);
assert!(arena.is_empty());
sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Empties the arena, removing all contained values.
let mut arena = la_arena::Arena::new();
arena.alloc(1);
arena.alloc(2);
arena.alloc(3);
assert_eq!(arena.len(), 3);
arena.clear();
assert!(arena.is_empty());
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the length of the arena.
let mut arena = la_arena::Arena::new();
assert_eq!(arena.len(), 0);
arena.alloc("foo");
assert_eq!(arena.len(), 1);
arena.alloc("bar");
assert_eq!(arena.len(), 2);
arena.alloc("baz");
assert_eq!(arena.len(), 3);
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns whether the arena contains no elements.
let mut arena = la_arena::Arena::new();
assert!(arena.is_empty());
arena.alloc(0.5);
assert!(!arena.is_empty());
sourcepub fn alloc(&mut self, value: T) -> Idx<T>
pub fn alloc(&mut self, value: T) -> Idx<T>
Allocates a new value on the arena, returning the value’s index.
let mut arena = la_arena::Arena::new();
let idx = arena.alloc(50);
assert_eq!(arena[idx], 50);
sourcepub fn iter(
&self
) -> impl Iterator<Item = (Idx<T>, &T)> + ExactSizeIterator + DoubleEndedIterator
pub fn iter(
&self
) -> impl Iterator<Item = (Idx<T>, &T)> + ExactSizeIterator + DoubleEndedIterator
Returns an iterator over the arena’s elements.
let mut arena = la_arena::Arena::new();
let idx1 = arena.alloc(20);
let idx2 = arena.alloc(40);
let idx3 = arena.alloc(60);
let mut iterator = arena.iter();
assert_eq!(iterator.next(), Some((idx1, &20)));
assert_eq!(iterator.next(), Some((idx2, &40)));
assert_eq!(iterator.next(), Some((idx3, &60)));
sourcepub fn iter_mut(
&mut self
) -> impl Iterator<Item = (Idx<T>, &mut T)> + ExactSizeIterator + DoubleEndedIterator
pub fn iter_mut(
&mut self
) -> impl Iterator<Item = (Idx<T>, &mut T)> + ExactSizeIterator + DoubleEndedIterator
Returns an iterator over the arena’s 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);
sourcepub fn values(
&mut self
) -> impl Iterator<Item = &T> + ExactSizeIterator + DoubleEndedIterator
pub fn values(
&mut self
) -> impl Iterator<Item = &T> + ExactSizeIterator + DoubleEndedIterator
Returns an iterator over the arena’s values.
let mut arena = la_arena::Arena::new();
let idx1 = arena.alloc(20);
let idx2 = arena.alloc(40);
let idx3 = arena.alloc(60);
let mut iterator = arena.values();
assert_eq!(iterator.next(), Some(&20));
assert_eq!(iterator.next(), Some(&40));
assert_eq!(iterator.next(), Some(&60));
sourcepub fn values_mut(
&mut self
) -> impl Iterator<Item = &mut T> + ExactSizeIterator + DoubleEndedIterator
pub fn values_mut(
&mut self
) -> impl Iterator<Item = &mut T> + ExactSizeIterator + DoubleEndedIterator
Returns an iterator over the arena’s mutable values.
let mut arena = la_arena::Arena::new();
let idx1 = arena.alloc(20);
assert_eq!(arena[idx1], 20);
let mut iterator = arena.values_mut();
*iterator.next().unwrap() = 10;
drop(iterator);
assert_eq!(arena[idx1], 10);
sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Reallocates the arena to make it take up as little space as possible.
Trait Implementations§
source§impl<T> FromIterator<T> for Arena<T>
impl<T> FromIterator<T> for Arena<T>
source§fn from_iter<I>(iter: I) -> Selfwhere
I: IntoIterator<Item = T>,
fn from_iter<I>(iter: I) -> Selfwhere
I: IntoIterator<Item = T>,
Creates a value from an iterator. Read more