pub struct Arena<T> { /* private fields */ }Expand description
A generational arena allocator.
Provides O(1) allocation, deallocation, and access with generation-based use-after-free detection. All operations are safe Rust.
§Type Parameter
T is the type of values stored in the arena. For the DOM, this will be
NodeData.
§Examples
use tinyxml2::arena::Arena;
let mut arena: Arena<String> = Arena::new();
// Allocate
let id = arena.alloc("hello".to_string());
assert_eq!(arena.get(id), Some(&"hello".to_string()));
// Deallocate
let removed = arena.dealloc(id);
assert_eq!(removed, Some("hello".to_string()));
// Stale ID returns None
assert_eq!(arena.get(id), None);Implementations§
Source§impl<T> Arena<T>
impl<T> Arena<T>
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates a new arena with the specified capacity pre-allocated.
Sourcepub fn alloc(&mut self, value: T) -> NodeId
pub fn alloc(&mut self, value: T) -> NodeId
Allocates a new slot and stores the value, returning its NodeId.
If a previously-deallocated slot is available, it will be reused. Otherwise, a new slot is appended.
Time complexity: O(1) amortized.
Sourcepub fn dealloc(&mut self, id: NodeId) -> Option<T>
pub fn dealloc(&mut self, id: NodeId) -> Option<T>
Deallocates the slot identified by id, returning the stored value.
Returns None if the ID is invalid (wrong generation, out of bounds,
or already deallocated).
The slot’s generation is incremented so that any remaining copies of
this NodeId become stale.
Time complexity: O(1).
Sourcepub fn get(&self, id: NodeId) -> Option<&T>
pub fn get(&self, id: NodeId) -> Option<&T>
Returns a reference to the value at id, or None if the ID is invalid.
Time complexity: O(1).
Sourcepub fn get_mut(&mut self, id: NodeId) -> Option<&mut T>
pub fn get_mut(&mut self, id: NodeId) -> Option<&mut T>
Returns a mutable reference to the value at id, or None if invalid.
Time complexity: O(1).
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Removes all items from the arena and resets all state.
All existing NodeId values become invalid after this call.
Sourcepub fn iter(&self) -> ArenaIter<'_, T> ⓘ
pub fn iter(&self) -> ArenaIter<'_, T> ⓘ
Returns an iterator over all occupied (NodeId, &T) pairs.
Sourcepub fn iter_mut(&mut self) -> ArenaIterMut<'_, T> ⓘ
pub fn iter_mut(&mut self) -> ArenaIterMut<'_, T> ⓘ
Returns a mutable iterator over all occupied (NodeId, &mut T) pairs.