Add more constructors for la-arena

This commit is contained in:
oxalica 2022-08-03 17:35:31 +08:00
parent a02b042ae7
commit c203ac2cf5
2 changed files with 21 additions and 1 deletions

View file

@ -208,6 +208,16 @@ impl<T> Arena<T> {
Arena { data: Vec::new() }
}
/// Create a new empty arena with specific capacity.
///
/// ```
/// let arena: la_arena::Arena<i32> = la_arena::Arena::with_capacity(42);
/// assert!(arena.is_empty());
/// ```
pub fn with_capacity(capacity: usize) -> Arena<T> {
Arena { data: Vec::with_capacity(capacity) }
}
/// Empties the arena, removing all contained values.
///
/// ```

View file

@ -11,6 +11,16 @@ pub struct ArenaMap<IDX, V> {
}
impl<T, V> ArenaMap<Idx<T>, V> {
/// Creates a new empty map.
pub const fn new() -> Self {
Self { v: Vec::new(), _ty: PhantomData }
}
/// Create a new empty map with specific capacity.
pub fn with_capacity(capacity: usize) -> Self {
Self { v: Vec::with_capacity(capacity), _ty: PhantomData }
}
/// Inserts a value associated with a given arena index into the map.
pub fn insert(&mut self, idx: Idx<T>, t: V) {
let idx = Self::to_idx(idx);
@ -70,6 +80,6 @@ impl<T, V> std::ops::IndexMut<Idx<V>> for ArenaMap<Idx<V>, T> {
impl<T, V> Default for ArenaMap<Idx<V>, T> {
fn default() -> Self {
ArenaMap { v: Vec::new(), _ty: PhantomData }
Self::new()
}
}