phylopomp
Phylodynamics for POMPs
Loading...
Searching...
No Matches
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 <unordered_map>
8#include <vector>
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
63 void reuniqify (name_t shift);
64
65public:
66
68 node_t (name_t u = 0, slate_t t = R_NaReal) {
69 uniq = u;
70 slate = t;
71 _green_ball = 0;
73 };
74
75 node_t (const node_t &p) = delete;
77 node_t (node_t && p) = delete;
79 node_t & operator= (const node_t & p) = delete;
81 node_t & operator= (node_t && p) = delete;
83 ~node_t (void) {
84 clean();
85 };
86
87public:
88
90 ball_t* green_ball (void) const {
91 return _green_ball;
92 };
93
94 ball_t*& green_ball (void) {
95 return _green_ball;
96 };
97
98 name_t deme (void) const {
99 return _green_ball->deme();
100 };
101
102 name_t& deme (void) {
103 return _green_ball->deme();
104 };
105
106 name_t lineage (void) const {
107 return _lineage;
108 };
109
110 name_t lineage (const ball_t *g) const {
111 return g->owner()->lineage();
112 };
113
114 name_t& lineage (void) {
115 return _lineage;
116 };
117 node_t* parent (void) const {
118 return _green_ball->holder();
119 };
120 bool is_root (void) const {
121 return (_green_ball->holder() == this);
122 };
123 bool dead_root (void) const {
124 return is_root() && size()==1;
125 };
126
127public:
128
130 int nchildren (void) const {
131 int n = 0;
132 for (ball_t *b : *this) {
133 switch (b->color) {
134 case green: case black:
135 n++;
136 break;
137 default:
138 break;
139 }
140 }
141 if (is_root()) n--;
142 return n;
143 };
144
150 void lineage_incr (int *incr, int *sat, int *etype) const;
151
154 slate_t joining_branch_length (const std::unordered_map<name_t, bool>&) const;
155
159 void cblv (std::vector<slate_t>&, std::vector<slate_t>&,
160 std::unordered_map<name_t, bool>&,
161 const std::unordered_map<name_t,std::vector<node_t*>>&,
162 slate_t) const;
163
164public:
165
167 void insert (ball_t *a) {
168 a->holder() = this;
169 pocket_t::insert(a);
170 };
171
173 string_t yaml (string_t tab = "") const;
175 SEXP structure (void) const;
177 string_t newick (const slate_t& tnow, const slate_t& tpar,
178 bool showdeme, bool extended) const;
179
180};
181
182#endif
@ green
Definition ball.h:12
@ black
Definition ball.h:12
Balls function as pointers.
Definition ball.h:27
node_t * owner(void) const
view owner of a green ball
Definition ball.h:92
node_t * holder(void) const
in whose pocket do I lie?
Definition ball.h:107
color_t color
Definition ball.h:36
string_t yaml(string_t tab="") const
human/machine-readable info
Definition yaml.cc:37
ball_t *& green_ball(void)
set green ball
Definition node.h:94
node_t * parent(void) const
Definition node.h:117
void cblv(std::vector< slate_t > &, std::vector< slate_t > &, std::unordered_map< name_t, bool > &, const std::unordered_map< name_t, std::vector< node_t * > > &, slate_t) const
Definition cblv.cc:44
void clean(void)
Definition node.h:30
name_t lineage(void) const
view lineage
Definition node.h:106
name_t & lineage(void)
set lineage
Definition node.h:114
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:123
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
slate_t joining_branch_length(const std::unordered_map< name_t, bool > &) const
Definition cblv.cc:36
bool is_root(void) const
Definition node.h:120
node_t(const node_t &p)=delete
copy constructor
void reuniqify(name_t shift)
shifts name to avoid overlap
Definition sum.cc:10
name_t lineage(const ball_t *g) const
view lineage associated with a green ball
Definition node.h:110
SEXP structure(void) const
R list description.
Definition structure.cc:55
~node_t(void)
destructor
Definition node.h:83
name_t & deme(void)
set deme
Definition node.h:102
node_t(name_t u=0, slate_t t=R_NaReal)
basic constructor for node class
Definition node.h:68
name_t deme(void) const
view deme
Definition node.h:98
name_t uniq
Definition node.h:34
ball_t * green_ball(void) const
pointer to my green ball
Definition node.h:90
void insert(ball_t *a)
insert a ball into the pocket of a node
Definition node.h:167
string_t newick(const slate_t &tnow, const slate_t &tpar, bool showdeme, bool extended) const
Newick-format output.
Definition newick.cc:46
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 lineages.cc:14
int nchildren(void) const
number of descendants
Definition node.h:130
A pocket is a set of balls.
Definition pocket.h:30
void repair_holder(node_t *p)
Definition pocket.h:72
size_t bytesize(void) const
size of binary serialization
Definition pocket.h:44
Rbyte raw_t
Definition internal.h:52
size_t name_t
Definition internal.h:54
double slate_t
Definition internal.h:53
#define n
Definition lbdp_pomp.c:9
static const name_t null_lineage
Definition node.h:13