-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathgraph.cpp
More file actions
369 lines (296 loc) · 7.76 KB
/
graph.cpp
File metadata and controls
369 lines (296 loc) · 7.76 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#include <util/graph.h>
#include "graphpimpl.h"
namespace cc
{
namespace util
{
Graph::Graph(const std::string name_, bool directed_, bool strict_)
: _graphPimpl(new GraphPimpl(name_, directed_, strict_)),
_directed(directed_), _strict(strict_), _isSubgraph(false)
{
setAttribute("fontsize", "11");
}
Graph::Graph(Graph&& other) noexcept
: _ids(std::move(other._ids)),
_directed(other._directed),
_strict(other._strict),
_isSubgraph(other._isSubgraph)
{
std::swap(this->_graphPimpl, other._graphPimpl);
}
Graph::~Graph()
{
delete _graphPimpl;
}
// TODO: layout algorithm
std::string Graph::dotToSvg(const std::string& graph_)
{
GVC_t* gvc = gvContext();
Agraph_t* graph = agmemread(const_cast<char*>(graph_.c_str()));
gvLayout(gvc, graph, "dot");
char** result = new char*;
unsigned int* length = new unsigned int;
gvRenderData(gvc, graph, "dot", result, length);
gvFreeLayout(gvc, graph);
std::string res = *result;
delete *result;
delete length;
agclose(graph);
gvFreeContext(gvc);
return res;
}
bool Graph::isDirected() const
{
return _directed;
}
bool Graph::isStrict() const
{
return _strict;
}
bool Graph::isSubGraph() const
{
return _isSubgraph;
}
int Graph::nodeCount() const
{
return agnnodes(_graphPimpl->_graph);
}
int Graph::edgeCount() const
{
return agnedges(_graphPimpl->_graph);
}
Graph::Node Graph::createNode(const Subgraph& subgraph_)
{
return getOrCreateNode(generateId(), subgraph_);
}
Graph::Node Graph::getOrCreateNode(
const Graph::Node& id_,
const Subgraph& subgraph_)
{
std::string id = id_.empty() ? generateId() : id_;
_ids.insert(id);
agnode(
subgraph_.empty()
? _graphPimpl->_graph
: _graphPimpl->_subgMap[subgraph_],
const_cast<char*>(id.c_str()),
1);
setNodeAttribute(id_, "fontsize", "11");
setNodeAttribute(id_, "id", id);
return id;
}
Graph::Edge Graph::createEdge(const Node& from_, const Node& to_)
{
std::string id = generateId();
_graphPimpl->_edgeMap[id] = agedge(
_graphPimpl->_graph,
agnode(_graphPimpl->_graph, const_cast<char*>(from_.c_str()), 0),
agnode(_graphPimpl->_graph, const_cast<char*>(to_.c_str()), 0),
const_cast<char*>(id.c_str()),
1);
setEdgeAttribute(id, "fontsize", "11");
setEdgeAttribute(id, "id", id);
return id;
}
Graph::Subgraph Graph::getOrCreateSubgraph(const std::string& id_)
{
std::string id = id_.empty() ? generateId() : id_;
_ids.insert(id);
_graphPimpl->_subgMap[id]
= agsubg(_graphPimpl->_graph, const_cast<char*>(id.c_str()), 1);
return id;
}
bool Graph::hasEdge(const Node& from_, const Node& to_) const
{
return agedge(
_graphPimpl->_graph,
agnode(_graphPimpl->_graph, const_cast<char*>(from_.c_str()), 0),
agnode(_graphPimpl->_graph, const_cast<char*>(to_.c_str()), 0),
0,
0);
}
bool Graph::hasNode(const Node& node_) const
{
return agnode(_graphPimpl->_graph, const_cast<char*>(node_.c_str()), 0);
}
void Graph::delNode(const Node& node_)
{
agdelnode(
_graphPimpl->_graph,
agnode(_graphPimpl->_graph, const_cast<char*>(node_.c_str()), 0));
_ids.erase(node_);
}
void Graph::delEdge(const Node& from_, const Node& to_)
{
agdeledge(
_graphPimpl->_graph,
agedge(_graphPimpl->_graph,
agnode(_graphPimpl->_graph, const_cast<char*>(from_.c_str()), 0),
agnode(_graphPimpl->_graph, const_cast<char*>(to_.c_str()), 0),
0,
0));
}
void Graph::setAttribute(const std::string& key_, const std::string& value_)
{
agsafeset(
_graphPimpl->_graph,
const_cast<char*>(key_.c_str()),
const_cast<char*>(value_.c_str()),
const_cast<char*>(""));
}
void Graph::setNodeAttribute(
const Node& node_,
const std::string& key_,
const std::string& value_,
bool html_)
{
const char* value = value_.c_str();
if (html_)
value = agstrdup_html(_graphPimpl->_graph,
const_cast<char*>(value_.c_str()));
agsafeset(
agnode(_graphPimpl->_graph, const_cast<char*>(node_.c_str()), 0),
const_cast<char*>(key_.c_str()),
const_cast<char*>(value),
const_cast<char*>(""));
}
void Graph::setEdgeAttribute(
const Edge& edge_,
const std::string& key_,
const std::string& value_,
bool html_)
{
const char* value = value_.c_str();
if (html_)
value = agstrdup_html(_graphPimpl->_graph,
const_cast<char*>(value_.c_str()));
agsafeset(
_graphPimpl->_edgeMap[edge_],
const_cast<char*>(key_.c_str()),
const_cast<char*>(value),
const_cast<char*>(""));
}
void Graph::setSubgraphAttribute(
const Subgraph& graph_,
const std::string& key_,
const std::string& value_,
bool html_)
{
const char* value = value_.c_str();
if (html_)
value = agstrdup_html(_graphPimpl->_graph,
const_cast<char*>(value_.c_str()));
agsafeset(
_graphPimpl->_subgMap[graph_],
const_cast<char*>(key_.c_str()),
const_cast<char*>(value),
const_cast<char*>(""));
}
void Graph::setNodeAttribute(
const Node& targetNode_,
const Node& sourceNode_)
{
agcopyattr(
agnode(_graphPimpl->_graph, const_cast<char*>(sourceNode_.c_str()), 0),
agnode(_graphPimpl->_graph, const_cast<char*>(targetNode_.c_str()), 0));
}
void Graph::setEdgeAttribute(
const Edge& targetEdge_,
const Edge& sourceEdge_)
{
agcopyattr(
_graphPimpl->_edgeMap[sourceEdge_],
_graphPimpl->_edgeMap[targetEdge_]);
}
std::string Graph::getNodeAttribute(
const Node& node_,
const std::string& key_)
{
auto ret =
agget(agnode(_graphPimpl->_graph, const_cast<char*>(node_.c_str()), 0),
const_cast<char*>(key_.c_str()));
return ret ? ret : "";
}
std::string Graph::getEdgeAttribute(const Edge& edge_, const std::string& key_)
{
auto ret =
agget(_graphPimpl->_edgeMap[edge_], const_cast<char*>(key_.c_str()));
return ret ? ret : "";
}
// TODO: layout algorithm
// TODO: It's almost the same as dotToSvg() -> should be extracted.
// TODO: Called twice after each other it segfaults.
std::string Graph::output(Graph::Format format_) const
{
char** result = new char*;
unsigned int* length = new unsigned int;
gvLayout(_graphPimpl->_gvc, _graphPimpl->_graph, "dot");
const char* render_format;
switch (format_) {
case Graph::DOT:
render_format = "dot";
break;
case Graph::SVG:
render_format = "svg";
break;
case Graph::CAIRO_SVG:
render_format = "svg:cairo";
break;
default:
__builtin_unreachable();
break;
}
gvRenderData(
_graphPimpl->_gvc,
_graphPimpl->_graph,
render_format,
result,
length);
gvFreeLayout(_graphPimpl->_gvc, _graphPimpl->_graph);
std::string res = *result;
delete *result;
delete length;
return res;
}
std::vector<Graph::Node> Graph::getChildren(const Node& node) const
{
std::vector<Graph::Node> result;
Agnode_t* n
= agnode(_graphPimpl->_graph, const_cast<char*>(node.c_str()), 0);
if (!n)
return result;
for (Agedge_t* edge = agfstout(_graphPimpl->_graph, n);
edge;
edge = agnxtout(_graphPimpl->_graph, edge))
result.push_back(std::string(agnameof(aghead(edge))));
return result;
}
std::vector<Graph::Node> Graph::getParents(const Node& node) const
{
std::vector<Graph::Node> result;
Agnode_t* n
= agnode(_graphPimpl->_graph, const_cast<char*>(node.c_str()), 0);
if (!n)
return result;
for (Agedge_t* edge = agfstin(_graphPimpl->_graph, n);
edge;
edge = agnxtin(_graphPimpl->_graph, edge))
result.push_back(std::string(agnameof(agtail(edge))));
return result;
}
std::string Graph::generateId()
{
do
{
std::size_t i;
for (i = 0; i < _currentId.size() && _currentId[i] == 'z'; ++i)
_currentId[i] = 'a';
if (i == _currentId.size())
_currentId.push_back('a');
else
++_currentId[i];
} while (_ids.find(_currentId) != _ids.end());
return _currentId;
}
} // util
} // cc