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 "ball.h"
8#include "pocket.h"
9#include "internal.h"
10
11static const name_t null_lineage = name_t(NA_INTEGER);
12
14
21class node_t : public pocket_t {
22
23private:
24
27
28 void clean (void) { };
29
30public:
31
34
35public:
36
38 size_t bytesize (void) const {
39 return 2*sizeof(name_t) + sizeof(slate_t)
41 };
42
43 friend raw_t* operator>> (const node_t &p, raw_t *o) {
44 name_t buf[2] = {p.uniq, p._lineage};
45 memcpy(o,buf,sizeof(buf)); o += sizeof(buf);
46 memcpy(o,&p.slate,sizeof(slate_t)); o += sizeof(slate_t);
47 return reinterpret_cast<const pocket_t&>(p) >> o;
48 };
49
50 friend raw_t* operator>> (raw_t *o, node_t &p) {
51 p.clean();
52 name_t buf[2];
53 memcpy(buf,o,sizeof(buf)); o += sizeof(buf);
54 memcpy(&p.slate,o,sizeof(slate_t)); o += sizeof(slate_t);
55 p.uniq = buf[0]; p._lineage = buf[1];
56 o = (o >> reinterpret_cast<pocket_t&>(p));
57 p.repair_holder(&p);
58 return o;
59 };
60
61 void reuniqify (name_t shift);
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
153public:
154
156 void insert (ball_t *a) {
157 a->holder() = this;
158 pocket_t::insert(a);
159 };
160
162 string_t yaml (string_t tab = "") const;
164 SEXP structure (void) const;
166 string_t newick (const slate_t& tnow, const slate_t& tpar,
167 bool showdeme, bool extended) const;
168
169};
170
171#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:92
node_t * parent(void) const
Definition node.h:115
void clean(void)
Definition node.h:28
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:38
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:43
name_t _lineage
Definition node.h:26
ball_t * _green_ball
Definition node.h:25
bool is_root(void) const
Definition node.h:121
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:108
SEXP structure(void) const
R list description.
Definition structure.cc:55
~node_t(void)
destructor
Definition node.h:81
name_t & deme(void)
set deme
Definition node.h:100
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:32
ball_t * green_ball(void) const
pointer to my green ball
Definition node.h:88
void insert(ball_t *a)
insert a ball into the pocket of a node
Definition node.h:156
string_t newick(const slate_t &tnow, const slate_t &tpar, bool showdeme, bool extended) const
Newick-format output.
Definition newick.cc:31
slate_t slate
Definition node.h:33
node_t(node_t &&p)=delete
move constructor
void lineage_incr(int *incr, int *sat, int *etype) const
Definition lineages.cc:14
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: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:11