phylopomp
Phylodynamics for POMPs
Loading...
Searching...
No Matches
nodeseq_t Class Reference

A sequence of nodes. More...

#include <nodeseq.h>

Inheritance diagram for nodeseq_t:
Collaboration diagram for nodeseq_t:

Public Member Functions

 ~nodeseq_t (void)
 destructor
 
size_t bytesize (void) const
 size of serialized binary form
 
void sort (void)
 order nodes in order of increasing time
 
pocket_tcolored (color_t col) const
 Get all balls of a color.
 
size_t ntime (slate_t t) const
 Number of distinct timepoints.
 
size_t length (void) const
 Number of nodes in the sequence.
 
node_tposition (int n)
 traverse to nth node, retrieve pointer
 
void move (ball_t *b, node_t *p, node_t *q)
 move ball b from p to q
 
void swap (ball_t *a, ball_t *b)
 swap balls a and b, wherever they lie
 
void attach (node_t *p, node_t *q)
 
void detach (node_t *p)
 
void add (node_t *p, ball_t *a)
 
void drop (ball_t *a)
 
void destroy_node (node_t *p)
 remove a dead root node
 
void weed (void)
 drop all dead roots
 
void comb (void)
 
void trace_lineages (void)
 
string_t yaml (string_t tab="") const
 human/machine-readable info
 
SEXP structure (void) const
 R list description.
 
string_t newick (slate_t t, bool showdeme, bool extended) const
 put genealogy at time t into Newick format.
 

Static Public Member Functions

static bool compare (node_t *p, node_t *q)
 

Private Member Functions

void clean (void)
 clean up: delete all nodes, reset globals
 
void repair_owners (std::unordered_map< name_t, ball_t * > &names)
 
slate_t dawn (void) const
 Earliest time in the sequence.
 
slate_t dusk (void) const
 Latest time in the sequence.
 
void trace_lineage (ball_t *b, name_t u)
 

Friends

raw_toperator>> (const nodeseq_t &G, raw_t *o)
 binary serialization
 
raw_toperator>> (raw_t *o, nodeseq_t &G)
 binary deserialization
 

Detailed Description

A sequence of nodes.

Definition at line 17 of file nodeseq.h.

Constructor & Destructor Documentation

◆ ~nodeseq_t()

nodeseq_t::~nodeseq_t ( void )
inline

destructor

Definition at line 30 of file nodeseq.h.

30 {
31 clean();
32 };
void clean(void)
clean up: delete all nodes, reset globals
Definition nodeseq.h:22
Here is the call graph for this function:

Member Function Documentation

◆ add()

void nodeseq_t::add ( node_t * p,
ball_t * a )
inline

add node p; take as parent the node holding ball a. the deme of p is changed to match that of a

Definition at line 184 of file nodeseq.h.

184 {
185 swap(a,p->green_ball());
186 p->deme() = a->deme();
187 push_back(p);
188 };
name_t deme(void) const
view deme
Definition ball.h:84
name_t deme(void) const
view deme
Definition node.h:96
ball_t * green_ball(void) const
pointer to my green ball
Definition node.h:88
void swap(ball_t *a, ball_t *b)
swap balls a and b, wherever they lie
Definition nodeseq.h:164
Here is the call graph for this function:
Here is the caller graph for this function:

◆ attach()

void nodeseq_t::attach ( node_t * p,
node_t * q )
inline

attach node q as descendant of node p. note that this does not push q into the nodeseq.

Definition at line 174 of file nodeseq.h.

174 {
175 move(q->green_ball(),q,p);
176 };
void move(ball_t *b, node_t *p, node_t *q)
move ball b from p to q
Definition nodeseq.h:159
Here is the call graph for this function:
Here is the caller graph for this function:

◆ bytesize()

size_t nodeseq_t::bytesize ( void ) const
inline

size of serialized binary form

Definition at line 38 of file nodeseq.h.

38 {
39 size_t s = sizeof(size_t);
40 for (node_t *p : *this)
41 s += p->bytesize();
42 return s;
43 };
size_t bytesize(void) const
size of binary serialization
Definition node.h:38
Here is the call graph for this function:
Here is the caller graph for this function:

