~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tsort.py

  • Committer: Martin Pool
  • Date: 2005-09-30 05:56:05 UTC
  • mto: (1185.14.2)
  • mto: This revision was merged to the branch mainline in revision 1396.
  • Revision ID: mbp@sourcefrog.net-20050930055605-a2c534529b392a7d
- fix upgrade for transport changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 Canonical Ltd
2
 
#
3
 
# This program is free software; you can redistribute it and/or modify
4
 
# it under the terms of the GNU General Public License as published by
5
 
# the Free Software Foundation; either version 2 of the License, or
6
 
# (at your option) any later version.
7
 
#
8
 
# This program is distributed in the hope that it will be useful,
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
# GNU General Public License for more details.
12
 
#
13
 
# You should have received a copy of the GNU General Public License
14
 
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 
 
17
 
 
18
 
"""Topological sorting routines."""
19
 
 
20
 
 
21
 
import bzrlib.errors as errors
22
 
 
23
 
 
24
 
__all__ = ["topo_sort", "TopoSorter", "merge_sort", "MergeSorter"]
25
 
 
26
 
 
27
 
def topo_sort(graph):
28
 
    """Topological sort a graph.
29
 
 
30
 
    graph -- sequence of pairs of node->parents_list.
31
 
 
32
 
    The result is a list of node names, such that all parents come before
33
 
    their children.
34
 
 
35
 
    node identifiers can be any hashable object, and are typically strings.
36
 
    """
37
 
    return TopoSorter(graph).sorted()
38
 
 
39
 
 
40
 
class TopoSorter(object):
41
 
 
42
 
    def __init__(self, graph):
43
 
        """Topological sorting of a graph.
44
 
    
45
 
        :param graph: sequence of pairs of node_name->parent_names_list.
46
 
                      i.e. [('C', ['B']), ('B', ['A']), ('A', [])]
47
 
                      For this input the output from the sort or
48
 
                      iter_topo_order routines will be:
49
 
                      'A', 'B', 'C'
50
 
        
51
 
        node identifiers can be any hashable object, and are typically strings.
52
 
 
53
 
        If you have a graph like [('a', ['b']), ('a', ['c'])] this will only use
54
 
        one of the two values for 'a'.
55
 
 
56
 
        The graph is sorted lazily: until you iterate or sort the input is
57
 
        not processed other than to create an internal representation.
58
 
 
59
 
        iteration or sorting may raise GraphCycleError if a cycle is present 
60
 
        in the graph.
61
 
        """
62
 
        # a dict of the graph.
63
 
        self._graph = dict(graph)
64
 
        ### if debugging:
65
 
        # self._original_graph = dict(graph)
66
 
        
67
 
        # this is a stack storing the depth first search into the graph.
68
 
        self._node_name_stack = []
69
 
        # at each level of 'recursion' we have to check each parent. This
70
 
        # stack stores the parents we have not yet checked for the node at the 
71
 
        # matching depth in _node_name_stack
72
 
        self._pending_parents_stack = []
73
 
        # this is a set of the completed nodes for fast checking whether a
74
 
        # parent in a node we are processing on the stack has already been
75
 
        # emitted and thus can be skipped.
76
 
        self._completed_node_names = set()
77
 
 
78
 
    def sorted(self):
79
 
        """Sort the graph and return as a list.
80
 
        
81
 
        After calling this the sorter is empty and you must create a new one.
82
 
        """
83
 
        return list(self.iter_topo_order())
84
 
 
85
 
###        Useful if fiddling with this code.
86
 
###        # cross check
87
 
###        sorted_names = list(self.iter_topo_order())
88
 
###        for index in range(len(sorted_names)):
89
 
###            rev = sorted_names[index]
90
 
###            for left_index in range(index):
91
 
###                if rev in self.original_graph[sorted_names[left_index]]:
92
 
###                    print "revision in parent list of earlier revision"
93
 
###                    import pdb;pdb.set_trace()
94
 
 
95
 
    def iter_topo_order(self):
96
 
        """Yield the nodes of the graph in a topological order.
97
 
        
98
 
        After finishing iteration the sorter is empty and you cannot continue
99
 
        iteration.
100
 
        """
