Skip to main content

PathTrie

Struct PathTrie 

Source
pub struct PathTrie<T> { /* private fields */ }
Expand description

A prefix trie keyed by path components.

Each node can optionally hold a value of type T, and has children indexed by path component strings. This provides O(k) operations where k is the path depth.

§Example

use structfs_core_store::{PathTrie, path};

let mut trie: PathTrie<i32> = PathTrie::new();
trie.insert(&path!("a/b"), 1);
trie.insert(&path!("a/b/c"), 2);

assert_eq!(trie.get(&path!("a/b")), Some(&1));

// find_ancestor returns the deepest value along the path
let (value, suffix) = trie.find_ancestor(&path!("a/b/c/d")).unwrap();
assert_eq!(*value, 2);
assert_eq!(suffix, path!("d"));

Implementations§

Source§

impl<T> PathTrie<T>

Source

pub fn new() -> Self

Create an empty trie.

Source

pub fn insert(&mut self, path: &Path, value: T) -> Option<T>

Insert a value at path. Returns previous value if any.

Source

pub fn remove(&mut self, path: &Path) -> Option<T>

Remove and return value at exact path. Children remain.

Source

pub fn remove_subtree(&mut self, path: &Path) -> Option<PathTrie<T>>

Remove and return entire subtree at path.

Source

pub fn get(&self, path: &Path) -> Option<&T>

Get reference to value at exact path.

Source

pub fn get_mut(&mut self, path: &Path) -> Option<&mut T>

Get mutable reference to value at exact path.

Source

pub fn get_subtrie(&self, path: &Path) -> Option<&PathTrie<T>>

Get reference to subtrie at path.

Source

pub fn get_subtrie_mut(&mut self, path: &Path) -> Option<&mut PathTrie<T>>

Get mutable reference to subtrie at path.

Source

pub fn contains_value(&self, path: &Path) -> bool

Check if exact path has a value.

Source

pub fn len(&self) -> usize

Count of values in trie (not nodes).

Source

pub fn is_empty(&self) -> bool

True if no values anywhere in trie.

Source

pub fn find_ancestor(&self, path: &Path) -> Option<(&T, Path)>

Find deepest ancestor with a value. Returns (value_ref, remaining_suffix).

Source

pub fn find_ancestor_mut(&mut self, path: &Path) -> Option<(&mut T, Path)>

Mutable version of find_ancestor. Due to borrow checker constraints, this uses a two-pass approach.

Source

pub fn iter(&self) -> PathTrieIter<'_, T>

Iterate over all (path, value) pairs.

Trait Implementations§

Source§

impl<T: Clone> Clone for PathTrie<T>

Source§

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

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for PathTrie<T>

Source§

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

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

impl<T> Default for PathTrie<T>

Source§

fn default() -> Self

Returns the “default value” for a type. 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> 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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.