phylopomp
Phylodynamics for POMPs
Loading...
Searching...
No Matches
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* first_ball (void) const {
125 return *cbegin();
126 };
127
128 ball_t* last_ball (void) const {
129 return *crbegin();
130 };
131
132 ball_t* ball (const color_t c) const {
133 for (ball_t *b : *this) {
134 if (b->color == c) return b;
135 }
136 err("in '%s' (%s line %d): no ball of color %s", // # nocov
137 __func__,__FILE__,__LINE__,colores[c]); // # nocov
138 return 0;
139 };
140
141 ball_t* other (const ball_t *b) const {
142 for (ball_t *a : *this) {
143 if (a != b) return a;
144 }
145 err("error in '%s' (%s line %d): there is no other.", // # nocov
146 __func__,__FILE__,__LINE__); // # nocov
147 return 0;
148 };
149
150 std::string describe (void) const {
151 std::string s = "{";
152 ball_it i = begin();
153 s += (*i)->describe(); ++i;
154 while (i != end()) {
155 s += ", " + (*i)->describe(); ++i;
156 }
157 s += "}";
158 return s;
159 };
160
161 SEXP structure (void) const {
162 SEXP o;
163 PROTECT(o = NEW_LIST(size()));
164 int k = 0;
165 for (ball_rev_it i = crbegin(); i != crend(); i++) {
166 SET_ELEMENT(o,k++,(*i)->structure());
167 }
168 UNPROTECT(1);
169 return o;
170 };
171
172 std::string yaml (std::string tab = "") const {
173 std::string o = "";
174 std::string t = tab + " ";
175 for (ball_t *b : *this) {
176 o += tab + "- " + b->yaml(t);
177 }
178 return o;
179 };
180};
181
182#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:132
SEXP structure(void) const
R list description.
Definition pocket.h:161
std::string yaml(std::string tab="") const
human/machine-readable info
Definition pocket.h:172
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:128
void repair_holder(node_t *p)
Definition pocket.h:74
std::string describe(void) const
human-readable info
Definition pocket.h:150
~pocket_t(void)
destructor
Definition pocket.h:104
size_t bytesize(void) const
size of binary serialization
Definition pocket.h:46
ball_t * first_ball(void) const
retrieve the first ball
Definition pocket.h:124
ball_t * other(const ball_t *b) const
return a pointer to another ball
Definition pocket.h:141
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