aboutsummaryrefslogtreecommitdiff
path: root/sys/libbio/phylo.c
diff options
context:
space:
mode:
authorNicholas Noll <nbnoll@eml.cc>2020-04-28 14:12:17 -0700
committerNicholas Noll <nbnoll@eml.cc>2020-04-28 14:12:17 -0700
commit4a03c700d5391e3fa18f9d9b219936ef5c9eb722 (patch)
tree7b2b11b7f7e2479a7c2e51298d4eaa12bd7a2f71 /sys/libbio/phylo.c
parentf64f5abd9987903ef75f52473efba4dbdeebf0fc (diff)
struct: tree node children now purely linked list instead of bespoke hybrid
Diffstat (limited to 'sys/libbio/phylo.c')
-rw-r--r--sys/libbio/phylo.c32
1 files changed, 14 insertions, 18 deletions
diff --git a/sys/libbio/phylo.c b/sys/libbio/phylo.c
index d623712..2bff92d 100644
--- a/sys/libbio/phylo.c
+++ b/sys/libbio/phylo.c
@@ -9,23 +9,19 @@ error
phylo·addchild(bio·Node* parent, bio·Node* child)
{
bio·Node *it, *sibling;
- switch (parent->nchild) {
- case 1:
- parent->child[0]->sibling = child;
- case 0:
- parent->child[parent->nchild++] = child;
- break;
-
- default:
- sibling = parent->child[1];
- for (it = parent->child[1]->sibling; it != nil; it = it->sibling) {
- sibling = it;
- }
- sibling->sibling = child;
- parent->nchild++;
+ if (!parent->nchild) {
+ parent->child = child;
+ goto SUCCESS;
+ }
+
+ for (it = parent->child, sibling = it; it != nil; it = it->sibling) {
+ sibling = it;
}
+ sibling->sibling = child;
+SUCCESS:
child->parent = parent;
+ parent->nchild++;
return 0;
}
@@ -39,7 +35,7 @@ phylo·rmchild(bio·Node* parent, bio·Node* child)
};
prev = nil;
- for (it = parent->child[0]; it != nil && it != child; it = it->sibling) {
+ for (it = parent->child; it != nil && it != child; it = it->sibling) {
prev = it;
}
if (it == nil) return error·notfound;
@@ -59,7 +55,7 @@ phylo·countnodes(bio·Node *node, int *n)
bio·Node *child;
*n += 1;
- for (child = node->child[0]; child != nil; child = child->sibling) {
+ for (child = node->child; child != nil; child = child->sibling) {
if (err = phylo·countnodes(child, n), err) {
errorf("node count: failure at '%s'", child->name);
return 1;
@@ -79,7 +75,7 @@ phylo·countleafs(bio·Node *node, int *n)
*n += 1;
}
- for (child = node->child[0]; child != nil; child = child->sibling) {
+ for (child = node->child; child != nil; child = child->sibling) {
if (err = phylo·countleafs(child, n), err) {
errorf("leaf count: failure at '%s'", child->name);
return 1;
@@ -103,7 +99,7 @@ phylo·ladderize(bio·Node *root)
if (!root->nchild) return 0;
Assert(root->nchild < arrlen(dists));
- for (i = 0, child = root->child[0]; child != nil; child = child->sibling, i++) {
+ for (i = 0, child = root->child; child != nil; child = child->sibling, i++) {
if (err = phylo·ladderize(child), err) {
errorf("ladderize: failure at '%s'", child->name);
return 1;