101
 
        while self._graph:
102
 
            # now pick a random node in the source graph, and transfer it to the
103
 
            # top of the depth first search stack.
104
 
            node_name, parents = self._graph.popitem()
105
 
            self._push_node(node_name, parents)
106
 
            while self._node_name_stack:
107
 
                # loop until this call completes.
108
 
                parents_to_visit = self._pending_parents_stack[-1]
109
 
                # if all parents are done, the revision is done
110
 
                if not parents_to_visit:
111
 
                    # append the revision to the topo sorted list
112
 
                    # all the nodes parents have been added to the output, now
113
 
                    # we can add it to the output.
114
 
                    yield self._pop_node()
115
 
                else:
116
 
                    while self._pending_parents_stack[-1]:
117
 
                        # recurse depth first into a single parent 
118
 
                        next_node_name = self._pending_parents_stack[-1].pop()
119
 
                        if next_node_name in self._completed_node_names:
120
 
                            # this parent was completed by a child on the
121
 
                            # call stack. skip it.
122
 
                            continue
123
 
                        # otherwise transfer it from the source graph into the
124
 
                        # top of the current depth first search stack.
125
 
                        try:
126
 
                            parents = self._graph.pop(next_node_name)
127
 
                        except KeyError:
128
 
                            # if the next node is not in the source graph it has
129
 
                            # already been popped from it and placed into the
130
 
                            # current search stack (but not completed or we would
131
 
                            # have hit the continue 4 lines up.
132
 
                            # this indicates a cycle.
133
 
                            raise errors.GraphCycleError(self._node_name_stack)
134
 
                        self._push_node(next_node_name, parents)
135
 
                        # and do not continue processing parents until this 'call' 
136
 
                        # has recursed.
137
 
                        break
138
 
 
139
 
    def _push_node(self, node_name, parents):
140
 
        """Add node_name to the pending node stack.
141
 
        
142
 
        Names in this stack will get emitted into the output as they are popped
143
 
        off the stack.
144
 
        """
145
 
        self._node_name_stack.append(node_name)
146
 
        self._pending_parents_stack.append(list(parents))
147
 
 
148
 
    def _pop_node(self):
149
 
        """Pop the top node off the stack 
150
 
 
151
 
        The node is appended to the sorted output.
152
 
        """
153
 
        # we are returning from the flattened call frame:
154
 
        # pop off the local variables
155
 
        node_name = self._node_name_stack.pop()
156
 
        self._pending_parents_stack.pop()
157
 
 
158
 
        self._completed_node_names.add(node_name)
159
 
        return node_name
160
 
 
161
 
 
162
 
def merge_sort(graph, branch_tip, mainline_revisions=None, generate_revno=False):
163
 
    """Topological sort a graph which groups merges.
164
 
 
165
 
    :param graph: sequence of pairs of node->parents_list.
166
 
    :param branch_tip: the tip of the branch to graph. Revisions not 
167
 
                       reachable from branch_tip are not included in the
168
 
                       output.
169
 
    :param mainline_revisions: If not None this forces a mainline to be
170
 
                               used rather than synthesised from the graph.
171
 
                               This must be a valid path through some part
172
 
                               of the graph. If the mainline does not cover all
173
 
                               the revisions, output stops at the start of the
174
 
                               old revision listed in the mainline revisions
175
 
                               list.
176
 
                               The order for this parameter is oldest-first.
177
 
    :param generate_revno: Optional parameter controlling the generation of
178
 
        revision number sequences in the output. See the output description of
179
 
        the MergeSorter docstring for details.
180
 
    :result: See the MergeSorter docstring for details.
181
 
    node identifiers can be any hashable object, and are typically strings.
182
 
    """
183
 
    return MergeSorter(graph, branch_tip, mainline_revisions,
184
 
        generate_revno).sorted()
185
 
 
186
 
 
187
 
class MergeSorter(object):
188
 
 
189
 
    def __init__(self, graph, branch_tip, mainline_revisions=None,
190
 
        generate_revno=False):
