Struct Node

Source
pub struct Node<T> {
    pub data: T,
    /* private fields */
}
Expand description

A node holds data in the arena. Node<T> can be accessed by indexing Arena<T> with Token, using the get or get_mut methods of Arena<T>, or through tree iterators.

Fields§

§data: T

The data field.

Implementations§

Source§

impl<T> Node<T>

Source

pub fn token(&self) -> Token

Returns the token of the given node.

Source

pub fn is_leaf(&self) -> bool

Checks whether a given node is actually a leaf.

Source

pub fn ancestors_tokens<'a>(&self, arena: &'a Arena<T>) -> AncestorTokens<'a, T>

Returns an iterator of tokens of ancestor nodes.

§Examples:
use atree::Arena;
use atree::Node;

let root_data = "Indo-European";
let (mut arena, root_token) = Arena::with_data(root_data);

let next_node_token = root_token.append(&mut arena, "Germanic");
let third_node_token = next_node_token.append(&mut arena, "English");

let third_node = &arena[third_node_token];
let mut ancestors_tokens = third_node.ancestors_tokens(&arena);

assert_eq!(ancestors_tokens.next(), Some(next_node_token));
assert_eq!(ancestors_tokens.next(), Some(root_token));
assert!(ancestors_tokens.next().is_none());
Source

pub fn preceding_siblings_tokens<'a>( &self, arena: &'a Arena<T>, ) -> PrecedingSiblingTokens<'a, T>

Returns an iterator of tokens of siblings preceding the current node.

§Examples:
use atree::Arena;

let root_data = "Indo-European";
let (mut arena, root_token) = Arena::with_data(root_data);

let first_child_token = root_token.append(&mut arena, "Germanic");
let second_child_token = root_token.append(&mut arena, "Romance");
let third_child_token = root_token.append(&mut arena, "Slavic");
root_token.append(&mut arena, "Hellenic");

let third_child = &arena[third_child_token];
let mut sibling_tokens = third_child.preceding_siblings_tokens(&arena);
assert_eq!(sibling_tokens.next(), Some(second_child_token));
assert_eq!(sibling_tokens.next(), Some(first_child_token));
assert!(sibling_tokens.next().is_none());
Source

pub fn following_siblings_tokens<'a>( &self, arena: &'a Arena<T>, ) -> FollowingSiblingTokens<'a, T>

Returns an iterator of tokens of siblings following the current node.

§Examples:
use atree::Arena;

let root_data = "Indo-European";
let (mut arena, root_token) = Arena::with_data(root_data);

root_token.append(&mut arena, "Romance");
let second_child_token = root_token.append(&mut arena, "Germanic");
let third_child_token = root_token.append(&mut arena, "Slavic");
let fourth_child_token = root_token.append(&mut arena, "Hellenic");

let second_child = &arena[second_child_token];
let mut sibling_tokens = second_child.following_siblings_tokens(&arena);
assert_eq!(sibling_tokens.next(), Some(third_child_token));
assert_eq!(sibling_tokens.next(), Some(fourth_child_token));
assert!(sibling_tokens.next().is_none());
Source

pub fn children_tokens<'a>(&self, arena: &'a Arena<T>) -> ChildrenTokens<'a, T>

Returns an iterator of tokens of child nodes in the order of insertion.

§Examples:
use atree::Arena;

let root_data = "Indo-European";
let (mut arena, root_token) = Arena::with_data(root_data);

let first_child_token = root_token.append(&mut arena, "Romance");
let second_child_token = root_token.append(&mut arena, "Germanic");
let third_child_token = root_token.append(&mut arena, "Slavic");
let fourth_child_token = root_token.append(&mut arena, "Hellenic");

let root = &arena[root_token];
let mut children_tokens = root_token.children_tokens(&arena);
assert_eq!(children_tokens.next(), Some(first_child_token));
assert_eq!(children_tokens.next(), Some(second_child_token));
assert_eq!(children_tokens.next(), Some(third_child_token));
assert_eq!(children_tokens.next(), Some(fourth_child_token));
assert!(children_tokens.next().is_none());
Source

pub fn ancestors<'a>(&self, arena: &'a Arena<T>) -> Ancestors<'a, T>

Returns an iterator of references of ancestor nodes.

§Examples:
use atree::Arena;

let root_data = "Indo-European";
let (mut arena, root_token) = Arena::with_data(root_data);

let next_node_token = root_token.append(&mut arena, "Germanic");
let third_node_token = next_node_token.append(&mut arena, "Swedish");

let third_node = &arena[third_node_token];
let mut ancestors = third_node.ancestors(&arena);

assert_eq!(ancestors.next().unwrap().data, "Germanic");
assert_eq!(ancestors.next().unwrap().data, "Indo-European");
assert!(ancestors.next().is_none());
Source

pub fn following_siblings<'a>( &self, arena: &'a Arena<T>, ) -> FollowingSiblings<'a, T>

Returns an iterator of references of sibling nodes following the current node.

§Examples:
use atree::Arena;

let root_data = "Indo-European";
let (mut arena, root_token) = Arena::with_data(root_data);

root_token.append(&mut arena, "Romance");
let second_child_token = root_token.append(&mut arena, "Germanic");
root_token.append(&mut arena, "Slavic");
root_token.append(&mut arena, "Hellenic");

