diff options
-rw-r--r-- | include/libbio.h | 3 | ||||
-rw-r--r-- | sys/libbio/io/newick.c | 5 | ||||
-rw-r--r-- | sys/libbio/phylo.c | 32 |
3 files changed, 18 insertions, 22 deletions
diff --git a/include/libbio.h b/include/libbio.h index 4b92587..261c732 100644 --- a/include/libbio.h +++ b/include/libbio.h @@ -12,9 +12,8 @@ typedef struct bio·Node int nchild; int ndescendent; struct bio·Node *parent; - // NOTE: Sibiling allows for polytomies - struct bio·Node *child[2]; struct bio·Node *sibling; + struct bio·Node *child; } bio·Node; typedef struct bio·Tree 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; |