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>
impl<B> Path<B>
Sourcepub fn l(&mut self, branch: impl Into<B>) -> &mut Self
pub fn l(&mut self, branch: impl Into<B>) -> &mut Self
Adds branch
at the end of the Path
.
Calls can be chained.
Sourcepub fn push_first(&mut self, branch: B)
pub fn push_first(&mut self, branch: B)
Adds branch
at the start of the Path
.
Sourcepub fn last(&self) -> Option<&B>
pub fn last(&self) -> Option<&B>
Returns the last branch of the Path
.
If the Path
is empty, None is returned.
Sourcepub fn first(&self) -> Option<&B>
pub fn first(&self) -> Option<&B>
Returns the first branch of the Path
.
If the Path
is empty, None is returned.
Sourcepub fn truncate_end(&mut self, n: usize)
pub fn truncate_end(&mut self, n: usize)
Removes n
branches from the end of the Path.
Sourcepub fn truncate_start(&mut self, n: usize)
pub fn truncate_start(&mut self, n: usize)
Removes n
branches from the start of the Path.
Sourcepub fn iter(&self) -> Iter<'_, B>
pub fn iter(&self) -> Iter<'_, B>
Traverses the Tree nodes depth first.
The children are visited in arbitrary order.
pub fn range<R>(&self, range: R) -> Iter<'_, B>where
R: RangeBounds<usize>,
Source§impl<B> Path<B>where
B: Clone,
impl<B> Path<B>where
B: Clone,
Sourcepub fn path_to(&self, path_idx: PathIDX) -> Path<B>
pub fn path_to(&self, path_idx: PathIDX) -> Path<B>
Creates a copy stopping at the given index (excluded).
Sourcepub fn path_from(&self, path_idx: PathIDX) -> Path<B>
pub fn path_from(&self, path_idx: PathIDX) -> Path<B>
Creates a copy starting at the given index (included).
Sourcepub fn apply<N>(&mut self, node: &Traversal<N, B>) -> bool
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());
Sourcepub fn apply_deref<N, C>(&mut self, node: &Traversal<N, C>) -> boolwhere
C: Deref<Target = B>,
pub fn apply_deref<N, C>(&mut self, node: &Traversal<N, C>) -> boolwhere
C: Deref<Target = B>,
Dereferences the branch in node
and applies it like apply()
.
Sourcepub fn apply_with<N, C>(
&mut self,
node: &Traversal<N, C>,
op: impl Fn(&C) -> B,
) -> bool
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)]);
Trait Implementations§
Source§impl<B> FromIterator<B> for Path<B>
impl<B> FromIterator<B> for Path<B>
Source§fn from_iter<T: IntoIterator<Item = B>>(iter: T) -> Self
fn from_iter<T: IntoIterator<Item = B>>(iter: T) -> Self
Source§impl<'a, B> IntoIterator for &'a Path<B>
impl<'a, B> IntoIterator for &'a Path<B>
Source§impl<'a, B> IntoIterator for &'a mut Path<B>
impl<'a, B> IntoIterator for &'a mut Path<B>
Source§impl<B> IntoIterator for Path<B>
impl<B> IntoIterator for Path<B>
Source§impl<B: Ord> Ord for Path<B>
impl<B: Ord> Ord for Path<B>
Source§impl<B: PartialOrd> PartialOrd for Path<B>
impl<B: PartialOrd> PartialOrd for Path<B>
impl<B: Eq> Eq for Path<B>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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