Struct indextree::Node

source ·
pub struct Node<T> { /* private fields */ }
Expand description

A node within a particular Arena.

Implementations§

source§

impl<T> Node<T>

source

pub fn get(&self) -> &T

Returns a reference to the node data.

source

pub fn get_mut(&mut self) -> &mut T

Returns a mutable reference to the node data.

source

pub fn parent(&self) -> Option<NodeId>

Returns the ID of the parent node, unless this node is the root of the tree.

§Examples
// arena
// `-- 1
//     |-- 1_1
//     |-- 1_2
//     `-- 1_3
assert_eq!(arena[n1].parent(), None);
assert_eq!(arena[n1_1].parent(), Some(n1));
assert_eq!(arena[n1_2].parent(), Some(n1));
assert_eq!(arena[n1_3].parent(), Some(n1));
source

pub fn first_child(&self) -> Option<NodeId>

Returns the ID of the first child of this node, unless it has no child.

§Examples
// arena
// `-- 1
//     |-- 1_1
//     |-- 1_2
//     `-- 1_3
assert_eq!(arena[n1].first_child(), Some(n1_1));
assert_eq!(arena[n1_1].first_child(), None);
assert_eq!(arena[n1_2].first_child(), None);
assert_eq!(arena[n1_3].first_child(), None);
source

pub fn last_child(&self) -> Option<NodeId>

Returns the ID of the last child of this node, unless it has no child.

§Examples
// arena
// `-- 1
//     |-- 1_1
//     |-- 1_2
//     `-- 1_3
assert_eq!(arena[n1].last_child(), Some(n1_3));
assert_eq!(arena[n1_1].last_child(), None);
assert_eq!(arena[n1_2].last_child(), None);
assert_eq!(arena[n1_3].last_child(), None);
source

pub fn previous_sibling(&self) -> Option<NodeId>

Returns the ID of the previous sibling of this node, unless it is a first child.

§Examples
// arena
// `-- 1
//     |-- 1_1
//     |-- 1_2
//     `-- 1_3
assert_eq!(arena[n1].previous_sibling(), None);
assert_eq!(arena[n1_1].previous_sibling(), None);
assert_eq!(arena[n1_2].previous_sibling(), Some(n1_1));
assert_eq!(arena[n1_3].previous_sibling(), Some(n1_2));

Note that newly created nodes are independent toplevel nodes, and they are not siblings by default.

let mut arena = Arena::new();
let n1 = arena.new_node("1");
let n2 = arena.new_node("2");
// arena
// |-- (implicit)
// |   `-- 1
// `-- (implicit)
//     `-- 2
assert_eq!(arena[n1].previous_sibling(), None);
assert_eq!(arena[n2].previous_sibling(), None);

n1.insert_after(n2, &mut arena);
// arena
// `-- (implicit)
//     |-- 1
//     `-- 2
assert_eq!(arena[n1].previous_sibling(), None);
assert_eq!(arena[n2].previous_sibling(), Some(n1));
source

pub fn next_sibling(&self) -> Option<NodeId>

Returns the ID of the next sibling of this node, unless it is a last child.

§Examples
// arena
// `-- 1
//     |-- 1_1
//     |-- 1_2
//     `-- 1_3
assert_eq!(arena[n1].next_sibling(), None);
assert_eq!(arena[n1_1].next_sibling(), Some(n1_2));
assert_eq!(arena[n1_2].next_sibling(), Some(n1_3));
assert_eq!(arena[n1_3].next_sibling(), None);

Note that newly created nodes are independent toplevel nodes, and they are not siblings by default.

let mut arena = Arena::new();
let n1 = arena.new_node("1");
let n2 = arena.new_node("2");
// arena
// |-- (implicit)
// |   `-- 1
// `-- (implicit)
//     `-- 2
assert_eq!(arena[n1].next_sibling(), None);
assert_eq!(arena[n2].next_sibling(), None);

n1.insert_after(n2, &mut arena);
// arena
// `-- (implicit)
//     |-- 1
//     `-- 2
assert_eq!(arena[n1].next_sibling(), Some(n2));
assert_eq!(arena[n2].next_sibling(), None);
source

pub fn is_removed(&self) -> bool

Checks if the node is marked as removed.

§Examples
// arena
// `-- 1
//     |-- 1_1
//     |-- 1_2 *
//     `-- 1_3
assert_eq!(arena[n1_1].next_sibling(), Some(n1_2));
assert_eq!(arena[n1_2].parent(), Some(n1));
assert!(!arena[n1_2].is_removed());
assert_eq!(arena[n1_3].previous_sibling(), Some(n1_2));

n1_2.remove(&mut arena);
// arena
// `-- 1
//     |-- 1_1
//     `-- 1_3
assert_eq!(arena[n1_1].next_sibling(), Some(n1_3));
assert_eq!(arena[n1_2].parent(), None);
assert!(arena[n1_2].is_removed());
assert_eq!(arena[n1_3].previous_sibling(), Some(n1_1));

Trait Implementations§

source§

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

source§

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

Returns a copy 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
source§

impl<T> Display for Node<T>

source§

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

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

impl<T: PartialEq> PartialEq for Node<T>

source§

fn eq(&self, other: &Node<T>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T: Eq> Eq for Node<T>

source§

impl<T> StructuralPartialEq for Node<T>

Auto Trait Implementations§

§

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> 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,

§

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> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

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

§

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>,

§

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.