19 template<
typename this_t>
35 BaseNode(
size_t n=0, this_t* p=
nullptr,
size_t i=0) : children(n), parent(p), pi(i) {
39 children = n.children;
47 children = std::move(n.children);
54 children = t.children;
60 children = std::move(t.children);
76 virtual std::string
string(
bool usedot=
true)
const {
101 using iterator_category = std::forward_iterator_tag;
102 using value_type = this_t;
103 using difference_type = int;
104 using pointer = this_t*;
105 using reference = this_t&;
119 assert(current !=
nullptr);
123 assert(current !=
nullptr);
129 assert(current !=
nullptr);
130 assert(not (*
this ==
EndNodeIterator) &&
"Can't iterate past the end!");
131 if(current ==
nullptr or current == start or current->is_root()) {
137 if(current->pi+1 < current->parent->children.size()) {
138 current = current->parent->children[current->pi+1].left_descend();
141 current = current->parent;
147 for(
size_t i=0;i<n;i++) this->
operator++();
161 return not (*
this == n);
182 return children.at(i);
185 const this_t&
child(
const size_t i)
const {
192 return children.at(i);
195 template<
typename... Args>
196 void fill(
size_t n, Args... args) {
202 for(
size_t i=0;i<n;i++) {
214 return children.size();
223 this_t* k =
const_cast<this_t*
>(
static_cast<const this_t*
>(
this));
224 while(k !=
nullptr and k->nchildren() > 0) {
232 for(
const auto& c: children) {
233 d = std::max(d, c.depth()+1);
246 for(
auto& c : children) {
248 c.parent =
static_cast<this_t*
>(
this);
259 for(
const auto& c : children) {
263 assert(c.parent ==
this);
276 return children.at(i);
280 return children.at(i);
290 while(children.size() <= i)
291 children.push_back(this_t());
295 children[i].parent =
static_cast<this_t*
>(
this);
305 while(children.size() <= i)
306 children.push_back(this_t());
310 children[i].parent =
static_cast<this_t*
>(
this);
325 return parent ==
nullptr;
334 this_t* x =
static_cast<this_t*
>(
this);
335 while(x->parent !=
nullptr) {
346 this_t*
get_via(std::function<
bool(this_t&)>& f ) {
347 for(
auto& n : *
this) {
361 for(
auto& c : children) {
372 virtual size_t count(
const this_t& n)
const {
373 size_t cnt = (n == *
static_cast<const this_t*
>(
this));
374 for(
auto& c : children) {
385 return children.size() == 0;
395 for(
const auto& n : *
this) {
396 if(n.is_terminal()) ++cnt;
401 virtual this_t*
get_nth(
int n, std::function<
int(
const this_t&)>& f) {
409 for(
auto& x : *
this) {
411 if(n == 0)
return &x;
419 std::function<int(const this_t&)> f = [](
const this_t& x) {
return 1;};
425 T
sum(std::function<T(
const this_t&)>& f )
const {
432 T s = f(* dynamic_cast<const this_t*>(
this));
433 for(
auto& c: this->children) {
440 T
sum(T(*f)(
const this_t&) )
const {
441 std::function ff = f;
446 bool all(std::function<
bool(
const this_t&)>& f )
const {
453 if(not f(* dynamic_cast<const this_t*>(
this)))
456 for(
auto& c: this->children) {
463 void map(
const std::function<
void(this_t&)>& f) {
469 f(* dynamic_cast<const this_t*>(
this));
470 for(
auto& c: this->children) {
478 std::string tabs(t,
'\t');
481 for(
auto& c : children) {
489 template<
typename this_t>
494 template<
typename this_t>
495 std::ostream& operator<<(std::ostream& o, const BaseNode<this_t>& t) {
virtual bool operator==(const this_t &n) const
Definition: BaseNode.h:82
void show(size_t t=0) const
Definition: BaseNode.h:476
virtual this_t * get_nth(int n, std::function< int(const this_t &)> &f)
Definition: BaseNode.h:401
size_t pi
Definition: BaseNode.h:27
void reserve_children(const size_t n)
Definition: BaseNode.h:164
decltype(children) & get_children()
Definition: BaseNode.h:168
std::vector< this_t > children
Definition: BaseNode.h:23
virtual bool is_terminal() const
Am I a terminal? I am if I have no children.
Definition: BaseNode.h:384
NodeIterator & operator++(int blah)
Definition: BaseNode.h:127
this_t * root()
Find the root of this node by walking up the tree.
Definition: BaseNode.h:333
void fix_child_info()
Definition: BaseNode.h:238
bool operator==(const NodeIterator &rhs) const
Definition: BaseNode.h:151
void push_back(this_t &n)
Definition: BaseNode.h:313
virtual std::string my_string() const
Definition: BaseNode.h:79
T sum(T(*f)(const this_t &)) const
Definition: BaseNode.h:440
void set_child(const size_t i, this_t &n)
Definition: BaseNode.h:283
this_t & operator*() const
Definition: BaseNode.h:118
this_t * get_via(std::function< bool(this_t &)> &f)
Return a pointer to the first node satisfying predicate f, in standard traversal; nullptr otherwise...
Definition: BaseNode.h:346
NodeIterator end() const
Definition: BaseNode.h:158
this_t & operator[](const size_t i)
Definition: BaseNode.h:269
decltype(children) const & get_children() const
Definition: BaseNode.h:171
T sum(std::function< T(const this_t &)> &f) const
Definition: BaseNode.h:425
NodeIterator & operator++()
Definition: BaseNode.h:128
virtual size_t count_terminals() const
Definition: BaseNode.h:388
BaseNode(this_t &&n)
Definition: BaseNode.h:44
void check_child_info() const
Definition: BaseNode.h:253
void map(const std::function< void(this_t &)> &f)
Definition: BaseNode.h:463
static NodeIterator EndNodeIterator
Definition: BaseNode.h:154
virtual bool operator!=(const this_t &n) const
Definition: BaseNode.h:160
BaseNode(size_t n=0, this_t *p=nullptr, size_t i=0)
Constructor of basenode – sizes children to n.
Definition: BaseNode.h:35
Definition: BaseNode.h:20
const this_t & child(const size_t i) const
Definition: BaseNode.h:185
void fill(size_t n, Args... args)
Definition: BaseNode.h:196
this_t * left_descend() const
Definition: BaseNode.h:217
NodeIterator(const this_t *n)
Definition: BaseNode.h:117
this_t & child(const size_t i)
Definition: BaseNode.h:175
void make_root()
Make a node root – just nulls the parent.
Definition: BaseNode.h:68
virtual std::string string(bool usedot=true) const
Definition: BaseNode.h:76
this_t * parent
Definition: BaseNode.h:26
Definition: BaseNode.h:95
#define ENDL
Definition: IO.h:21
virtual size_t count(const this_t &n) const
How many nodes below me are equal to n?
Definition: BaseNode.h:372
bool all(std::function< bool(const this_t &)> &f) const
Definition: BaseNode.h:446
BaseNode(const this_t &n)
Definition: BaseNode.h:38
bool operator!=(const NodeIterator &rhs) const
Definition: BaseNode.h:152
virtual this_t * get_nth(int n)
Definition: BaseNode.h:418
this_t * current
Definition: BaseNode.h:109
void operator=(const this_t &t)
Definition: BaseNode.h:51
size_t depth() const
Definition: BaseNode.h:230
const this_t * start
Definition: BaseNode.h:113
const this_t & operator[](const size_t i) const
Definition: BaseNode.h:279
void operator=(const this_t &&t)
Definition: BaseNode.h:57
void set_child(const size_t i, this_t &&n)
Definition: BaseNode.h:297
this_t * operator->() const
Definition: BaseNode.h:122
virtual ~BaseNode()
Definition: BaseNode.h:73
virtual size_t count() const
How many nodes total are below me?
Definition: BaseNode.h:358
NodeIterator & operator+(size_t n)
Definition: BaseNode.h:146
#define COUT
Definition: IO.h:24
NodeIterator begin() const
Definition: BaseNode.h:157
virtual bool is_root() const
Am I a root node? I am if my parent is nullptr.
Definition: BaseNode.h:324
size_t nchildren() const
Definition: BaseNode.h:208
void push_back(this_t &&n)
Definition: BaseNode.h:316