From 4a03c700d5391e3fa18f9d9b219936ef5c9eb722 Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Tue, 28 Apr 2020 14:12:17 -0700 Subject: struct: tree node children now purely linked list instead of bespoke hybrid --- sys/libbio/io/newick.c | 5 +++-- sys/libbio/phylo.c | 32 ++++++++++++++------------------ 2 files changed, 17 insertions(+), 20 deletions(-) (limited to 'sys') diff --git a/sys/libbio/io/newick.c b/sys/libbio/io/newick.c index 6370686..806feaa 100644 --- a/sys/libbio/io/newick.c +++ b/sys/libbio/io/newick.c @@ -381,12 +381,13 @@ dump(bio·Node *node, void *impl, io·Putter out) if (!node) { return 1; } + bio·Node *child; if (node->nchild) { out.put(impl, '('); - dump(node->child[0], impl, out); - for (child = node->child[1]; child != nil; child = child->sibling) { + dump(node->child, impl, out); + for (child = node->child->sibling; child != nil; child = child->sibling) { out.put(impl, ','); dump(child, impl, out); } 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; -- cgit v1.2.1