m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2017-01-03 23:24:13 -0500
committerMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2017-01-03 23:24:13 -0500
commit9f5d959a3af3582a7b3fa1f7b4b2183c8f73b380 (patch)
treec82553991a0986cb7b1233bbaeb3259ee2eaf204
parentc5289accb8a27ca2cce537019e5bd4f37fef157e (diff)
Implement graph parser
-rw-r--r--src/parse.h50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/parse.h b/src/parse.h
new file mode 100644
index 0000000..3f86eec
--- /dev/null
+++ b/src/parse.h
@@ -0,0 +1,50 @@
+#ifndef PARSE_H
+#define PARSE_H
+
+#include <fstream>
+#include <set>
+#include "graph.h"
+
+class Parser {
+public:
+ Parser(std::string filename) : graph_(), input_file_(filename) {
+ parse_number_edges_();
+
+ for (int i = 0; i < number_edges_; i++) {
+ parse_edge_();
+ }
+ }
+
+ Graph get_graph() {
+ return graph_;
+ }
+private:
+ Graph graph_;
+ std::ifstream input_file_;
+ int number_edges_;
+ std::set<int> vertices_;
+
+ void parse_number_edges_() {
+ input_file_ >> number_edges_;
+ }
+
+ void parse_edge_() {
+ int from, to;
+
+ input_file_ >> from >> to;
+
+ if (vertices_.find(from) == vertices_.end()) {
+ graph_.add_vertex(from);
+ vertices_.insert(from);
+ }
+
+ if (vertices_.find(to) == vertices_.end()) {
+ graph_.add_vertex(to);
+ vertices_.insert(to);
+ }
+
+ graph_.add_edge(from, to);
+ }
+};
+
+#endif