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 for (ball_t *b : *this) {
84 if (b->is(green)) {
85 b->owner() = node_name.at(b->uniq);
86 (*ball_name)[b->uniq] = b;
87 }
88 }
89 };
90
91public:
92
94 ~pocket_t (void) {
95 clean();
96 };
97
98public:
99
101 bool holds (ball_t *b) const {
102 ball_it i = find(b);
103 return (i != end());
104 };
105
106 bool holds (color_t c) const {
107 bool result = false;
108 for (ball_it i = begin(); !result && i != end(); i++) {
109 result = ((*i)->color == c);
110 }
111 return result;
112 };
113
114 ball_t* first_ball (void) const {
115 return *cbegin();
116 };
117
118 ball_t* last_ball (void) const {
119 return *crbegin();
120 };
121
122 ball_t* ball (const color_t c) const {
123 ball_t *a = 0;
124 for (ball_t *b : *this) {
125 if (b->color == c) a = b;
126 }
127 assert(a != 0);
128 return a;
129 };
130
131 string_t yaml (string_t tab = "") const;
133 SEXP structure (void) const;
134};
135
136#endif
color_t
BALL COLORS.
Definition ball.h:12
@ green
Definition ball.h:12
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:23
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:122
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:106
ball_t * last_ball(void) const
retrieve the last ball
Definition pocket.h:118
void repair_holder(node_t *p)
Definition pocket.h:72
~pocket_t(void)
destructor
Definition pocket.h:94
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:114
bool holds(ball_t *b) const
does this node hold the given ball?
Definition pocket.h:101
Rbyte raw_t
Definition internal.h:52
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