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, 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 (std::unordered_map< name_t, ball_t * > &names)
 
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 173 of file nodeseq.h.

173 {
174 swap(a,p->green_ball());
175 p->deme() = a->deme();
176 push_back(p);
177 };
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:153
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 163 of file nodeseq.h.

163 {
164 move(q->green_ball(),q,p);
165 };
void move(ball_t *b, node_t *p, node_t *q)
move ball b from p to q
Definition nodeseq.h:148
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 110 of file nodeseq.h.

110 {
111 pocket_t *p = new pocket_t;
112 for (node_t *q : *this) {
113 for (ball_t *b : *q ) {
114 if (b->is(col)) p->insert(b);
115 }
116 }
117 return p;
118 };
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 215 of file nodeseq.h.

215 {
216 for (node_t *p : *this) {
217 if (p->size() == 1 && p->holds(green)) {
218 swap(p->last_ball(),p->green_ball());
219 }
220 }
221 weed();
222 };
@ green
Definition ball.h:12
void weed(void)
drop all dead roots
Definition nodeseq.h:203
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:

◆ destroy_node()

void nodeseq_t::destroy_node ( node_t * p)
inline

remove a dead root node

Definition at line 197 of file nodeseq.h.

197 {
198 assert(p->dead_root());
199 remove(p);
200 delete p;
201 };
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 168 of file nodeseq.h.

168 {
169 move(p->green_ball(),p->parent(),p);
170 };
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 181 of file nodeseq.h.

181 {
182 assert(a->is(black));
183 node_t *p = a->holder();
184 if (p->size() > 1) {
185 p->erase(a);
186 delete a;
187 if (p->dead_root()) { // remove isolated root
188 destroy_node(p);
189 }
190 } else {
191 swap(a,p->green_ball());
192 destroy_node(p);
193 drop(a); // recurse
194 }
195 };
@ 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:197
void drop(ball_t *a)
Definition nodeseq.h:181
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 131 of file nodeseq.h.

131 {
132 return this->size();
133 };
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 148 of file nodeseq.h.

148 {
149 assert(b->holder() == p);
150 p->erase(b); q->insert(b);
151 };
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,
slate_t te,
bool showdeme,
bool extended ) const

put genealogy at time t into Newick format.

Definition at line 95 of file newick.cc.

97{
98 string_t o = "";
99 for (node_t *p : *this) {
100 if (p->is_root()) {
101 o += p->newick(t,te,showdeme,extended) + ";";
102 }
103 }
104 return o;
105}
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:48
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 120 of file nodeseq.h.

120 {
121 size_t count = 1;
122 for (node_t *p : *this) {
123 if (t < p->slate) {
124 t = p->slate;
125 count++;
126 }
127 }
128 return count;
129 };
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 135 of file nodeseq.h.

135 {
136 int i = 0;
137 node_it k = cbegin();
138 while (i < n && k != cend()) {
139 i++; k++;
140 }
141 assert(k != cend());
142 return *k;
143 };
#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 153 of file nodeseq.h.

153 {
154 node_t *p = a->holder();
155 node_t *q = b->holder();
156 if (p != q) {
157 p->erase(a); q->insert(a);
158 q->erase(b); p->insert(b);
159 }
160 };
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 229 of file nodeseq.h.

229 {
230 node_t *p = b->holder();
231 while (p->lineage() == null_lineage) {
232 p->lineage() = u;
233 p = p->parent();
234 }
235 };
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 242 of file nodeseq.h.

242 {
243 // we trace each lineage in turn.
244 // because we move from early to late,
245 // the order is guaranteed to be valid.
246 name_t u = 0;
247 for (node_t *p : *this ) {
248 for (ball_t *b : *p) {
249 if (b->color==blue) {
250 trace_lineage(b,u);
251 u++;
252 }
253 }
254 }
255 };
@ blue
Definition ball.h:12
void trace_lineage(ball_t *b, name_t u)
Definition nodeseq.h:229
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 203 of file nodeseq.h.

203 {
204 node_nit j = begin();
205 while (j != end()) {
206 if ((*j)->dead_root()) {
207 destroy_node(*(j++));
208 } else {
209 j++;
210 }
211 }
212 };
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: