-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph_output.hpp
executable file
·78 lines (65 loc) · 2.36 KB
/
graph_output.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
////////////////////////////////////////////////////////////////////////////////
// graph_output.hpp
// Graph output in different formats, for example graphviz dot
// TH, 6.7.15
////////////////////////////////////////////////////////////////////////////////
#ifndef __GRAPH_OUTPUT_HPP__
#define __GRAPH_OUTPUT_HPP__
#include <iostream>
#include "labeled_graph.hpp"
#include "graph_transform.hpp"
namespace GraphLibrary {
/**
@brief Function object for outputting graphs as dot
Each outputter function object has 4 functions:
1. prolog(): output stuff before the actual graph is outputted
2. operator()(Node&): output a node
3. operation()(Edge&): output an edge
4. epilog(): output stuff after the graph has been outputted
*/
template<typename GRAPHEDGE>
struct GraphDotOutputter
{
/// Constructor takes an ostream reference
GraphDotOutputter(std::ostream& o) : out(o) {}
/// Write dot prolog
void prolog()
{
out << "digraph DoNotNameThisOnlyGraph {" << std::endl;
out << " graph [rankdir = LR, center = 1, orientation = Portrait]" << std::endl;
out << " node [fontsize = 14, shape = box, style = filled, color = blue, fontcolor = white]" << std::endl;
out << " edge [fontsize = 14 ];" << std::endl << std::endl;
}
/// Node output
void operator()(const typename GRAPHEDGE::Node& node)
{
out << " " << "\"" << node << "\"" << std::endl;
}
/// Edge output
void operator()(const GRAPHEDGE& edge)
{
const char* quote = "\"";
out << " " << quote << edge.source() << quote
<< " -> " << quote << edge.target() << quote
<< " [label = " << quote << edge.label() << quote
<< "]" << std::endl;
}
/// Write dot epilog
void epilog()
{
out << "}" << std::endl;
}
std::ostream& out; ///< Stream everything is written to
}; // GraphDotOutputter
/// Output graph in graphviz dot format
template<typename GRAPHEDGE>
void graph_as_dot(const LabeledDirectedGraph<GRAPHEDGE>& g,
std::ostream& o)
{
// Construct dot output function object
GraphDotOutputter<GRAPHEDGE> dot_outputter(o);
// Call generic transformation function which creates the dot output
graph_transform(g,dot_outputter);
}
} // namespace GraphLibrary
#endif