-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadg.pyx
377 lines (286 loc) · 8.11 KB
/
readg.pyx
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
from time import time
import cython
from utils import print_func_name, set_stdout, restore_stdout
from graph cimport graph_c, create_graph_c, add_edge, print_graph, print_graph_ext, free_graph
from array_c cimport array_c, create_arr, resize_arr, free_arr, print_array, max_arr
"""
######################## Directed graphs #########################
File format:
Space separated list of edges (v1, v2) per each line.
[v1] [space] [v2] ([space], [caret ret]) [new line]
or
[v1] [0x20] [v2] ([0x20], [0x0D]) [0x0A]
Possibly missing indices. Need to loop through all possible vertices
to find size of the graph.
"""
cdef inline (size_t, size_t) str2int(char* buf):
"""
Converts (tab/space/newline) terminated string to unsigned integer
Terminating symbols:
0x20 - space
0x09 - tab
0x0D - carret return \r
0x0A - new line \n
:param buf: pointer to char buffer
:return: integer value, bytes read including terminating symbol
"""
cdef:
size_t x = 0
size_t i = 0
size_t s
while buf[i] != 0x20 and buf[i] != 0x0D and buf[i] != 0x0A and buf[i] != 0x09:
# print(hex(buf[i]))
x = x * 10 + buf[i] - 0x30
i += 1
return x, i + 1
cdef inline (size_t, size_t, size_t) read_edge(char* buf):
"""
Read edge from space separated line. Terminated by \n or \r\n
:param buf: input char buf
:return: [v1, v2] indexes, total bytes read
"""
cdef:
size_t i = 0
size_t v1, v2, rb
v1, rb = str2int(buf + i)
i += rb
v2, rb = str2int(buf + i)
i += rb
# in case \r\n
if buf[i] == 0x0D:
i += 1
if buf[i] == 0x0A:
i += 1
return v1, v2, i
cdef void read_buff(char* buf, size_t n):
cdef:
size_t i = 0
size_t v1, v2, rb
while i < n:
# read edge
v1, rb = str2int(buf + i)
i += rb
print(v1, i)
v2, rb = str2int(buf + i)
i += rb
print(v2, i)
# in case \r\n
if buf[i] == 0x0D:
i += 1
if buf[i] == 0x0A:
i += 1
cdef array_c* read_array(str filename):
"""
Read graph as C array of directed edges.
File Format: v1 [space] v2 ([space], [caret return]) [new line] ...
:param filename: str
:return: array of edges [v1, v2]
"""
with open(filename, 'rb') as f:
read_buf = f.read()
cdef:
size_t v1, v2, rb
size_t i = 0
size_t j = 0
char * ptr = read_buf
size_t length = len(read_buf)
array_c * v_arr = create_arr(4)
while i < length:
v1, v2, rb = read_edge(ptr + i)
# normalize vertices
v1 -= 1
v2 -= 1
i += rb
if 2*j >= v_arr.capacity:
resize_arr(v_arr)
v_arr.items[2*j] = v1
v_arr.items[2*j + 1] = v2
j += 1
v_arr.size = 2*j
return v_arr
cdef graph_c* arr2graph(array_c* arr):
cdef:
graph_c* g
size_t i
size_t v1, v2, g_len
g_len = max_arr(arr) + 1
g = create_graph_c(g_len)
for i in range(arr.size//2):
v1 = arr.items[2*i]
v2 = arr.items[2*i + 1]
add_edge(g, v1, v2)
return g
cdef (graph_c*, graph_c*) arr2graphs(array_c* arr):
cdef:
graph_c* g
graph_c* g_rev
size_t i
size_t v1, v2, g_len
g_len = max_arr(arr) + 1
g = create_graph_c(g_len)
g_rev = create_graph_c(g_len)
for i in range(arr.size//2):
v1 = arr.items[2*i]
v2 = arr.items[2*i + 1]
add_edge(g, v1, v2)
add_edge(g_rev, v2, v1)
return g, g_rev
cdef graph_c* read_graph(str filename):
cdef:
array_c* arr
graph_c* g
arr = read_array(filename)
# print("g_len: ", g_len)
# print_array(arr)
g = arr2graph(arr)
free_arr(arr)
return g
cdef (graph_c*, graph_c*) read_graphs(str filename):
cdef:
array_c* arr
graph_c* g
graph_c* g_rev
arr = read_array(filename)
# print("g_len: ", g_len)
# print_array(arr)
g, g_rev = arr2graphs(arr)
free_arr(arr)
return g, g_rev
"""
######################## Directed graphs with lengths #########################
File format:
Tab separated adjacency list with edge lengths.
[i] [tab] [v1] [coma] [length] [tab] ... ([caret ret]) [new line]
or
[i] [0x09] [v1] [0x2C] [length] [0x09] ... [0x0D] [0x0A]
No missing vertices [i]. Total nodes - last index [i]
"""
@cython.wraparound
cdef graph_c* read_graph_l(str filename):
with open(filename, "r") as f:
read_buf = f.readlines()
last_line = read_buf[-1]
graph_size = int(last_line.split("\t")[0])
cdef graph_c* g = create_graph_c(graph_size)
for line in read_buf:
line = line.replace("\n", "").replace("\r", "")
line = line.split("\t")
# print(line)
v = int(line[0])
edges = line[1:]
# print("v:", v)
# print("edges")
for e in edges:
e = [int(s) for s in e.split(",")]
add_edge(g, v - 1, e[0] - 1, e[1])
# print(e)
return g
""" ################################################################ """
""" ######################### UNIT TESTS ########################### """
""" ################################################################ """
def test_ascii2int():
cdef:
char * buf = "1234 \r\n" # 0x20 0x0D 0x0A
assert str2int(buf)[0] == 1234
assert str2int(buf)[1] == 5
def test_ascii2int_1():
cdef:
char * buf = "1234 \n" # 0x20 0xA0
assert str2int(buf)[0] == 1234
assert str2int(buf)[1] == 5
def test_ascii2int_2():
cdef:
char * buf = "1234\n" # 0xA0
assert str2int(buf)[0] == 1234
assert str2int(buf)[1] == 5
def test_read_edge_spc_n():
cdef:
char * buf = "12 34 \n5"
size_t v1, v2, i
v1, v2, i = read_edge(buf)
assert v1 == 12
assert v2 == 34
assert i == 7
assert buf[i] == 0x35
def test_read_edge_spc_rn():
cdef:
char * buf = "12 34 \r\n5"
size_t v1, v2, i
v1, v2, i = read_edge(buf)
assert v1 == 12
assert v2 == 34
assert i == 8
assert buf[i] == 0x35
# v1, v2, i = read_edge(buf + i)
# assert v1 == 56
# assert v2 == 78
# assert i == 8
# v1, v2, i = read_edge(buf + i)
# assert v1 == 1
# assert v2 == 2
def test_read_edge_rn():
cdef:
char * buf = "12 34\r\n5"
size_t v1, v2, i
v1, v2, i = read_edge(buf)
assert v1 == 12
assert v2 == 34
assert i == 7
assert buf[i] == 0x35
def test_read_edge_n():
cdef:
char * buf = "12 34\n5"
size_t v1, v2, i
v1, v2, i = read_edge(buf)
assert v1 == 12
assert v2 == 34
assert i == 6
assert buf[i] == 0x35
def test_read_buf_1():
cdef:
char * buf = "12 34 \n567 8 \r\n"
s = set_stdout()
read_buff(buf, 15)
out = s.getvalue()
restore_stdout()
assert out == '12 3\n34 6\n567 11\n8 13\n'
def test_read_array():
cdef:
array_c* arr
arr = read_array("scc_small.txt")
assert arr.items[0] == 0
assert arr.items[1] == 0
assert arr.items[2] == 0
assert arr.items[3] == 1
assert arr.items[4] == 1
assert arr.items[5] == 0
assert arr.items[6] == 1
assert arr.items[7] == 2
assert arr.size == 8
free_arr(arr)
def test_read_graph():
cdef graph_c* g
g = read_graph("scc_small.txt")
assert g.node[0].adj.items[0] == 0
assert g.node[0].adj.items[1] == 1
assert g.node[1].adj.items[0] == 0
assert g.node[1].adj.items[1] == 2
free_graph(g)
# print_graph(g)
# def test_read_big():
# cdef graph_c* g = read_graph("scc.txt")
# free_graph(g)
def test_read_big_pair():
cdef:
graph_c* g
graph_c* g_rev
g, g_rev = read_graphs("scc.txt")
free_graph(g)
def test_read_graph_l():
path = "tests//course2_assignment2Dijkstra//"
filename = "input_random_1_4.txt"
cdef:
graph_c* g = read_graph_l(path + filename)
# print_graph_ext(g, 5)
# print_array(g.node[3].adj)
free_graph(g)