63 | 63 |
self.fall_in_map = new_fall_in_map
|
64 | 64 |
|
65 | 65 |
def serialize(self):
|
66 | |
self.fall_out_map = {}
|
67 | |
for key, values in self.fall_in_map.iteritems():
|
68 | |
for value in values:
|
69 | |
assert value not in self.fall_out_map
|
70 | |
self.fall_out_map[value] = key
|
71 | |
for routine in self.program.routines:
|
72 | |
if routine.name not in self.fall_out_map:
|
73 | |
self.fall_out_map[routine.name] = None
|
|
66 |
pending_routines = sorted(self.fall_in_map.keys())
|
|
67 |
routine_names = sorted([routine.name for routine in self.program.routines])
|
|
68 |
for routine_name in routine_names:
|
|
69 |
if routine_name not in pending_routines:
|
|
70 |
pending_routines.append(routine_name)
|
74 | 71 |
|
75 | 72 |
roster = []
|
76 | |
pending_routines = copy(self.fall_out_map)
|
77 | 73 |
while pending_routines:
|
78 | 74 |
# Pick a routine that is still pending to be serialized.
|
79 | |
key = pending_routines.keys()[0]
|
|
75 |
key = pending_routines[0]
|
80 | 76 |
|
81 | 77 |
in_set = self.fall_in_map.get(key, [])
|
82 | 78 |
|
|
85 | 81 |
chains = find_chains(self.fall_in_map, key, lambda k: k in pending_routines)
|
86 | 82 |
chains.sort(key=len, reverse=True)
|
87 | 83 |
routines = chains[0]
|
|
84 |
routines.reverse()
|
88 | 85 |
|
89 | 86 |
# Append (r1,r2,...,rn) to the roster and remove r1,r2,...rn from R.
|
90 | 87 |
# A sublist like this appearing in the roster has meaning
|
91 | 88 |
# "optimize the final goto out of all but the last routine in the sublist".
|
92 | 89 |
for r in routines:
|
93 | |
del pending_routines[r]
|
|
90 |
pending_routines.remove(r)
|
94 | 91 |
roster.append(routines)
|
95 | 92 |
|
96 | 93 |
return roster
|