◆ clean()

void nodeseq_t::clean ( void )
inlineprivate

clean up: delete all nodes, reset globals

Definition at line 22 of file nodeseq.h.

22 {
23 for (node_t *p : *this) delete p;
24 clear();
25 };
Here is the caller graph for this function:

◆ colored()

pocket_t * nodeseq_t::colored ( color_t col) const
inline

Get all balls of a color.

Definition at line 121 of file nodeseq.h.

121 {
122 pocket_t *p = new pocket_t;
123 for (node_t *q : *this) {
124 for (ball_t *b : *q ) {
125 if (b->is(col)) p->insert(b);
126 }
127 }
128 return p;
129 };
Here is the caller graph for this function:

◆ comb()

void nodeseq_t::comb ( void )
inline

drop all inline nodes i.e., those holding just one ball that is green.

Definition at line 226 of file nodeseq.h.

226 {
227 for (node_t *p : *this) {
228 if (p->size() == 1 && p->holds(green)) {
229 swap(p->last_ball(),p->green_ball());
230 }
231 }
232 weed();
233 };
@ green
Definition ball.h:12
void weed(void)
drop all dead roots
Definition nodeseq.h:214
ball_t * last_ball(void) const
retrieve the last ball
Definition pocket.h:126
bool holds(ball_t *b) const
does this node hold the given ball?
Definition pocket.h:109
Here is the call graph for this function:
Here is the caller graph for this function:

◆ compare()

static bool nodeseq_t::compare ( node_t * p,
node_t * q )
inlinestatic

Order relation among nodes. An ancestor node should always come before its descendants. Nodes should be ordered by time, then arbitrarily.

Definition at line 95 of file nodeseq.h.

95 {
96 return (p->slate < q->slate) ||
97 ((p->slate == q->slate) &&
98 ((p==q->green_ball()->holder()) ||
99 ((q!=p->green_ball()->holder()) && (p->uniq < q->uniq))));
100 };
node_t * holder(void) const
in whose pocket do I lie?
Definition ball.h:107
name_t uniq
Definition node.h:32
slate_t slate
Definition node.h:33
Here is the call graph for this function:
Here is the caller graph for this function:

◆ dawn()

slate_t nodeseq_t::dawn ( void ) const
inlineprivate

Earliest time in the sequence.

Definition at line 110 of file nodeseq.h.

110 {
111 return (empty()) ? R_NaReal : front()->slate;
112 };
Here is the caller graph for this function:

◆ destroy_node()

void nodeseq_t::destroy_node ( node_t * p)
inline

remove a dead root node

Definition at line 208 of file nodeseq.h.

208 {
209 assert(p->dead_root());
210 remove(p);
211 delete p;
212 };
bool dead_root(void) const
Definition node.h:124
Here is the call graph for this function:
Here is the caller graph for this function:

◆ detach()

void nodeseq_t::detach ( node_t * p)
inline

detach node p from its parent. note that this does not remove q from the nodeseq.

Definition at line 179 of file nodeseq.h.

179 {
180 move(p->green_ball(),p->parent(),p);
181 };
node_t * parent(void) const
Definition node.h:115
Here is the call graph for this function:
Here is the caller graph for this function:

◆ drop()

void nodeseq_t::drop ( ball_t * a)
inline

drop the black ball 'a' and the node if either (1) the node becomes thereby a dead root, or (2) the node's pocket becomes thereby empty.

Definition at line 192 of file nodeseq.h.

192 {
193 assert(a->is(black));
194 node_t *p = a->holder();
195 if (p->size() > 1) {
196 p->erase(a);
197 delete a;
198 if (p->dead_root()) { // remove isolated root
199 destroy_node(p);
200 }
201 } else {
202 swap(a,p->green_ball());
203 destroy_node(p);
204 drop(a); // recurse
205 }
206 };
@ black
Definition ball.h:12
bool is(color_t c) const
is a given ball of the given color?
Definition ball.h:115
void destroy_node(node_t *p)
remove a dead root node
Definition nodeseq.h:208
void drop(ball_t *a)
Definition nodeseq.h:192
Here is the call graph for this function:
Here is the caller graph for this function:

