-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.h
113 lines (90 loc) · 2.42 KB
/
data.h
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
#include <fstream>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
class Load_data {
private:
int num_edges = 0;
int num_nodes = 0;
void read_nodes(std::fstream &myfile); // process nodes
void read_edges(std::fstream &myfile); // process edges
void resize_adj();
std::vector<std::vector<int>> adj_matrix;
std::unordered_map<int, std::string> graph;
public:
Load_data(){};
~Load_data(){};
void read_gml(std::string address);
std::vector<std::vector<int>> get_adj();
std::unordered_map<int, std::string> get_graph();
};
void Load_data::read_gml(std::string address) {
std::string line;
std::fstream myfile;
// open file
myfile.open(address);
// pass three line
getline(myfile, line);
getline(myfile, line);
getline(myfile, line);
while (1) {
if (getline(myfile, line)) {
if (line.find("node") != line.npos) {
read_nodes(myfile);
continue;
}
resize_adj(); // resize the adj_matrix
if (line.find("edge") != line.npos) {
read_edges(myfile);
continue;
}
} else {
break;
}
}
}
void Load_data::read_nodes(std::fstream &myfile) {
num_nodes++;
std::string line;
// pass
getline(myfile, line);
// get id
getline(myfile, line);
int id = atoi(line.substr(line.find("d") + 1).c_str());
// get label
getline(myfile, line);
int pos = line.find("\"") + 1;
std::string label = line.substr(pos, line.find("\"", pos) - pos);
// insert into graph
graph.insert(std::pair<int, std::string>(id, label));
// pass
getline(myfile, line);
}
void Load_data::read_edges(std::fstream &myfile) {
std::string line;
num_edges++;
// pass
getline(myfile, line);
// get source
getline(myfile, line);
int source = atoi(line.substr(line.find("e") + 1).c_str());
std::cout << "source:" << source << std::endl;
// get target
getline(myfile, line);
int target = atoi(line.substr(line.find("e") + 2).c_str());
// get value
getline(myfile, line);
std::cout << "target:" << target << std::endl;
int value = atoi(line.substr(line.find("e") + 1).c_str());
// pass
getline(myfile, line);
}
void Load_data::resize_adj() {
adj_matrix.resize(num_nodes);
for (int i = 0; i < num_nodes; i++) {
adj_matrix.resize(num_nodes);
}
}
std::vector<std::vector<int>> Load_data::get_adj() { return adj_matrix; }
std::unordered_map<int, std::string> Load_data::get_graph() { return graph; }