-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraphIO.cpp
105 lines (93 loc) · 3.1 KB
/
graphIO.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
#include "graphIO.h"
#include <nlohmann/json.hpp>
using json = nlohmann::json;
// conversion from graph to json
void to_json(json& j, const graph& p) {
if (p.positions.size())
j = json{ {"size", p.size}, {"edges", p.edges}, {"positions", p.positions} };
else {
j = json{ {"size", p.size}, {"edges", p.edges} };
}
}
bool load_positions = false;
// conversion from json to graph
void from_json(const json& j, graph& p) {
j.at("size").get_to(p.size);
j.at("edges").get_to(p.edges);
if (load_positions) {
j.at("positions").get_to(p.positions);
}
}
static graph read_json(std::istream& stream) {
json j;
stream >> j;
return j;
}
static graph read_normal(std::istream& stream) {
size_t size, m;
stream >> size >> m;
std::vector<std::pair<double, double>> positions;
if (load_positions) {
positions.resize(size);
for (size_t i = 0; i < size; i++) {
if (stream.peek() == EOF) {
throw std::runtime_error("input reading error");
}
int node; // node number
stream >> node;
double x, y;
stream >> x >> y;
positions[node] = { x, y };
}
}
std::vector<std::pair<int, int>> edges;
edges.reserve(m);
for (size_t i = 0; i < m; i++) {
if (stream.peek() == EOF) {
throw std::runtime_error("input reading error");
}
int l, r;
stream >> l >> r;
edges.push_back({ l, r });
}
return graph(size, edges, positions);
}
graph graphIO::read_graph(std::istream& stream, bool use_json, bool with_positions) {
load_positions = with_positions;
if (use_json) {
return read_json(stream);
}
else {
return read_normal(stream);
}
}
static void write_normal(std::ostream& stream, graph& g) {
stream << g.size << ' ' << g.edges.size() << '\n';
for (int i = 0; i < g.size; i++) {
stream << i << ' ' << g.positions[i].first << ' ' << g.positions[i].second << '\n';
}
}
static void write_tikz(std::ostream& stream, graph& g) {
stream << "\\documentclass[border=5mm]{standalone}\n\\usepackage[utf8]{inputenc}\n\\usepackage{tikz}\n\\begin{document}\n\\begin{tikzpicture}\n\\begin{scope}[every node/.style={circle,thick,draw}]\n";
for (int i = 0; i < g.size; i++) {
stream << "\\node (" << i << ") at (" << 20 * g.positions[i].first << ", " << 20 * g.positions[i].second << ") {" << i << "};\n";
}
stream << "\\end{scope}\n\n";
stream << "\\begin{scope}[every node/.style={fill=white,circle}, every edge/.style={draw=red,very thick}]\n";
for (auto p : g.edges) {
stream << "\\draw [-] (" << p.first << ") -- (" << p.second << ");\n";
}
stream << "\\end{scope}\n\\end{tikzpicture}\n\\end{document}\n";
}
void graphIO::write_graph(std::ostream& stream, graph& g, std::string out_type) {
if (out_type == "normal") {
write_normal(stream, g);
}
else if (out_type == "json") {
json j = g;
stream << j.dump(4) << '\n';
}
else if (out_type == "tikz") {
write_tikz(stream, g);
}
}