Arena

Struct Arena 

Source
pub struct Arena<T> { /* private fields */ }
Expand description

An Arena structure containing certain Nodes.

Implementations§

Source§

impl<T> Arena<T>

Source

pub fn new() -> Arena<T>

Creates a new empty Arena.

Examples found in repository?
examples/simple.rs (line 5)
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}
Source

pub fn with_capacity(n: usize) -> Arena<T>

Create a new empty Arena with pre-allocated memory for n items.

Source

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?
examples/simple.rs (line 8)
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}
Source

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);
Source

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);
Source

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());
Source

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());
Source

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!"));
Source

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");
Source

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);
Source

pub fn iter_pairs(&self) -> impl Iterator<Item = (NodeId, &Node<T>)>

Returns an iterator of all pairs (NodeId, &Node) in the arena in storage-order.

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§

Source§

impl<T: Clone> Clone for Arena<T>

Source§

fn clone(&self) -> Arena<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for Arena<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Default for Arena<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T> Index<NodeId> for Arena<T>

Source§

type Output = Node<T>

The returned type after indexing.
Source§

fn index(&self, node: NodeId) -> &Node<T>

Performs the indexing (container[index]) operation. Read more
Source§

impl<T> IndexMut<NodeId> for Arena<T>

Source§

fn index_mut(&mut self, node: NodeId) -> &mut Node<T>

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<T: PartialEq> PartialEq for Arena<T>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialEq> Eq for Arena<T>

Auto Trait Implementations§

§

impl<T> Freeze for Arena<T>

§

impl<T> RefUnwindSafe for Arena<T>
where T: RefUnwindSafe,

§

impl<T> Send for Arena<T>
where T: Send,

§

impl<T> Sync for Arena<T>
where T: Sync,

§

impl<T> Unpin for Arena<T>
where T: Unpin,

§

impl<T> UnwindSafe for Arena<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.