-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGraphIO.cpp
100 lines (84 loc) · 2.64 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
#include "include/GraphIO.h"
using namespace Escape;
static bool isBlankLine(const char *line) {
while (*line) {
if (!isspace(*line))
return false;
++line;
}
return true;
}
static ErrorCode loadGraph_Escape(const char *path, Graph &graph, int undirected) {
FILE *f = fopen(path, "r");
if (!f) {
fprintf(stderr, "could not open file %s\n", path);
return ecInvalidInput;
}
graph.nVertices = 0;
EdgeIdx iEdge = 0;
char line[1024];
while (fgets(line, sizeof(line), f)) {
//Ignore comment lines.
if (line[0] == '#')
continue;
if (isBlankLine(line))
continue;
int64_t i1, i2;
sscanf(line, "%lld%lld", &i1, &i2);
if (graph.nVertices == 0) {
graph.nVertices = i1;
if (undirected)
graph.nEdges = 2 * i2;
else
graph.nEdges = i2;
graph.srcs = new VertexIdx[graph.nEdges];
graph.dsts = new VertexIdx[graph.nEdges];
} else {
graph.srcs[iEdge] = i1;
graph.dsts[iEdge] = i2;
++iEdge;
if (undirected) {
graph.srcs[iEdge] = i2;
graph.dsts[iEdge] = i1;
++iEdge;
}
}
}
fclose(f);
if (iEdge < graph.nEdges) {
fprintf(stderr, "expected %lld edges, only got %lld\n", graph.nEdges, iEdge);
return ecIOError;
}
return ecNone;
}
ErrorCode Escape::loadGraphCSR(const char *path, CGraph &cg, int undirected)
{
VertexIdx nVertices;
EdgeIdx nEdges;
auto in_file = std::ifstream(path, std::ios::in | std::ios::binary);
if(!in_file) {
std::cout << "Cannot open file " << path << std::endl;
return ecIOError;
}
in_file.read(reinterpret_cast<char*> (&nVertices), sizeof(nVertices));
in_file.read(reinterpret_cast<char*>(&nEdges), sizeof(nEdges));
cg.nVertices = nVertices;
cg.nEdges = nEdges;
cg.offsets = new EdgeIdx[cg.nVertices+1];
cg.nbors = new VertexIdx[cg.nEdges];
in_file.read(reinterpret_cast<char*>(cg.offsets), (nVertices+1)* sizeof(VertexIdx));
in_file.read(reinterpret_cast<char*>(cg.nbors), nEdges*sizeof(EdgeIdx));
in_file.close();
if(!in_file.good()) {
std::cout << "Error occurred at writing time!" << std::endl;
}
return ecNone;
}
ErrorCode Escape::loadGraph(const char *path, Graph &graph, int undirected, IOFormat fmt) {
switch (fmt) {
case IOFormat::escape:
return loadGraph_Escape(path, graph, undirected);
default:
return ecUnsupportedFormat;
}
}