-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.cpp
executable file
·200 lines (165 loc) · 5.04 KB
/
helper.cpp
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "helper.h"
std::vector<std::string> texts;
char *empty_str = strdup("");
char *label_str = strdup("label");
char *shape_str = strdup("shape");
char *style_str = strdup("style");
char *weight_str = strdup("weight");
char *ten_weight_str = strdup("10");
char *invis_str = strdup("invis");
char *none_str = strdup("none");
std::map<int, char *> num_strs;
int get_key_from_text(char *text)
{
auto result = std::find(texts.begin(), texts.end(), text);
if (result == texts.end())
{
result = texts.insert(texts.end(), text);
}
return result - texts.begin();
}
char *num_to_str(int num)
{
auto num_pos = num_strs.find(num);
if (num_pos == num_strs.end())
{
std::string num_str = std::to_string(num);
num_pos = num_strs.insert(num_pos, std::make_pair(num, strdup(num_str.c_str())));
}
return num_pos->second;
}
const std::string *get_text_from_key(int key)
{
if (key >= texts.size())
{
return nullptr;
}
return (const std::string *)(&texts[key]);
}
void free_text_key(int key)
{
if (key >= texts.size())
{
return;
}
texts.erase(texts.begin() + key);
}
Node *new_node(int text_key, Node *first_child, Node *next_sibling)
{
Node *node = new Node;
if (!node) { yyerror("out of memory\n"); exit(1); }
node->text_key = text_key;
node->first_child = first_child;
node->next_sibling = next_sibling;
node->num_children = count_children(node);
return node;
}
int count_children(Node *parent)
{
int num_children = 0;
Node *curr = parent->first_child;
while (curr != nullptr)
{
++num_children;
curr = curr->next_sibling;
}
return num_children;
}
void insert_sibling(Node *first_child, Node *new_sibling)
{
Node *curr = first_child->next_sibling;
if (curr == nullptr)
{
first_child->next_sibling = new_sibling;
}
else
{
while (curr->next_sibling != nullptr)
{
curr = curr->next_sibling;
}
curr->next_sibling = new_sibling;
}
}
// NOTE: What is being done with the strings here is usually dangerous.
// However, I am assuming gvc does not actually do anything with these "non-const"
// strings, so I just simply convert const strings to a normal "char *".
// I unfortunately cannot just pass "const char *" into gvc functions, since
// they all take "char *" and
char *get_next_node_name()
{
static int curr = 0;
return num_to_str(curr++);
}
Agnode_t *node_to_graphviz(Agraph_t *g, Node *node)
{
char *node_name = get_next_node_name();
Agnode_t *g_node = agnode(g, node_name, 1);
Node *curr = node->first_child;
Agnode_t *g_curr = nullptr;
int current_child_index = 1;
bool should_create_middle = curr != nullptr && node->num_children > 0 && node->num_children % 2 == 0;
// Draw all the children
while (curr != nullptr)
{
g_curr = node_to_graphviz(g, curr);
Agedge_t *node_child_edge = agedge(g, g_node, g_curr, get_next_node_name(), 1);
// agsafeset(node_child_edge, weight_str, ten_weight_str, empty_str);
// "Invisible middle" is a hacky (but apparently officially given as a solution,
// see https://graphviz.org/faq/#FaqBalanceTree) way of balancing the tree, such
// that parents will be centered above its children.
if (should_create_middle && current_child_index == node->num_children / 2)
{
Agnode_t *g_invisible_middle = nullptr;
char *middle_name = get_next_node_name();
g_invisible_middle = agnode(g, middle_name, 1);
agsafeset(g_invisible_middle, label_str, empty_str, empty_str);
agsafeset(g_invisible_middle, style_str, invis_str, empty_str);
agsafeset(g_invisible_middle, shape_str, none_str, empty_str);
// agsafeset(g_invisible_middle, width_str, zero_width_str, empty_str);
Agedge_t *middle_edge = agedge(g, g_node, g_invisible_middle, get_next_node_name(), 1);
agsafeset(middle_edge, style_str, invis_str, empty_str);
agsafeset(middle_edge, weight_str, ten_weight_str, empty_str);
}
curr = curr->next_sibling;
++current_child_index;
}
// Set some attributes for the node
char *label = (char *)(get_text_from_key(node->text_key)->c_str());
agsafeset(g_node, "label", label, empty_str);
agsafeset(g_node, shape_str, none_str, empty_str);
return g_node;
}
void node_free(Node *node)
{
if (node->first_child != nullptr)
{
node_free(node->first_child);
}
if (node->next_sibling != nullptr)
{
node_free(node->next_sibling);
}
free_text_key(node->text_key);
free(node);
}
void graph_strings_free()
{
for (auto it = num_strs.begin(); it != num_strs.end(); ++it)
{
delete (*it).second;
}
num_strs.clear();
}
void yyerror(const char *s, ...)
{
va_list args;
va_start(args, s);
fprintf(stderr, "%d: error: ", yylineno);
vprintf(s, args);
fprintf(stderr, "\n");
}
int main()
{
return yyparse();
}