-
Notifications
You must be signed in to change notification settings - Fork 0
/
edge2reader.js
163 lines (141 loc) · 3.38 KB
/
edge2reader.js
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
160
161
162
163
var fs = require('fs');
function edgewrite(outputfile, g, cb) {
var data = "";
var i = 0;
data += "" + g.num_vertices + " " + g.num_edges + "\n";
while (i < g.adj_begin.length) {
var pos = g.adj_begin[i];
var j = 0;
while (j < g.adj_length[i]) {
data += "" + i + " " + g.adj_list[pos+j] + "\n";
j++;
}
i++;
}
fs.writeFile(outputfile,data,function(){cb(null);})
}
function edgeread(inputfile, cb) {
var linenumber = 0;
var error = false;
var llreader = require('line-by-line');
lr = new llreader(inputfile);
lr.on('error', function (err) {
// 'err' contains error object
error = true;
cb(err);
});
var indexmap = {};
var next_index = 0;
var num_edges = 0;
var num_vertices = 0;
var from, to;
var data = [];
var N, M;
lr.on('line', function (line) {
linenumber++;
if (linenumber == 1) {
a = line.split(" ");
if (a.length < 2) {
console.log("error:" + linenumber + "format error");
return;
}
if (a.length == 1) {
M = parseInt(a[0]); // edge count
} else if (a.length == 2) {
N = parseInt(a[0]); // vertex count
M = parseInt(a[1]); // edge count
}
return;
}
// 'line' contains the current line without the trailing newline character.
// console.log(line);
a = line.split(" ");
if (a.length != 2) {
console.log("error:" + linenumber + "format error");
return;
}
var from = parseInt(a[0]);
var to = parseInt(a[1]);
// console.log(from, to);
if (indexmap[from] == undefined) {
// console.log("map", from, next_index);
indexmap[from] = next_index++;
}
if (indexmap[to] == undefined) {
// console.log("map", to, next_index);
indexmap[to] = next_index++;
}
num_edges++;
data.push(a);
});
var adj_begin = [];
var adj_length = [];
var adj_vec = [];
var adj_list = [];
function max(i, j) { return (i>j)?i:j; }
function syscopy(targ, targfrom, src, srcfrom, count)
{
// console.log("syscpy", targfrom, srcfrom, count);
var i = 0;
while (i < count) {
targ[targfrom+i] = src[srcfrom+i];
i++;
}
}
function alength(o) {
return Object.keys(o).length;
}
lr.on('end', function () {
if (error) return;
// All lines are read, file is closed now.
num_vertices = next_index;
// console.log("um_vertices", num_vertices);
var i = 0;
while (i < num_vertices) {
adj_begin[i] = 0;
adj_length[i] = 0;
adj_vec[i] = [];
i++;
}
i = 0;
while (i < num_edges) {
adj_list[i] = 0;
i++;
}
i = 0;
var max_degree = -1;
while (i < data.length) {
var a = data[i];
// console.log(a[0]);
from = a[0];
to = a[1];
// console.log("x=",indexmap[from]);
adj_vec[indexmap[from]].push(indexmap[to]);
i++;
}
var next_offset = 0;
i = 0;
while (i < num_vertices)
{
var list_size = alength(adj_vec[i]);
adj_begin[i] = next_offset;
adj_length[i] = list_size;
// console.log("syscpy", next_offset, 0, list_size);
syscopy(adj_list, next_offset, adj_vec[i], 0, list_size);
next_offset += list_size;
i++;
}
var arg = {
vertices: indexmap,
num_vertices: num_vertices,
num_edges:num_edges,
max_degree: max_degree,
adj_begin: adj_begin,
adj_length: adj_length,
adjs: adj_vec,
adj_list: adj_list};
cb(null, arg);
});
}
module.exports.read = edgeread;
module.exports.write = edgewrite;