phylopomp
Phylodynamics for POMPs
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ball.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 // BALL CLASS
3 #ifndef _BALL_H_
4 #define _BALL_H_
5 
6 #include <string>
7 #include <cstring>
8 #include "internal.h"
9 
11 
14 typedef enum {green, blue, black} color_t;
15 static const char* colores[] = {"green", "blue", "black"};
16 static const char* colorsymb[] = {"g", "b", "o"};
17 static const name_t undeme = name_t(NA_INTEGER);
18 
19 class node_t;
20 
22 
29 class ball_t {
30 
31 private:
35 
36 public:
39 
40 public:
41 
43  static const size_t bytesize = 2*sizeof(name_t)+sizeof(color_t);
45  friend raw_t* operator>> (const ball_t &b, raw_t *o) {
46  name_t buf[2] = {b.uniq, b._deme};
47  memcpy(o,buf,sizeof(buf)); o += sizeof(buf);
48  memcpy(o,&b.color,sizeof(color_t)); o += sizeof(color_t);
49  return o;
50  };
52  friend raw_t* operator>> (raw_t *o, ball_t &b) {
53  name_t buf[2];
54  memcpy(buf,o,sizeof(buf)); o += sizeof(buf);
55  b.uniq = buf[0]; b._deme = buf[1];
56  memcpy(&b.color,o,sizeof(color_t)); o += sizeof(color_t);
57  b._holder = 0; // must be set elsewhere
58  b._owner = 0; // must be set elsewhere
59  return o;
60  };
61 
62 public:
63 
65  ball_t (node_t *who = 0, name_t u = 0,
66  color_t col = green, name_t d = undeme) {
67  _holder = _owner = who;
68  uniq = u;
69  color = col;
70  _deme = d;
71  };
73  ball_t (const ball_t&) = delete;
75  ball_t (ball_t&&) = delete;
77  ball_t & operator= (const ball_t&) = delete;
79  ball_t & operator= (ball_t&&) = delete;
81  ~ball_t (void) = default;
82 
83 public:
84 
86  name_t deme (void) const {
87  return _deme;
88  };
90  name_t& deme (void) {
91  return _deme;
92  };
94  node_t* owner (void) const {
95  assert(color==green);
96  return _owner;
97  };
99  node_t*& owner (void) {
100  assert(color==green);
101  return _owner;
102  };
104  node_t* child (void) const {
105  assert(color==green);
106  return _owner;
107  };
109  node_t* holder (void) const {
110  return _holder;
111  };
113  node_t*& holder (void) {
114  return _holder;
115  };
117  bool is (color_t c) const {
118  return color==c;
119  };
121  std::string color_name (void) const {
122  return colores[color];
123  };
125  std::string color_symbol (void) const {
126  if (is(green) && _holder==_owner)
127  return "m"; // brown balls
128  else
129  return colorsymb[color];
130  };
131 
132 public:
133 
135  std::string describe (void) const {
136  std::string o = color_name()
137  + "(" + std::to_string(uniq) + ",";
138  if (_deme != undeme) {
139  o += std::to_string(_deme);
140  }
141  o += ")";
142  return o;
143  };
145  std::string yaml (std::string tab = "") const {
146  std::string o;
147  o = "color: " + color_name() + "\n"
148  + tab + "name: " + std::to_string(uniq) + "\n";
149  if (color==black) {
150  o += tab + "deme: " + std::to_string(_deme) + "\n";
151  }
152  return o;
153  };
155  SEXP structure (void) const {
156  SEXP O, On, Name, Color, Deme;
157  int size = (is(black)) ? 3 : 2;
158  PROTECT(O = NEW_LIST(size));
159  PROTECT(On = NEW_CHARACTER(size));
160  PROTECT(Name = NEW_INTEGER(1));
161  *INTEGER(Name) = int(uniq);
162  PROTECT(Color = NEW_CHARACTER(1));
163  SET_STRING_ELT(Color,0,mkChar(color_symbol().c_str()));
164  set_list_elem(O,On,Name,"name",0);
165  set_list_elem(O,On,Color,"color",1);
166  if (is(black)) {
167  PROTECT(Deme = NEW_INTEGER(1));
168  *INTEGER(Deme) = int(_deme);
169  set_list_elem(O,On,Deme,"deme",2);
170  UNPROTECT(1);
171  }
172  SET_NAMES(O,On);
173  UNPROTECT(4);
174  return O;
175  };
177  std::string newick (const slate_t &t) const {
178  return color_symbol()
179  + "_" + std::to_string(_deme)
180  + "_" + std::to_string(uniq)
181  + ":" + std::to_string(t);
182  };
183 };
184 
185 #endif
color_t
BALL COLORS.
Definition: ball.h:14
@ green
Definition: ball.h:14
@ black
Definition: ball.h:14
@ blue
Definition: ball.h:14
static const name_t undeme
Definition: ball.h:17
static const char * colorsymb[]
Definition: ball.h:16
static const char * colores[]
Definition: ball.h:15
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
ball_t(ball_t &&)=delete
move constructor
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 yaml(std::string tab="") const
machine-readable info
Definition: ball.h:145
name_t uniq
Definition: ball.h:37
node_t * _holder
Definition: ball.h:32
std::string color_symbol(void) const
machine-readable color symbols
Definition: ball.h:125
static const size_t bytesize
size of binary serialization
Definition: ball.h:43
std::string describe(void) const
human-readable info
Definition: ball.h:135
std::string newick(const slate_t &t) const
element of a newick representation
Definition: ball.h:177
friend raw_t * operator>>(const ball_t &b, raw_t *o)
binary serialization
Definition: ball.h:45
SEXP structure(void) const
R list description.
Definition: ball.h:155
node_t *& holder(void)
in whose pocket do I lie?
Definition: ball.h:113
bool is(color_t c) const
is a given ball of the given color?
Definition: ball.h:117
ball_t(node_t *who=0, name_t u=0, color_t col=green, name_t d=undeme)
basic constructor for ball class
Definition: ball.h:65
node_t * holder(void) const
in whose pocket do I lie?
Definition: ball.h:109
std::string color_name(void) const
human-readable colors
Definition: ball.h:121
~ball_t(void)=default
destructor
ball_t(const ball_t &)=delete
copy constructor
ball_t & operator=(const ball_t &)=delete
copy assignment operator
color_t color
Definition: ball.h:38
node_t * _owner
Definition: ball.h:33
name_t _deme
Definition: ball.h:34
name_t & deme(void)
change deme
Definition: ball.h:90
node_t *& owner(void)
change owner of a green ball
Definition: ball.h:99
Encodes a genealogical node.
Definition: node.h:23
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