8#include <unordered_map>
14typedef typename std::list<node_t*>::const_iterator
node_it;
15typedef typename std::list<node_t*>::iterator
node_nit;
16typedef typename std::list<node_t*>::const_reverse_iterator
node_rev_it;
25 for (
node_t *p : *
this)
delete p;
41 size_t s =
sizeof(size_t);
48 size_t nnode = G.size();
49 memcpy(o,&nnode,
sizeof(
size_t)); o +=
sizeof(size_t);
58 std::unordered_map<name_t,node_t*> node_names;
59 std::unordered_map<name_t,ball_t*> ball_names;
61 memcpy(&nnode,o,
sizeof(
size_t)); o +=
sizeof(size_t);
62 node_names.reserve(nnode);
63 ball_names.reserve(nnode);
64 for (
size_t i = 0; i < nnode; i++) {
68 node_names.insert({p->
uniq,p});
71 q->repair_owners(node_names,&ball_names);
83 std::unordered_map<name_t,ball_t*>::const_iterator
n;
85 n = names.find(p->
uniq);
86 assert(
n != names.end());
111 std::list<node_t*>::sort(
compare);
118 return (empty()) ? R_NaReal : front()->slate;
122 return (empty()) ? R_NaReal : back()->slate;
132 if (b->is(col)) p->insert(b);
156 while (i <
n && k != cend()) {
170 p->erase(a); q->insert(a); a->
holder() = q;
171 q->erase(b); p->insert(b); b->
holder() = p;
208 for (
node_rev_it i = crbegin(); i != crend(); i++) {
209 if ((*i)->size() == 1 && (*i)->holds(
green)) {
210 swap((*i)->last_ball(),(*i)->green_ball());
215 if ((*j)->dead_root()) {
246 for (
node_t *p : *
this ) {
248 if (b->color==
blue) {
267 virtual std::string
yaml (std::string tab =
"")
const {
269 std::string t = tab +
" ";
271 o += tab +
"- " + p->
yaml(t);
267 virtual std::string
yaml (std::string tab =
"")
const {
…}
278 PROTECT(Nodes = NEW_LIST(size()));
292 o += p->
newick(t,te) +
";";
Balls function as pointers.
name_t deme(void) const
view deme
node_t * holder(void) const
in whose pocket do I lie?
bool is(color_t c) const
is a given ball of the given color?
Encodes a genealogical node.
node_t * parent(void) const
name_t lineage(void) const
view lineage
size_t bytesize(void) const
size of binary serialization
bool dead_root(void) const
std::string newick(const slate_t &tnow, const slate_t &tpar) const
Newick format.
SEXP structure(void) const
R list description.
std::string describe(void) const
human-readable info
std::string yaml(std::string tab="") const
machine-readable info
name_t deme(void) const
view deme
ball_t * green_ball(void) const
pointer to my green ball
slate_t dawn(void) const
Earliest time in the sequence.
std::string describe(void) const
human-readable info
void clean(void)
clean up: delete all nodes, reset globals
void trace_lineage(ball_t *b, name_t u)
virtual std::string yaml(std::string tab="") const
human- & machine-readable info
size_t ntime(slate_t t) const
Number of distinct timepoints.
void sort(void)
order nodes in order of increasing time
friend raw_t * operator>>(const nodeseq_t &G, raw_t *o)
binary serialization
void destroy_node(node_t *p)
remove a dead root node
nodeseq_t & operator+=(nodeseq_t &other)
merge two node sequences
SEXP structure(void) const
R list description.
size_t bytesize(void) const
size of serialized binary form
void repair_owners(std::unordered_map< name_t, ball_t * > &names)
static bool compare(node_t *p, node_t *q)
void swap(ball_t *a, ball_t *b)
swap balls a and b, wherever they lie
size_t length(void) const
Number of nodes in the sequence.
pocket_t * colored(color_t col) const
Get all balls of a color.
void trace_lineages(void)
slate_t dusk(void) const
Latest time in the sequence.
node_t * position(int n)
traverse to nth node, retrieve pointer
~nodeseq_t(void)
destructor
std::string newick(slate_t t) const
put genealogy at time t into Newick format.
void add(node_t *p, ball_t *a)
A pocket is a set of balls.
static const name_t null_lineage
std::list< node_t * >::const_reverse_iterator node_rev_it
std::list< node_t * >::const_iterator node_it
std::list< node_t * >::iterator node_nit