191
 
        """Merge-aware topological sorting of a graph.
192
 
    
193
 
        :param graph: sequence of pairs of node_name->parent_names_list.
194
 
                      i.e. [('C', ['B']), ('B', ['A']), ('A', [])]
195
 
                      For this input the output from the sort or
196
 
                      iter_topo_order routines will be:
197
 
                      'A', 'B', 'C'
198
 
        :param branch_tip: the tip of the branch to graph. Revisions not 
199
 
                       reachable from branch_tip are not included in the
200
 
                       output.
201
 
        :param mainline_revisions: If not None this forces a mainline to be
202
 
                               used rather than synthesised from the graph.
203
 
                               This must be a valid path through some part
204
 
                               of the graph. If the mainline does not cover all
205
 
                               the revisions, output stops at the start of the
206
 
                               old revision listed in the mainline revisions
207
 
                               list.
208
 
                               The order for this parameter is oldest-first.
209
 
        :param generate_revno: Optional parameter controlling the generation of
210
 
            revision number sequences in the output. See the output description
211
 
            for more details.
212
 
 
213
 
        The result is a list sorted so that all parents come before
214
 
        their children. Each element of the list is a tuple containing:
215
 
        (sequence_number, node_name, merge_depth, end_of_merge)
216
 
         * sequence_number: The sequence of this row in the output. Useful for 
217
 
           GUIs.
218
 
         * node_name: The node name: opaque text to the merge routine.
219
 
         * merge_depth: How many levels of merging deep this node has been
220
 
           found.
221
 
         * revno_sequence: When requested this field provides a sequence of
222
 
             revision numbers for all revisions. The format is:
223
 
             REVNO[[.BRANCHREVNO.REVNO] ...]. BRANCHREVNO is the number of the
224
 
             branch that the revno is on. From left to right the REVNO numbers
225
 
             are the sequence numbers within that branch of the revision.
226
 
             For instance, the graph {A:[], B:['A'], C:['A', 'B']} will get
227
 
             the following revno_sequences assigned: A:(1,), B:(1,1,1), C:(2,).
228
 
             This should be read as 'A is the first commit in the trunk',
229
 
             'B is the first commit on the first branch made from A', 'C is the
230
 
             second commit in the trunk'.
231
 
         * end_of_merge: When True the next node is part of a different merge.
232
 
 
233
 
        
234
 
        node identifiers can be any hashable object, and are typically strings.
235
 
 
236
 
        If you have a graph like [('a', ['b']), ('a', ['c'])] this will only use
237
 
        one of the two values for 'a'.
238
 
 
239
 
        The graph is sorted lazily: until you iterate or sort the input is
240
 
        not processed other than to create an internal representation.
241
 
 
242
 
        iteration or sorting may raise GraphCycleError if a cycle is present 
243
 
        in the graph.
244
 
 
245
 
        Background information on the design:
246
 
        -------------------------------------
247
 
        definition: the end of any cluster or 'merge' occurs when:
248
 
            1 - the next revision has a lower merge depth than we do.
249
 
              i.e.
250
 
              A 0
251
 
              B  1
252
 
              C   2
253
 
              D  1
254
 
              E 0
255
 
              C, D are the ends of clusters, E might be but we need more data.
256
 
            2 - or the next revision at our merge depth is not our left most
257
 
              ancestor.
258
 
              This is required to handle multiple-merges in one commit.
259
 
              i.e.
260
 
              A 0    [F, B, E]
261
 
              B  1   [D, C]
262
 
              C   2  [D]
263
 
              D  1   [F]
264
 
              E  1   [F]
265
 
              F 0
266
 
              C is the end of a cluster due to rule 1.
267
 
              D is not the end of a cluster from rule 1, but is from rule 2: E 
268
 
                is not its left most ancestor
269
 
              E is the end of a cluster due to rule 1
270
 
              F might be but we need more data.
271
 
              
272
 
        we show connecting lines to a parent when:
273
 
         - The parent is the start of a merge within this cluster.
274
 
           That is, the merge was not done to the mainline before this cluster 
275
 
           was merged to the mainline.
276
 
           This can be detected thus:
277
 
            * The parent has a higher merge depth and is the next revision in 
278
 
              the list.
279
 
          
280
 
          The next revision in the list constraint is needed for this case:
281
 
          A 0   [D, B]   
282
 
          B  1  [C, F]   # we do not want to show a line to F which is depth 2 
283
 
                           but not a merge
284
 
          C  1  [H]      # note that this is a long line to show back to the 
285
 
                           ancestor - see the end of merge rules.
286
 
          D 0   [G, E]
287
 
          E  1  [G, F]
288
 
          F   2 [G]
289
 
          G  1  [H]
290
 
          H 0
291
 
         - Part of this merges 'branch':
292
 
          The parent has the same merge depth and is our left most parent and we
293
 
           are not the end of the cluster.
294
 
          A 0   [C, B] lines: [B, C]
295
 
          B  1  [E, C] lines: [C]
296
 
          C 0   [D]    lines: [D]
297
 
          D 0   [F, E] lines: [E, F]
298
 
          E  1  [F]    lines: [F]
299
 
          F 0
300
 
         - The end of this merge/cluster:
301
 
          we can ONLY have multiple parents at the end of a cluster if this
302
 
          branch was previously merged into the 'mainline'.
303
 
          - if we have one and only one parent, show it
304
 
            Note that this may be to a greater merge depth - for instance if
305
 
            this branch continued from a deeply nested branch to add something
306
 
            to it.
307
 
          - if we have more than one parent - show the second oldest (older ==
308
 
            further down the list) parent with
309
 
            an equal or lower merge depth
310
 
             XXXX revisit when awake. ddaa asks about the relevance of each one
311
 
             - maybe more than one parent is relevant
312
 
        """
