-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom.cpp
More file actions
159 lines (117 loc) · 4.85 KB
/
Copy pathcustom.cpp
File metadata and controls
159 lines (117 loc) · 4.85 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
#include <sstream>
#include "custom.hpp"
#include "util.hpp"
static Cell parse_num_board_cells(const std::vector<std::string>& lines) {
for (const auto& line : lines) {
if (line.rfind("num_board_cells:", 0) == 0) {
const auto comps = split(line, ':');
if (comps.size() != 2) {
std::ostringstream err{};
err << "error: unable to parse \"" << line << "\"";
throw std::runtime_error(err.str());
}
const auto num_board_cells = parse_int<Cell>(trim_copy(comps.at(1)));
if (num_board_cells == 0) {
throw std::runtime_error("error: num_board_cells cannot be 0");
}
return num_board_cells;
}
}
throw std::runtime_error("error: no value for num_board_cells found");
}
static std::vector<Cell> parse_cell_list(const std::string& line, const Cell num_board_cells) {
const auto comps = split(line, ' ');
std::vector<Cell> list{};
for (const auto& comp : comps) {
const auto num = parse_int<Cell>(comp);
if (num >= num_board_cells) {
std::ostringstream err{};
err << "error: cell " << static_cast<uint32_t>(num) << " is greater than the board size";
throw std::runtime_error(err.str());
}
list.push_back(num);
}
return list;
}
static std::vector<std::vector<Cell>> parse_board_graph(const std::vector<std::string>& lines, const Cell num_board_cells) {
std::vector<std::vector<Cell>> board_graph{num_board_cells};
for (const auto& line : lines) {
if (line.rfind("-", 0) == 0) {
const auto new_line = replace_copy(line, "-", "");
const auto comps = split(new_line, ':');
if (comps.size() != 2) {
std::ostringstream err{};
err << "error: unable to parse \"" << line << "\"";
throw std::runtime_error(err.str());
}
const auto cell_number = parse_int<Cell>(trim_copy(comps.at(0)));
if (cell_number >= num_board_cells) {
std::ostringstream err{};
err << "error: cell " << static_cast<uint32_t>(cell_number) << " is greater than the board size";
throw std::runtime_error(err.str());
}
const auto neighbors = parse_cell_list(comps.at(1), num_board_cells);
if (neighbors.empty()) {
std::ostringstream err{};
err << "error: cell " << static_cast<uint32_t>(cell_number) << " does not have any neighbors";
throw std::runtime_error(err.str());
}
if (!board_graph.at(cell_number).empty()) {
std::ostringstream err{};
err << "error: neighbors for cell " << static_cast<uint32_t>(cell_number) << " specified multiple times";
throw std::runtime_error(err.str());
}
board_graph.at(cell_number) = neighbors;
}
}
for (Cell i = 0; i < num_board_cells; ++i) {
if (board_graph.at(i).empty()) {
std::ostringstream err{};
err << "error: no neighbors given for cell " << static_cast<uint32_t>(i);
throw std::runtime_error(err.str());
}
}
return board_graph;
}
static std::vector<Cell> parse_edge_list(const std::string& line, const Cell num_board_cells) {
const auto comps = split(line, ':');
if (comps.size() != 2) {
std::ostringstream err{};
err << "error: unable to parse \"" << line << "\"";
throw std::runtime_error(err.str());
}
return parse_cell_list(comps.at(1), num_board_cells);
}
static std::vector<Edge> parse_cell_edges(const std::vector<std::string>& lines, const Cell num_board_cells) {
std::vector<Edge> edges{num_board_cells, Edge::None};
for (const auto& line : lines) {
Edge edge = Edge::None;
std::vector<Cell> cells{};
if (line.rfind("left_edge_cells:", 0) == 0) {
edge = Edge::Left;
cells = parse_edge_list(line, num_board_cells);
}
if (line.rfind("bottom_edge_cells:", 0) == 0) {
edge = Edge::Bottom;
cells = parse_edge_list(line, num_board_cells);
}
if (line.rfind("right_edge_cells:", 0) == 0) {
edge = Edge::Right;
cells = parse_edge_list(line, num_board_cells);
}
for (const auto cell : cells) {
edges.at(cell) |= edge;
}
}
return edges;
}
CustomY::CustomY(const std::string& file_path) {
const auto file = read_file(file_path);
auto lines = split(file, '\n');
for (auto& line : lines) {
line = trim_copy(line);
}
const auto num_board_cells = parse_num_board_cells(lines);
graph_ = parse_board_graph(lines, num_board_cells);
edges_ = parse_cell_edges(lines, num_board_cells);
}