Skip to main content

PathTrie

Struct PathTrie 

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

Source

pub fn new() -> Self

Creates an empty trie.

Source

pub fn len(&self) -> usize

Returns the number of values stored at this node.

Source

pub fn is_empty(&self) -> bool

Returns true if this node has no values.

Source

pub fn total_count(&self) -> usize

Returns the total number of values in this trie and all descendants.

Source

pub fn unique_paths(&self) -> usize

Returns the number of unique paths (nodes with values) in this trie.

Source

pub fn has_children(&self) -> bool

Returns true if this node has children.

Source§

impl<V: Clone> PathTrie<V>

Source

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.

Source

pub fn get_exact(&self, path: &CapabilityPath) -> Vec<&V>

Returns values at the exact path (not including descendants).

Source

pub fn get_prefix(&self, path: &CapabilityPath) -> Vec<&V>

Returns values at the path and all descendant paths (prefix match).

Source

pub fn remove<F>(&mut self, path: &CapabilityPath, predicate: F) -> usize
where F: Fn(&V) -> bool,

Removes values at the path that match the predicate.

Returns the number of values removed.

Source

pub fn clear(&mut self)

Clears all values from the trie.

Trait Implementations§

Source§

impl<V: Clone> Clone for PathTrie<V>

Source§

fn clone(&self) -> Self

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<V: Debug> Debug for PathTrie<V>

Source§

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

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

impl<V> Default for PathTrie<V>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<V> Freeze for PathTrie<V>

§

impl<V> RefUnwindSafe for PathTrie<V>

§

impl<V> Send for PathTrie<V>
where HashMap<String, PathTrie<V>>: Send, Vec<V>: Send,

§

impl<V> Sync for PathTrie<V>
where HashMap<String, PathTrie<V>>: Sync, Vec<V>: Sync,

§

impl<V> Unpin for PathTrie<V>
where HashMap<String, PathTrie<V>>: Unpin, Vec<V>: Unpin,

§

impl<V> UnsafeUnpin for PathTrie<V>

§

impl<V> UnwindSafe for PathTrie<V>

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> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V