Ensure outgoing edge map is sorted by upstream label

This commit is contained in:
Davide Depau 2018-01-26 15:38:14 +01:00
parent 19f316e9c5
commit 926295449a
No known key found for this signature in database
GPG Key ID: C7D999B6A55EFE86

View File

@ -2,7 +2,7 @@ from __future__ import unicode_literals
from ._utils import get_hash, get_hash_int
from builtins import object
from collections import namedtuple
from collections import namedtuple, OrderedDict
class DagNode(object):
@ -157,7 +157,7 @@ def topo_sort(downstream_nodes):
raise RuntimeError('Graph is not a DAG')
if downstream_node is not None:
outgoing_edge_map = outgoing_edge_maps.get(upstream_node, {})
outgoing_edge_map = outgoing_edge_maps.get(upstream_node, OrderedDict())
outgoing_edge_infos = outgoing_edge_map.get(upstream_label, [])
outgoing_edge_infos += [(downstream_node, downstream_label)]
outgoing_edge_map[upstream_label] = outgoing_edge_infos
@ -174,4 +174,10 @@ def topo_sort(downstream_nodes):
while unmarked_nodes:
upstream_node, upstream_label = unmarked_nodes.pop()
visit(upstream_node, upstream_label, None, None)
# Sort outgoing edge maps by upstream label
for map in outgoing_edge_maps.values():
for label in sorted(map.keys()):
map.move_to_end(label)
return sorted_nodes, outgoing_edge_maps