◆ dusk()

slate_t nodeseq_t::dusk ( void ) const
inlineprivate

Latest time in the sequence.

Definition at line 114 of file nodeseq.h.

114 {
115 return (empty()) ? R_NaReal : back()->slate;
116 }

◆ length()

size_t nodeseq_t::length ( void ) const
inline

Number of nodes in the sequence.

Definition at line 142 of file nodeseq.h.

142 {
143 return this->size();
144 };
Here is the caller graph for this function:

◆ move()

void nodeseq_t::move ( ball_t * b,
node_t * p,
node_t * q )
inline

move ball b from p to q

Definition at line 159 of file nodeseq.h.

159 {
160 assert(b->holder() == p);
161 p->erase(b); q->insert(b);
162 };
void insert(ball_t *a)
insert a ball into the pocket of a node
Definition node.h:156
Here is the call graph for this function:
Here is the caller graph for this function:

◆ newick()

string_t nodeseq_t::newick ( slate_t t,
bool showdeme,
bool extended ) const

put genealogy at time t into Newick format.

Definition at line 78 of file newick.cc.

80{
81 slate_t te = dawn();
82 string_t o = "";
83 for (node_t *p : *this) {
84 if (p->is_root()) {
85 o += p->newick(t,te,showdeme,extended) + ";";
86 }
87 }
88 return o;
89}
bool is_root(void) const
Definition node.h:121
string_t newick(const slate_t &tnow, const slate_t &tpar, bool showdeme, bool extended) const
Newick-format output.
Definition newick.cc:31
slate_t dawn(void) const
Earliest time in the sequence.
Definition nodeseq.h:110
double slate_t
Definition internal.h:53
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ntime()

size_t nodeseq_t::ntime ( slate_t t) const
inline

Number of distinct timepoints.

Definition at line 131 of file nodeseq.h.

131 {
132 size_t count = 1;
133 for (node_t *p : *this) {
134 if (t < p->slate) {
135 t = p->slate;
136 count++;
137 }
138 }
139 return count;
140 };
Here is the caller graph for this function:

◆ position()

node_t * nodeseq_t::position ( int n)
inline

traverse to nth node, retrieve pointer

Definition at line 146 of file nodeseq.h.

146 {
147 int i = 0;
148 node_it k = cbegin();
149 while (i < n && k != cend()) {
150 i++; k++;
151 }
152 assert(k != cend());
153 return *k;
154 };
#define n
Definition lbdp_pomp.c:9
std::list< node_t * >::const_iterator node_it
Definition nodeseq.h:12

◆ repair_owners()

void nodeseq_t::repair_owners ( std::unordered_map< name_t, ball_t * > & names)
inlineprivate

Needed in deserialization. This function repairs the links green balls and their names.

Definition at line 80 of file nodeseq.h.

80 {
81 std::unordered_map<name_t,ball_t*>::const_iterator n;
82 for (node_t *p : *this) {
83 n = names.find(p->uniq);
84 assert(n != names.end());
85 ball_t *b = n->second;
86 p->green_ball() = b;
87 }
88 };
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sort()

void nodeseq_t::sort ( void )
inline

order nodes in order of increasing time

Definition at line 103 of file nodeseq.h.

103 {
104 std::list<node_t*>::sort(compare);
105 };
static bool compare(node_t *p, node_t *q)
Definition nodeseq.h:95
Here is the call graph for this function:
Here is the caller graph for this function:

◆ structure()

SEXP nodeseq_t::structure ( void ) const

R list description.

Definition at line 70 of file structure.cc.

72{
73 SEXP Nodes;
74 PROTECT(Nodes = NEW_LIST(size()));
75 int k = 0;
76 for (node_t *p : *this) {
77 SET_ELEMENT(Nodes,k++,p->structure());
78 }
79 UNPROTECT(1);
80 return Nodes;
81}
SEXP structure(void) const
R list description.
Definition structure.cc:55
Here is the call graph for this function:
Here is the caller graph for this function:

◆ swap()

void nodeseq_t::swap ( ball_t * a,
ball_t * b )
inline

swap balls a and b, wherever they lie

Definition at line 164 of file nodeseq.h.

