Struct generational_indextree::Arena
source · [−]pub struct Arena<T> { /* private fields */ }
Expand description
An Arena
structure containing certain Node
s.
Implementations
Create a new empty Arena
with pre-allocated memory for n
items.
Creates a new node via specified create
function.
create
is called with the new node’s node ID, allowing nodes that know their own ID.
Panics
Panics if the arena already has usize::max_value()
nodes.
Examples
let mut arena = Arena::new();
struct A { id: NodeId, val: u32 }
let foo = arena.new_node_with(|id| A { id, val: 10 });
assert_eq!(arena[foo].get().val, 10);
assert_eq!(arena[foo].get().id, foo);
Counts the number of nodes in arena and returns it.
Examples
let mut arena = Arena::new();
let foo = arena.new_node("foo");
let _bar = arena.new_node("bar");
assert_eq!(arena.count(), 2);
foo.remove(&mut arena);
assert_eq!(arena.count(), 1);
Returns true
if arena has no nodes, false
otherwise.
Examples
let mut arena = Arena::new();
assert!(arena.is_empty());
let foo = arena.new_node("foo");
assert!(!arena.is_empty());
foo.remove(&mut arena);
assert!(arena.is_empty());
Returns a reference to the node with the given id if in the arena.
Returns None
if not available.
Examples
let mut arena = Arena::new();
let foo = arena.new_node("foo");
assert_eq!(arena.get(foo).map(|node| *node.get()), Some("foo"));
Note that this does not check whether the given node ID is created by the arena.
let mut arena = Arena::new();
let foo = arena.new_node("foo");
let bar = arena.new_node("bar");
assert_eq!(arena.get(foo).map(|node| *node.get()), Some("foo"));
let mut another_arena = Arena::new();
let _ = another_arena.new_node("Another arena");
assert_eq!(another_arena.get(foo).map(|node| *node.get()), Some("Another arena"));
assert!(another_arena.get(bar).is_none());
Returns a mutable reference to the node with the given id if in the arena.
Returns None
if not available.
Examples
let mut arena = Arena::new();
let foo = arena.new_node("foo");
assert_eq!(arena.get(foo).map(|node| *node.get()), Some("foo"));
*arena.get_mut(foo).expect("The `foo` node exists").get_mut() = "FOO!";
assert_eq!(arena.get(foo).map(|node| *node.get()), Some("FOO!"));
Get a pair of exclusive references to the elements at index i1
and i2
if it is in the
arena.
If the element at index i1
or i2
is not in the arena, then None
is returned for this
element.
Panics
Panics if i1
and i2
are pointing to the same item of the arena.
Examples
use generational_indextree::Arena;
let mut arena = Arena::new();
let idx1 = arena.new_node("foo");
let idx2 = arena.new_node("bar");
{
let (item1, item2) = arena.get2_mut(idx1, idx2);
*item1.unwrap().get_mut() = "jig";
*item2.unwrap().get_mut() = "saw";
}
assert_eq!(arena[idx1].get(), &"jig");
assert_eq!(arena[idx2].get(), &"saw");
Returns an iterator of all nodes in the arena in storage-order.
Examples
let mut arena = Arena::new();
let _foo = arena.new_node("foo");
let _bar = arena.new_node("bar");
let mut iter = arena.iter();
assert_eq!(iter.next().map(|node| *node.get()), Some("foo"));
assert_eq!(iter.next().map(|node| *node.get()), Some("bar"));
assert_eq!(iter.next().map(|node| *node.get()), None);
let mut arena = Arena::new();
let _foo = arena.new_node("foo");
let bar = arena.new_node("bar");
bar.remove(&mut arena);
let mut iter = arena.iter();
assert_eq!(iter.next().map(|node| *node.get()), Some("foo"));
assert_eq!(iter.next().map(|node| *node.get()), None);
Returns an iterator of all pairs (NodeId, &Node
let mut arena = Arena::new();
let _foo = arena.new_node("foo");
let _bar = arena.new_node("bar");
let mut iter = arena.iter_pairs();
assert_eq!(iter.next().map(|node| (node.0, *node.1.get())), Some((_foo, "foo")));
assert_eq!(iter.next().map(|node| (node.0, *node.1.get())), Some((_bar, "bar")));
assert_eq!(iter.next().map(|node| (node.0, *node.1.get())), None);
Trait Implementations
Auto Trait Implementations
impl<T> RefUnwindSafe for Arena<T> where
T: RefUnwindSafe,
impl<T> UnwindSafe for Arena<T> where
T: UnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more