Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Extract remove_leaf to its own ops function #54

Open
lsetiawan opened this issue Dec 11, 2024 · 0 comments
Open

refactor: Extract remove_leaf to its own ops function #54

lsetiawan opened this issue Dec 11, 2024 · 0 comments

Comments

@lsetiawan
Copy link
Member

Need to extract the following remove_leaf method to its own ops function and then call it here so it can be used independently of TreeVec

pub fn remove_leaf(&mut self, leaf: usize) -> usize {
let ancestry = self.get_ancestry();
let leaf_coords = ops::find_coords_of_first_leaf(&ancestry, leaf);
let leaf_row = leaf_coords.0;
let leaf_col = leaf_coords.1;
// Find the parent of the leaf to remove
let parent = ancestry[leaf_row][2];
let sister = ancestry[leaf_row][1 - leaf_col];
let num_cherries = ancestry.len();
let mut ancestry_rm = Vec::with_capacity(num_cherries - 1);
for r in 0..num_cherries - 1 {
let mut new_row = if r < leaf_row {
ancestry[r].clone()
} else {
ancestry[r + 1].clone()
};
for c in 0..3 {
let mut node = new_row[c];
if node == parent {
node = sister;
}
// Subtract 1 for leaves > "leaf"
// (so that the vector is still valid)
if node > leaf {
node -= 1;
if node >= parent {
node -= 1;
}
}
new_row[c] = node;
}
ancestry_rm.push(new_row);
}
ops::order_cherries(&mut ancestry_rm);
ops::order_cherries_no_parents(&mut ancestry_rm);
self.data = ops::build_vector(ancestry_rm);
return sister;
}
}

@lsetiawan lsetiawan changed the title refactor: Extract remove_leaf to its own ops function refactor: Extract remove_leaf to its own ops function Dec 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant