pub struct PathTrie<V> { /* private fields */ }Expand description
Trie structure for efficient prefix matching on capability paths.
Supports:
- O(d) insert where d is path depth
- O(d) exact lookup
- O(d + n) prefix lookup where n is the number of descendants
§Type Parameter
V- The value type stored at each path node
§Examples
use agent_uri::CapabilityPath;
use agent_uri_dht::PathTrie;
let mut trie: PathTrie<String> = PathTrie::new();
let path1 = CapabilityPath::parse("assistant/chat").unwrap();
let path2 = CapabilityPath::parse("assistant/code").unwrap();
trie.insert(&path1, "Chat Agent".to_string());
trie.insert(&path2, "Code Agent".to_string());
// Exact lookup
let chat_agents = trie.get_exact(&path1);
assert_eq!(chat_agents.len(), 1);
// Prefix lookup for "assistant" finds both
let prefix = CapabilityPath::parse("assistant").unwrap();
let all_assistants = trie.get_prefix(&prefix);
assert_eq!(all_assistants.len(), 2);Implementations§
Source§impl<V> PathTrie<V>
impl<V> PathTrie<V>
Sourcepub fn total_count(&self) -> usize
pub fn total_count(&self) -> usize
Returns the total number of values in this trie and all descendants.
Sourcepub fn unique_paths(&self) -> usize
pub fn unique_paths(&self) -> usize
Returns the number of unique paths (nodes with values) in this trie.
Sourcepub fn has_children(&self) -> bool
pub fn has_children(&self) -> bool
Returns true if this node has children.
Source§impl<V: Clone> PathTrie<V>
impl<V: Clone> PathTrie<V>
Sourcepub fn insert(&mut self, path: &CapabilityPath, value: V)
pub fn insert(&mut self, path: &CapabilityPath, value: V)
Inserts a value at the given path.
Multiple values can be stored at the same path.
Sourcepub fn get_exact(&self, path: &CapabilityPath) -> Vec<&V>
pub fn get_exact(&self, path: &CapabilityPath) -> Vec<&V>
Returns values at the exact path (not including descendants).
Sourcepub fn get_prefix(&self, path: &CapabilityPath) -> Vec<&V>
pub fn get_prefix(&self, path: &CapabilityPath) -> Vec<&V>
Returns values at the path and all descendant paths (prefix match).
Sourcepub fn remove<F>(&mut self, path: &CapabilityPath, predicate: F) -> usize
pub fn remove<F>(&mut self, path: &CapabilityPath, predicate: F) -> usize
Removes values at the path that match the predicate.
Returns the number of values removed.
Trait Implementations§
Auto Trait Implementations§
impl<V> Freeze for PathTrie<V>
impl<V> RefUnwindSafe for PathTrie<V>
impl<V> Send for PathTrie<V>
impl<V> Sync for PathTrie<V>
impl<V> Unpin for PathTrie<V>
impl<V> UnsafeUnpin for PathTrie<V>
impl<V> UnwindSafe for PathTrie<V>
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