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)
 
std::unordered_map< name_t, std::vector< node_t * > > children_map (void) const
 map nodes onto vector of children
 
std::vector< node_t * > sorted_roots (const std::unordered_map< name_t, slate_t > &height) const
 collect root nodes and sort them in order of decreasing subtree height
 
std::vector< node_t * > ladderize (std::unordered_map< name_t, std::vector< node_t * > > &children) const
 
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, slate_t te, 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 (const std::unordered_map< name_t, ball_t * > &names)
 
std::unordered_map< name_t, node_t * > node_map (void) const
 map node names onto pointers
 
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 19 of file nodeseq.h.

Constructor & Destructor Documentation

◆ ~nodeseq_t()

nodeseq_t::~nodeseq_t ( void )
inline

destructor

Definition at line 32 of file nodeseq.h.

32 {
33 clean();
34 };
void clean(void)
clean up: delete all nodes, reset globals
Definition nodeseq.h:24
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 181 of file nodeseq.h.

181 {
182 swap(a,p->green_ball());
183 p->deme() = a->deme();
184 push_back(p);
185 };
name_t deme(void) const
view deme
Definition ball.h:84
name_t deme(void) const
view deme
Definition node.h:98
ball_t * green_ball(void) const
pointer to my green ball
Definition node.h:90
void swap(ball_t *a, ball_t *b)
swap balls a and b, wherever they lie
Definition nodeseq.h:161
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 171 of file nodeseq.h.

171 {
172 move(q->green_ball(),q,p);
173 };
void move(ball_t *b, node_t *p, node_t *q)
move ball b from p to q
Definition nodeseq.h:156
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 40 of file nodeseq.h.

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

◆ children_map()

std::unordered_map< name_t, std::vector< node_t * > > nodeseq_t::children_map ( void ) const
inline

map nodes onto vector of children

Definition at line 249 of file nodeseq.h.

249 {
250 std::unordered_map<name_t, std::vector<node_t*>> children;
251 children.reserve(size());
252 // initialise every node with an empty vector
253 for (node_t* p : *this)
254 children[p->uniq];
255 for (node_t* p : *this) {
256 if (!p->is_root())
257 children[p->parent()->uniq].push_back(p);
258 }
259 return children;
260 };
node_t * parent(void) const
Definition node.h:117
bool is_root(void) const
Definition node.h:120
name_t uniq
Definition node.h:34
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 24 of file nodeseq.h.

24 {
25 for (node_t *p : *this) delete p;
26 clear();
27 };
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 118 of file nodeseq.h.

118 {
119 pocket_t *p = new pocket_t;
120 for (node_t *q : *this) {
121 for (ball_t *b : *q ) {
122 if (b->is(col)) p->insert(b);
123 }
124 }
125 return p;
126 };
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 223 of file nodeseq.h.

223 {
224 for (node_t *p : *this) {
225 if (p->size() == 1 && p->holds(green)) {
226 swap(p->last_ball(),p->green_ball());
227 }
228 }
229 weed();
230 };
@ green
Definition ball.h:12
void weed(void)
drop all dead roots
Definition nodeseq.h:211
ball_t * last_ball(void) const
retrieve the last ball
Definition pocket.h:118
bool holds(ball_t *b) const
does this node hold the given ball?
Definition pocket.h:101
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 103 of file nodeseq.h.

103 {
104 return (p->slate < q->slate) ||
105 ((p->slate == q->slate) &&
106 ((p == q->parent()) ||
107 ((q != p->parent()) && (p->uniq < q->uniq))));
108 };
slate_t slate
Definition node.h:35
Here is the call graph for this function:
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 205 of file nodeseq.h.

205 {
206 assert(p->dead_root());
207 remove(p);
208 delete p;
209 };
bool dead_root(void) const
Definition node.h:123
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 176 of file nodeseq.h.

176 {
177 move(p->green_ball(),p->parent(),p);
178 };
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 189 of file nodeseq.h.

189 {
190 assert(a->is(black));
191 node_t *p = a->holder();
192 if (p->size() > 1) {
193 p->erase(a);
194 delete a;
195 if (p->dead_root()) { // remove isolated root
196 destroy_node(p);
197 }
198 } else {
199 swap(a,p->green_ball());
200 destroy_node(p);
201 drop(a); // recurse
202 }
203 };
@ black
Definition ball.h:12
node_t * holder(void) const
in whose pocket do I lie?
Definition ball.h:107
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:205
void drop(ball_t *a)
Definition nodeseq.h:189
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ladderize()

std::vector< node_t * > nodeseq_t::ladderize ( std::unordered_map< name_t, std::vector< node_t * > > & children) const
inline

ladderize the tree by sorting each vector in 'children' according to decreasing subtree height. Return the root nodes, sorted in the same way.

Definition at line 282 of file nodeseq.h.

