423
423
# These are safe to offload to local variables, because they are used
424
424
# as a stack and modified in place, never assigned to.
425
425
node_name_stack = self._node_name_stack
426
node_merge_depth_stack = self._node_merge_depth_stack
426
427
pending_parents_stack = self._pending_parents_stack
427
428
left_subtree_pushed_stack = self._left_subtree_pushed_stack
428
429
completed_node_names = self._completed_node_names
430
pop_node = self._pop_node
431
push_node = self._push_node
430
scheduled_nodes = self._scheduled_nodes
432
graph_pop = self._graph.pop
434
def push_node(node_name, merge_depth, parents,
435
node_name_stack_append=node_name_stack.append,
436
node_merge_depth_stack_append=node_merge_depth_stack.append,
437
left_subtree_pushed_stack_append=left_subtree_pushed_stack.append,
438
pending_parents_stack_append=pending_parents_stack.append,
439
assigned_sequence_stack_append=self._assigned_sequence_stack.append,
440
original_graph=self._original_graph,
443
"""Add node_name to the pending node stack.
445
Names in this stack will get emitted into the output as they are popped
448
This inlines a lot of self._variable.append functions as local
451
node_name_stack_append(node_name)
452
node_merge_depth_stack_append(merge_depth)
453
left_subtree_pushed_stack_append(False)
454
pending_parents_stack_append(list(parents))
455
# as we push it, assign it a sequence number against its parent:
456
parents = original_graph[node_name]
458
# node has parents, assign from the left most parent.
459
parent_revno = revnos[parents[0]]
460
sequence = parent_revno[1]
463
# no parents, use the root sequence
464
sequence = self._root_sequence
465
self._root_sequence +=1
466
assigned_sequence_stack_append(sequence)
468
def pop_node(node_name_stack_pop=node_name_stack.pop,
469
node_merge_depth_stack_pop=node_merge_depth_stack.pop,
470
assigned_sequence_stack_pop=self._assigned_sequence_stack.pop,
471
left_subtree_pushed_stack_pop=left_subtree_pushed_stack.pop,
472
pending_parents_stack_pop=pending_parents_stack.pop,
473
original_graph=self._original_graph,
475
completed_node_names_add=self._completed_node_names.add,
476
scheduled_nodes_append=scheduled_nodes.append,
478
"""Pop the top node off the stack
480
The node is appended to the sorted output.
482
# we are returning from the flattened call frame:
483
# pop off the local variables
484
node_name = node_name_stack_pop()
485
merge_depth = node_merge_depth_stack_pop()
486
sequence = assigned_sequence_stack_pop()
487
# remove this node from the pending lists:
488
left_subtree_pushed_stack_pop()
489
pending_parents_stack_pop()
491
parents = original_graph[node_name]
493
# node has parents, assign from the left most parent.
494
parent_revno = revnos[parents[0]]
496
# not the first child, make a new branch
497
revno = parent_revno[0] + (sequence, 1)
499
# increment the sequence number within the branch
500
revno = parent_revno[0][:-1] + (parent_revno[0][-1] + 1,)
502
# no parents, use the root sequence
504
# make a parallel import revision number
505
revno = (0, sequence, 1)
509
# store the revno for this node for future reference
510
revnos[node_name][0] = revno
511
completed_node_names_add(node_name)
512
scheduled_nodes_append((node_name, merge_depth, revno))
433
516
while node_name_stack:
434
517
# loop until this call completes.