-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphviz_utility.h
More file actions
216 lines (184 loc) · 5.01 KB
/
graphviz_utility.h
File metadata and controls
216 lines (184 loc) · 5.01 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
#ifndef GRAPHVIZ_UTILITY_H
#define GRAPHVIZ_UTILITY_H
#include <fstream>
#include <string>
#include <vector>
#include <map>
class GraphvizUtility
{
public:
GraphvizUtility(bool directed = true);
~GraphvizUtility();
// Adds a node to the visualizer. If a node with the same ID is
// present, it and its edges are removed, and the new node takes its place.
void AddNode(int id, std::string label, std::string options = "");
// Updates the node for the given id without removing its edges.
void EditNode(int id, std::string label, std::string options = "");
// Remove the node and all of its edges
void RemoveNode(int id);
// Add an edge connecting two nodes by their IDs
void AddEdge(int from_id, int to_id, std::string label);
// Remove all edges between two nodes
void RemoveEdges(int id_1, int id_2);
// Remove edges between two nodes by direction
void RemoveEdgesDirected(int from_id, int to_id);
// Export the graph to Graphviz .dot format
void Export(std::string filename);
// Clear the nodes in the graph
void ClearGraph();
private:
struct GraphvizEdge
{
int to_id;
std::string label;
};
struct GraphvizNode
{
int id;
std::vector<struct GraphvizEdge> edges;
std::vector<int> incoming_edges;
std::string label;
std::string options;
GraphvizNode(const int& s_id = 0) : id(s_id)
{
}
bool operator==(const GraphvizNode& a) const
{
return (id == a.id);
}
bool operator<(const GraphvizNode& a) const
{
return (id < a.id);
}
};
std::map<int,struct GraphvizNode> m_nodes;
bool m_directed;
};
GraphvizUtility::GraphvizUtility(bool directed)
{
m_directed = directed;
}
GraphvizUtility::~GraphvizUtility()
{
}
void GraphvizUtility::AddNode(int id, std::string label, std::string options)
{
RemoveNode(id);
struct GraphvizNode node;
node.id = id;
node.label = label;
node.options = options;
std::pair<int,struct GraphvizNode> node_pair(id, node);
m_nodes.insert(node_pair);
}
void GraphvizUtility::EditNode(int id, std::string label, std::string options)
{
std::map<int,struct GraphvizNode>::iterator it;
it = m_nodes.find(id);
if (it != m_nodes.end())
{
m_nodes[id].label = label;
m_nodes[id].options = options;
}
}
void GraphvizUtility::RemoveNode(int id)
{
std::map<int,struct GraphvizNode>::iterator it;
it = m_nodes.find(id);
if (it != m_nodes.end())
{
for (unsigned int w = 0; w < m_nodes[id].incoming_edges.size(); w++)
{
int from_id = m_nodes[id].incoming_edges[w];
RemoveEdgesDirected(from_id, id);
}
for (unsigned int w = 0; w < m_nodes[id].incoming_edges.size(); w++)
{
int to_id = m_nodes[id].edges[w].to_id;
RemoveEdgesDirected(id, to_id);
}
m_nodes.erase(id);
}
}
void GraphvizUtility::AddEdge(int from_id, int to_id, std::string label)
{
std::map<int,struct GraphvizNode>::iterator from_it, to_it;
from_it = m_nodes.find(from_id);
to_it = m_nodes.find(to_id);
if (from_it != m_nodes.end() && to_it != m_nodes.end())
{
struct GraphvizEdge edge;
edge.to_id = to_id;
edge.label = label;
m_nodes[from_id].edges.push_back(edge);
m_nodes[to_id].incoming_edges.push_back(from_id);
}
}
void GraphvizUtility::RemoveEdges(int id_1, int id_2)
{
RemoveEdgesDirected(id_1, id_2);
RemoveEdgesDirected(id_2, id_1);
}
void GraphvizUtility::RemoveEdgesDirected(int from_id, int to_id)
{
unsigned int x = 0;
while (x < m_nodes[from_id].edges.size())
{
if (m_nodes[from_id].edges[x].to_id == to_id)
{
m_nodes[from_id].edges.erase(m_nodes[from_id].edges.begin()+x);
}
else
{
x++;
}
}
x = 0;
while (x < m_nodes[to_id].incoming_edges.size())
{
if (m_nodes[to_id].incoming_edges[x] == from_id)
{
m_nodes[to_id].incoming_edges.erase(m_nodes[to_id].incoming_edges.begin()+x);
}
else
{
x++;
}
}
}
void GraphvizUtility::Export(std::string filename)
{
std::ofstream out(filename);
std::string edge_repr;
if (m_directed)
{
out << "\ndigraph{";
edge_repr = "->";
}
else
{
out << "\ngraph{";
edge_repr = "--";
}
for(auto node : m_nodes)
{
out << "\n_" << node.second.id
<< " [ "<< node.second.options << " label=\"" << node.second.label << "\"];";
}
for(auto node : m_nodes)
{
for(auto edge : node.second.edges)
{
out << "\n_" << node.second.id
<< " " << edge_repr << " _" << edge.to_id
<< " [label=\"" << edge.label << "\"];";
}
}
out << "\n}";
out.close();
}
void GraphvizUtility::ClearGraph()
{
m_nodes.clear();
}
#endif