phylopomp
Phylodynamics for POMPs
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
pocket_t Class Reference

A pocket is a set of balls. More...

#include <pocket.h>

Inheritance diagram for pocket_t:
Collaboration diagram for pocket_t:

Public Member Functions

size_t bytesize (void) const
 size of binary serialization More...
 
void repair_owners (const std::unordered_map< name_t, node_t * > &node_name, std::unordered_map< name_t, ball_t * > *ball_name)
 
 ~pocket_t (void)
 destructor More...
 
bool holds (ball_t *b) const
 does this node hold the given ball? More...
 
bool holds (color_t c) const
 does this node hold a ball of this color? More...
 
ball_tlast_ball (void) const
 retrieve the last ball More...
 
ball_tball (const color_t c) const
 retrieve the first ball of the specified color. More...
 
ball_tother (const ball_t *b) const
 return a pointer to another ball More...
 
std::string describe (void) const
 human-readable info More...
 
SEXP structure (void) const
 R list description. More...
 
std::string yaml (std::string tab="") const
 human/machine-readable info More...
 

Protected Member Functions

void repair_holder (node_t *p)
 

Private Member Functions

void clean (void)
 delete balls and clear pocket More...
 

Friends

raw_toperator>> (const pocket_t &p, raw_t *o)
 binary serialization More...
 
raw_toperator>> (raw_t *o, pocket_t &p)
 

Detailed Description

A pocket is a set of balls.

An order relation among balls ensures the uniqueness of the internal representation.

Definition at line 32 of file pocket.h.

Constructor & Destructor Documentation

◆ ~pocket_t()

pocket_t::~pocket_t ( void  )
inline

destructor

Definition at line 105 of file pocket.h.

105  {
106  clean();
107  };
void clean(void)
delete balls and clear pocket
Definition: pocket.h:37
Here is the call graph for this function:

Member Function Documentation

◆ ball()

ball_t* pocket_t::ball ( const color_t  c) const
inline

retrieve the first ball of the specified color.

Definition at line 129 of file pocket.h.

129  {
130  for (ball_it i = begin(); i != end(); i++) {
131  if ((*i)->color == c) return *i;
132  }
133  err("in '%s' (%s line %d): no ball of color %s", // # nocov
134  __func__,__FILE__,__LINE__,colores[c]); // # nocov
135  return 0;
136  };
static const char * colores[]
Definition: ball.h:15
#define err(...)
Definition: internal.h:18
std::set< ball_t *, ball_order >::const_iterator ball_it
Definition: pocket.h:26

◆ bytesize()

size_t pocket_t::bytesize ( void  ) const
inline

size of binary serialization

Definition at line 46 of file pocket.h.

46  {
47  return sizeof(size_t) + size()*(ball_t::bytesize);
48  };
static const size_t bytesize
size of binary serialization
Definition: ball.h:43
Here is the caller graph for this function:

◆ clean()

void pocket_t::clean ( void  )
inlineprivate

delete balls and clear pocket

Definition at line 37 of file pocket.h.

37  {
38  for (ball_it i = begin(); i != end(); i++) delete *i;
39  clear();
40  };
Here is the caller graph for this function:

◆ describe()

std::string pocket_t::describe ( void  ) const
inline

human-readable info

Definition at line 147 of file pocket.h.

147  {
148  std::string s = "{";
149  ball_it i = begin();
150  s += (*i)->describe(); ++i;
151  while (i != end()) {
152  s += ", " + (*i)->describe(); ++i;
153  }
154  s += "}";
155  return s;
156  };
Here is the caller graph for this function:

◆ holds() [1/2]

bool pocket_t::holds ( ball_t b) const
inline

does this node hold the given ball?

Definition at line 112 of file pocket.h.

112  {
113  ball_it i = find(b);
114  return (i != end());
115  };
Here is the caller graph for this function:

◆ holds() [2/2]

bool pocket_t::holds ( color_t  c) const
inline

does this node hold a ball of this color?

Definition at line 117 of file pocket.h.

117  {
118  bool result = false;
119  for (ball_it i = begin(); !result && i != end(); i++) {
120  result = ((*i)->color == c);
121  }
122  return result;
123  };

