phylopomp
Phylodynamics for POMPs
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 More...
 
size_t bytesize (void) const
 size of serialized binary form More...
 
nodeseq_toperator+= (nodeseq_t &other)
 merge two node sequences More...
 
void sort (void)
 order nodes in order of increasing time More...
 
pocket_tcolored (color_t col) const
 Get all balls of a color. More...
 
size_t ntime (slate_t t) const
 Number of distinct timepoints. More...
 
size_t length (void) const
 Number of nodes in the sequence. More...
 
node_tposition (int n)
 traverse to nth node, retrieve pointer More...
 
void swap (ball_t *a, ball_t *b)
 swap balls a and b, wherever they lie More...
 
void add (node_t *p, ball_t *a)
 
void drop (ball_t *a)
 
void destroy_node (node_t *p)
 remove a dead root node More...
 
void comb (void)
 
void trace_lineages (void)
 
std::string describe (void) const
 human-readable info More...
 
virtual std::string yaml (std::string tab="") const
 human- & machine-readable info More...
 
SEXP structure (void) const
 R list description. More...
 
std::string newick (slate_t t) const
 put genealogy at time t into Newick format. More...
 

Private Member Functions

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

Static Private Member Functions

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

Friends

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

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 179 of file nodeseq.h.

179  {
180  swap(a,p->green_ball());
181  p->deme() = a->deme();
182  push_back(p);
183  };
name_t deme(void) const
view deme
Definition: ball.h:86
ball_t * green_ball(void) const
pointer to my green ball
Definition: node.h:88
name_t deme(void) const
view deme
Definition: node.h:96
void swap(ball_t *a, ball_t *b)
swap balls a and b, wherever they lie
Definition: nodeseq.h:169
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_it i = begin(); i != end(); i++)
43  s += (*i)->bytesize();
44  return s;
45  };
std::list< node_t * >::const_iterator node_it
Definition: nodeseq.h:14
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_it i = begin(); i != end(); i++) delete *i;
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 129 of file nodeseq.h.

129  {
130  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);
134  }
135  }
136  return p;
137  };
A pocket is a set of balls.
Definition: pocket.h:32
std::set< ball_t *, ball_order >::const_iterator ball_it
Definition: pocket.h:26
Here is the caller graph for this function:

◆ comb()

void nodeseq_t::comb ( void  )
inline

pass through the sequence, dropping superfluous nodes i.e., those holding just one ball that is green.

Definition at line 210 of file nodeseq.h.

210  {
211  for (node_rev_it i = crbegin(); i != crend(); i++) {
212  if ((*i)->size() == 1 && (*i)->holds(green)) {
213  swap((*i)->last_ball(),(*i)->green_ball());
214  }
215  }
216  node_nit j = begin();
217  while (j != end()) {
218  if ((*j)->dead_root()) {
219  destroy_node(*(j++));
220  } else {
221  j++;
222  }
223  }
224  };
@ green
Definition: ball.h:14
void destroy_node(node_t *p)
remove a dead root node
Definition: nodeseq.h:203
std::list< node_t * >::const_reverse_iterator node_rev_it
Definition: nodeseq.h:16
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:

◆ compare()

static bool nodeseq_t::compare ( node_t p,
node_t q 
)
inlinestaticprivate

Order relation among nodes. Nodes should be ordered by time, then by unique name.

Definition at line 97 of file nodeseq.h.

97  {
98  return (p->slate < q->slate) ||
99  ((p->slate == q->slate) && (p->uniq < q->uniq));
100  };
name_t uniq
Definition: node.h:30
slate_t slate
Definition: node.h:35
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 118 of file nodeseq.h.

118  {
119  return (empty()) ? R_NaReal : front()->slate;
120  };
Here is the caller graph for this function:

◆ describe()

std::string nodeseq_t::describe ( void  ) const
inline

human-readable info

Definition at line 264 of file nodeseq.h.

264  {
265  std::string o = "";
266  for (node_it p = begin(); p != end(); p++) {
267  o += (*p)->describe();
268  }
269  return o;
270  };
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 203 of file nodeseq.h.

203  {
204  assert(p->dead_root());
205  remove(p);
206  delete p;
207  };
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:

◆ 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 187 of file nodeseq.h.

187  {
188  assert(a->is(black));
189  node_t *p = a->holder();
190  if (p->size() > 1) {
191  p->erase(a);
192  delete a;
193  if (p->dead_root()) { // remove isolated root
194  destroy_node(p);
195  }
196  } else {
197  swap(a,p->green_ball());
198  destroy_node(p);
199  drop(a); // recurse
200  }
201  };
@ black
Definition: ball.h:14
bool is(color_t c) const
is a given ball of the given color?
Definition: ball.h:117
node_t * holder(void) const
in whose pocket do I lie?
Definition: ball.h:109
Encodes a genealogical node.
Definition: node.h:23
void drop(ball_t *a)
Definition: nodeseq.h:187
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 122 of file nodeseq.h.

122  {
123  return (empty()) ? R_NaReal : back()->slate;
124  }

◆ length()

size_t nodeseq_t::length ( void  ) const
inline