313
 
        self._generate_revno = generate_revno
314
 
        # a dict of the graph.
315
 
        self._graph = dict(graph)
316
 
        # if there is an explicit mainline, alter the graph to match. This is
317
 
        # easier than checking at every merge whether we are on the mainline and
318
 
        # if so which path to take.
319
 
        if mainline_revisions is None:
320
 
            self._mainline_revisions = []
321
 
            self._stop_revision = None
322
 
        else:
323
 
            self._mainline_revisions = list(mainline_revisions)
324
 
            self._stop_revision = self._mainline_revisions[0]
325
 
        # skip the first revision, its what we reach and its parents are 
326
 
        # therefore irrelevant
327
 
        for index, revision in enumerate(self._mainline_revisions[1:]):
328
 
            # NB: index 0 means self._mainline_revisions[1]
329
 
            # if the mainline matches the graph, nothing to do.
330
 
            parent = self._mainline_revisions[index]
331
 
            if parent is None:
332
 
                # end of mainline_revisions history
333
 
                continue
334
 
            if self._graph[revision][0] == parent:
335
 
                continue
336
 
            # remove it from its prior spot
337
 
            self._graph[revision].remove(parent)
338
 
            # insert it into the start of the mainline
339
 
            self._graph[revision].insert(0, parent)
340
 
        # we need to do a check late in the process to detect end-of-merges
341
 
        # which requires the parents to be accessible: its easier for now
342
 
        # to just keep the original graph around.
343
 
        self._original_graph = dict(self._graph.items())
344
 
        # we need to know the revision numbers of revisions to determine
345
 
        # the revision numbers of their descendants
346
 
        # this is a graph from node to [revno_tuple, sequence_number]
347
 
        # where sequence is the number of branches made from the node,
348
 
        # and revno_tuple is the tuple that was assigned to the node.
349
 
        # we dont know revnos to start with, so we start it seeded with
350
 
        # [None, 0]
351
 
        self._revnos = dict((revision, [None, 0]) for revision in self._graph)
352
 
        # the global implicit root node has revno 0, but we need to know
353
 
        # the sequence number for it too:
354
 
        self._root_sequence = 0
355
 
        
356
 
        # this is a stack storing the depth first search into the graph.
357
 
        self._node_name_stack = []
358
 
        # at each level of recursion we need the merge depth this node is at:
359
 
        self._node_merge_depth_stack = []
360
 
        # at each level of 'recursion' we have to check each parent. This
361
 
        # stack stores the parents we have not yet checked for the node at the 
362
 
        # matching depth in _node_name_stack
363
 
        self._pending_parents_stack = []
364
 
        # When we first look at a node we assign it a seqence number from its
