phylopomp
Phylodynamics for POMPs
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
inventory.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 // INVENTORY CLASS
3 
4 #ifndef _INVENTORY_H_
5 #define _INVENTORY_H_
6 
7 #include <utility>
8 #include "ball.h"
9 #include "pocket.h"
10 #include "node.h"
11 #include "internal.h"
12 
14 
17 template <size_t NDEME = 1>
18 class inventory_t {
19 
20 private:
21 
22  pocket_t _inven[NDEME];
23  const static size_t ndeme = NDEME;
24 
25 public:
26 
27  // CONSTRUCTORS, DESTRUCTORS, ETC.
29  inventory_t (void) = default;
32  o >> *this;
33  };
36  inventory_t (std::pair<node_it,node_it>&& I) {
37  clean();
38  for (node_it i = I.first; i != I.second; i++) {
39  for (ball_it j = (*i)->begin(); j != (*i)->end(); j++) {
40  insert(*j); // 'insert' checks color
41  }
42  }
43  };
45  inventory_t (const inventory_t &) = default;
47  inventory_t (inventory_t &&) = delete;
49  inventory_t& operator= (std::pair<node_it,node_it>&& I) {
50  clean();
51  for (node_it i = I.first; i != I.second; i++) {
52  for (ball_it j = (*i)->begin(); j != (*i)->end(); j++) {
53  insert(*j); // 'insert' checks color
54  }
55  }
56  return *this;
57  };
59  inventory_t & operator= (const inventory_t &) = default;
63  ~inventory_t (void) {
64  clean();
65  };
67  void clean (void) {
68  clear();
69  };
71  void clear (void) {
72  for (size_t i = 0; i < ndeme; i++)
73  _inven[i].clear();
74  };
75  // SERIALIZATION
77  size_t bytesize (void) const {
78  size_t s = 0;
79  for (size_t i = 0; i < ndeme; i++)
80  s += _inven[i].bytesize();
81  return s;
82  };
84  friend raw_t* operator>> (const inventory_t& I, raw_t* o) {
85  for (size_t i = 0; i < ndeme; i++) {
86  o = (I._inven[i] >> o);
87  }
88  return o;
89  };
92  I.clean();
93  for (size_t i = 0; i < ndeme; i++) {
94  o = (o >> I._inven[i]);
95  }
96  return o;
97  };
100  size_t size (void) const {
101  size_t n = 0;
102  for (name_t i = 0; i < ndeme; i++) {
103  n += size(i);
104  }
105  return n;
106  };
108  size_t size (name_t i) const {
109  return _inven[i].size();
110  };
113  return _inven[n];
114  };
116  bool empty (void) const {
117  bool q = true;
118  for (name_t i = 0; i < ndeme; i++) {
119  q = q && _inven[i].empty();
120  }
121  return q;
122  };
124  ball_t* random_ball (name_t i = 0) const {
125  name_t n = _inven[i].size();
126  assert(n > 0);
127  name_t draw = random_integer(n);
128  ball_it k = _inven[i].begin();
129  while (draw-- > 0) k++;
130  return *k;
131  };
133  pocket_t* random_balls (name_t i = 0, int n = 1) const {
134  pocket_t *p = new pocket_t();
135  if (n == 1) {
136  ball_t *b = random_ball(i);
137  p->insert(b);
138  } else if (n > 1) {
139  int N = _inven[i].size();
140  assert(N > 0);
141  assert(n <= N);
142  ball_it j = _inven[i].begin();
143  int k = 0, m = 0;
144  while (m < n && k < N) {
145  int u = random_integer(N-k);
146  if (u < n-m) {
147  p->insert(*j);
148  m++;
149  }
150  k++; j++;
151  }
152  } else {
153  assert(0); // #nocov
154  }
155  return p;
156  };
160  void insert (ball_t *b) {
161  if (b->is(black)) {
162  _inven[b->deme()].insert(b);
163  }
164  };
168  void erase (ball_t *b) {
169  if (b->is(black)) {
170  assert(!(_inven[b->deme()].empty()));
171  _inven[b->deme()].erase(b);
172  }
173  };
174 };
175 
176 #endif
@ black
Definition: ball.h:14
Balls function as pointers.
Definition: ball.h:29
name_t deme(void) const
view deme
Definition: ball.h:86
bool is(color_t c) const
is a given ball of the given color?
Definition: ball.h:117
Representation for the inventory process.
Definition: inventory.h:18
void insert(ball_t *b)
Definition: inventory.h:160
inventory_t(void)=default
basic constructor for inventory class
void clean(void)
memory cleanup
Definition: inventory.h:67
size_t size(name_t i) const
size of deme
Definition: inventory.h:108
inventory_t(const inventory_t &)=default
copy constructor
inventory_t(raw_t *o)
constructor from serialized binary form
Definition: inventory.h:31
size_t bytesize(void) const
size of serialized binary form
Definition: inventory.h:77
pocket_t * random_balls(name_t i=0, int n=1) const
choose a random set of n balls from deme i
Definition: inventory.h:133
ball_t * random_ball(name_t i=0) const
choose one random ball from deme i
Definition: inventory.h:124
inventory_t(inventory_t &&)=delete
move constructor
~inventory_t(void)
destructor
Definition: inventory.h:63
inventory_t(std::pair< node_it, node_it > &&I)
Definition: inventory.h:36
pocket_t _inven[NDEME]
Definition: inventory.h:22
static const size_t ndeme
Definition: inventory.h:23
void clear(void)
memory cleanup
Definition: inventory.h:71
size_t size(void) const
Definition: inventory.h:100
bool empty(void) const
are all demes empty?
Definition: inventory.h:116
pocket_t & operator[](const name_t n)
return the n-th deme
Definition: inventory.h:112
void erase(ball_t *b)
Definition: inventory.h:168
friend raw_t * operator>>(const inventory_t &I, raw_t *o)
binary serialization
Definition: inventory.h:84
inventory_t & operator=(std::pair< node_it, node_it > &&I)
copy an inventory by iterating over a node sequence
Definition: inventory.h:49
A pocket is a set of balls.
Definition: pocket.h:32
Rbyte raw_t
Definition: internal.h:43
size_t name_t
Definition: internal.h:45
static int random_integer(int n)
Definition: internal.h:48
#define n
Definition: lbdp_pomp.c:8
std::list< node_t * >::const_iterator node_it
Definition: nodeseq.h:14
std::set< ball_t *, ball_order >::const_iterator ball_it
Definition: pocket.h:26
#define N
Definition: seirs_pomp.c:32
#define I
Definition: seirs_pomp.c:35