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
19struct 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
26typedef typename std::set<ball_t*,ball_order>::const_iterator ball_it;
27typedef typename std::set<ball_t*,ball_order>::const_reverse_iterator ball_rev_it;
28
30
32class pocket_t : public std::set<ball_t*,ball_order> {
33
34private:
35
37 void clean (void) {
38 for (ball_t *b : *this) delete b;
39 clear();
40 };
41
42public:
43
44 // SERIALIZATION
46 size_t bytesize (void) const {
47 return sizeof(size_t) + size()*(ball_t::bytesize);
48 };
49
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_t *i : p)
54 o = (*i >> o);
55 return o;
56 };
57
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
71protected:
75 for (ball_t *b : *this) {
76 b->holder() = p;
77 }
78 };
79
80public:
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_t *b : *this) {
87 if (b->is(green)) {
88 n = node_name.find(b->uniq);
89 if (n != node_name.end()) {
90 node_t *p = n->second;
91 b->owner() = p;
92 ball_name->insert({b->uniq,b});
93 } else {
94 err("in '%s' (%s line %d): cannot find ball %zd", // #nocov
95 __func__,__FILE__,__LINE__,b->uniq); // #nocov
96 }
97 }
98 }
99 };
100
101public:
102
104 ~pocket_t (void) {
105 clean();
106 };
107
108public:
109
111 bool holds (ball_t *b) const {
112 ball_it i = find(b);
113 return (i != end());
114 };
115
116 bool holds (color_t c) const {
117 bool result = false;
118 for (ball_it i = begin(); !result && i != end(); i++) {
119 result = ((*i)->color == c);
120 }
121 return result;
122 };
123
124 ball_t* last_ball (void) const {
125 return *crbegin();
126 };
127
128 ball_t* ball (const color_t c) const {
129 for (ball_t *b : *this) {
130 if (b->color == c) return b;
131 }
132 err("in '%s' (%s line %d): no ball of color %s", // # nocov
133 __func__,__FILE__,__LINE__,colores[c]); // # nocov
134 return 0;
135 };
136
137 ball_t* other (const ball_t *b) const {
138 for (ball_t *a : *this) {
139 if (a != b) return a;
140 }
141 err("error in '%s' (%s line %d): there is no other.", // # nocov
142 __func__,__FILE__,__LINE__); // # nocov
143 return 0;
144 };
145
146 std::string describe (void) const {
147 std::string s = "{";
148 ball_it i = begin();
149 s += (*i)->describe(); ++i;
150 while (i != end()) {
151 s += ", " + (*i)->describe(); ++i;
152 }
153 s += "}";
154 return s;
155 };
156
157 SEXP structure (void) const {
158 SEXP o;
159 PROTECT(o = NEW_LIST(size()));
160 int k = 0;
161 for (ball_rev_it i = crbegin(); i != crend(); i++) {
162 SET_ELEMENT(o,k++,(*i)->structure());
163 }
164 UNPROTECT(1);
165 return o;
166 };
167
168 std::string yaml (std::string tab = "") const {
169 std::string o = "";
170 std::string t = tab + " ";
171 for (ball_t *b : *this) {
172 o += tab + "- " + b->yaml(t);
173 }
174 return o;
175 };
176};
177
178#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
std::string yaml(std::string tab="") const
machine-readable info
Definition ball.h:145
name_t uniq
Definition ball.h:37
static const size_t bytesize
size of binary serialization
Definition ball.h:43
node_t * holder(void) const
in whose pocket do I lie?
Definition ball.h:109
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
friend raw_t * operator>>(const pocket_t &p, raw_t *o)
binary serialization
Definition pocket.h:50
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
ball_t * ball(const color_t c) const
retrieve the first ball of the specified color.
Definition pocket.h:128
SEXP structure(void) const
R list description.
Definition pocket.h:157
std::string yaml(std::string tab="") const
human/machine-readable info
Definition pocket.h:168
bool holds(color_t c) const
does this node hold a ball of this color?
Definition pocket.h:116
ball_t * last_ball(void) const
retrieve the last ball
Definition pocket.h:124
void repair_holder(node_t *p)
Definition pocket.h:74
std::string describe(void) const
human-readable info
Definition pocket.h:146
~pocket_t(void)
destructor
Definition pocket.h:104
size_t bytesize(void) const
size of binary serialization
Definition pocket.h:46
ball_t * other(const ball_t *b) const
return a pointer to another ball
Definition pocket.h:137
bool holds(ball_t *b) const
does this node hold the given ball?
Definition pocket.h:111
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