Number of nodes in the sequence.

Definition at line 150 of file nodeseq.h.

150  {
151  size_t count = 0;
152  for (node_it i = begin(); i != end(); i++) count++;
153  return count;
154  };
Here is the caller graph for this function:

◆ newick()

std::string nodeseq_t::newick ( slate_t  t) const
inline

put genealogy at time t into Newick format.

Definition at line 292 of file nodeseq.h.

292  {
293  slate_t te = dawn();
294  std::string o = "";
295  for (node_it i = begin(); i != end(); i++) {
296  if ((*i)->is_root()) {
297  o += (*i)->newick(t,te) + ";";
298  }
299  }
300  return o;
301  };
slate_t dawn(void) const
Earliest time in the sequence.
Definition: nodeseq.h:118
double slate_t
Definition: internal.h:44
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 139 of file nodeseq.h.

139  {
140  size_t count = 1;
141  for (node_it i = begin(); i != end(); i++) {
142  if (t < (*i)->slate) {
143  t = (*i)->slate;
144  count++;
145  }
146  }
147  return count;
148  };
Here is the caller graph for this function:

◆ operator+=()

nodeseq_t& nodeseq_t::operator+= ( nodeseq_t other)
inline

merge two node sequences

Definition at line 105 of file nodeseq.h.

105  {
106  merge(other,compare);
107  return *this;
108  };
static bool compare(node_t *p, node_t *q)
Definition: nodeseq.h:97
Here is the call graph for this function:

◆ position()

node_t* nodeseq_t::position ( int  n)
inline

traverse to nth node, retrieve pointer

Definition at line 156 of file nodeseq.h.

156  {
157  int i = 0;
158  node_it k = cbegin();
159  while (i < n && k != cend()) {
160  i++; k++;
161  }
162  assert(k != cend());
163  return *k;
164  };
#define n
Definition: lbdp_pomp.c:8

◆ 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 82 of file nodeseq.h.

82  {
83  std::unordered_map<name_t,ball_t*>::const_iterator n;
84  for (node_it i = begin(); i != end(); i++) {
85  node_t *p = *i;
86  n = names.find(p->uniq);
87  assert(n != names.end());
88  ball_t *b = n->second;
89  p->green_ball() = b;
90  }
91  };
Balls function as pointers.
Definition: ball.h:29
Here is the call 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  };
Here is the call graph for this function:
Here is the caller graph for this function:

◆ structure()

SEXP nodeseq_t::structure ( void  ) const
inline

R list description.

Definition at line 281 of file nodeseq.h.

281  {
282  SEXP Nodes;
283  PROTECT(Nodes = NEW_LIST(size()));
284  int k = 0;
285  for (node_it i = begin(); i != end(); i++) {
286  SET_ELEMENT(Nodes,k++,(*i)->structure());
287  }
288  UNPROTECT(1);
289  return Nodes;
290  };
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 169 of file nodeseq.h.

169  {
170  node_t *p = a->holder();
171  node_t *q = b->holder();
172  if (p != q) {
173  p->erase(a); q->insert(a); a->holder() = q;
174  q->erase(b); p->insert(b); b->holder() = p;
175  }
176  };
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 231 of file nodeseq.h.

231  {
232  node_t *p = b->holder();
233  while (p->lineage() == null_lineage) {
234  p->lineage() = u;
235  p = p->parent();
236  }
237  };
name_t lineage(void) const
view lineage
Definition: node.h:104
node_t * parent(void) const
Definition: node.h:115
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 244 of file nodeseq.h.

244  {
245  // we trace each lineage in turn.
246  // because we move from early to late,
247  // the order is guaranteed to be valid.
248  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;
253  if (b->color==blue) {
254  trace_lineage(b,u);
255  u++;
256  }
257  }
258  }
259  };
@ blue
Definition: ball.h:14
color_t color
Definition: ball.h:38
void trace_lineage(ball_t *b, name_t u)
Definition: nodeseq.h:231
size_t name_t
Definition: internal.h:45
Here is the call graph for this function:
Here is the caller graph for this function:

◆ yaml()

virtual std::string nodeseq_t::yaml ( std::string  tab = "") const
inlinevirtual

human- & machine-readable info

Reimplemented in genealogy_t.

Definition at line 272 of file nodeseq.h.

272  {
273  std::string o = "";
274  std::string t = tab + " ";
275  for (node_it p = begin(); p != end(); p++) {
276  o += tab + "- " + (*p)->yaml(t);
277  }
278  return o;
279  };
Here is the caller graph for this function:

Friends And Related Function 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_it i = G.begin(); i != G.end(); i++) {
51  o = (**i >> 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.insert({p->uniq,p});
69  }
70  for (node_it i = G.begin(); i != G.end(); i++) {
71  (*i)->repair_owners(node_names,&ball_names);
72  }
73  G.repair_owners(ball_names);
74  G.trace_lineages();
75  return o;
76  };
void repair_owners(std::unordered_map< name_t, ball_t * > &names)
Definition: nodeseq.h:82
void trace_lineages(void)
Definition: nodeseq.h:244

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