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 "ball.h"
10#include "internal.h"
11
13
17struct ball_order {
18 bool operator() (const ball_t* a, const ball_t* b) const {
19 return (a->color < b->color) ||
20 ((a->color == b->color) && (a->uniq < b->uniq));
21 };
22};
23
24typedef typename std::set<ball_t*,ball_order>::const_iterator ball_it;
25typedef typename std::set<ball_t*,ball_order>::const_reverse_iterator ball_rev_it;
26
28
30class pocket_t : public std::set<ball_t*,ball_order> {
31
32private:
33
35 void clean (void) {
36 for (ball_t *b : *this) delete b;
37 clear();
38 };
39
40public:
41
42 // SERIALIZATION
44 size_t bytesize (void) const {
45 return sizeof(size_t) + size()*(ball_t::bytesize);
46 };
47
48 friend raw_t* operator>> (const pocket_t &p, raw_t *o) {
49 size_t psize = p.size();
50 memcpy(o,&psize,sizeof(size_t)); o += sizeof(size_t);
51 for (ball_t *i : p)
52 o = (*i >> o);
53 return o;
54 };
55
57 friend raw_t* operator>> (raw_t *o, pocket_t &p) {
58 p.clean();
59 size_t psize;
60 memcpy(&psize,o,sizeof(size_t)); o += sizeof(size_t);
61 for (size_t i = 0; i < psize; i++) {
62 ball_t *b = new ball_t();
63 o = (o >> *b);
64 p.insert(b);
65 }
66 return o;
67 };
68
69protected:
73 for (ball_t *b : *this) {
74 b->holder() = p;
75 }
76 };
77
78public:
81 void repair_owners (const std::unordered_map<name_t,node_t*>& node_name,
82 std::unordered_map<name_t,ball_t*> *ball_name) {
83 std::unordered_map<name_t,node_t*>::const_iterator n;
84 for (ball_t *b : *this) {
85 if (b->is(green)) {
86 n = node_name.find(b->uniq);
87 if (n != node_name.end()) {
88 node_t *p = n->second;
89 b->owner() = p;
90 ball_name->insert({b->uniq,b});
91 } else {
92 err("in '%s' (%s line %d): cannot find ball %zd", // #nocov
93 __func__,__FILE__,__LINE__,b->uniq); // #nocov
94 }
95 }
96 }
97 };
98
99public:
100
102 ~pocket_t (void) {
103 clean();
104 };
105
106public:
107
109 bool holds (ball_t *b) const {
110 ball_it i = find(b);
111 return (i != end());
112 };
113
114 bool holds (color_t c) const {
115 bool result = false;
116 for (ball_it i = begin(); !result && i != end(); i++) {
117 result = ((*i)->color == c);
118 }
119 return result;
120 };
121
122 ball_t* first_ball (void) const {
123 return *cbegin();
124 };
125
126 ball_t* last_ball (void) const {
127 return *crbegin();
128 };
129
130 ball_t* ball (const color_t c) const {
131 for (ball_t *b : *this) {
132 if (b->color == c) return b;
133 }
134 err("in '%s' (%s line %d): no ball of color %s", // # nocov
135 __func__,__FILE__,__LINE__,colores[c]); // # nocov
136 return 0;
137 };
138
139 ball_t* other (const ball_t *b) const {
140 for (ball_t *a : *this) {
141 if (a != b) return a;
142 }
143 err("error in '%s' (%s line %d): there is no other.", // # nocov
144 __func__,__FILE__,__LINE__); // # nocov
145 return 0;
146 };
147
148 string_t yaml (string_t tab = "") const;
150 SEXP structure (void) const;
151};
152
153#endif
color_t
BALL COLORS.
Definition ball.h:12
@ green
Definition ball.h:12
static const char * colores[]
Definition ball.h:13
Balls function as pointers.
Definition ball.h:27
node_t * owner(void) const
view owner of a green ball
Definition ball.h:92
name_t uniq
Definition ball.h:35
static const size_t bytesize
size of binary serialization
Definition ball.h:41
node_t * holder(void) const
in whose pocket do I lie?
Definition ball.h:107
bool is(color_t c) const
is a given ball of the given color?
Definition ball.h:115
color_t color
Definition ball.h:36
Encodes a genealogical node.
Definition node.h:21
void insert(ball_t *a)
insert a ball into the pocket of a node
Definition node.h:156
A pocket is a set of balls.
Definition pocket.h:30
friend raw_t * operator>>(const pocket_t &p, raw_t *o)
binary serialization
Definition pocket.h:48
void clean(void)
delete balls and clear pocket
Definition pocket.h:35
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:81
ball_t * ball(const color_t c) const
retrieve the first ball of the specified color.
Definition pocket.h:130
SEXP structure(void) const
R list description.
Definition structure.cc:41
bool holds(color_t c) const
does this node hold a ball of this color?
Definition pocket.h:114
ball_t * last_ball(void) const
retrieve the last ball
Definition pocket.h:126
void repair_holder(node_t *p)
Definition pocket.h:72
~pocket_t(void)
destructor
Definition pocket.h:102
size_t bytesize(void) const
size of binary serialization
Definition pocket.h:44
string_t yaml(string_t tab="") const
human/machine-readable info
Definition yaml.cc:25
ball_t * first_ball(void) const
retrieve the first ball
Definition pocket.h:122
ball_t * other(const ball_t *b) const
return a pointer to another ball
Definition pocket.h:139
bool holds(ball_t *b) const
does this node hold the given ball?
Definition pocket.h:109
Rbyte raw_t
Definition internal.h:52
#define err(...)
Definition internal.h:18
#define n
Definition lbdp_pomp.c:9
std::set< ball_t *, ball_order >::const_reverse_iterator ball_rev_it
Definition pocket.h:25
std::set< ball_t *, ball_order >::const_iterator ball_it
Definition pocket.h:24
Ordering for balls in pockets.
Definition pocket.h:17
bool operator()(const ball_t *a, const ball_t *b) const
Definition pocket.h:18