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[p->
uniq] = p;
71 q->repair_owners(node_names,&ball_names);
89 std::unordered_map<name_t, node_t*>
91 std::unordered_map<name_t, node_t*> m;
112 std::list<node_t*>::sort(
compare);
122 if (b->is(col)) p->insert(b);
146 while (i <
n && k != cend()) {
158 p->erase(b); q->
insert(b);
165 p->erase(a); q->
insert(a);
166 q->erase(b); p->
insert(b);
214 if ((*j)->dead_root()) {
248 std::unordered_map<name_t, std::vector<node_t*>>
250 std::unordered_map<name_t, std::vector<node_t*>> children;
251 children.reserve(size());
266 const std::unordered_map<name_t, slate_t>& height
268 std::vector<node_t*> roots;
270 if (p->
is_root()) roots.push_back(p);
271 std::sort(roots.begin(), roots.end(),
273 return height.at(a->uniq) > height.at(b->uniq);
284 std::unordered_map<
name_t,std::vector<node_t*>>& children
286 std::unordered_map<name_t, slate_t> height;
287 height.reserve(size());
288 for (
auto it = rbegin(); it != rend(); ++it) {
290 auto& ch = children.at(p->
uniq);
294 std::sort(ch.begin(), ch.end(),
296 return height.at(a->uniq) > height.at(b->uniq);
298 height[p->
uniq] = height.at(ch[0]->uniq);
314 for (
node_t *p : *
this ) {
316 if (b->color==
blue) {
327 string_t
yaml (string_t tab =
"")
const;
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
name_t deme(void) const
view deme
ball_t * green_ball(void) const
pointer to my green ball
void insert(ball_t *a)
insert a ball into the pocket of a node
void weed(void)
drop all dead roots
std::unordered_map< name_t, std::vector< node_t * > > children_map(void) const
map nodes onto vector of children
void clean(void)
clean up: delete all nodes, reset globals
void trace_lineage(ball_t *b, name_t u)
string_t newick(slate_t t, slate_t te, bool showdeme, bool extended) const
put genealogy at time t into Newick format.
std::unordered_map< name_t, node_t * > node_map(void) const
map node names onto pointers
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
void attach(node_t *p, node_t *q)
std::vector< node_t * > ladderize(std::unordered_map< name_t, std::vector< node_t * > > &children) const
SEXP structure(void) const
R list description.
size_t bytesize(void) const
size of serialized binary form
string_t yaml(string_t tab="") const
human/machine-readable info
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.
void move(ball_t *b, node_t *p, node_t *q)
move ball b from p to q
pocket_t * colored(color_t col) const
Get all balls of a color.
void trace_lineages(void)
std::vector< node_t * > sorted_roots(const std::unordered_map< name_t, slate_t > &height) const
collect root nodes and sort them in order of decreasing subtree height
node_t * position(int n)
traverse to nth node, retrieve pointer
void repair_owners(const std::unordered_map< name_t, ball_t * > &names)
~nodeseq_t(void)
destructor
void add(node_t *p, ball_t *a)
A pocket is a set of balls.
ball_t * last_ball(void) const
retrieve the last ball
bool holds(ball_t *b) const
does this node hold the given ball?
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