let second_child = &arena[second_child_token];
let mut siblings = second_child_token.following_siblings(&arena);
assert_eq!(siblings.next().unwrap().data, "Slavic");
assert_eq!(siblings.next().unwrap().data, "Hellenic");
assert!(siblings.next().is_none());
Source

pub fn preceding_siblings<'a>( &self, arena: &'a Arena<T>, ) -> PrecedingSiblings<'a, T>

Returns an iterator of references of sibling nodes preceding the current node.

§Examples:
use atree::Arena;

let root_data = "Indo-European";
let (mut arena, root_token) = Arena::with_data(root_data);

root_token.append(&mut arena, "Romance");
root_token.append(&mut arena, "Germanic");
let third_child_token = root_token.append(&mut arena, "Slavic");
root_token.append(&mut arena, "Celtic");

let third_child = &arena[third_child_token];
let mut siblings = third_child.preceding_siblings(&arena);
assert_eq!(siblings.next().unwrap().data, "Germanic");
assert_eq!(siblings.next().unwrap().data, "Romance");
assert!(siblings.next().is_none());
Source

pub fn children<'a>(&self, arena: &'a Arena<T>) -> Children<'a, T>

Returns an iterator of child node references in the order of insertion.

§Examples:
use atree::Arena;

let root_data = "Indo-European";
let (mut arena, root_token) = Arena::with_data(root_data);

let first_child_token = root_token.append(&mut arena, "Germanic");
let second_child_token = root_token.append(&mut arena, "Romance");
let third_child_token = root_token.append(&mut arena, "Slavic");
let fourth_child_token = root_token.append(&mut arena, "Celtic");

let root = &arena[root_token];
let mut children = root.children(&arena);
assert_eq!(children.next().unwrap().data, "Germanic");
assert_eq!(children.next().unwrap().data, "Romance");
assert_eq!(children.next().unwrap().data, "Slavic");
assert_eq!(children.next().unwrap().data, "Celtic");
assert!(children.next().is_none());
Source

pub fn subtree_tokens<'a>( &self, arena: &'a Arena<T>, order: TraversalOrder, ) -> SubtreeTokens<'a, T>

Returns an iterator of tokens of subtree nodes of the given node.

§Examples:
use atree::Arena;
use atree::iter::TraversalOrder;

let root_data = "Indo-European";
let (mut arena, root_token) = Arena::with_data(root_data);

let first_child = root_token.append(&mut arena, "Romance");
let second_child = root_token.append(&mut arena, "Germanic");
let third_child = root_token.append(&mut arena, "Slavic");
let first_grandchild = second_child.append(&mut arena, "English");
let second_grandchild = second_child.append(&mut arena, "Icelandic");
let fourth_child = root_token.append(&mut arena, "Celtic");

let root = &arena[root_token];
let mut subtree = root.subtree_tokens(&arena, TraversalOrder::Pre);
assert_eq!(subtree.next(), Some(root_token));
assert_eq!(subtree.next(), Some(first_child));
assert_eq!(subtree.next(), Some(second_child));
assert_eq!(subtree.next(), Some(first_grandchild));
assert_eq!(subtree.next(), Some(second_grandchild));
assert_eq!(subtree.next(), Some(third_child));
assert_eq!(subtree.next(), Some(fourth_child));
assert!(subtree.next().is_none());

let second_child_node = &arena[second_child];
let mut subtree = second_child_node.subtree_tokens(&arena, TraversalOrder::Pre);
assert_eq!(subtree.next(), Some(second_child));
assert_eq!(subtree.next(), Some(first_grandchild));
assert_eq!(subtree.next(), Some(second_grandchild));
assert!(subtree.next().is_none());
Source

pub fn subtree<'a>( &self, arena: &'a Arena<T>, order: TraversalOrder, ) -> Subtree<'a, T>

Returns an iterator of references of subtree nodes of the given node.

§Examples:
use atree::Arena;
use atree::iter::TraversalOrder;

let root_data = "Indo-European";
let (mut arena, root_token) = Arena::with_data(root_data);

root_token.append(&mut arena, "Romance");
root_token.append(&mut arena, "Germanic");
let third_child = root_token.append(&mut arena, "Slavic");
root_token.append(&mut arena, "Celtic");
third_child.append(&mut arena, "Polish");
third_child.append(&mut arena, "Slovakian");

let root = &arena[root_token];
let mut subtree = root.subtree(&arena, TraversalOrder::Pre);
assert_eq!(subtree.next().unwrap().data, "Indo-European");
assert_eq!(subtree.next().unwrap().data, "Romance");
assert_eq!(subtree.next().unwrap().data, "Germanic");
assert_eq!(subtree.next().unwrap().data, "Slavic");
assert_eq!(subtree.next().unwrap().data, "Polish");
assert_eq!(subtree.next().unwrap().data, "Slovakian");
assert_eq!(subtree.next().unwrap().data, "Celtic");
assert!(subtree.next().is_none());

Trait Implementations§

Source§

impl<T: Clone> Clone for Node<T>

Source§

fn clone(&self) -> Node<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 Node<T>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Node<T>
where T: Freeze,

§

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

§

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

§

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

§

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

§

impl<T> UnwindSafe for Node<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.