Struct Navigator

Source
pub struct Navigator { /* private fields */ }
Expand description

The navigator stores the structure of the tree. It knows which nodes are neighbors, parents and children of a node and allows navigating to them. With “navigation” we mean that the index in the flat Vector storing all the nodes is returned.

§Example:

use flange_flat_tree::navigator::Builder;

// A navigator just stores neigbors, so we provide no values
let mut b = Builder::default();
let root = b.start_element();
let child1 = b.start_end_element();
let child2 = b.start_end_element();

let nav = b.build();

assert_eq!(nav.children(root), [child1,child2]);
assert_eq!(nav.next_sibling(child1), Some(child2));
assert_eq!(nav.prev_sibling(child2), Some(child1));

Implementations§

Source§

impl Navigator

Source

pub fn for_each_depth_first<F>(&self, f: F)
where F: FnMut(usize, Vec<usize>),

Iterates through all nodes in a depth-first order. That means the children of a node are garuanteed to be visited before the node itself.

§Example
use flange_flat_tree::navigator::Builder;

// A navigator just stores neigbors, so we provide no values
let mut b = Builder::default();
let root = b.start_element();
let child1 = b.start_end_element();
let child2 = b.start_end_element();
// ...

let nav = b.build();
let mut visited = vec![false;3];

 nav.for_each_depth_first(|i, childs| {
     visited[i] = true;
     for c in childs {
         assert!(visited[c]);
     }
 });
Source

pub fn get_neighbors(&self, index: usize) -> &Neighbors<usize>

Returns the neighbors structure for a node.

§Arguments
  • index - The index to find the neighbors of.
§Result

The neighbors of the nodes (in a Neighbors structure).

Source

pub fn parent(&self, index: usize) -> Option<usize>

Return the index of the parent (if any).

§Arguments
  • index - The index to find the parent of.
§Result

The index of the parent or none if the node has no parent.

Source

pub fn first_child(&self, index: usize) -> Option<usize>

Return the index of the first child (if any).

§Arguments
  • index - The index to find the first child of.
§Result

The index of the first child or none if the node has no first child.

Source

pub fn children(&self, index: usize) -> Vec<usize>

Return the indices of the all children.

§Arguments
  • index - The index to find the children of.
§Result

The indices of the childre in a Vec.

Source

pub fn prev_sibling(&self, index: usize) -> Option<usize>

Return the index of the the next sibling (if any).

The next sibling is the next node that has the same parent as the given node.

§Arguments
  • index - The index to find the next sibling of.
§Result

The index of the next sibling or none if the node has no next sibling.

Source

pub fn next_sibling(&self, index: usize) -> Option<usize>

Return the index of the the prev sibling (if any).

The prev sibling is the prev node that has the same parent as the given node.

§Arguments
  • index - The index to find the prev sibling of.
§Result

The index of the prev sibling or none if the node has no prev sibling.

Trait Implementations§

Source§

impl Debug for Navigator

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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