forked from VictorTaelin/abstract-algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodes.py
More file actions
58 lines (52 loc) · 2.18 KB
/
nodes.py
File metadata and controls
58 lines (52 loc) · 2.18 KB
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
class Nodes:
def __init__(self):
self.nodes = []
self.steps = []
self.read('debug.txt')
self.cur_index = 0
self.cur_nodes = {i: self.nodes[i] for i in self.steps[0]['mnodes'] if not i in self.steps[0]['rnodes']}
def __len__(self):
return len(self.steps)
def __getitem__(self, index):
if index > self.cur_index:
add, mnodes, rnodes = 1, 'mnodes', 'rnodes'
else:
add, mnodes, rnodes = -1, 'rnodes', 'mnodes'
while self.cur_index != index:
if add == 1: self.cur_index += 1
for i in self.steps[self.cur_index][mnodes]:
self.cur_nodes[i] = self.nodes[i]
for i in self.steps[self.cur_index][rnodes]:
del self.cur_nodes[i]
if add == -1: self.cur_index -= 1
return (self.steps[self.cur_index]['gonext'], self.cur_nodes)
def read(self, fileName):
maxid = -1
ndids = []
with open(fileName, 'r') as file:
lines = iter(file.readlines())
lines.__next__()
while True:
try:
gonext = int(lines.__next__())
except StopIteration:
break
remove = [int(i) for i in lines.__next__().split()]
self.steps.append({'gonext': gonext, 'mnodes': [], 'rnodes': []})
for line in lines:
if line == '\n': break
line = [int(i) for i in line.split()]
maxid += 1
self.nodes.append(line)
self.steps[-1]['mnodes'].append(maxid)
if len(ndids) > line[0]:
if ndids[line[0]] != -1:
self.steps[-1]['rnodes'].append(ndids[line[0]])
ndids[line[0]] = -1
ndids[line[0]] = maxid
else:
ndids.append(maxid)
for indx in remove:
if ndids[indx] != -1:
self.steps[-1]['rnodes'].append(ndids[indx])
ndids[indx] = -1