1 #ifndef GL_YAML_READER_HPP
2 #define GL_YAML_READER_HPP
4 #include "../structures/Graph.hpp"
5 #include "../gl_base.hpp"
16 #define IO_GRAPH (*arg)
23 #define IO_CALL_ON_GRAPH(g, func) std::visit([&](auto arg) { func; }, g)
34 using graph_variant_type = std::variant<graphMdu *, graphMdd *, graphLdu *, graphLdd *,
35 graphMfu *, graphMfd *, graphLfu *, graphLfd *,
36 graphMiu *, graphMid *, graphLiu *, graphLid *>;
71 stream_ = std::fstream(filename);
79 stream_ = std::fstream(filename);
94 NodeType(idx_t index,
double cap,
gl::Color col, std::string label) : index(index),
105 EdgeType(idx_t from, idx_t to,
double weight,
gl::Color col) : from(from),
113 std::pair<float, float> pos;
114 PositionType(idx_t
id, std::pair<float, float> pos) : id(
id), pos(pos) {}
118 std::string value_type, storage_type, direction_type, graph_label;
119 idx_t number_of_nodes;
121 std::vector<EdgeType> edges;
122 std::vector<NodeType> nodes;
123 std::vector<PositionType> positions;
129 if (line[0] ==
'#' || line.empty())
131 std::size_t delimiterPos = line.find(
":");
132 std::string name = line.substr(0, delimiterPos);
133 std::string value = line.substr(delimiterPos + 1);
135 value.erase(value.begin(), std::find_if(value.begin(), value.end(),
136 std::not1(std::ptr_fun<int, int>(std::isspace))));
137 if (name ==
"value_type")
141 else if (name ==
"direction_type")
143 direction_type = value;
145 else if (name ==
"storage_type")
147 storage_type = value;
149 else if (name ==
"number_nodes")
151 number_of_nodes = std::stoi(value);
153 else if (name ==
"label")
157 else if (name ==
"edge")
159 std::stringstream ss(value);
172 ss >> std::hex >> hex;
175 edges.push_back(EdgeType(a, b, weight, c));
177 else if (name ==
"node")
181 std::stringstream ss(value);
187 ss >> std::hex >> hex;
192 nodes.push_back(NodeType(node, cap, col, label));
194 else if (name ==
"position")
198 std::stringstream ss(value);
199 ss >> node >> x >> y;
200 std::pair<float, float> pos(x, y);
201 positions.push_back(PositionType(node, pos));
205 GL_ASSERT(
false,
"Unrecognized option: " + line);
210 if (value_type ==
"double" && storage_type ==
"Matrix" && direction_type ==
"Undirected")
212 graph_ =
new graphMdu(number_of_nodes, graph_label);
214 else if (value_type ==
"double" && storage_type ==
"Matrix" && direction_type ==
"Directed")
216 graph_ =
new graphMdd(number_of_nodes, graph_label);
218 else if (value_type ==
"double" && storage_type ==
"List" && direction_type ==
"Undirected")
220 graph_ =
new graphLdu(number_of_nodes, graph_label);
222 else if (value_type ==
"double" && storage_type ==
"List" && direction_type ==
"Directed")
224 graph_ =
new graphLdd(number_of_nodes, graph_label);
226 else if (value_type ==
"float" && storage_type ==
"Matrix" && direction_type ==
"Undirected")
228 graph_ =
new graphMfu(number_of_nodes, graph_label);
230 else if (value_type ==
"float" && storage_type ==
"Matrix" && direction_type ==
"Directed")
232 graph_ =
new graphMfd(number_of_nodes, graph_label);
234 else if (value_type ==
"float" && storage_type ==
"List" && direction_type ==
"Undirected")
236 graph_ =
new graphLfu(number_of_nodes, graph_label);
238 else if (value_type ==
"float" && storage_type ==
"List" && direction_type ==
"Directed")
240 graph_ =
new graphLfd(number_of_nodes, graph_label);
242 else if (value_type ==
"int" && storage_type ==
"Matrix" && direction_type ==
"Undirected")
244 graph_ =
new graphMiu(number_of_nodes, graph_label);
246 else if (value_type ==
"int" && storage_type ==
"Matrix" && direction_type ==
"Directed")
248 graph_ =
new graphMid(number_of_nodes, graph_label);
250 else if (value_type ==
"int" && storage_type ==
"List" && direction_type ==
"Undirected")
252 graph_ =
new graphLiu(number_of_nodes, graph_label);
254 else if (value_type ==
"int" && storage_type ==
"List" && direction_type ==
"Directed")
256 graph_ =
new graphLid(number_of_nodes, graph_label);
260 GL_ASSERT(
false,
"Please enter valid data into config.");
264 for (EdgeType &e : edges)
268 for (NodeType &n : nodes)
272 for(PositionType& p : positions) {
280 #endif // GL_YAML_READER_HPP
void setFilename(const char *filename)
Set the Filename to use.
Definition: YAMLReader.hpp:77
std::variant< graphMdu *, graphMdd *, graphLdu *, graphLdd *, graphMfu *, graphMfd *, graphLfu *, graphLfd *, graphMiu *, graphMid *, graphLiu *, graphLid * > graph_variant_type
All possible variants of graphs.
Definition: YAMLReader.hpp:36
std::fstream stream_
the filestream to use
Definition: YAMLReader.hpp:65
Stores an RGBA Color.
Definition: Color.hpp:21
YAMLReader()
Default constructor.
Definition: YAMLReader.hpp:40
#define IO_GRAPH
Template object to use with IO_CALL_ON_GRAPH.
Definition: YAMLReader.hpp:16
int hex() const
Gets the hexadecimal value of the RGBA color.
Definition: Color.hpp:160
#define IO_CALL_ON_GRAPH(g, func)
Custom function on parsed graphs.
Definition: YAMLReader.hpp:23
#define GL_ASSERT(EXPR, ERROR_MSG)
Custom assert that supports attaching a message.
Definition: gl_assert.hpp:14
Class to Read from a YAML file.
Definition: YAMLReader.hpp:28
std::size_t index_type
Definition: gl_base.hpp:18
void read()
Reads the file, generates graph This is automatically done in YAMLReader(const char *filename) ...
Definition: YAMLReader.hpp:83
graph_variant_type graph_
the graph variant
Definition: YAMLReader.hpp:66