phylopomp
Phylodynamics for POMPs
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
pocket.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 // The POCKET class
3 
4 #ifndef _POCKET_H_
5 #define _POCKET_H_
6 
7 #include <set>
8 #include <unordered_map>
9 #include <string>
10 #include <cstring>
11 #include "ball.h"
12 #include "internal.h"
13 
15 
19 struct ball_order {
20  bool operator() (const ball_t* a, const ball_t* b) const {
21  return (a->color < b->color) ||
22  ((a->color == b->color) && (a->uniq < b->uniq));
23  };
24 };
25 
26 typedef typename std::set<ball_t*,ball_order>::const_iterator ball_it;
27 typedef typename std::set<ball_t*,ball_order>::const_reverse_iterator ball_rev_it;
28 
30 
32 class pocket_t : public std::set<ball_t*,ball_order> {
33 
34 private:
35 
37  void clean (void) {
38  for (ball_it i = begin(); i != end(); i++) delete *i;
39  clear();
40  };
41 
42 public:
43 
44  // SERIALIZATION
46  size_t bytesize (void) const {
47  return sizeof(size_t) + size()*(ball_t::bytesize);
48  };
50  friend raw_t* operator>> (const pocket_t &p, raw_t *o) {
51  size_t psize = p.size();
52  memcpy(o,&psize,sizeof(size_t)); o += sizeof(size_t);
53  for (ball_it i = p.begin(); i != p.end(); i++)
54  o = (**i >> o);
55  return o;
56  };
59  friend raw_t* operator>> (raw_t *o, pocket_t &p) {
60  p.clean();
61  size_t psize;
62  memcpy(&psize,o,sizeof(size_t)); o += sizeof(size_t);
63  for (size_t i = 0; i < psize; i++) {
64  ball_t *b = new ball_t();
65  o = (o >> *b);
66  p.insert(b);
67  }
68  return o;
69  };
70 
71 protected:
74  void repair_holder (node_t* p) {
75  for (ball_it i = begin(); i != end(); i++) {
76  (*i)->holder() = p;
77  }
78  };
79 
80 public:
83  void repair_owners (const std::unordered_map<name_t,node_t*>& node_name,
84  std::unordered_map<name_t,ball_t*> *ball_name) {
85  std::unordered_map<name_t,node_t*>::const_iterator n;
86  for (ball_it i = begin(); i != end(); i++) {
87  ball_t *b = *i;
88  if (b->is(green)) {
89  n = node_name.find(b->uniq);
90  if (n != node_name.end()) {
91  node_t *p = n->second;
92  b->owner() = p;
93  ball_name->insert({b->uniq,b});
94  } else {
95  err("in '%s' (%s line %d): cannot find ball %zd", // #nocov
96  __func__,__FILE__,__LINE__,b->uniq); // #nocov
97  }
98  }
99  }
100  };
101 
102 public:
103 
105  ~pocket_t (void) {
106  clean();
107  };
108 
109 public:
110 
112  bool holds (ball_t *b) const {
113  ball_it i = find(b);
114  return (i != end());
115  };
117  bool holds (color_t c) const {
118  bool result = false;
119  for (ball_it i = begin(); !result && i != end(); i++) {
120  result = ((*i)->color == c);
121  }
122  return result;
123  };
125  ball_t* last_ball (void) const {
126  return *crbegin();
127  };
129  ball_t* ball (const color_t c) const {
130  for (ball_it i = begin(); i != end(); i++) {
131  if ((*i)->color == c) return *i;
132  }
133  err("in '%s' (%s line %d): no ball of color %s", // # nocov
134  __func__,__FILE__,__LINE__,colores[c]); // # nocov
135  return 0;
136  };
138  ball_t* other (const ball_t *b) const {
139  for (ball_it i = begin(); i != end(); i++) {
140  if (*i != b) return *i;
141  }
142  err("error in '%s' (%s line %d): there is no other.", // # nocov
143  __func__,__FILE__,__LINE__); // # nocov
144  return 0;
145  };
147  std::string describe (void) const {
148  std::string s = "{";
149  ball_it i = begin();
150  s += (*i)->describe(); ++i;
151  while (i != end()) {
152  s += ", " + (*i)->describe(); ++i;
153  }
154  s += "}";
155  return s;
156  };
158  SEXP structure (void) const {
159  SEXP o;
160  PROTECT(o = NEW_LIST(size()));
161  int k = 0;
162  for (ball_rev_it i = crbegin(); i != crend(); i++) {
163  SET_ELEMENT(o,k++,(*i)->structure());
164  }
165  UNPROTECT(1);
166  return o;
167  };
169  std::string yaml (std::string tab = "") const {
170  std::string o = "";
171  std::string t = tab + " ";
172  for (ball_it i = begin(); i != end(); i++) {
173  o += tab + "- " + (*i)->yaml(t);
174  }
175  return o;
176  };
177 };
178 
179 #endif
color_t
BALL COLORS.
Definition: ball.h:14
@ green
Definition: ball.h:14
static const char * colores[]
Definition: ball.h:15
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 uniq
Definition: ball.h:37
static const size_t bytesize
size of binary serialization
Definition: ball.h:43
bool is(color_t c) const
is a given ball of the given color?
Definition: ball.h:117
color_t color
Definition: ball.h:38
Encodes a genealogical node.
Definition: node.h:23
A pocket is a set of balls.
Definition: pocket.h:32
void clean(void)
delete balls and clear pocket
Definition: pocket.h:37
void repair_owners(const std::unordered_map< name_t, node_t * > &node_name, std::unordered_map< name_t, ball_t * > *ball_name)
Definition: pocket.h:83
SEXP structure(void) const
R list description.
Definition: pocket.h:158
friend raw_t * operator>>(const pocket_t &p, raw_t *o)
binary serialization
Definition: pocket.h:50
std::string yaml(std::string tab="") const
human/machine-readable info
Definition: pocket.h:169
bool holds(color_t c) const
does this node hold a ball of this color?
Definition: pocket.h:117
ball_t * ball(const color_t c) const
retrieve the first ball of the specified color.
Definition: pocket.h:129
ball_t * other(const ball_t *b) const
return a pointer to another ball
Definition: pocket.h:138
void repair_holder(node_t *p)
Definition: pocket.h:74
std::string describe(void) const
human-readable info
Definition: pocket.h:147
~pocket_t(void)
destructor
Definition: pocket.h:105
size_t bytesize(void) const
size of binary serialization
Definition: pocket.h:46
ball_t * last_ball(void) const
retrieve the last ball
Definition: pocket.h:125
bool holds(ball_t *b) const
does this node hold the given ball?
Definition: pocket.h:112
Rbyte raw_t
Definition: internal.h:43
#define err(...)
Definition: internal.h:18
#define n
Definition: lbdp_pomp.c:8
std::set< ball_t *, ball_order >::const_reverse_iterator ball_rev_it
Definition: pocket.h:27
std::set< ball_t *, ball_order >::const_iterator ball_it
Definition: pocket.h:26
Ordering for balls in pockets.
Definition: pocket.h:19
bool operator()(const ball_t *a, const ball_t *b) const
Definition: pocket.h:20