-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractTree.hpp
63 lines (50 loc) · 1.19 KB
/
AbstractTree.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#pragma once
#include <vector>
#include <string>
#include <fstream>
#include <cassert>
#include <iostream>
namespace peg {
class AbstractTree {
public:
struct Node {
std::string data;
std::vector<Node*> childs;
};
AbstractTree(Node* root) : root(root) {}
AbstractTree(const AbstractTree& other) = delete;
AbstractTree(AbstractTree&& other) = delete;
static inline void purge(const Node* root)
{
assert(root);
for (const auto& c: root->childs)
purge(c);
delete root;
}
inline void dump(std::ostream& out) const
{
out << "digraph Tree {\n";
dump_inner(root, out);
out << "}\n";
}
inline bool empty() const {
return root == nullptr;
}
~AbstractTree() {
if (root) purge(root);
}
private:
Node* root;
static inline void dump_inner(const Node* root, std::ostream& out)
{
out << "NODE" << root
<< "[shape=box label=\"" << root->data
<< "\"]\n";
for (const auto& c: root->childs) {
out << "NODE" << root
<< "-> NODE" << c << "\n";
dump_inner(c, out);
}
}
};
}