Skip to content

Commit

Permalink
tree knuckle merger: Avoid extra sorting
Browse files Browse the repository at this point in the history
Filter the list of nodes before we sort,
and use a cache to avoid method calls.

Updates #814
  • Loading branch information
shawnlaffan committed Sep 12, 2022
1 parent 2f99545 commit 366ee31
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/Biodiverse/Tree.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2715,13 +2715,19 @@ sub trim_to_last_common_ancestor {
}


# merge any single-child nodes with their children
sub merge_knuckle_nodes {
my $self = shift;

# merge any single-child nodes with their children
my @node_refs = reverse sort {$a->get_depth <=> $b->get_depth} $self->get_node_refs;
# we start from the deepest nodes and work up
my %depth_cache;
my @node_refs
= sort {($depth_cache{$b} //= $b->get_depth) <=> ($depth_cache{$a} //= $a->get_depth)}
grep {$_->get_child_count == 1}
$self->get_node_refs;

my %deleted;
foreach my $node_ref (grep {$_->get_child_count == 1} @node_refs) {
foreach my $node_ref (@node_refs) {
my $node_name = $node_ref->get_name;
next if $deleted{$node_name}; # skip ones we already deleted
my $children = $node_ref->get_children;
Expand Down

0 comments on commit 366ee31

Please sign in to comment.