164 {
165 node_t *p = a->holder();
166 node_t *q = b->holder();
167 if (p != q) {
168 p->erase(a); q->insert(a);
169 q->erase(b); p->insert(b);
170 }
171 };
Here is the call graph for this function:
Here is the caller graph for this function:

◆ trace_lineage()

void nodeseq_t::trace_lineage ( ball_t * b,
name_t u )
inlineprivate

trace back a single lineage. this results in the deme slot for all green balls along the lineage of 'b' begin replaced by the lineage of 'b'.

Definition at line 240 of file nodeseq.h.

240 {
241 node_t *p = b->holder();
242 while (p->lineage() == null_lineage) {
243 p->lineage() = u;
244 p = p->parent();
245 }
246 };
name_t lineage(void) const
view lineage
Definition node.h:104
static const name_t null_lineage
Definition node.h:11
Here is the call graph for this function:
Here is the caller graph for this function:

◆ trace_lineages()

void nodeseq_t::trace_lineages ( void )
inline

trace back all sample lineages. this results in the deme slots of all green balls being replaced by the unique names of the lineages they trace.

Definition at line 253 of file nodeseq.h.

253 {
254 // we trace each lineage in turn.
255 // because we move from early to late,
256 // the order is guaranteed to be valid.
257 name_t u = 0;
258 for (node_t *p : *this ) {
259 for (ball_t *b : *p) {
260 if (b->color==blue) {
261 trace_lineage(b,u);
262 u++;
263 }
264 }
265 }
266 };
@ blue
Definition ball.h:12
void trace_lineage(ball_t *b, name_t u)
Definition nodeseq.h:240
size_t name_t
Definition internal.h:54
Here is the call graph for this function:
Here is the caller graph for this function:

◆ weed()

void nodeseq_t::weed ( void )
inline

drop all dead roots

Definition at line 214 of file nodeseq.h.

214 {
215 node_nit j = begin();
216 while (j != end()) {
217 if ((*j)->dead_root()) {
218 destroy_node(*(j++));
219 } else {
220 j++;
221 }
222 }
223 };
std::list< node_t * >::iterator node_nit
Definition nodeseq.h:13
Here is the call graph for this function:
Here is the caller graph for this function:

◆ yaml()

string_t nodeseq_t::yaml ( string_t tab = "") const

human/machine-readable info

Definition at line 51 of file yaml.cc.

53{
54 string_t o = "";
55 string_t t = tab + " ";
56 for (node_t *p : *this) {
57 o += tab + "- " + p->yaml(t);
58 }
59 return o;
60}
string_t yaml(string_t tab="") const
human/machine-readable info
Definition yaml.cc:37
Here is the call graph for this function:
Here is the caller graph for this function:

Friends And Related Symbol Documentation

◆ operator>> [1/2]

raw_t * operator>> ( const nodeseq_t & G,
raw_t * o )
friend

binary serialization

Definition at line 45 of file nodeseq.h.

45 {
46 size_t nnode = G.size();
47 memcpy(o,&nnode,sizeof(size_t)); o += sizeof(size_t);
48 for (node_t *p : G) {
49 o = (*p >> o);
50 }
51 return o;
52 };

◆ operator>> [2/2]

raw_t * operator>> ( raw_t * o,
nodeseq_t & G )
friend

binary deserialization

Definition at line 54 of file nodeseq.h.

54 {
55 G.clean();
56 std::unordered_map<name_t,node_t*> node_names;
57 std::unordered_map<name_t,ball_t*> ball_names;
58 size_t nnode = 0;
59 memcpy(&nnode,o,sizeof(size_t)); o += sizeof(size_t);
60 node_names.reserve(nnode);
61 ball_names.reserve(nnode);
62 for (size_t i = 0; i < nnode; i++) {
63 node_t *p = new node_t();
64 o = (o >> *p);
65 G.push_back(p);
66 node_names.insert({p->uniq,p});
67 }
68 for (node_t *q : G) {
69 q->repair_owners(node_names,&ball_names);
70 }
71 G.repair_owners(ball_names);
72 G.trace_lineages();
73 return o;
74 };

The documentation for this class was generated from the following files: