Skip to content

Commit

Permalink
use foreach syntax where appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
kingaa committed Nov 25, 2024
1 parent 644566d commit 0cb4911
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 75 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: phylopomp
Type: Package
Title: Phylodynamic Inference for POMP Models
Version: 0.14.8.1
Version: 0.14.8.2
Date: 2024-11-25
Authors@R: c(person(given=c("Aaron","A."),family="King",role=c("aut","cre"),email="[email protected]",comment=c(ORCID="0000-0001-6159-3207")),
person(given=c("Qianying"),family="Lin",role=c("aut"),comment=c(ORCID="0000-0001-8620-9910"))
Expand Down
11 changes: 5 additions & 6 deletions src/genealogy.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,7 @@ class genealogy_t : public nodeseq_t {
sat[j] = ell[j] = 0;
etype[j] = 0;
}
for (node_it i = begin(); i != end(); i++) {
node_t *p = *i;
for (const node_t *p : *this) {
if (tcur < p->slate) {
tout += _ndeme; ell += _ndeme; sat += _ndeme;
deme += _ndeme; etype += _ndeme;
Expand Down Expand Up @@ -310,8 +309,8 @@ class genealogy_t : public nodeseq_t {
//! number of samples
size_t nsample (void) const {
size_t n = 0;
for (node_it k = cbegin(); k != cend(); k++) {
if ((*k)->holds(blue)) n++;
for (const node_t *p : *this) {
if (p->holds(blue)) n++;
}
return n;
};
Expand Down Expand Up @@ -483,8 +482,8 @@ class genealogy_t : public nodeseq_t {
}
delete blacks;
// erase deme information from nodes.
for (node_it i = begin(); i != end(); i++) {
(*i)->deme() = 0;
for (node_t *p : *this) {
p->deme() = 0;
}
// drop superfluous nodes (holding just one ball).
comb();
Expand Down
10 changes: 5 additions & 5 deletions src/inventory.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class inventory_t {
inventory_t (std::pair<node_it,node_it>&& I) {
clean();
for (node_it i = I.first; i != I.second; i++) {
for (ball_it j = (*i)->begin(); j != (*i)->end(); j++) {
insert(*j); // 'insert' checks color
for (ball_t *b : *i) {
insert(b); // 'insert' checks color
}
}
};
Expand All @@ -49,8 +49,8 @@ class inventory_t {
inventory_t& operator= (std::pair<node_it,node_it>&& I) {
clean();
for (node_it i = I.first; i != I.second; i++) {
for (ball_it j = (*i)->begin(); j != (*i)->end(); j++) {
insert(*j); // 'insert' checks color
for (ball_t *b : **i) {
insert(b); // 'insert' checks color
}
}
return *this;
Expand Down Expand Up @@ -150,7 +150,7 @@ class inventory_t {
k++; j++;
}
} else {
assert(0); // #nocov
assert(0); // #nocov
}
return p;
};
Expand Down
10 changes: 5 additions & 5 deletions src/master.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,18 +165,18 @@ class master_t : public POPN {
//! sample in deme i
void sample (name_t i = 0, int n = 1) {
pocket_t *p = inventory.random_balls(i,n);
for (ball_it a = p->begin(); a != p->end(); a++) {
geneal.sample(*a,time());
for (ball_t *a : *p) {
geneal.sample(a,time());
}
p->clear();
delete p;
};
//! sample_death in deme i
void sample_death (name_t i = 0, int n = 1) {
pocket_t *p = inventory.random_balls(i,n);
for (ball_it a = p->begin(); a != p->end(); a++) {
inventory.erase(*a);
geneal.sample_death(*a,time());
for (ball_t *a : *p) {
inventory.erase(a);
geneal.sample_death(a,time());
}
p->clear();
delete p;
Expand Down
10 changes: 4 additions & 6 deletions src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ class node_t : public pocket_t {
//! number of descendants
int nchildren (void) const {
int n = 0;
for (ball_it i = begin(); i != end(); i++) {
switch ((*i)->color) {
for (ball_t *b : *this) {
switch (b->color) {
case green: case black:
n++;
break;
Expand All @@ -151,8 +151,7 @@ class node_t : public pocket_t {
void lineage_incr (int *incr, int *sat, int *etype) const {
const name_t d = deme();
incr[d]--;
for (ball_it i = cbegin(); i != cend(); i++) {
ball_t *b = *i;
for (ball_t *b : *this) {
switch (b->color) {
case green: case black:
incr[b->deme()]++;
Expand Down Expand Up @@ -226,8 +225,7 @@ class node_t : public pocket_t {
o3 += "g_";
}
n = 0;
for (ball_it i = begin(); i != end(); i++) {
ball_t *b = *i;
for (ball_t *b : *this) {
node_t *p = 0;
switch (b->color) {
case green:
Expand Down
57 changes: 26 additions & 31 deletions src/nodeseq.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class nodeseq_t : public std::list<node_t*> {

//! clean up: delete all nodes, reset globals
void clean (void) {
for (node_it i = begin(); i != end(); i++) delete *i;
for (node_t *p : *this) delete p;
clear();
};

Expand All @@ -39,16 +39,16 @@ class nodeseq_t : public std::list<node_t*> {
//! size of serialized binary form
size_t bytesize (void) const {
size_t s = sizeof(size_t);
for (node_it i = begin(); i != end(); i++)
s += (*i)->bytesize();
for (node_t *p : *this)
s += p->bytesize();
return s;
};
//! binary serialization
friend raw_t* operator>> (const nodeseq_t& G, raw_t* o) {
size_t nnode = G.size();
memcpy(o,&nnode,sizeof(size_t)); o += sizeof(size_t);
for (node_it i = G.begin(); i != G.end(); i++) {
o = (**i >> o);
for (node_t *p : G) {
o = (*p >> o);
}
return o;
};
Expand All @@ -67,8 +67,8 @@ class nodeseq_t : public std::list<node_t*> {
G.push_back(p);
node_names.insert({p->uniq,p});
}
for (node_it i = G.begin(); i != G.end(); i++) {
(*i)->repair_owners(node_names,&ball_names);
for (node_t *q : G) {
q->repair_owners(node_names,&ball_names);
}
G.repair_owners(ball_names);
G.trace_lineages();
Expand All @@ -81,8 +81,7 @@ class nodeseq_t : public std::list<node_t*> {
//! This function repairs the links green balls and their names.
void repair_owners (std::unordered_map<name_t,ball_t*>& names) {
std::unordered_map<name_t,ball_t*>::const_iterator n;
for (node_it i = begin(); i != end(); i++) {
node_t *p = *i;
for (node_t *p : *this) {
n = names.find(p->uniq);
assert(n != names.end());
ball_t *b = n->second;
Expand Down Expand Up @@ -128,29 +127,27 @@ class nodeseq_t : public std::list<node_t*> {
//! Get all balls of a color.
pocket_t* colored (color_t col) const {
pocket_t *p = new pocket_t;
for (node_it i = begin(); i != end(); i++) {
for (ball_it j = (*i)->begin(); j != (*i)->end(); j++) {
if ((*j)->is(col)) p->insert(*j);
for (node_t *q : *this) {
for (ball_t *b : *q ) {
if (b->is(col)) p->insert(b);
}
}
return p;
};
//! Number of distinct timepoints.
size_t ntime (slate_t t) const {
size_t count = 1;
for (node_it i = begin(); i != end(); i++) {
if (t < (*i)->slate) {
t = (*i)->slate;
for (node_t *p : *this) {
if (t < p->slate) {
t = p->slate;
count++;
}
}
return count;
};
//! Number of nodes in the sequence.
size_t length (void) const {
size_t count = 0;
for (node_it i = begin(); i != end(); i++) count++;
return count;
return this->size();
};
//! traverse to nth node, retrieve pointer
node_t *position (int n) {
Expand Down Expand Up @@ -246,10 +243,8 @@ class nodeseq_t : public std::list<node_t*> {
// because we move from early to late,
// the order is guaranteed to be valid.
name_t u = 0;
for (node_it i = begin(); i != end(); i++) {
node_t *p = *i;
for (ball_it j = p->begin(); j != p->end(); j++) {
ball_t *b = *j;
for (node_t *p : *this ) {
for (ball_t *b : *p) {
if (b->color==blue) {
trace_lineage(b,u);
u++;
Expand All @@ -263,17 +258,17 @@ class nodeseq_t : public std::list<node_t*> {
//! human-readable info
std::string describe (void) const {
std::string o = "";
for (node_it p = begin(); p != end(); p++) {
o += (*p)->describe();
for (node_t *p : *this) {
o += p->describe();
}
return o;
};
//! human- & machine-readable info
virtual std::string yaml (std::string tab = "") const {
std::string o = "";
std::string t = tab + " ";
for (node_it p = begin(); p != end(); p++) {
o += tab + "- " + (*p)->yaml(t);
for (node_t *p : *this) {
o += tab + "- " + p->yaml(t);
}
return o;
};
Expand All @@ -282,8 +277,8 @@ class nodeseq_t : public std::list<node_t*> {
SEXP Nodes;
PROTECT(Nodes = NEW_LIST(size()));
int k = 0;
for (node_it i = begin(); i != end(); i++) {
SET_ELEMENT(Nodes,k++,(*i)->structure());
for (node_t *p : *this) {
SET_ELEMENT(Nodes,k++,p->structure());
}
UNPROTECT(1);
return Nodes;
Expand All @@ -292,9 +287,9 @@ class nodeseq_t : public std::list<node_t*> {
std::string newick (slate_t t) const {
slate_t te = dawn();
std::string o = "";
for (node_it i = begin(); i != end(); i++) {
if ((*i)->is_root()) {
o += (*i)->newick(t,te) + ";";
for (node_t *p : *this) {
if (p->is_root()) {
o += p->newick(t,te) + ";";
}
}
return o;
Expand Down
25 changes: 12 additions & 13 deletions src/pocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class pocket_t : public std::set<ball_t*,ball_order> {

//! delete balls and clear pocket
void clean (void) {
for (ball_it i = begin(); i != end(); i++) delete *i;
for (ball_t *b : *this) delete b;
clear();
};

Expand All @@ -50,8 +50,8 @@ class pocket_t : public std::set<ball_t*,ball_order> {
friend raw_t* operator>> (const pocket_t &p, raw_t *o) {
size_t psize = p.size();
memcpy(o,&psize,sizeof(size_t)); o += sizeof(size_t);
for (ball_it i = p.begin(); i != p.end(); i++)
o = (**i >> o);
for (ball_t *i : p)
o = (*i >> o);
return o;
};
//! binary deserialization.
Expand All @@ -72,8 +72,8 @@ class pocket_t : public std::set<ball_t*,ball_order> {
//! Needed in deserialization.
//! Inform all balls as to their holder.
void repair_holder (node_t* p) {
for (ball_it i = begin(); i != end(); i++) {
(*i)->holder() = p;
for (ball_t *b : *this) {
b->holder() = p;
}
};

Expand All @@ -83,8 +83,7 @@ class pocket_t : public std::set<ball_t*,ball_order> {
void repair_owners (const std::unordered_map<name_t,node_t*>& node_name,
std::unordered_map<name_t,ball_t*> *ball_name) {
std::unordered_map<name_t,node_t*>::const_iterator n;
for (ball_it i = begin(); i != end(); i++) {
ball_t *b = *i;
for (ball_t *b : *this) {
if (b->is(green)) {
n = node_name.find(b->uniq);
if (n != node_name.end()) {
Expand Down Expand Up @@ -127,17 +126,17 @@ class pocket_t : public std::set<ball_t*,ball_order> {
};
//! retrieve the first ball of the specified color.
ball_t* ball (const color_t c) const {
for (ball_it i = begin(); i != end(); i++) {
if ((*i)->color == c) return *i;
for (ball_t *b : *this) {
if (b->color == c) return b;
}
err("in '%s' (%s line %d): no ball of color %s", // # nocov
__func__,__FILE__,__LINE__,colores[c]); // # nocov
return 0;
};
//! return a pointer to another ball
ball_t* other (const ball_t *b) const {
for (ball_it i = begin(); i != end(); i++) {
if (*i != b) return *i;
for (ball_t *a : *this) {
if (a != b) return a;
}
err("error in '%s' (%s line %d): there is no other.", // # nocov
__func__,__FILE__,__LINE__); // # nocov
Expand Down Expand Up @@ -169,8 +168,8 @@ class pocket_t : public std::set<ball_t*,ball_order> {
std::string yaml (std::string tab = "") const {
std::string o = "";
std::string t = tab + " ";
for (ball_it i = begin(); i != end(); i++) {
o += tab + "- " + (*i)->yaml(t);
for (ball_t *b : *this) {
o += tab + "- " + b->yaml(t);
}
return o;
};
Expand Down
Loading

0 comments on commit 0cb4911

Please sign in to comment.