pub struct Arena<T> { /* private fields */ }Expand description
An Arena structure containing certain Nodes.
Implementations§
Source§impl<T> Arena<T>
impl<T> Arena<T>
Sourcepub fn new() -> Arena<T>
pub fn new() -> Arena<T>
Creates a new empty Arena.
Examples found in repository?
3pub fn main() {
4 // Create a new arena
5 let arena = &mut Arena::new();
6
7 // Add some new nodes to the arena
8 let a = arena.new_node(1);
9 let b = arena.new_node(2);
10
11 // Append a to b
12 a.append(b, arena);
13 assert_eq!(b.ancestors(arena).into_iter().count(), 2);
14}Sourcepub fn with_capacity(n: usize) -> Arena<T>
pub fn with_capacity(n: usize) -> Arena<T>
Create a new empty Arena with pre-allocated memory for n items.
Sourcepub fn new_node(&mut self, data: T) -> NodeId
pub fn new_node(&mut self, data: T) -> NodeId
Creates a new node from its associated data.
§Panics
Panics if the arena already has usize::max_value() nodes.
§Examples
let mut arena = Arena::new();
let foo = arena.new_node("foo");
assert_eq!(*arena[foo].get(), "foo");Examples found in repository?
3pub fn main() {
4 // Create a new arena
5 let arena = &mut Arena::new();
6
7 // Add some new nodes to the arena
8 let a = arena.new_node(1);
9 let b = arena.new_node(2);
10
11 // Append a to b
12 a.append(b, arena);
13 assert_eq!(b.ancestors(arena).into_iter().count(), 2);
14}Sourcepub fn new_node_with(&mut self, create: impl FnOnce(NodeId) -> T) -> NodeId
pub fn new_node_with(&mut self, create: impl FnOnce(NodeId) -> T) -> NodeId
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);Sourcepub fn count(&self) -> usize
pub fn count(&self) -> usize
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);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
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());Sourcepub fn get(&self, id: NodeId) -> Option<&Node<T>>
pub fn get(&self, id: NodeId) -> Option<&Node<T>>
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());Sourcepub fn get_mut(&mut self, id: NodeId) -> Option<&mut Node<T>>
pub fn get_mut(&mut self, id: NodeId) -> Option<&mut Node<T>>
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!"));Sourcepub fn get2_mut(
&mut self,
i1: NodeId,
i2: NodeId,
) -> (Option<&mut Node<T>>, Option<&mut Node<T>>)
pub fn get2_mut( &mut self, i1: NodeId, i2: NodeId, ) -> (Option<&mut Node<T>>, Option<&mut Node<T>>)
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");Sourcepub fn iter(&self) -> impl Iterator<Item = &Node<T>>
pub fn iter(&self) -> impl Iterator<Item = &Node<T>>
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);Sourcepub fn iter_pairs(&self) -> impl Iterator<Item = (NodeId, &Node<T>)>
pub fn iter_pairs(&self) -> impl Iterator<Item = (NodeId, &Node<T>)>
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);