Struct Path

Source
pub struct Path<B> { /* private fields */ }
Expand description

A path to a Tree node.

A Path is a collection of branches to follow to get to the desired node. An empty Path represents the root of the Tree.

§Examples

use nb_tree::{path, prelude::{Tree, Path}};
let mut tree = Tree::new();
// Paths from string litterals, vectors, or manually built
let path_d: Path<String> = "/b/d".into();
let path_e: Path<String> = vec!["a", "c", "e"].into();
let path_g: Path<String> = path!["a", "g"];
let mut path_f = Path::new();
path_f.l("b").l("f");
// Use paths to insert data
tree.i("/", 0)
    .i("/a", 1)
    .i("/b", 2)
    .i("/a/c", 3)
    .i(path_d, 4)
    .i(path_e, 5)
    .i(path_f, 6)
    .i(path_g, 7);

// Use paths to retrieve data
assert_eq!(tree.get(&"/a/c".into()), Ok(&3));

// Use paths to remove data
assert_eq!(tree.len(), 8);
assert_eq!(tree.remove_subtree(&"/a".into()), Ok(()));
// The whole "/a/[c/e, g]" subtree is removed, removing all nodes below it too
assert_eq!(tree.len(), 4);

Implementations§

Source§

impl<B> Path<B>

Source

pub fn new() -> Self

Creates a new empty Path.

Source

pub fn with(root: B) -> Self

Creates a new empty Path.

Source

pub fn pop_first(&mut self) -> Option<B>

Removes the first branch of the Path.

Source

pub fn pop_last(&mut self) -> Option<B>

Removes the last branch of the Path.

Source

pub fn push_last(&mut self, branch: B)

Adds branch at the end of the Path.

Source

pub fn append(&mut self, branches: Path<B>)

Appends all branches at the end of the Path.

Source

pub fn l(&mut self, branch: impl Into<B>) -> &mut Self

Adds branch at the end of the Path.

Calls can be chained.

Source

pub fn push_first(&mut self, branch: B)

Adds branch at the start of the Path.

Source

pub fn last(&self) -> Option<&B>

Returns the last branch of the Path.

If the Path is empty, None is returned.

Source

pub fn first(&self) -> Option<&B>

Returns the first branch of the Path.

If the Path is empty, None is returned.

Source

pub fn is_empty(&self) -> bool

Returns true if the Path is empty, false otherwise.

Source

pub fn len(&self) -> usize

Returns the length of the Path.

Source

pub fn truncate_end(&mut self, n: usize)

Removes n branches from the end of the Path.

Source

pub fn truncate_start(&mut self, n: usize)

Removes n branches from the start of the Path.

Source

pub fn clear(&mut self)

Clears the Path.

Source

pub fn iter(&self) -> Iter<'_, B>

Traverses the Tree nodes depth first.

The children are visited in arbitrary order.

Source

pub fn range<R>(&self, range: R) -> Iter<'_, B>
where R: RangeBounds<usize>,

Source§

impl<B> Path<B>
where B: Clone,

Source

pub fn path_to(&self, path_idx: PathIDX) -> Path<B>

Creates a copy stopping at the given index (excluded).

Source

pub fn path_from(&self, path_idx: PathIDX) -> Path<B>

Creates a copy starting at the given index (included).

Source

pub fn apply<N>(&mut self, node: &Traversal<N, B>) -> bool

Follows the given [DepthFirstTraversalNode], moving up the Path and going down a branch.

The branch type can be something else than B, as long as it can be converted to it.

§Examples
use nb_tree::prelude::{Tree, Path};
let mut tree: Tree<usize, String> = Tree::new();
tree.i("/", 0).i("/a", 1).i("/a/b", 2);
let mut iter = tree.into_iter();
let mut path: Path<String> = Path::new();
path.apply(&iter.next().unwrap());
assert_eq!(path, "/".into());
path.apply(&iter.next().unwrap());
assert_eq!(path, "/a".into());
path.apply(&iter.next().unwrap());
assert_eq!(path, "/a/b".into());
Source

pub fn apply_deref<N, C>(&mut self, node: &Traversal<N, C>) -> bool
where C: Deref<Target = B>,

Dereferences the branch in node and applies it like apply().

Source

