phylopomp
Phylodynamics for POMPs
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
master.h
Go to the documentation of this file.
1// -*- C++ -*-
8
9#ifndef _MASTER_H_
10#define _MASTER_H_
11
12#include <string>
13#include <cstring>
14#include "popul_proc.h"
15#include "genealogy.h"
16#include "internal.h"
17
19
21template <class POPN, size_t NDEME = 1>
22class master_t : public POPN {
23
24public:
25
26 typedef POPN popul_t;
27 const static size_t ndeme = NDEME;
28
29public:
30 // DATA MEMBERS
33
34public:
36 size_t bytesize (void) const {
37 return popul_t::bytesize() + geneal.bytesize();
38 };
39
40 friend raw_t* operator>> (const master_t& A, raw_t* o) {
41 o = (reinterpret_cast<const popul_t&>(A) >> o);
42 o = (A.geneal >> o);
43 return o;
44 }
45
46 friend raw_t* operator>> (raw_t* o, master_t& A) {
47 A.clean();
48 o = (o >> reinterpret_cast<popul_t&>(A));
49 o = (o >> A.geneal);
50 A.inventory = A.geneal.extant();
51 return o;
52 }
53
54private:
55
56 void clean (void) { };
57
58public:
59 // CONSTRUCTORS, ETC.
62 master_t (double t0 = 0) : popul_t(t0), geneal(t0,0,ndeme) {};
65 o >> *this;
66 };
67
68 master_t (SEXP o) {
69 if (LENGTH(o)==0)
70 err("in %s (%s line %d): cannot deserialize a NULL.",
71 __func__,__FILE__,__LINE__);
72 PROTECT(o = AS_RAW(o));
73 RAW(o) >> *this;
74 UNPROTECT(1);
75 };
76
77 master_t (const master_t& A) {
78 raw_t *o = new raw_t[A.bytesize()];
79 A >> o;
80 o >> *this;
81 delete[] o;
82 };
83
85 clean();
86 raw_t *o = new raw_t[A.bytesize()];
87 A >> o;
88 o >> *this;
89 delete[] o;
90 return *this;
91 };
92
93 master_t (master_t &&) = default;
95 master_t & operator= (master_t &&) = default;
97 ~master_t (void) {
98 clean();
99 };
100
101 slate_t timezero (void) const {
102 return geneal.timezero();
103 };
104
105 int play (double tfin) {
106 int count = popul_t::play(tfin);
107 geneal.time() = tfin;
108 return count;
109 };
110
111public:
113 slate_t time (void) const {
114 return popul_t::time();
115 };
116
117 std::string describe (void) const {
118 return geneal.describe();
119 };
120
121 std::string yaml (std::string tab = "") const {
122 std::string t = tab + " ";
123 std::string s = popul_t::yaml(tab)
124 + "genealogy:\n" + geneal.yaml(t);
125 return s;
126 };
127
128 std::string newick (void) const {
129 return geneal.newick();
130 };
131
132 SEXP lineage_count (void) const {
133 return geneal.lineage_count();
134 };
135
136 SEXP structure (void) const {
137 return geneal.structure();
138 };
139
140public:
142 void birth (name_t i = 0, name_t j = 0, int n = 1) {
143 ball_t *a = inventory.random_ball(i);
144 ball_t *b = geneal.birth(a,time(),j);
145 inventory.insert(b);
146 while (n > 1) {
147 b = geneal.birth(b->holder(),j);
148 inventory.insert(b);
149 n--;
150 }
151 };
152
153 void death (name_t i = 0) {
154 ball_t *a = inventory.random_ball(i);
155 inventory.erase(a);
156 geneal.death(a,time());
157 };
158
159 void graft (name_t i = 0, int m = 1) {
160 for (int j = 0; j < m; j++) {
161 ball_t *a = geneal.graft(time(),i);
162 inventory.insert(a);
163 }
164 };
165
166 void sample (name_t i = 0, int n = 1) {
167 pocket_t *p = inventory.random_balls(i,n);
168 for (ball_t *a : *p) {
169 geneal.sample(a,time());
170 }
171 p->clear();
172 delete p;
173 };
174
175 void sample_death (name_t i = 0, int n = 1) {
176 pocket_t *p = inventory.random_balls(i,n);
177 for (ball_t *a : *p) {
178 inventory.erase(a);
179 geneal.sample_death(a,time());
180 }
181 p->clear();
182 delete p;
183 };
184
185 void migrate (name_t i = 0, name_t j = 0) {
186 ball_t *a = inventory.random_ball(i);
187 inventory.erase(a);
188 geneal.migrate(a,time(),j);
189 a->deme() = j;
190 inventory.insert(a);
191 };
192
193 void rinit (void);
195 void jump (int e);
196};
197
198#endif
Balls function as pointers.
Definition ball.h:29
name_t deme(void) const
view deme
Definition ball.h:86
node_t * holder(void) const
in whose pocket do I lie?
Definition ball.h:109
Encodes a genealogy.
Definition genealogy.h:22
std::pair< node_it, node_it > extant(void) const
Definition genealogy.h:464
Representation for the inventory process.
Definition inventory.h:18
std::string yaml(std::string tab="") const
machine/human readable info
Definition master.h:121
slate_t time(void) const
current time
Definition master.h:113
void clean(void)
Definition master.h:56
void birth(name_t i=0, name_t j=0, int n=1)
n births into deme j with parent in deme i
Definition master.h:142
int play(double tfin)
runs the process to time tfin
Definition master.h:105
void jump(int e)
makes a jump
std::string describe(void) const
human-readable info
Definition master.h:117
void sample(name_t i=0, int n=1)
sample in deme i
Definition master.h:166
master_t(raw_t *o)
constructor from serialized binary form
Definition master.h:64
void death(name_t i=0)
death in deme i
Definition master.h:153
master_t(master_t &&)=default
move constructor
master_t(double t0=0)
Definition master.h:62
slate_t timezero(void) const
get zero-time
Definition master.h:101
void graft(name_t i=0, int m=1)
new root in deme i
Definition master.h:159
std::string newick(void) const
tree in Newick format
Definition master.h:128
master_t(const master_t &A)
copy constructor
Definition master.h:77
static const size_t ndeme
Definition master.h:27
master_t(SEXP o)
constructor from RAW SEXP (containing binary serialization)
Definition master.h:68
void rinit(void)
initialize the state
inventory_t< ndeme > inventory
Definition master.h:32
~master_t(void)
destructor
Definition master.h:97
master_t & operator=(const master_t &A)
copy assignment operator
Definition master.h:84
size_t bytesize(void) const
size of serialized binary form
Definition master.h:36
SEXP lineage_count(void) const
lineage count table
Definition master.h:132
void migrate(name_t i=0, name_t j=0)
migration from deme i to deme j
Definition master.h:185
void sample_death(name_t i=0, int n=1)
sample_death in deme i
Definition master.h:175
friend raw_t * operator>>(const master_t &A, raw_t *o)
binary serialization
Definition master.h:40
SEXP structure(void) const
structure in R list format
Definition master.h:136
A pocket is a set of balls.
Definition pocket.h:32
std::string yaml(std::string tab) const
Definition lbdp.cc:26
Rbyte raw_t
Definition internal.h:43
size_t name_t
Definition internal.h:45
#define err(...)
Definition internal.h:18
double slate_t
Definition internal.h:44
#define n
Definition lbdp_pomp.c:8