365
 
        # leftmost parent.
366
 
        self._assigned_sequence_stack = []
367
 
        # this is a set of the nodes who have been completely analysed for fast
368
 
        # membership checking
369
 
        self._completed_node_names = set()
370
 
        # this is the scheduling of nodes list.
371
 
        # Nodes are scheduled
372
 
        # from the bottom left of the tree: in the tree
373
 
        # A 0  [D, B]
374
 
        # B  1 [C]
375
 
        # C  1 [D]
376
 
        # D 0  [F, E]
377
 
        # E  1 [F]
378
 
        # F 0
379
 
        # the scheduling order is: F, E, D, C, B, A 
380
 
        # that is - 'left subtree, right subtree, node'
381
 
        # which would mean that when we schedule A we can emit the entire tree.
382
 
        self._scheduled_nodes = []
383
 
        # This records for each node when we have processed its left most 
384
 
        # unmerged subtree. After this subtree is scheduled, all other subtrees
385
 
        # have their merge depth increased by one from this nodes merge depth.
386
 
        # it contains tuples - name, merge_depth
387
 
        self._left_subtree_pushed_stack = []
388
 
 
389
 
        # seed the search with the tip of the branch
390
 
        if branch_tip is not None:
391
 
            parents = self._graph.pop(branch_tip)
392
 
            self._push_node(branch_tip, 0, parents)
393
 
 
394
 
    def sorted(self):
395
 
        """Sort the graph and return as a list.
396
 
        
397
 
        After calling this the sorter is empty and you must create a new one.
398
 
        """
399
 
        return list(self.iter_topo_order())
400
 
 
401
 
    def iter_topo_order(self):
402
 
        """Yield the nodes of the graph in a topological order.
403
 
        
404
 
        After finishing iteration the sorter is empty and you cannot continue
405
 
        iteration.
406
 
        """
407
 
        while self._node_name_stack:
408
 
            # loop until this call completes.
409
 
            parents_to_visit = self._pending_parents_stack[-1]
410
 
            # if all parents are done, the revision is done
411
 
            if not parents_to_visit:
412
 
                # append the revision to the topo sorted scheduled list:
413
 
                # all the nodes parents have been scheduled added, now
414
 
                # we can add it to the output.
415
 
                self._pop_node()
416
 
            else:
417
 
                while self._pending_parents_stack[-1]:
418
 
                    if not self._left_subtree_pushed_stack[-1]:
419
 
                        # recurse depth first into the primary parent
420
 
                        next_node_name = self._pending_parents_stack[-1].pop(0)
421
 
                    else:
422
 
                        # place any merges in right-to-left order for scheduling
423
 
                        # which gives us left-to-right order after we reverse
424
 
                        # the scheduled queue. XXX: This has the effect of 
425
 
                        # allocating common-new revisions to the right-most
426
 
                        # subtree rather than the left most, which will 
427
 
                        # display nicely (you get smaller trees at the top
428
 
                        # of the combined merge).
429
 
                        next_node_name = self._pending_parents_stack[-1].pop()
430
 
                    if next_node_name in self._completed_node_names:
431
 
                        # this parent was completed by a child on the
432
 
                        # call stack. skip it.
433
 
                        continue
434
 
                    # otherwise transfer it from the source graph into the
435
 
                    # top of the current depth first search stack.
436
 
                    try:
437
 
                        parents = self._graph.pop(next_node_name)
438
 
                    except KeyError:
439
 
                        # if the next node is not in the source graph it has
440
 
                        # already been popped from it and placed into the
441
 
                        # current search stack (but not completed or we would
442
 
                        # have hit the continue 4 lines up.
443
 
                        # this indicates a cycle.
444
 
                        raise errors.GraphCycleError(self._node_name_stack)
445
 
                    next_merge_depth = 0
446
 
                    if self._left_subtree_pushed_stack[-1]:
447
 
                        # a new child branch from name_stack[-1]
448
 
                        next_merge_depth = 1
449
 
                    else:
450
 
                        next_merge_depth = 0
451
 
                        self._left_subtree_pushed_stack[-1] = True
452
 
                    next_merge_depth = (
453
 
                        self._node_merge_depth_stack[-1] + next_merge_depth)
