phylopomp
Phylodynamics for POMPs
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
node.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 // NODE CLASS
3 
4 #ifndef _NODE_H_
5 #define _NODE_H_
6 
7 #include <string>
8 #include <cstring>
9 #include "ball.h"
10 #include "pocket.h"
11 #include "internal.h"
12 
13 static const name_t null_lineage = name_t(NA_INTEGER);
14 
16 
23 class node_t : public pocket_t {
24 
25 private:
26 
29 
30  void clean (void) { };
31 
32 public:
33 
36 
37 public:
38 
40  size_t bytesize (void) const {
41  return 2*sizeof(name_t) + sizeof(slate_t)
43  };
45  friend raw_t* operator>> (const node_t &p, raw_t *o) {
46  name_t buf[2] = {p.uniq, p._lineage};
47  memcpy(o,buf,sizeof(buf)); o += sizeof(buf);
48  memcpy(o,&p.slate,sizeof(slate_t)); o += sizeof(slate_t);
49  return reinterpret_cast<const pocket_t&>(p) >> o;
50  };
52  friend raw_t* operator>> (raw_t *o, node_t &p) {
53  p.clean();
54  name_t buf[2];
55  memcpy(buf,o,sizeof(buf)); o += sizeof(buf);
56  memcpy(&p.slate,o,sizeof(slate_t)); o += sizeof(slate_t);
57  p.uniq = buf[0]; p._lineage = buf[1];
58  o = (o >> reinterpret_cast<pocket_t&>(p));
59  p.repair_holder(&p);
60  return o;
61  };
62 
63 public:
64 
66  node_t (name_t u = 0, slate_t t = R_NaReal) {
67  uniq = u;
68  slate = t;
69  _green_ball = 0;
71  };
73  node_t (const node_t &p) = delete;
75  node_t (node_t && p) = delete;
77  node_t & operator= (const node_t & p) = delete;
79  node_t & operator= (node_t && p) = delete;
81  ~node_t (void) {
82  clean();
83  };
84 
85 public:
86 
88  ball_t* green_ball (void) const {
89  return _green_ball;
90  };
92  ball_t*& green_ball (void) {
93  return _green_ball;
94  };
96  name_t deme (void) const {
97  return _green_ball->deme();
98  };
100  name_t& deme (void) {
101  return _green_ball->deme();
102  };
104  name_t lineage (void) const {
105  return _lineage;
106  };
108  name_t lineage (const ball_t *g) const {
109  return g->owner()->lineage();
110  };
112  name_t& lineage (void) {
113  return _lineage;
114  };
115  node_t* parent (void) const {
116  return _green_ball->holder();
117  };
118  bool holds_own (void) const {
119  return (_green_ball->holder() == this);
120  };
121  bool is_root (void) const {
122  return holds_own();
123  };
124  bool dead_root (void) const {
125  return holds_own() && size()==1;
126  };
127 
128 public:
129 
131  int nchildren (void) const {
132  int n = 0;
133  for (ball_it i = begin(); i != end(); i++) {
134  switch ((*i)->color) {
135  case green: case black:
136  n++;
137  break;
138  default:
139  break;
140  }
141  }
142  if (holds_own()) n--;
143  return n;
144  };
151  void lineage_incr (int *incr, int *sat, int *etype) const {
152  const name_t d = deme();
153  incr[d]--;
154  for (ball_it i = cbegin(); i != cend(); i++) {
155  ball_t *b = *i;
156  switch (b->color) {
157  case green: case black:
158  incr[b->deme()]++;
159  sat[b->deme()]++;
160  break;
161  default:
162  break;
163  }
164  }
165  if (holds_own()) {
166  sat[d]--;
167  etype[d] = -1;
168  } else if (holds(blue)) {
169  etype[d] = 1;
170  } else {
171  etype[d] = 2;
172  }
173  };
174 
175 public:
176 
178  std::string describe (void) const {
179  std::string s = "node("
180  + std::to_string(uniq)
181  + "," + std::to_string(deme()) + ",";
182  if (lineage() != null_lineage) {
183  s += std::to_string(lineage());
184  }
185  s += ")" + pocket_t::describe();
186  s += ", t = " + std::to_string(slate) + "\n";
187  return s;
188  };
190  std::string yaml (std::string tab = "") const {
191  std::string t = tab + " ";
192  std::string o = "name: " + std::to_string(uniq) + "\n"
193  + tab + "time: " + std::to_string(slate) + "\n"
194  + tab + "deme: " + std::to_string(deme()) + "\n";
195  if (lineage() != null_lineage) {
196  o += tab + "lineage: " + std::to_string(lineage()) + "\n";
197  }
198  o += tab + "pocket:\n" + pocket_t::yaml(tab);
199  return o;
200  };
202  SEXP structure (void) const {
203  SEXP O, On;
204  PROTECT(O = NEW_LIST(4));
205  PROTECT(On = NEW_CHARACTER(4));
206  set_list_elem(O,On,ScalarInteger(int(uniq)),"name",0);
207  set_list_elem(O,On,ScalarReal(double(slate)),"time",1);
208  set_list_elem(O,On,ScalarInteger(int(deme())),"deme",2);
209  set_list_elem(O,On,pocket_t::structure(),"pocket",3);
210  SET_NAMES(O,On);
211  UNPROTECT(2);
212  return O;
213  };
215  std::string newick (const slate_t& tnow, const slate_t& tpar) const {
216  std::string o1 = "", o2 = "", o3 = "";
217  int n = nchildren();
218  if (n > 0) {
219  o1 = "("; o3 = ")";
220  }
221  if (holds(blue)) {
222  o3 += "b_";
223  } else if (holds_own()) {
224  o3 += "m_";
225  } else {
226  o3 += "g_";
227  }
228  n = 0;
229  for (ball_it i = begin(); i != end(); i++) {
230  ball_t *b = *i;
231  node_t *p = 0;
232  switch (b->color) {
233  case green:
234  p = b->child();
235  if (p != this) {
236  if (n++ > 0) o2 += ",";
237  o2 += p->newick(tnow,slate);
238  }
239  break;
240  case black:
241  if (n++ > 0) o2 += ",";
242  o2 += b->newick(tnow-slate);
243  break;
244  case blue:
245  break;
246  }
247  }
248  return o1 + o2 + o3
249  + std::to_string(deme())
250  + "_" + std::to_string(uniq)
251  + ":" + std::to_string(slate - tpar);
252  };
253 };
254 
255 #endif
@ green
Definition: ball.h:14
@ black
Definition: ball.h:14
@ blue
Definition: ball.h:14
Balls function as pointers.
Definition: ball.h:29
node_t * child(void) const
a child is the owner of a green ball
Definition: ball.h:104
node_t * owner(void) const
view owner of a green ball
Definition: ball.h:94
name_t deme(void) const
view deme
Definition: ball.h:86
std::string newick(const slate_t &t) const
element of a newick representation
Definition: ball.h:177
node_t * holder(void) const
in whose pocket do I lie?
Definition: ball.h:109
color_t color
Definition: ball.h:38
Encodes a genealogical node.
Definition: node.h:23
node_t & operator=(const node_t &p)=delete
copy assignment operator
void clean(void)
Definition: node.h:30
friend raw_t * operator>>(const node_t &p, raw_t *o)
binary serialization of node_t
Definition: node.h:45
name_t lineage(void) const
view lineage
Definition: node.h:104
size_t bytesize(void) const
size of binary serialization
Definition: node.h:40
name_t & lineage(void)
set lineage
Definition: node.h:112
bool dead_root(void) const
Definition: node.h:124
node_t * parent(void) const
Definition: node.h:115
name_t _lineage
Definition: node.h:28
ball_t * _green_ball
Definition: node.h:27
bool is_root(void) const
Definition: node.h:121
node_t(const node_t &p)=delete
copy constructor
name_t & deme(void)
set deme
Definition: node.h:100
std::string newick(const slate_t &tnow, const slate_t &tpar) const
Newick format.
Definition: node.h:215
name_t lineage(const ball_t *g) const
view lineage associated with a green ball
Definition: node.h:108
SEXP structure(void) const
R list description.
Definition: node.h:202
~node_t(void)
destructor
Definition: node.h:81
std::string describe(void) const
human-readable info
Definition: node.h:178
ball_t * green_ball(void) const
pointer to my green ball
Definition: node.h:88
std::string yaml(std::string tab="") const
machine-readable info
Definition: node.h:190
node_t(name_t u=0, slate_t t=R_NaReal)
basic constructor for node class
Definition: node.h:66
name_t deme(void) const
view deme
Definition: node.h:96
name_t uniq
Definition: node.h:30
ball_t *& green_ball(void)
set green ball
Definition: node.h:92
slate_t slate
Definition: node.h:35
node_t(node_t &&p)=delete
move constructor
void lineage_incr(int *incr, int *sat, int *etype) const
Definition: node.h:151
bool holds_own(void) const
Definition: node.h:118
int nchildren(void) const
number of descendants
Definition: node.h:131
A pocket is a set of balls.
Definition: pocket.h:32
SEXP structure(void) const
R list description.
Definition: pocket.h:158
std::string yaml(std::string tab="") const
human/machine-readable info
Definition: pocket.h:169
void repair_holder(node_t *p)
Definition: pocket.h:74
std::string describe(void) const
human-readable info
Definition: pocket.h:147
size_t bytesize(void) const
size of binary serialization
Definition: pocket.h:46
bool holds(ball_t *b) const
does this node hold the given ball?
Definition: pocket.h:112
Rbyte raw_t
Definition: internal.h:43
static int set_list_elem(SEXP list, SEXP names, SEXP element, const char *name, int pos)
Definition: internal.h:67
size_t name_t
Definition: internal.h:45
double slate_t
Definition: internal.h:44
#define n
Definition: lbdp_pomp.c:8
static const name_t null_lineage
Definition: node.h:13
std::set< ball_t *, ball_order >::const_iterator ball_it
Definition: pocket.h:26