Skip to content

Commit

Permalink
Add (incomplete) pop method
Browse files Browse the repository at this point in the history
  • Loading branch information
voltrevo committed Aug 17, 2023
1 parent 8a3b8bd commit 2c547af
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
17 changes: 17 additions & 0 deletions radix_tree/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,21 @@ mod tests {
assert_eq!(*v, i);
}
}

#[test]
#[should_panic] // TODO: fix
fn pop_100() {
let mut tree = RadixTree::<usize, 4>::new();

for i in 0..100 {
tree.push(i);
}

for i in (0..100).rev() {
assert_eq!(tree.pop(), Some(i));
assert_eq!(tree.len(), i);
}

assert_eq!(tree.pop(), None);
}
}
22 changes: 22 additions & 0 deletions radix_tree/src/radix_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,28 @@ impl<T: Clone, const N: usize> RadixTree<T, N> {
meta_node_with_space.push(RadixTree(Rc::new(RadixTreeData::Leaves(new_leaves))));
}

pub fn pop(&mut self) -> Option<T> {
let mut tree = self;

loop {
match tree.data_mut() {
RadixTreeData::Meta(meta) => {
let last = meta.len() - 1;
tree = &mut meta[last];
}
RadixTreeData::Leaves(leaves) => {
let res = leaves.pop();

if leaves.is_empty() {
todo!(); // Shrink tree
}

return res;
}
}
}
}

pub fn first(&self) -> Option<&T> {
let mut tree = self;

Expand Down

0 comments on commit 2c547af

Please sign in to comment.