-
Notifications
You must be signed in to change notification settings - Fork 1
/
dsv.hpp
699 lines (608 loc) · 21.1 KB
/
dsv.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
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
/**
MIT License
Copyright (c) 2020 西风逍遥游
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
#pragma once
#include <cassert>
#include <map>
#include <ostream>
#include <sstream>
#include <string>
#include <vector>
namespace DSViz {
class IViz;
class Node;
/**
* @brief A interface for data structure to print graphviz dot node
*/
class IDataStructure {
public:
virtual void dsviz_show(IViz &viz) = 0;
};
/**
* @brief A mock class which provides a non-invasive way to print graphviz dot
* @param T The type of data structure you want to mock
* @param F The function to print the data structure
*/
template <typename T, void (*F)(T*, IViz &)>
class Mock : public IDataStructure {
public:
virtual void dsviz_show(IViz &viz) override {
F(ds, viz);
}
/**
* @brief Get the mock object pointer from the original pointer
* @param ds The original pointer
*/
static Mock<T,F>* get(T *ds) {
auto& c = container();
if (c.find(ds) == c.end()) {
c[ds] = new Mock(ds);
}
return c[ds];
}
private:
static std::map<T*, Mock<T,F>*>& container() {
static std::map<T*, Mock<T,F>*> inst;
return inst;
}
Mock(T *ds) : ds(ds) {}
T *ds;
};
/**
* @brief An abstract interface to print graphviz dot graph
*/
class IViz {
public:
/**
* @brief Print the graphviz dot file
* @return The graphviz dot file in std::string
*/
virtual std::string print() const = 0;
/**
* @brief Generate a unique port name
* @return The port name `_portN`
*/
virtual std::string genPortName() = 0;
/**
* @brief Generate a unique edge name
* @return The edge name `_edgeN`
*/
virtual std::string genEdgeName() = 0;
/**
* @brief Generate a unique node name
* @return The node name `_nodeN`
*/
virtual std::string genNodeName() = 0;
/**
* @brief Add an edge to the graph
* @param from The name of the source node
* @param to The name of the target node
* @param edge The edge label
*/
virtual void addEdge(std::string from, std::string to,
std::string edge = "") = 0;
/**
* @brief Add an edge to the graph
* @param from The name of the source node
* @param to The name of the target node
* @param edge The edge label
*/
virtual void addEdge(std::string from, void *to, std::string edge = "") {
addEdge(from, getName(to), edge);
}
/**
* @brief Add an edge to the graph
* @param from The name of the source node
* @param to The name of the target node
* @param edge The edge label
*/
virtual void addEdge(void *from, std::string to, std::string edge = "") {
addEdge(getName(from), to, edge);
}
/**
* @brief Add an edge to the graph
* @param from The name of the source node
* @param to The name of the target node
* @param edge The edge label
*/
virtual void addEdge(void *from, void *to, std::string edge = "") {
addEdge(getName(from), getName(to), edge);
}
/**
* @brief Check if a pointer is already mapped to a node in dot file
*/
virtual bool hasNode(void *ds) const = 0;
/**
* @brief Add a node to the graph
* @param name The name of the node
* @param node The node content
*/
virtual void addNode(std::string name, std::string node) = 0;
/**
* @brief Add a subgraph to the graph
* @param name The content of the subgraph in graphviz dot format
*/
virtual void addSubGraph(std::string sg) = 0;
/**
* @brief Set the name of a node
* @param ds The pointer to the node
* @param name The name of the node
*/
virtual void setName(void *ds, std::string name) = 0;
/**
* @brief Get the name of a node
* @param ds The pointer to the node
* @return The name of the node
*/
virtual std::string getName(void *ds) const = 0;
/**
* @brief Get the pointer from the name
* @param name The name of the pointer
* @return The pointer
*/
virtual void *getDS(std::string name) const = 0;
/**
* @brief Load a data structure to the graph
* @details This function will start a depth-first search from the root node.
* Then all the nodes are able to reached will be added to the graph.
* @param ds The pointer to the data structure
*/
virtual void load_ds(IDataStructure *ds) {
if (!hasNode(ds)) {
ds->dsviz_show(*this);
}
}
/**
* @brief Load a data structure to the graph
* @details This function will start a depth-first search from the root node.
* Then all the nodes are able to reached will be added to the graph.
* @param ds The pointer to the data structure
*/
template <class T> void load_ds_c(T *ds) {
if (!hasNode(ds)) {
dsviz_show(ds, *this);
}
}
inline static std::string encode(std::string data) {
std::string ans;
for (auto c : data) {
if (c == '<') {
ans += "<";
continue;
}
if (c == '>') {
ans += ">";
continue;
}
if (c == '=' || c == '?' || c == ':' || c == '&' || c == '^' ||
c == '~' || c == '*' || c == '%' || c == '/' || c == '(' ||
c == ')' || c == ';' || c == '[' || c == ']' || c == '{' ||
c == '}') {
ans += "&#";
int ascii = (int)c;
ans += std::to_string(ascii);
ans += ';';
continue;
}
ans += c;
}
return ans;
}
};
/**
* @brief A class representing a node in graphviz dot file
*/
class Node {
public:
Node(IViz &viz, std::string name = "", std::string shape = "",
std::string style = "")
: viz(viz), shape(shape), style(style) {
if (name == "")
this->name = viz.genNodeName();
else
this->name = name;
ss << "[";
}
virtual ~Node() { Done(); }
virtual void Done() {
if (isDone) return;
this->genShape();
this->genLabel();
this->genStyle();
for (auto &p : other_attrs)
genAttr(p.first, p.second);
ss << "]";
viz.addNode(this->name, ss.str());
isDone = true;
}
virtual void addEdge(IDataStructure *ds, std::string edge_label = "",
std::string edge = "") {
if (ds != nullptr) {
viz.load_ds(ds);
if (edge_label != "") edge += " label=\"" + edge_label + "\"";
viz.addEdge(this->name, ds, "[" + edge + "]");
}
}
virtual void addAttr(std::string attr, std::string value) {
other_attrs[attr] = value;
}
std::string name, label, shape, style;
protected:
virtual void genAttr(std::string name, const std::string &attr) {
if (!attr.empty()) ss << " " << name << "=\"" << attr << "\"";
}
virtual void genLabel() { genAttr("label", label); }
virtual void genShape() { genAttr("shape", shape); }
virtual void genStyle() { genAttr("style", style); }
IViz &viz;
bool isDone = false;
std::stringstream ss;
std::map<std::string, std::string> other_attrs;
};
/**
* @brief A class representing a table node in graphviz dot file
* such as:
* node [lable=< <table border='0' cellborder='1' cellspacing='0'>...</table> >]
*/
class TableNode : public Node {
public:
TableNode(IViz &viz, int span = 1, std::string name = "",
std::string shape = "", std::string style = "")
: Node(viz, name, shape, style), span(span) {
tss << "<table border='0' cellborder='1' cellspacing='0' "
"cellpadding='2'>";
}
virtual ~TableNode() {
tss << "</table>";
label = tss.str();
Done();
}
inline void attr_name(std::string name, std::string attr) {
tss << "<td " << IViz::encode(attr) << ">" << IViz::encode(name)
<< "</td>";
}
inline void attr_value(std::string value, std::string attr,
std::string pt_name = "") {
tss << "<td";
if (span != 1) tss << " colspan='" << span << "'";
if (!pt_name.empty()) tss << " PORT='" << pt_name << "'";
tss << " " << IViz::encode(attr) << ">" << IViz::encode(value)
<< "</td>";
}
inline void attr_value_nospan(std::string value, std::string attr,
std::string pt_name = "") {
tss << "<td";
if (!pt_name.empty()) tss << " PORT='" << pt_name << "'";
tss << " " << IViz::encode(attr) << ">" << IViz::encode(value)
<< "</td>";
}
template <typename T>
inline void add(std::string name, T number, std::string attr = "",
std::string attr2 = "") {
tss << "<tr>";
attr_name(name, attr);
attr_value(std::to_string(number), attr2.empty() ? attr : attr2);
tss << "</tr>";
}
inline void addPointer(std::string name, IDataStructure *ds,
std::string content = "", std::string attr = "",
std::string attr2 = "", std::string edge = "") {
if (ds == nullptr) return;
std::string pt_name = viz.genPortName();
tss << "<tr>";
attr_name(name, attr);
attr_value(content, attr2.empty() ? attr : attr2, pt_name);
tss << "</tr>";
if (ds != nullptr) {
viz.load_ds(ds);
viz.addEdge(this->name + ":" + pt_name, ds, edge);
}
}
inline void addLeftRightSubTree(std::string name, IDataStructure *left,
IDataStructure *right,
std::string content_left = "",
std::string content_right = "",
std::string attr = "",
std::string attr2 = "") {
if (left == nullptr && right == nullptr) return;
std::string pt_name_l = viz.genPortName();
std::string pt_name_r = viz.genPortName();
tss << "<tr>";
attr_name(name, attr);
attr_value_nospan(content_left, attr2.empty() ? attr : attr2,
pt_name_l);
attr_value_nospan(content_right, attr2.empty() ? attr : attr2,
pt_name_r);
tss << "</tr>";
if (left != nullptr) {
viz.load_ds(left);
viz.addEdge(this->name + ":" + pt_name_l, left);
}
if (right != nullptr) {
viz.load_ds(right);
viz.addEdge(this->name + ":" + pt_name_r, right);
}
}
inline void
addChildren(std::string name, IDataStructure **children, size_t size,
std::vector<std::string> content = std::vector<std::string>(0),
std::string attr = "", std::string attr2 = "") {
tss << "<tr>";
attr_name(name, attr);
for (size_t i = 0; i < size; ++i) {
std::string pt_name = viz.genPortName();
std::string content_i = content.size() > i ? content[i] : "";
attr_value_nospan(content_i, attr2.empty() ? attr : attr2, pt_name);
if (children[i] != nullptr) {
viz.load_ds(children[i]);
viz.addEdge(this->name + ":" + pt_name, children[i]);
}
}
tss << "</tr>";
}
template <class T>
inline void addPointerC(std::string name, T *ds, std::string content = "",
std::string attr = "", std::string attr2 = "") {
if (ds == nullptr) return;
std::string pt_name = viz.genPortName();
tss << "<tr>";
attr_name(name, attr);
attr_value(content, attr2.empty() ? attr : attr2, pt_name);
tss << "</tr>";
if (ds != nullptr) {
viz.load_ds_c(ds);
viz.addEdge(this->name + ":" + pt_name, ds);
}
}
template <class T>
inline void
addChildrenC(std::string name, T **children, size_t size,
std::vector<std::string> content = std::vector<std::string>(0),
std::string attr = "", std::string attr2 = "") {
tss << "<tr>";
attr_name(name, attr);
for (size_t i = 0; i < size; ++i) {
std::string pt_name = viz.genPortName();
attr_value_nospan(i < content.size() ? content[i] : " ",
attr2.empty() ? attr : attr2, pt_name);
if (children[i] != nullptr) {
viz.load_ds_c(children[i]);
viz.addEdge(this->name + ":" + pt_name, children[i]);
}
}
tss << "</tr>";
}
template <class T>
inline void addArray(std::string name, T *numbers, size_t size,
std::string attr = "", std::string attr2 = "") {
tss << "<tr>";
attr_name(name, attr);
for (size_t i = 0; i < size; ++i) {
attr_value_nospan(std::to_string(numbers[i]),
attr2.empty() ? attr : attr2);
}
tss << "</tr>";
}
virtual void genArrowAttr(std::string name, const std::string &attr) {
if (!attr.empty()) ss << " " << name << "=<" << attr << ">";
}
virtual void genLabel() override { genArrowAttr("label", label); }
private:
int span;
std::stringstream tss;
};
template <>
inline void
TableNode::add<std::string>(std::string name, std::string str, std::string attr,
std::string attr2) {
tss << "<tr>";
attr_name(name, attr);
attr_value(str, attr2.empty() ? attr : attr2);
tss << "</tr>";
}
template <>
inline void
TableNode::add<bool>(std::string name, bool b, std::string attr,
std::string attr2) {
tss << "<tr>";
attr_name(name, attr);
attr_value(b ? "true" : "false", attr2.empty() ? attr : attr2);
tss << "</tr>";
}
template <>
inline void
TableNode::add<const char *>(std::string name, const char *str,
std::string attr, std::string attr2) {
tss << "<tr>";
attr_name(name, attr);
attr_value(std::string(str), attr2.empty() ? attr : attr2);
tss << "</tr>";
}
/**
* @brief The configuration of the system
*/
struct Config {
std::string node_style{"shape=plaintext"};
std::string edge_style{""};
std::string graph_style{""};
std::string other{""};
std::string genGraphStyle() const {
std::stringstream ss;
if (!node_style.empty())
ss << "node [" << node_style << "];" << std::endl;
if (!edge_style.empty())
ss << "edge [" << edge_style << "];" << std::endl;
if (!graph_style.empty())
ss << "graph [" << graph_style << "];" << std::endl;
ss << other << std::endl;
return ss.str();
}
};
/**
* @brief A class representing an edge in graphviz dot file
*/
class Edge {
public:
Edge(std::string from, std::string to) : from(from), to(to) {}
std::string from, to;
bool operator<(const Edge &rhs) const {
if (from < rhs.from) return true;
if (rhs.from < from) return false;
return to < rhs.to;
}
};
/**
* @brief A class representing a subgraph in graphviz dot file
*/
class SubGraph : public IViz {
public:
SubGraph(IViz &viz, std::string name = "", std::string label = "",
Config config = {})
: viz(viz) {
ss << "subgraph cluster_" << name << " {" << std::endl;
if (!label.empty()) ss << "label = \"" << label << "\";" << std::endl;
ss << config.genGraphStyle() << std::endl;
}
virtual ~SubGraph() {
for (auto subgraph : subgraphs) {
ss << subgraph << std::endl;
}
for (auto node : nodes) {
ss << node.first << " " << node.second << ";" << std::endl;
}
for (auto edge : edges) {
ss << edge.first.from << " -> " << edge.first.to << " "
<< edge.second << ";" << std::endl;
}
ss << "}" << std::endl;
viz.addSubGraph(print());
}
virtual std::string print() const override { return ss.str(); }
virtual void setName(void *ds, std::string name) override {
viz.setName(ds, name);
}
virtual std::string getName(void *ds) const override {
return viz.getName(ds);
}
virtual void *getDS(std::string name) const override {
return viz.getDS(name);
}
using IViz::addEdge;
virtual void addEdge(std::string from, std::string to,
std::string edge = "") override {
assert(!from.empty());
assert(!to.empty());
edges[Edge(from, to)] = edge;
}
virtual void addNode(std::string name, std::string node) override {
assert(!name.empty());
nodes[name] = node;
}
virtual void addSubGraph(std::string subgraph) override {
subgraphs.push_back(subgraph);
}
virtual bool hasNode(void *ds) const override { return viz.hasNode(ds); }
virtual std::string genNodeName() override { return viz.genNodeName(); }
virtual std::string genEdgeName() override { return viz.genEdgeName(); }
virtual std::string genPortName() override { return viz.genPortName(); }
private:
IViz &viz;
std::stringstream ss;
std::map<std::string, std::string> nodes;
std::map<Edge, std::string> edges;
std::vector<std::string> subgraphs;
};
inline std::ostream &
operator<<(std::ostream &out, const IViz &viz) {
out << viz.print() << std::endl;
return out;
}
/**
* @brief A class representing the whole dot file in graphviz
*/
class Dot : public IViz {
public:
Dot(Config config = {}) : config(config) {}
virtual std::string print() const override {
std::stringstream ss;
ss << "digraph structs {" << std::endl;
ss << config.genGraphStyle() << std::endl;
for (auto subgraph : subgraphs) {
ss << subgraph << std::endl;
}
for (auto node : nodes) {
ss << node.first << " " << node.second << ";" << std::endl;
}
for (auto edge : edges) {
ss << edge.first.from << " -> " << edge.first.to << " "
<< edge.second << ";" << std::endl;
}
ss << "}" << std::endl;
return ss.str();
}
virtual void setName(void *ds, std::string name) override {
names[ds] = name;
if (!name.empty()) DSs[name] = ds;
}
virtual std::string getName(void *ds) const override {
assert(hasNode(ds));
return names.find(ds)->second;
}
virtual void *getDS(std::string name) const override {
assert(!name.empty());
return DSs.find(name)->second;
}
using IViz::addEdge;
virtual void addEdge(std::string from, std::string to,
std::string edge = "") override {
assert(!from.empty());
assert(!to.empty());
edges[Edge(from, to)] = edge;
}
virtual void addNode(std::string name, std::string node) override {
assert(!name.empty());
nodes[name] = node;
}
virtual void addSubGraph(std::string subgraph) override {
subgraphs.push_back(subgraph);
}
virtual bool hasNode(void *ds) const override {
assert(ds);
return (names.find(ds) != names.end());
}
virtual std::string genNodeName() override {
return "_node" + std::to_string(count0++);
}
virtual std::string genEdgeName() override {
return "_edge" + std::to_string(count1++);
}
virtual std::string genPortName() override {
return "_port" + std::to_string(count2++);
}
protected:
int count0 = 0, count1 = 0, count2 = 0;
std::map<std::string, std::string> nodes;
std::map<void *, std::string> names;
std::map<std::string, void *> DSs;
std::map<Edge, std::string> edges;
std::vector<std::string> subgraphs;
Config config;
friend class Node;
};
} // namespace DSViz