Skip to content

Commit

Permalink
feat: Exporse get_mut
Browse files Browse the repository at this point in the history
  • Loading branch information
allevo authored and vemonet committed Dec 9, 2024
1 parent e1c553a commit b858503
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,26 @@ impl<K: Eq + Ord + Clone, V: Clone> Trie<K, V> {
self.find_node(key).and_then(|node| node.get_value())
}

/// Gets the mutable value from the tree by key
///
/// # Example
///
/// ```rust
/// use ptrie::Trie;
///
/// let mut t = Trie::new();
/// let data = "test".bytes();
/// let another_data = "notintest".bytes();
/// assert_eq!(t.get_mut(data.clone()), None);
/// t.insert(data.clone(), 42);
///
/// assert_eq!(t.get_mut(data), Some(42).as_mut());
/// assert_eq!(t.get(another_data), None);
/// ```
pub fn get_mut<I: Iterator<Item = K>>(&mut self, key: I) -> Option<&mut V> {
self.find_node_mut(key).and_then(|node| node.get_value_mut())
}

/// Sets the value pointed by a key
///
/// # Example
Expand Down
4 changes: 4 additions & 0 deletions src/trie_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ impl<K: Eq + Ord + Clone, V: Clone> TrieNode<K, V> {
self.value.as_ref()
}

pub fn get_value_mut(&mut self) -> Option<&mut V> {
self.value.as_mut()
}

pub fn may_be_leaf(&self) -> bool {
self.value.is_some()
}
Expand Down

0 comments on commit b858503

Please sign in to comment.