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
13static const name_t null_lineage = name_t(NA_INTEGER);
14
16
23class node_t : public pocket_t {
24
25private:
26
29
30 void clean (void) { };
31
32public:
33
36
37public:
38
40 size_t bytesize (void) const {
41 return 2*sizeof(name_t) + sizeof(slate_t)
43 };
44
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 };
51
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
63public:
64
66 node_t (name_t u = 0, slate_t t = R_NaReal) {
67 uniq = u;
68 slate = t;
69 _green_ball = 0;
71 };
72
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
85public:
86
88 ball_t* green_ball (void) const {
89 return _green_ball;
90 };
91
92 ball_t*& green_ball (void) {
93 return _green_ball;
94 };
95
96 name_t deme (void) const {
97 return _green_ball->deme();
98 };
99
100 name_t& deme (void) {
101 return _green_ball->deme();
102 };
103
104 name_t lineage (void) const {
105 return _lineage;
106 };
107
108 name_t lineage (const ball_t *g) const {
109 return g->owner()->lineage();
110 };
111
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
128public:
129
131 int nchildren (void) const {
132 int n = 0;
133 for (ball_t *b : *this) {
134 switch (b->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 };
145
151 void lineage_incr (int *incr, int *sat, int *etype) const {
152 const name_t d = deme();
153 incr[d]--;
154 for (ball_t *b : *this) {
155 switch (b->color) {
156 case green: case black:
157 incr[b->deme()]++;
158 sat[b->deme()]++;
159 break;
160 default:
161 break;
162 }
163 }
164 if (holds_own()) {
165 sat[d]--;
166 etype[d] = -1;
167 } else if (holds(blue)) {
168 etype[d] = 1;
169 } else {
170 etype[d] = 2;
171 }
172 };
173
174public:
175
177 std::string describe (void) const {
178 std::string s = "node("
179 + std::to_string(uniq)
180 + "," + std::to_string(deme()) + ",";
181 if (lineage() != null_lineage) {
182 s += std::to_string(lineage());
183 }
184 s += ")" + pocket_t::describe();
185 s += ", t = " + std::to_string(slate) + "\n";
186 return s;
187 };
188
189 std::string yaml (std::string tab = "") const {
190 std::string t = tab + " ";
191 std::string o = "name: " + std::to_string(uniq) + "\n"
192 + tab + "time: " + std::to_string(slate) + "\n"
193 + tab + "deme: " + std::to_string(deme()) + "\n";
194 if (lineage() != null_lineage) {
195 o += tab + "lineage: " + std::to_string(lineage()) + "\n";
196 }
197 o += tab + "pocket:\n" + pocket_t::yaml(tab);
198 return o;
199 };
200
201 SEXP structure (void) const {
202 SEXP O, On;
203 PROTECT(O = NEW_LIST(4));
204 PROTECT(On = NEW_CHARACTER(4));
205 set_list_elem(O,On,ScalarInteger(int(uniq)),"name",0);
206 set_list_elem(O,On,ScalarReal(double(slate)),"time",1);
207 set_list_elem(O,On,ScalarInteger(int(deme())),"deme",2);
208 set_list_elem(O,On,pocket_t::structure(),"pocket",3);
209 SET_NAMES(O,On);
210 UNPROTECT(2);
211 return O;
212 };
213
214 std::string newick (const slate_t& tnow, const slate_t& tpar) const {
215 std::string o1 = "", o2 = "", o3 = "";
216 int n = nchildren();
217 if (n > 0) {
218 o1 = "("; o3 = ")";
219 }
220 if (holds(blue)) {
221 o3 += "b_";
222 } else if (holds_own()) {
223 o3 += "m_";
224 } else {
225 o3 += "g_";
226 }
227 n = 0;
228 for (ball_t *b : *this) {
229 node_t *p = 0;
230 switch (b->color) {
231 case green:
232 p = b->child();
233 if (p != this) {
234 if (n++ > 0) o2 += ",";
235 o2 += p->newick(tnow,slate);
236 }
237 break;
238 case black:
239 if (n++ > 0) o2 += ",";
240 o2 += b->newick(tnow-slate);
241 break;
242 case blue:
243 break;
244 }
245 }
246 return o1 + o2 + o3
247 + std::to_string(deme())
248 + "_" + std::to_string(uniq)
249 + ":" + std::to_string(slate - tpar);
250 };
251};
252
253#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 * 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
color_t color
Definition ball.h:38
node_t * child(void) const
a child is the owner of a green ball
Definition ball.h:104
ball_t *& green_ball(void)
set green ball
Definition node.h:92
node_t * parent(void) const
Definition node.h:115
void clean(void)
Definition node.h:30
name_t lineage(void) const
view lineage
Definition node.h:104
name_t & lineage(void)
set lineage
Definition node.h:112
node_t & operator=(const node_t &p)=delete
copy assignment operator
size_t bytesize(void) const
size of binary serialization
Definition node.h:40
bool dead_root(void) const
Definition node.h:124
friend raw_t * operator>>(const node_t &p, raw_t *o)
binary serialization of node_t
Definition node.h:45
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
std::string newick(const slate_t &tnow, const slate_t &tpar) const
Newick format.
Definition node.h:214
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:201
~node_t(void)
destructor
Definition node.h:81
std::string describe(void) const
human-readable info
Definition node.h:177
name_t & deme(void)
set deme
Definition node.h:100
std::string yaml(std::string tab="") const
machine-readable info
Definition node.h:189
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:34
ball_t * green_ball(void) const
pointer to my green ball
Definition node.h:88
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:157
std::string yaml(std::string tab="") const
human/machine-readable info
Definition pocket.h:168
void repair_holder(node_t *p)
Definition pocket.h:74
std::string describe(void) const
human-readable info
Definition pocket.h:146
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:111
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