8 #include <unordered_map>
14 typedef typename std::list<node_t*>::const_iterator
node_it;
15 typedef typename std::list<node_t*>::iterator
node_nit;
16 typedef typename std::list<node_t*>::const_reverse_iterator
node_rev_it;
25 for (
node_it i = begin(); i != end(); i++)
delete *i;
41 size_t s =
sizeof(size_t);
42 for (
node_it i = begin(); i != end(); i++)
43 s += (*i)->bytesize();
48 size_t nnode = G.size();
49 memcpy(o,&nnode,
sizeof(
size_t)); o +=
sizeof(size_t);
50 for (
node_it i = G.begin(); i != G.end(); i++) {
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});
70 for (
node_it i = G.begin(); i != G.end(); i++) {
83 std::unordered_map<name_t,ball_t*>::const_iterator
n;
84 for (
node_it i = begin(); i != end(); i++) {
86 n = names.find(p->
uniq);
87 assert(
n != names.end());
112 std::list<node_t*>::sort(
compare);
119 return (empty()) ? R_NaReal : front()->slate;
123 return (empty()) ? R_NaReal : back()->slate;
131 for (
node_it i = begin(); i != end(); i++) {
132 for (
ball_it j = (*i)->begin(); j != (*i)->end(); j++) {
133 if ((*j)->is(col)) p->insert(*j);
141 for (
node_it i = begin(); i != end(); i++) {
142 if (t < (*i)->slate) {
152 for (
node_it i = begin(); i != end(); i++) count++;
159 while (i <
n && k != cend()) {
173 p->erase(a); q->insert(a); a->
holder() = q;
174 q->erase(b); p->insert(b); b->
holder() = p;
211 for (
node_rev_it i = crbegin(); i != crend(); i++) {
212 if ((*i)->size() == 1 && (*i)->holds(
green)) {
213 swap((*i)->last_ball(),(*i)->green_ball());
218 if ((*j)->dead_root()) {
249 for (
node_it i = begin(); i != end(); i++) {
251 for (
ball_it j = p->begin(); j != p->end(); j++) {
266 for (
node_it p = begin(); p != end(); p++) {
267 o += (*p)->describe();
272 virtual std::string
yaml (std::string tab =
"")
const {
274 std::string t = tab +
" ";
275 for (
node_it p = begin(); p != end(); p++) {
276 o += tab +
"- " + (*p)->yaml(t);
283 PROTECT(Nodes = NEW_LIST(size()));
285 for (
node_it i = begin(); i != end(); i++) {
286 SET_ELEMENT(Nodes,k++,(*i)->structure());
295 for (
node_it i = begin(); i != end(); i++) {
296 if ((*i)->is_root()) {
297 o += (*i)->newick(t,te) +
";";
Balls function as pointers.
name_t deme(void) const
view deme
bool is(color_t c) const
is a given ball of the given color?
node_t * holder(void) const
in whose pocket do I lie?
Encodes a genealogical node.
name_t lineage(void) const
view lineage
bool dead_root(void) const
node_t * parent(void) const
ball_t * green_ball(void) const
pointer to my green ball
name_t deme(void) const
view deme
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.
node_t * position(int n)
traverse to nth node, retrieve pointer
void sort(void)
order nodes in order of increasing time
nodeseq_t & operator+=(nodeseq_t &other)
merge two node sequences
void destroy_node(node_t *p)
remove a dead root node
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.
void trace_lineages(void)
slate_t dusk(void) const
Latest time in the sequence.
pocket_t * colored(color_t col) const
Get all balls of a color.
~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)
friend raw_t * operator>>(const nodeseq_t &G, raw_t *o)
binary serialization
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
std::set< ball_t *, ball_order >::const_iterator ball_it