454
 
                    self._push_node(
455
 
                        next_node_name,
456
 
                        next_merge_depth,
457
 
                        parents)
458
 
                    # and do not continue processing parents until this 'call' 
459
 
                    # has recursed.
460
 
                    break
461
 
        # We have scheduled the graph. Now deliver the ordered output:
462
 
        sequence_number = 0
463
 
        while self._scheduled_nodes:
464
 
            node_name, merge_depth, revno = self._scheduled_nodes.pop()
465
 
            if node_name == self._stop_revision:
466
 
                return
467
 
            if not len(self._scheduled_nodes):
468
 
                # last revision is the end of a merge
469
 
                end_of_merge = True
470
 
            elif self._scheduled_nodes[-1][1] < merge_depth:
471
 
                # the next node is to our left
472
 
                end_of_merge = True
473
 
            elif (self._scheduled_nodes[-1][1] == merge_depth and
474
 
                  (self._scheduled_nodes[-1][0] not in
475
 
                   self._original_graph[node_name])):
476
 
                # the next node was part of a multiple-merge.
477
 
                end_of_merge = True
478
 
            else:
479
 
                end_of_merge = False
480
 
            if self._generate_revno:
481
 
                yield (sequence_number, node_name, merge_depth, revno, end_of_merge)
482
 
            else:
483
 
                yield (sequence_number, node_name, merge_depth, end_of_merge)
484
 
            sequence_number += 1
485
 
 
486
 
    def _push_node(self, node_name, merge_depth, parents):
487
 
        """Add node_name to the pending node stack.
488
 
        
489
 
        Names in this stack will get emitted into the output as they are popped
490
 
        off the stack.
491
 
        """
492
 
        self._node_name_stack.append(node_name)
493
 
        self._node_merge_depth_stack.append(merge_depth)
494
 
        self._left_subtree_pushed_stack.append(False)
495
 
        self._pending_parents_stack.append(list(parents))
496
 
        # as we push it, assign it a sequence number against its parent:
497
 
        parents = self._original_graph[node_name]
498
 
        if parents:
499
 
            # node has parents, assign from the left most parent.
500
 
            parent_revno = self._revnos[parents[0]]
501
 
            sequence = parent_revno[1]
502
 
            parent_revno[1] += 1
503
 
        else:
504
 
            # no parents, use the root sequence
505
 
            sequence = self._root_sequence
506
 
            self._root_sequence +=1
507
 
        self._assigned_sequence_stack.append(sequence)
508
 
 
509
 
    def _pop_node(self):
510
 
        """Pop the top node off the stack 
511
 
 
512
 
        The node is appended to the sorted output.
513
 
        """
514
 
        # we are returning from the flattened call frame:
515
 
        # pop off the local variables
516
 
        node_name = self._node_name_stack.pop()
517
 
        merge_depth = self._node_merge_depth_stack.pop()
518
 
        sequence = self._assigned_sequence_stack.pop()
519
 
        # remove this node from the pending lists:
520
 
        self._left_subtree_pushed_stack.pop()
521
 
        self._pending_parents_stack.pop()
522
 
 
523
 
        parents = self._original_graph[node_name]
524
 
        if parents:
525
 
            # node has parents, assign from the left most parent.
526
 
            parent_revno = self._revnos[parents[0]]
527
 
            if sequence:
528
 
                # not the first child, make a new branch
529
 
                revno = parent_revno[0] + (sequence, 1)
530
 
            else:
531
 
                # increment the sequence number within the branch
532
 
                revno = parent_revno[0][:-1] + (parent_revno[0][-1] + 1,)
533
 
        else:
534
 
            # no parents, use the root sequence
535
 
            if sequence:
536
 
                # make a parallel import revision number
537
 
                revno = (0, sequence, 1)
538
 
            else:
539
 
                revno = (1,)
540
 
 
541
 
        # store the revno for this node for future reference
542
 
        self._revnos[node_name][0] = revno
543
 
        self._completed_node_names.add(node_name)
544
 
        self._scheduled_nodes.append((node_name, merge_depth, self._revnos[node_name][0]))
545
 
        return node_name