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>
impl<T> PathTrie<T>
Sourcepub fn insert(&mut self, path: &Path, value: T) -> Option<T>
pub fn insert(&mut self, path: &Path, value: T) -> Option<T>
Insert a value at path. Returns previous value if any.
Sourcepub fn remove(&mut self, path: &Path) -> Option<T>
pub fn remove(&mut self, path: &Path) -> Option<T>
Remove and return value at exact path. Children remain.
Sourcepub fn remove_subtree(&mut self, path: &Path) -> Option<PathTrie<T>>
pub fn remove_subtree(&mut self, path: &Path) -> Option<PathTrie<T>>
Remove and return entire subtree at path.
Sourcepub fn get_mut(&mut self, path: &Path) -> Option<&mut T>
pub fn get_mut(&mut self, path: &Path) -> Option<&mut T>
Get mutable reference to value at exact path.
Sourcepub fn get_subtrie(&self, path: &Path) -> Option<&PathTrie<T>>
pub fn get_subtrie(&self, path: &Path) -> Option<&PathTrie<T>>
Get reference to subtrie at path.
Sourcepub fn get_subtrie_mut(&mut self, path: &Path) -> Option<&mut PathTrie<T>>
pub fn get_subtrie_mut(&mut self, path: &Path) -> Option<&mut PathTrie<T>>
Get mutable reference to subtrie at path.
Sourcepub fn contains_value(&self, path: &Path) -> bool
pub fn contains_value(&self, path: &Path) -> bool
Check if exact path has a value.
Sourcepub fn find_ancestor(&self, path: &Path) -> Option<(&T, Path)>
pub fn find_ancestor(&self, path: &Path) -> Option<(&T, Path)>
Find deepest ancestor with a value. Returns (value_ref, remaining_suffix).
Sourcepub fn find_ancestor_mut(&mut self, path: &Path) -> Option<(&mut T, Path)>
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.
Sourcepub fn iter(&self) -> PathTrieIter<'_, T> ⓘ
pub fn iter(&self) -> PathTrieIter<'_, T> ⓘ
Iterate over all (path, value) pairs.
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for PathTrie<T>
impl<T> RefUnwindSafe for PathTrie<T>
impl<T> Send for PathTrie<T>
impl<T> Sync for PathTrie<T>
impl<T> Unpin for PathTrie<T>
impl<T> UnsafeUnpin for PathTrie<T>
impl<T> UnwindSafe for PathTrie<T>
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
Mutably borrows from an owned value. Read more