285 {
286 std::unordered_map<name_t, slate_t> height;
287 height.reserve(size());
288 for (auto it = rbegin(); it != rend(); ++it) {
289 node_t* p = *it;
290 auto& ch = children.at(p->uniq);
291 if (ch.empty()) {
292 height[p->uniq] = p->slate;
293 } else {
294 std::sort(ch.begin(), ch.end(),
295 [&height](node_t* a, node_t* b) {
296 return height.at(a->uniq) > height.at(b->uniq);
297 });
298 height[p->uniq] = height.at(ch[0]->uniq);
299 }
300 }
301 return sorted_roots(height);
302 };
std::vector< node_t * > sorted_roots(const std::unordered_map< name_t, slate_t > &height) const
collect root nodes and sort them in order of decreasing subtree height
Definition nodeseq.h:265
Here is the call graph for this function:
Here is the caller graph for this function:

◆ length()

size_t nodeseq_t::length ( void ) const
inline

Number of nodes in the sequence.

Definition at line 139 of file nodeseq.h.

139 {
140 return this->size();
141 };
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 156 of file nodeseq.h.

156 {
157 assert(b->holder() == p);
158 p->erase(b); q->insert(b);
159 };
void insert(ball_t *a)
insert a ball into the pocket of a node
Definition node.h:167
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,
slate_t te,
bool showdeme,
bool extended ) const

put genealogy at time t into Newick format.

Definition at line 92 of file newick.cc.

94{
95 string_t o = "";
96 for (node_t *p : *this) {
97 if (p->is_root()) {
98 o += p->newick(t,te,showdeme,extended) + ";";
99 }
100 }
101 return o;
102}
string_t newick(const slate_t &tnow, const slate_t &tpar, bool showdeme, bool extended) const
Newick-format output.
Definition newick.cc:46
Here is the call graph for this function:
Here is the caller graph for this function:

◆ node_map()

std::unordered_map< name_t, node_t * > nodeseq_t::node_map ( void ) const
inlineprivate

map node names onto pointers

Definition at line 90 of file nodeseq.h.

90 {
91 std::unordered_map<name_t, node_t*> m;
92 m.reserve(size());
93 for (node_t* p : *this)
94 m[p->uniq] = p;
95 return m;
96 };

◆ ntime()

size_t nodeseq_t::ntime ( slate_t t) const
inline

Number of distinct timepoints.

Definition at line 128 of file nodeseq.h.

128 {
129 size_t count = 1;
130 for (node_t *p : *this) {
131 if (t < p->slate) {
132 t = p->slate;
133 count++;
134 }
135 }
136 return count;
137 };
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 143 of file nodeseq.h.

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

◆ repair_owners()

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

Repair the links green balls and their names. Needed in deserialization.

Definition at line 82 of file nodeseq.h.

82 {
83 for (node_t *p : *this) {
84 p->green_ball() = names.at(p->uniq);
85 }
86 };
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 111 of file nodeseq.h.

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

◆ sorted_roots()

std::vector< node_t * > nodeseq_t::sorted_roots ( const std::unordered_map< name_t, slate_t > & height) const
inline

collect root nodes and sort them in order of decreasing subtree height

Definition at line 264 of file nodeseq.h.

267 {
268 std::vector<node_t*> roots;
269 for (node_t* p : *this)
270 if (p->is_root()) roots.push_back(p);
271 std::sort(roots.begin(), roots.end(),
272 [&height](node_t* a, node_t* b) {
273 return height.at(a->uniq) > height.at(b->uniq);
274 });
275 return roots;
276 };
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 161 of file nodeseq.h.

161 {
162 node_t *p = a->holder();
163 node_t *q = b->holder();
164 if (p != q) {
165 p->erase(a); q->insert(a);
166 q->erase(b); p->insert(b);
167 }
168 };
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 237 of file nodeseq.h.

237 {
238 node_t *p = b->holder();
239 while (p->lineage() == null_lineage) {
240 p->lineage() = u;
241 p = p->parent();
242 }
243 };
name_t lineage(void) const
view lineage
Definition node.h:106
static const name_t null_lineage
Definition node.h:13
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 309 of file nodeseq.h.

309 {
310 // we trace each lineage in turn.
311 // because we move from early to late,
312 // the order is guaranteed to be valid.
313 name_t u = 0;
314 for (node_t *p : *this ) {
315 for (ball_t *b : *p) {
316 if (b->color==blue) {
317 trace_lineage(b,u);
318 u++;
319 }
320 }
321 }
322 };
@ blue
Definition ball.h:12
void trace_lineage(ball_t *b, name_t u)
Definition nodeseq.h:237
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 211 of file nodeseq.h.

211 {
212 node_nit j = begin();
213 while (j != end()) {
214 if ((*j)->dead_root()) {
215 destroy_node(*(j++));
216 } else {
217 j++;
218 }
219 }
220 };
std::list< node_t * >::iterator node_nit
Definition nodeseq.h:15
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 47 of file nodeseq.h.

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

◆ operator>> [2/2]

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

binary deserialization

Definition at line 56 of file nodeseq.h.

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

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