◆ last_ball()

ball_t* pocket_t::last_ball ( void  ) const
inline

retrieve the last ball

Definition at line 125 of file pocket.h.

125  {
126  return *crbegin();
127  };
Here is the caller graph for this function:

◆ other()

ball_t* pocket_t::other ( const ball_t b) const
inline

return a pointer to another ball

Definition at line 138 of file pocket.h.

138  {
139  for (ball_it i = begin(); i != end(); i++) {
140  if (*i != b) return *i;
141  }
142  err("error in '%s' (%s line %d): there is no other.", // # nocov
143  __func__,__FILE__,__LINE__); // # nocov
144  return 0;
145  };

◆ repair_holder()

void pocket_t::repair_holder ( node_t p)
inlineprotected

Needed in deserialization. Inform all balls as to their holder.

Definition at line 74 of file pocket.h.

74  {
75  for (ball_it i = begin(); i != end(); i++) {
76  (*i)->holder() = p;
77  }
78  };

◆ repair_owners()

void pocket_t::repair_owners ( const std::unordered_map< name_t, node_t * > &  node_name,
std::unordered_map< name_t, ball_t * > *  ball_name 
)
inline

Needed in deserialization. This function repairs the links green balls and their names.

Definition at line 83 of file pocket.h.

84  {
85  std::unordered_map<name_t,node_t*>::const_iterator n;
86  for (ball_it i = begin(); i != end(); i++) {
87  ball_t *b = *i;
88  if (b->is(green)) {
89  n = node_name.find(b->uniq);
90  if (n != node_name.end()) {
91  node_t *p = n->second;
92  b->owner() = p;
93  ball_name->insert({b->uniq,b});
94  } else {
95  err("in '%s' (%s line %d): cannot find ball %zd", // #nocov
96  __func__,__FILE__,__LINE__,b->uniq); // #nocov
97  }
98  }
99  }
100  };
@ green
Definition: ball.h:14
Balls function as pointers.
Definition: ball.h:29
node_t * owner(void) const
view owner of a green ball
Definition: ball.h:94
name_t uniq
Definition: ball.h:37
bool is(color_t c) const
is a given ball of the given color?
Definition: ball.h:117
Encodes a genealogical node.
Definition: node.h:23
#define n
Definition: lbdp_pomp.c:8
Here is the call graph for this function:

◆ structure()

SEXP pocket_t::structure ( void  ) const
inline

R list description.

Definition at line 158 of file pocket.h.

158  {
159  SEXP o;
160  PROTECT(o = NEW_LIST(size()));
161  int k = 0;
162  for (ball_rev_it i = crbegin(); i != crend(); i++) {
163  SET_ELEMENT(o,k++,(*i)->structure());
164  }
165  UNPROTECT(1);
166  return o;
167  };
std::set< ball_t *, ball_order >::const_reverse_iterator ball_rev_it
Definition: pocket.h:27
Here is the caller graph for this function:

◆ yaml()

std::string pocket_t::yaml ( std::string  tab = "") const
inline

human/machine-readable info

Definition at line 169 of file pocket.h.

169  {
170  std::string o = "";
171  std::string t = tab + " ";
172  for (ball_it i = begin(); i != end(); i++) {
173  o += tab + "- " + (*i)->yaml(t);
174  }
175  return o;
176  };
Here is the caller graph for this function:

Friends And Related Function Documentation

◆ operator>> [1/2]

raw_t* operator>> ( const pocket_t p,
raw_t o 
)
friend

binary serialization

Definition at line 50 of file pocket.h.

50  {
51  size_t psize = p.size();
52  memcpy(o,&psize,sizeof(size_t)); o += sizeof(size_t);
53  for (ball_it i = p.begin(); i != p.end(); i++)
54  o = (**i >> o);
55  return o;
56  };

◆ operator>> [2/2]

raw_t* operator>> ( raw_t o,
pocket_t p 
)
friend

binary deserialization. this leaves the balls without knowledge of their holder.

Definition at line 59 of file pocket.h.

59  {
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  };

The documentation for this class was generated from the following file: