Skip to content

Commit

Permalink
Add HamTree::remove
Browse files Browse the repository at this point in the history
  • Loading branch information
ureeves committed Jan 14, 2024
1 parent b145a4a commit 07d3b9b
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,15 @@ where
}
}

/// Removes the leaf at the given index, returning it if present.
pub fn remove(&mut self, index: usize) -> Option<T> {
if self.is_unallocated() {
return None;
}

self.insert(index, empty_node())
}

fn empty(node: &T) -> Option<&T> {
if *node == empty_node::<T>() {
None
Expand Down Expand Up @@ -429,7 +438,7 @@ mod tests {
const N_INSERTIONS: usize = 100;

#[test]
fn insertion() {
fn insert() {
let mut rng = StdRng::seed_from_u64(0xBAAD_F00D);

let mut tree = Tree::new();
Expand All @@ -445,6 +454,27 @@ mod tests {
assert!(matches!(tree.root(), Some(x) if *x == Count(n_insertions)));
}

#[test]
fn remove() {
let mut rng = StdRng::seed_from_u64(0xBAAD_F00D);

let mut tree = Tree::new();
let mut index_set = BTreeSet::new();

for _ in 0..N_INSERTIONS {
let i = (rng.next_u64() % Tree::N_LEAVES as u64) as usize;
index_set.insert(i);
tree.insert(i, Count(1));
}

for i in index_set {
tree.remove(i);
assert!(tree.leaf(i).is_none());
}

assert!(tree.root().is_none());
}

#[test]
fn leaf() {
let mut rng = StdRng::seed_from_u64(0xBAAD_F00D);
Expand Down

0 comments on commit 07d3b9b

Please sign in to comment.