From affa52ba738713ba2fcabb7a48aa8007c0d9d252 Mon Sep 17 00:00:00 2001 From: Janis Date: Mon, 11 Aug 2025 22:43:34 +0200 Subject: [PATCH] feat(tree): add mutable subtree access and traversal methods - Introduced `into_value_mut` for mutable value access in `Handle` - Added `into_subtree` for converting OccupiedEntries into mutable subtrees - Refactored `SubtreeMut` into a generic `Subtree` supporting borrow types - Implemented `get_subtree` and `get_subtree_mut` for subtree traversal - Updated `Tree` to use the new `Subtree` abstraction --- src/tree.rs | 77 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 70 insertions(+), 7 deletions(-) diff --git a/src/tree.rs b/src/tree.rs index 863e452..e31dee5 100644 --- a/src/tree.rs +++ b/src/tree.rs @@ -636,6 +636,11 @@ impl<'a, K: 'a, V: 'a, NodeType> Handle, K, V, NodeType> let v = leaf.value.as_mut().unwrap(); v } + + pub fn into_value_mut(self) -> &'a mut V { + let leaf = self.node.into_leaf_mut(); + leaf.value.as_mut().unwrap() + } } impl Handle, marker::Edge> { @@ -1136,6 +1141,12 @@ mod entry { pub fn get_mut(&mut self) -> &mut V { unsafe { self.handle.value_mut() } } + + pub fn into_subtree(self) -> super::subtree::Subtree> { + super::subtree::Subtree { + root: self.handle.node.as_leaf_or_internal(), + } + } } impl<'a, Q, K: Ord, V> Entry<'a, Q, K, V> @@ -1189,12 +1200,11 @@ mod subtree { marker, }; - pub struct SubtreeMut<'a, K, V> { - pub(super) root: NodeRef, K, V, marker::LeafOrInternal>, - pub(super) _marker: core::marker::PhantomData<&'a mut (K, V)>, + pub struct Subtree { + pub(super) root: NodeRef, } - impl<'a, K, V> SubtreeMut<'a, K, V> + impl Subtree where K: Ord, { @@ -1212,6 +1222,60 @@ mod subtree { } } + pub fn get_subtree(&self, mut key_seq: Q) -> Option>> + where + Q: Iterator, + { + let root = self.root.reborrow(); + match root.search_tree(&mut key_seq) { + search::SearchResult::Found(handle) => Some(Subtree { + root: handle.node.as_leaf_or_internal(), + }), + _ => { + // key not found + None + } + } + } + } + + impl<'a, K, V> Subtree> + where + K: Ord, + { + pub fn get_mut(&mut self, mut key_seq: Q) -> Option<&mut V> + where + Q: Iterator, + { + let root = unsafe { self.root.reborrow_mut() }; + match root.search_tree(&mut key_seq) { + search::SearchResult::Found(handle) => Some(handle.into_value_mut()), + _ => { + // key not found + None + } + } + } + + pub fn get_subtree_mut( + &'_ mut self, + mut key_seq: Q, + ) -> Option>> + where + Q: Iterator, + { + let root = unsafe { self.root.reborrow_mut() }; + match root.search_tree(&mut key_seq) { + search::SearchResult::Found(handle) => Some(Subtree { + root: handle.node.as_leaf_or_internal(), + }), + _ => { + // key not found + None + } + } + } + pub fn entry(&'_ mut self, mut key_seq: Q) -> Entry<'_, OnceAndIter, K, V> where Q: Iterator, @@ -1438,7 +1502,7 @@ impl Tree where K: Ord, { - fn as_subtree_mut<'a>(&'a mut self) -> Option> { + fn as_subtree_mut<'a>(&'a mut self) -> Option>> { let root = self .root .as_mut()? @@ -1446,9 +1510,8 @@ where .dormant() .as_leaf_or_internal(); - Some(subtree::SubtreeMut { + Some(subtree::Subtree { root: unsafe { root.awaken() }, - _marker: PhantomData, }) }