218
222
# We started from the parents, so we don't need to do anymore work
219
223
return topo_order
226
"""Return a reverse topological ordering which is 'stable'.
228
There are a few constraints:
229
1) Reverse topological (all children before all parents)
231
3) 'stable' sorting, so that we get the same result, independent of
232
machine, or extra data.
233
To do this, we use the same basic algorithm as topo_sort, but when we
234
aren't sure what node to access next, we sort them lexicographically.
236
tips = self._find_tips()
237
# Split the tips based on prefix
240
if node.key.__class__ is str or len(node.key) == 1:
244
prefix_tips.setdefault(prefix, []).append(node)
246
num_seen_children = dict.fromkeys(self._nodes, 0)
249
for prefix in sorted(prefix_tips):
250
pending = sorted(prefix_tips[prefix], key=lambda n:n.key,
254
if node.parent_keys is None:
255
# Ghost node, skip it
257
result.append(node.key)
258
for parent_key in sorted(node.parent_keys, reverse=True):
259
parent_node = self._nodes[parent_key]
260
seen_children = num_seen_children[parent_key] + 1
261
if seen_children == len(parent_node.child_keys):
262
# All children have been processed, enqueue this parent
263
pending.append(parent_node)
264
# This has been queued up, stop tracking it
265
del num_seen_children[parent_key]
267
num_seen_children[parent_key] = seen_children
221
270
def merge_sort(self, tip_key):
222
271
"""Compute the merge sorted graph output."""
223
272
from bzrlib import tsort