pub fn apply_with<N, C>( &mut self, node: &Traversal<N, C>, op: impl Fn(&C) -> B, ) -> bool

Follows the given [DepthFirstTraversalNode] like apply() transformed by the given op.

§Examples
use nb_tree::{path, prelude::{Tree, Path}};
let mut tree: Tree<usize, String> = Tree::new();
tree.i("/", 0).i("/a", 1).i("/a/b", 2);
let mut iter = tree.iter();
let mut path = Path::new();
let mut e = 0;
path.apply_with(&iter.next().unwrap(), |&c: &&String| (c.clone(), e.clone()));
e += 1;
path.apply_with(&iter.next().unwrap(), |&c: &&String| (c.clone(), e.clone()));
assert_eq!(path, path![("a".into(), 1)]);
e += 1;
path.apply_with(&iter.next().unwrap(), |&c: &&String| (c.clone(), e.clone()));
assert_eq!(path, path![("a".into(), 1), ("b".into(), 2)]);
Source§

impl<B> Path<B>
where B: Clone + Eq,

Source

pub fn offshoot_from(&self, branch: Self) -> Path<B>

Source§

impl<B> Path<&B>
where B: Clone,

Source

pub fn branches_to_owned(&self) -> Path<B>

Trait Implementations§

Source§

impl<B: Clone> Clone for Path<B>

Source§

fn clone(&self) -> Self

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

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

Performs copy-assignment from source. Read more
Source§

impl<B: Debug> Debug for Path<B>

Source§

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

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

impl<B> Default for Path<B>

Source§

fn default() -> Self

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

impl<B: Display> Display for Path<B>

Source§

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

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

impl<B> From<&str> for Path<B>
where B: FromStr, <B as FromStr>::Err: Dbg,

Source§

fn from(value: &str) -> Self

Converts to this type from the input type.
Source§

impl<A: Into<B>, B> From<Vec<A>> for Path<B>

Source§

fn from(value: Vec<A>) -> Self

Converts to this type from the input type.
Source§

impl<B> FromIterator<B> for Path<B>

Source§

fn from_iter<T: IntoIterator<Item = B>>(iter: T) -> Self

Creates a value from an iterator. Read more
Source§

impl<B: FromStr> FromStr for Path<B>

Source§

type Err = ParsePathError<<B as FromStr>::Err>

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl<B: Hash> Hash for Path<B>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<N, B> Index<&Path<B>> for Tree<N, B>
where B: Eq + Hash + Clone,

Source§

type Output = N

The returned type after indexing.
Source§

fn index(&self, index: &Path<B>) -> &Self::Output

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

impl<B> Index<usize> for Path<B>

Source§

type Output = B

The returned type after indexing.
Source§

fn index(&self, index: PathIDX) -> &Self::Output

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

impl<B> IndexMut<usize> for Path<B>

Source§

fn index_mut(&mut self, index: PathIDX) -> &mut Self::Output

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

impl<'a, B> IntoIterator for &'a Path<B>

Source§

type Item = &'a B

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, B>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, B> IntoIterator for &'a mut Path<B>

Source§

type Item = &'a mut B

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, B>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<B> IntoIterator for Path<B>

Source§

type Item = B

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<B>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<B: Ord> Ord for Path<B>

Source§

fn cmp(&self, other: &Path<B>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<B: PartialEq> PartialEq for Path<B>

Source§

fn eq(&self, other: &Path<B>) -> bool

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

const 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<B: PartialOrd> PartialOrd for Path<B>

Source§

fn partial_cmp(&self, other: &Path<B>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<B: Eq> Eq for Path<B>

Source§

impl<B> StructuralPartialEq for Path<B>

Auto Trait Implementations§

§

impl<B> Freeze for Path<B>

§

impl<B> RefUnwindSafe for Path<B>
where B: RefUnwindSafe,

§

impl<B> Send for Path<B>
where B: Send,

§

impl<B> Sync for Path<B>
where B: Sync,

§

impl<B> Unpin for Path<B>
where B: Unpin,

§

impl<B> UnwindSafe for Path<B>
where B: 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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

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

impl<N, B> TreeIntoIterTarget<N, B> for N

Source§

type Target = N

Source§

fn target(node: TreeNode<N, B>) -> <N as TreeIntoIterTarget<N, B>>::Target

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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more