8#include <unordered_map>
26typedef typename std::set<ball_t*,ball_order>::const_iterator
ball_it;
27typedef typename std::set<ball_t*,ball_order>::const_reverse_iterator
ball_rev_it;
32class pocket_t :
public std::set<ball_t*,ball_order> {
38 for (
ball_t *b : *
this)
delete b;
51 size_t psize = p.size();
52 memcpy(o,&psize,
sizeof(
size_t)); o +=
sizeof(size_t);
62 memcpy(&psize,o,
sizeof(
size_t)); o +=
sizeof(size_t);
63 for (
size_t i = 0; i < psize; i++) {
83 void repair_owners (
const std::unordered_map<name_t,node_t*>& node_name,
84 std::unordered_map<name_t,ball_t*> *ball_name) {
85 std::unordered_map<name_t,node_t*>::const_iterator
n;
88 n = node_name.find(b->
uniq);
89 if (
n != node_name.end()) {
92 ball_name->insert({b->
uniq,b});
94 err(
"in '%s' (%s line %d): cannot find ball %zd",
95 __func__,__FILE__,__LINE__,b->
uniq);
118 for (
ball_it i = begin(); !result && i != end(); i++) {
119 result = ((*i)->color == c);
130 if (b->
color == c)
return b;
132 err(
"in '%s' (%s line %d): no ball of color %s",
133 __func__,__FILE__,__LINE__,
colores[c]);
139 if (a != b)
return a;
141 err(
"error in '%s' (%s line %d): there is no other.",
142 __func__,__FILE__,__LINE__);
149 s += (*i)->describe(); ++i;
151 s +=
", " + (*i)->describe(); ++i;
159 PROTECT(o = NEW_LIST(size()));
161 for (
ball_rev_it i = crbegin(); i != crend(); i++) {
162 SET_ELEMENT(o,k++,(*i)->structure());
168 std::string
yaml (std::string tab =
"")
const {
170 std::string t = tab +
" ";
172 o += tab +
"- " + b->
yaml(t);
168 std::string
yaml (std::string tab =
"")
const {
…}
32class pocket_t :
public std::set<ball_t*,ball_order> {
…};
static const char * colores[]
Balls function as pointers.
node_t * owner(void) const
view owner of a green ball
std::string yaml(std::string tab="") const
machine-readable info
static const size_t bytesize
size of binary serialization
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.
A pocket is a set of balls.
friend raw_t * operator>>(const pocket_t &p, raw_t *o)
binary serialization
void clean(void)
delete balls and clear pocket
void repair_owners(const std::unordered_map< name_t, node_t * > &node_name, std::unordered_map< name_t, ball_t * > *ball_name)
ball_t * ball(const color_t c) const
retrieve the first ball of the specified color.
SEXP structure(void) const
R list description.
std::string yaml(std::string tab="") const
human/machine-readable info
bool holds(color_t c) const
does this node hold a ball of this color?
ball_t * last_ball(void) const
retrieve the last ball
void repair_holder(node_t *p)
std::string describe(void) const
human-readable info
~pocket_t(void)
destructor
size_t bytesize(void) const
size of binary serialization
ball_t * other(const ball_t *b) const
return a pointer to another ball
bool holds(ball_t *b) const
does this node hold the given ball?
std::set< ball_t *, ball_order >::const_reverse_iterator ball_rev_it
std::set< ball_t *, ball_order >::const_iterator ball_it
Ordering for balls in pockets.
bool operator()(const ball_t *a, const ball_t *b) const