Skip to content

Commit 0cb4911

Browse files
committed
use foreach syntax where appropriate
1 parent 644566d commit 0cb4911

File tree

8 files changed

+66
-75
lines changed

8 files changed

+66
-75
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: phylopomp
22
Type: Package
33
Title: Phylodynamic Inference for POMP Models
4-
Version: 0.14.8.1
4+
Version: 0.14.8.2
55
Date: 2024-11-25
66
Authors@R: c(person(given=c("Aaron","A."),family="King",role=c("aut","cre"),email="[email protected]",comment=c(ORCID="0000-0001-6159-3207")),
77
person(given=c("Qianying"),family="Lin",role=c("aut"),comment=c(ORCID="0000-0001-8620-9910"))

src/genealogy.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,7 @@ class genealogy_t : public nodeseq_t {
177177
sat[j] = ell[j] = 0;
178178
etype[j] = 0;
179179
}
180-
for (node_it i = begin(); i != end(); i++) {
181-
node_t *p = *i;
180+
for (const node_t *p : *this) {
182181
if (tcur < p->slate) {
183182
tout += _ndeme; ell += _ndeme; sat += _ndeme;
184183
deme += _ndeme; etype += _ndeme;
@@ -310,8 +309,8 @@ class genealogy_t : public nodeseq_t {
310309
//! number of samples
311310
size_t nsample (void) const {
312311
size_t n = 0;
313-
for (node_it k = cbegin(); k != cend(); k++) {
314-
if ((*k)->holds(blue)) n++;
312+
for (const node_t *p : *this) {
313+
if (p->holds(blue)) n++;
315314
}
316315
return n;
317316
};
@@ -483,8 +482,8 @@ class genealogy_t : public nodeseq_t {
483482
}
484483
delete blacks;
485484
// erase deme information from nodes.
486-
for (node_it i = begin(); i != end(); i++) {
487-
(*i)->deme() = 0;
485+
for (node_t *p : *this) {
486+
p->deme() = 0;
488487
}
489488
// drop superfluous nodes (holding just one ball).
490489
comb();

src/inventory.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ class inventory_t {
3636
inventory_t (std::pair<node_it,node_it>&& I) {
3737
clean();
3838
for (node_it i = I.first; i != I.second; i++) {
39-
for (ball_it j = (*i)->begin(); j != (*i)->end(); j++) {
40-
insert(*j); // 'insert' checks color
39+
for (ball_t *b : *i) {
40+
insert(b); // 'insert' checks color
4141
}
4242
}
4343
};
@@ -49,8 +49,8 @@ class inventory_t {
4949
inventory_t& operator= (std::pair<node_it,node_it>&& I) {
5050
clean();
5151
for (node_it i = I.first; i != I.second; i++) {
52-
for (ball_it j = (*i)->begin(); j != (*i)->end(); j++) {
53-
insert(*j); // 'insert' checks color
52+
for (ball_t *b : **i) {
53+
insert(b); // 'insert' checks color
5454
}
5555
}
5656
return *this;
@@ -150,7 +150,7 @@ class inventory_t {
150150
k++; j++;
151151
}
152152
} else {
153-
assert(0); // #nocov
153+
assert(0); // #nocov
154154
}
155155
return p;
156156
};

src/master.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,18 +165,18 @@ class master_t : public POPN {
165165
//! sample in deme i
166166
void sample (name_t i = 0, int n = 1) {
167167
pocket_t *p = inventory.random_balls(i,n);
168-
for (ball_it a = p->begin(); a != p->end(); a++) {
169-
geneal.sample(*a,time());
168+
for (ball_t *a : *p) {
169+
geneal.sample(a,time());
170170
}
171171
p->clear();
172172
delete p;
173173
};
174174
//! sample_death in deme i
175175
void sample_death (name_t i = 0, int n = 1) {
176176
pocket_t *p = inventory.random_balls(i,n);
177-
for (ball_it a = p->begin(); a != p->end(); a++) {
178-
inventory.erase(*a);
179-
geneal.sample_death(*a,time());
177+
for (ball_t *a : *p) {
178+
inventory.erase(a);
179+
geneal.sample_death(a,time());
180180
}
181181
p->clear();
182182
delete p;

src/node.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ class node_t : public pocket_t {
130130
//! number of descendants
131131
int nchildren (void) const {
132132
int n = 0;
133-
for (ball_it i = begin(); i != end(); i++) {
134-
switch ((*i)->color) {
133+
for (ball_t *b : *this) {
134+
switch (b->color) {
135135
case green: case black:
136136
n++;
137137
break;
@@ -151,8 +151,7 @@ class node_t : public pocket_t {
151151
void lineage_incr (int *incr, int *sat, int *etype) const {
152152
const name_t d = deme();
153153
incr[d]--;
154-
for (ball_it i = cbegin(); i != cend(); i++) {
155-
ball_t *b = *i;
154+
for (ball_t *b : *this) {
156155
switch (b->color) {
157156
case green: case black:
158157
incr[b->deme()]++;
@@ -226,8 +225,7 @@ class node_t : public pocket_t {
226225
o3 += "g_";
227226
}
228227
n = 0;
229-
for (ball_it i = begin(); i != end(); i++) {
230-
ball_t *b = *i;
228+
for (ball_t *b : *this) {
231229
node_t *p = 0;
232230
switch (b->color) {
233231
case green:

src/nodeseq.h

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class nodeseq_t : public std::list<node_t*> {
2222

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

@@ -39,16 +39,16 @@ class nodeseq_t : public std::list<node_t*> {
3939
//! size of serialized binary form
4040
size_t bytesize (void) const {
4141
size_t s = sizeof(size_t);
42-
for (node_it i = begin(); i != end(); i++)
43-
s += (*i)->bytesize();
42+
for (node_t *p : *this)
43+
s += p->bytesize();
4444
return s;
4545
};
4646
//! binary serialization
4747
friend raw_t* operator>> (const nodeseq_t& G, raw_t* o) {
4848
size_t nnode = G.size();
4949
memcpy(o,&nnode,sizeof(size_t)); o += sizeof(size_t);
50-
for (node_it i = G.begin(); i != G.end(); i++) {
51-
o = (**i >> o);
50+
for (node_t *p : G) {
51+
o = (*p >> o);
5252
}
5353
return o;
5454
};
@@ -67,8 +67,8 @@ class nodeseq_t : public std::list<node_t*> {
6767
G.push_back(p);
6868
node_names.insert({p->uniq,p});
6969
}
70-
for (node_it i = G.begin(); i != G.end(); i++) {
71-
(*i)->repair_owners(node_names,&ball_names);
70+
for (node_t *q : G) {
71+
q->repair_owners(node_names,&ball_names);
7272
}
7373
G.repair_owners(ball_names);
7474
G.trace_lineages();
@@ -81,8 +81,7 @@ class nodeseq_t : public std::list<node_t*> {
8181
//! This function repairs the links green balls and their names.
8282
void repair_owners (std::unordered_map<name_t,ball_t*>& names) {
8383
std::unordered_map<name_t,ball_t*>::const_iterator n;
84-
for (node_it i = begin(); i != end(); i++) {
85-
node_t *p = *i;
84+
for (node_t *p : *this) {
8685
n = names.find(p->uniq);
8786
assert(n != names.end());
8887
ball_t *b = n->second;
@@ -128,29 +127,27 @@ class nodeseq_t : public std::list<node_t*> {
128127
//! Get all balls of a color.
129128
pocket_t* colored (color_t col) const {
130129
pocket_t *p = new pocket_t;
131-
for (node_it i = begin(); i != end(); i++) {
132-
for (ball_it j = (*i)->begin(); j != (*i)->end(); j++) {
133-
if ((*j)->is(col)) p->insert(*j);
130+
for (node_t *q : *this) {
131+
for (ball_t *b : *q ) {
132+
if (b->is(col)) p->insert(b);
134133
}
135134
}
136135
return p;
137136
};
138137
//! Number of distinct timepoints.
139138
size_t ntime (slate_t t) const {
140139
size_t count = 1;
141-
for (node_it i = begin(); i != end(); i++) {
142-
if (t < (*i)->slate) {
143-
t = (*i)->slate;
140+
for (node_t *p : *this) {
141+
if (t < p->slate) {
142+
t = p->slate;
144143
count++;
145144
}
146145
}
147146
return count;
148147
};
149148
//! Number of nodes in the sequence.
150149
size_t length (void) const {
151-
size_t count = 0;
152-
for (node_it i = begin(); i != end(); i++) count++;
153-
return count;
150+
return this->size();
154151
};
155152
//! traverse to nth node, retrieve pointer
156153
node_t *position (int n) {
@@ -246,10 +243,8 @@ class nodeseq_t : public std::list<node_t*> {
246243
// because we move from early to late,
247244
// the order is guaranteed to be valid.
248245
name_t u = 0;
249-
for (node_it i = begin(); i != end(); i++) {
250-
node_t *p = *i;
251-
for (ball_it j = p->begin(); j != p->end(); j++) {
252-
ball_t *b = *j;
246+
for (node_t *p : *this ) {
247+
for (ball_t *b : *p) {
253248
if (b->color==blue) {
254249
trace_lineage(b,u);
255250
u++;
@@ -263,17 +258,17 @@ class nodeseq_t : public std::list<node_t*> {
263258
//! human-readable info
264259
std::string describe (void) const {
265260
std::string o = "";
266-
for (node_it p = begin(); p != end(); p++) {
267-
o += (*p)->describe();
261+
for (node_t *p : *this) {
262+
o += p->describe();
268263
}
269264
return o;
270265
};
271266
//! human- & machine-readable info
272267
virtual std::string yaml (std::string tab = "") const {
273268
std::string o = "";
274269
std::string t = tab + " ";
275-
for (node_it p = begin(); p != end(); p++) {
276-
o += tab + "- " + (*p)->yaml(t);
270+
for (node_t *p : *this) {
271+
o += tab + "- " + p->yaml(t);
277272
}
278273
return o;
279274
};
@@ -282,8 +277,8 @@ class nodeseq_t : public std::list<node_t*> {
282277
SEXP Nodes;
283278
PROTECT(Nodes = NEW_LIST(size()));
284279
int k = 0;
285-
for (node_it i = begin(); i != end(); i++) {
286-
SET_ELEMENT(Nodes,k++,(*i)->structure());
280+
for (node_t *p : *this) {
281+
SET_ELEMENT(Nodes,k++,p->structure());
287282
}
288283
UNPROTECT(1);
289284
return Nodes;
@@ -292,9 +287,9 @@ class nodeseq_t : public std::list<node_t*> {
292287
std::string newick (slate_t t) const {
293288
slate_t te = dawn();
294289
std::string o = "";
295-
for (node_it i = begin(); i != end(); i++) {
296-
if ((*i)->is_root()) {
297-
o += (*i)->newick(t,te) + ";";
290+
for (node_t *p : *this) {
291+
if (p->is_root()) {
292+
o += p->newick(t,te) + ";";
298293
}
299294
}
300295
return o;

src/pocket.h

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class pocket_t : public std::set<ball_t*,ball_order> {
3535

3636
//! delete balls and clear pocket
3737
void clean (void) {
38-
for (ball_it i = begin(); i != end(); i++) delete *i;
38+
for (ball_t *b : *this) delete b;
3939
clear();
4040
};
4141

@@ -50,8 +50,8 @@ class pocket_t : public std::set<ball_t*,ball_order> {
5050
friend raw_t* operator>> (const pocket_t &p, raw_t *o) {
5151
size_t psize = p.size();
5252
memcpy(o,&psize,sizeof(size_t)); o += sizeof(size_t);
53-
for (ball_it i = p.begin(); i != p.end(); i++)
54-
o = (**i >> o);
53+
for (ball_t *i : p)
54+
o = (*i >> o);
5555
return o;
5656
};
5757
//! binary deserialization.
@@ -72,8 +72,8 @@ class pocket_t : public std::set<ball_t*,ball_order> {
7272
//! Needed in deserialization.
7373
//! Inform all balls as to their holder.
7474
void repair_holder (node_t* p) {
75-
for (ball_it i = begin(); i != end(); i++) {
76-
(*i)->holder() = p;
75+
for (ball_t *b : *this) {
76+
b->holder() = p;
7777
}
7878
};
7979

@@ -83,8 +83,7 @@ class pocket_t : public std::set<ball_t*,ball_order> {
8383
void repair_owners (const std::unordered_map<name_t,node_t*>& node_name,
8484
std::unordered_map<name_t,ball_t*> *ball_name) {
8585
std::unordered_map<name_t,node_t*>::const_iterator n;
86-
for (ball_it i = begin(); i != end(); i++) {
87-
ball_t *b = *i;
86+
for (ball_t *b : *this) {
8887
if (b->is(green)) {
8988
n = node_name.find(b->uniq);
9089
if (n != node_name.end()) {
@@ -127,17 +126,17 @@ class pocket_t : public std::set<ball_t*,ball_order> {
127126
};
128127
//! retrieve the first ball of the specified color.
129128
ball_t* ball (const color_t c) const {
130-
for (ball_it i = begin(); i != end(); i++) {
131-
if ((*i)->color == c) return *i;
129+
for (ball_t *b : *this) {
130+
if (b->color == c) return b;
132131
}
133132
err("in '%s' (%s line %d): no ball of color %s", // # nocov
134133
__func__,__FILE__,__LINE__,colores[c]); // # nocov
135134
return 0;
136135
};
137136
//! return a pointer to another ball
138137
ball_t* other (const ball_t *b) const {
139-
for (ball_it i = begin(); i != end(); i++) {
140-
if (*i != b) return *i;
138+
for (ball_t *a : *this) {
139+
if (a != b) return a;
141140
}
142141
err("error in '%s' (%s line %d): there is no other.", // # nocov
143142
__func__,__FILE__,__LINE__); // # nocov
@@ -169,8 +168,8 @@ class pocket_t : public std::set<ball_t*,ball_order> {
169168
std::string yaml (std::string tab = "") const {
170169
std::string o = "";
171170
std::string t = tab + " ";
172-
for (ball_it i = begin(); i != end(); i++) {
173-
o += tab + "- " + (*i)->yaml(t);
171+
for (ball_t *b : *this) {
172+
o += tab + "- " + b->yaml(t);
174173
}
175174
return o;
176175
};

0 commit comments

Comments
 (0)