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 {
95 class NodeIterator :
public std::iterator<std::forward_iterator_tag, this_t> {
110 assert(current !=
nullptr);
114 assert(current !=
nullptr);
120 assert(current !=
nullptr);
121 assert(not (*
this ==
EndNodeIterator) &&
"Can't iterate past the end!");
122 if(current ==
nullptr or current == start or current->is_root()) {
128 if(current->pi+1 < current->parent->children.size()) {
129 current = current->parent->children[current->pi+1].left_descend();
132 current = current->parent;
138 for(
size_t i=0;i<n;i++) this->
operator++();
152 return not (*
this == n);
173 return children.at(i);
176 const this_t&
child(
const size_t i)
const {
183 return children.at(i);
186 template<
typename... Args>
187 void fill(
size_t n, Args... args) {
193 for(
size_t i=0;i<n;i++) {
205 return children.size();
214 this_t* k =
const_cast<this_t*
>(
static_cast<const this_t*
>(
this));
215 while(k !=
nullptr and k->nchildren() > 0) {
223 for(
const auto& c: children) {
224 d = std::max(d, c.depth()+1);
237 for(
auto& c : children) {
239 c.parent =
static_cast<this_t*
>(
this);
250 for(
const auto& c : children) {
254 assert(c.parent ==
this);
267 return children.at(i);
271 return children.at(i);
281 while(children.size() <= i)
282 children.push_back(this_t());
286 children[i].parent =
static_cast<this_t*
>(
this);
296 while(children.size() <= i)
297 children.push_back(this_t());
301 children[i].parent =
static_cast<this_t*
>(
this);
316 return parent ==
nullptr;
325 this_t* x =
static_cast<this_t*
>(
this);
326 while(x->parent !=
nullptr) {
337 this_t*
get_via(std::function<
bool(this_t&)>& f ) {
338 for(
auto& n : *
this) {
352 for(
auto& c : children) {
363 virtual size_t count(
const this_t& n)
const {
364 size_t cnt = (n == *
static_cast<const this_t*
>(
this));
365 for(
auto& c : children) {
376 return children.size() == 0;
386 for(
const auto& n : *
this) {
387 if(n.is_terminal()) ++cnt;
392 virtual this_t*
get_nth(
int n, std::function<
int(
const this_t&)>& f) {
400 for(
auto& x : *
this) {
402 if(n == 0)
return &x;
410 std::function<int(const this_t&)> f = [](
const this_t& x) {
return 1;};
416 T
sum(std::function<T(
const this_t&)>& f )
const {
423 T s = f(* dynamic_cast<const this_t*>(
this));
424 for(
auto& c: this->children) {
431 T
sum(T(*f)(
const this_t&) )
const {
432 std::function ff = f;
437 bool all(std::function<
bool(
const this_t&)>& f )
const {
444 if(not f(* dynamic_cast<const this_t*>(
this)))
447 for(
auto& c: this->children) {
454 void map(
const std::function<
void(this_t&)>& f) {
460 f(* dynamic_cast<const this_t*>(
this));
461 for(
auto& c: this->children) {
469 std::string tabs(t,
'\t');
472 for(
auto& c : children) {
480 template<
typename this_t>
485 template<
typename this_t>
486 std::ostream& operator<<(std::ostream& o, const BaseNode<this_t>& t) {
virtual bool operator==(const this_t &n) const
Definition: BaseNode.h:82
virtual this_t * get_nth(int n, std::function< int(const this_t &)> &f)
Definition: BaseNode.h:392
size_t pi
Definition: BaseNode.h:27
void reserve_children(const size_t n)
Definition: BaseNode.h:155
decltype(children) & get_children()
Definition: BaseNode.h:159
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:375
NodeIterator & operator++(int blah)
Definition: BaseNode.h:118
this_t * root()
Find the root of this node by walking up the tree.
Definition: BaseNode.h:324
void fix_child_info()
Definition: BaseNode.h:229
bool operator==(const NodeIterator &rhs) const
Definition: BaseNode.h:142
void push_back(this_t &n)
Definition: BaseNode.h:304
virtual std::string my_string() const
Definition: BaseNode.h:79
T sum(T(*f)(const this_t &)) const
Definition: BaseNode.h:431
void set_child(const size_t i, this_t &n)
Definition: BaseNode.h:274
void print(size_t t=0) const
Definition: BaseNode.h:467
this_t & operator*() const
Definition: BaseNode.h:109
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:337
NodeIterator end() const
Definition: BaseNode.h:149
this_t & operator[](const size_t i)
Definition: BaseNode.h:260
decltype(children) const & get_children() const
Definition: BaseNode.h:162
T sum(std::function< T(const this_t &)> &f) const
Definition: BaseNode.h:416
NodeIterator & operator++()
Definition: BaseNode.h:119
virtual size_t count_terminals() const
Definition: BaseNode.h:379
BaseNode(this_t &&n)
Definition: BaseNode.h:44
void check_child_info() const
Definition: BaseNode.h:244
void map(const std::function< void(this_t &)> &f)
Definition: BaseNode.h:454
static NodeIterator EndNodeIterator
Definition: BaseNode.h:145
virtual bool operator!=(const this_t &n) const
Definition: BaseNode.h:151
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:176
void fill(size_t n, Args... args)
Definition: BaseNode.h:187
this_t * left_descend() const
Definition: BaseNode.h:208
NodeIterator(const this_t *n)
Definition: BaseNode.h:108
this_t & child(const size_t i)
Definition: BaseNode.h:166
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:363
bool all(std::function< bool(const this_t &)> &f) const
Definition: BaseNode.h:437
BaseNode(const this_t &n)
Definition: BaseNode.h:38
bool operator!=(const NodeIterator &rhs) const
Definition: BaseNode.h:143
virtual this_t * get_nth(int n)
Definition: BaseNode.h:409
this_t * current
Definition: BaseNode.h:100
void operator=(const this_t &t)
Definition: BaseNode.h:51
size_t depth() const
Definition: BaseNode.h:221
const this_t * start
Definition: BaseNode.h:104
const this_t & operator[](const size_t i) const
Definition: BaseNode.h:270
void operator=(const this_t &&t)
Definition: BaseNode.h:57
void set_child(const size_t i, this_t &&n)
Definition: BaseNode.h:288
this_t * operator->() const
Definition: BaseNode.h:113
virtual ~BaseNode()
Definition: BaseNode.h:73
virtual size_t count() const
How many nodes total are below me?
Definition: BaseNode.h:349
NodeIterator & operator+(size_t n)
Definition: BaseNode.h:137
#define COUT
Definition: IO.h:24
NodeIterator begin() const
Definition: BaseNode.h:148
virtual bool is_root() const
Am I a root node? I am if my parent is nullptr.
Definition: BaseNode.h:315
size_t nchildren() const
Definition: BaseNode.h:199
void push_back(this_t &&n)
Definition: BaseNode.h:307