Show More
@@ -1,120 +1,75 | |||
|
1 | 1 | # dagutil.py - dag utilities for mercurial |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2010 Benoit Boissinot <bboissin@gmail.com> |
|
4 | 4 | # and Peter Arrenbrecht <peter@arrenbrecht.ch> |
|
5 | 5 | # |
|
6 | 6 | # This software may be used and distributed according to the terms of the |
|
7 | 7 | # GNU General Public License version 2 or any later version. |
|
8 | 8 | |
|
9 | 9 | from __future__ import absolute_import |
|
10 | 10 | |
|
11 | 11 | from .node import nullrev |
|
12 | 12 | |
|
13 |
class |
|
|
14 | '''generic interface for DAGs | |
|
15 | ||
|
16 | terms: | |
|
17 | "ix" (short for index) identifies a nodes internally, | |
|
18 | "id" identifies one externally. | |
|
19 | ||
|
20 | All params are ixs unless explicitly suffixed otherwise. | |
|
21 | Pluralized params are lists or sets. | |
|
22 | ''' | |
|
23 | ||
|
24 | def parents(self, ix): | |
|
25 | '''list of parents ixs of ix''' | |
|
26 | raise NotImplementedError | |
|
27 | ||
|
28 | def headsetofconnecteds(self, ixs): | |
|
29 | ''' | |
|
30 | subset of connected list of ixs so that no node has a descendant in it | |
|
31 | ||
|
32 | By "connected list" we mean that if an ancestor and a descendant are in | |
|
33 | the list, then so is at least one path connecting them. | |
|
34 | ''' | |
|
35 | raise NotImplementedError | |
|
36 | ||
|
37 | class genericdag(basedag): | |
|
38 | '''generic implementations for DAGs''' | |
|
39 | ||
|
40 | def headsetofconnecteds(self, ixs): | |
|
41 | hds = set(ixs) | |
|
42 | if not hds: | |
|
43 | return hds | |
|
44 | for n in ixs: | |
|
45 | for p in self.parents(n): | |
|
46 | hds.discard(p) | |
|
47 | assert hds | |
|
48 | return hds | |
|
49 | ||
|
50 | ||
|
51 | class revlogbaseddag(basedag): | |
|
52 | '''generic dag interface to a revlog''' | |
|
53 | ||
|
54 | def __init__(self, revlog): | |
|
55 | basedag.__init__(self) | |
|
56 | self._revlog = revlog | |
|
57 | ||
|
58 | class revlogdag(revlogbaseddag): | |
|
13 | class revlogdag(object): | |
|
59 | 14 | '''dag interface to a revlog''' |
|
60 | 15 | |
|
61 | 16 | def __init__(self, revlog): |
|
62 | revlogbaseddag.__init__(self, revlog) | |
|
17 | self._revlog = revlog | |
|
63 | 18 | |
|
64 | 19 | def parents(self, ix): |
|
65 | 20 | rlog = self._revlog |
|
66 | 21 | idx = rlog.index |
|
67 | 22 | revdata = idx[ix] |
|
68 | 23 | prev = revdata[5] |
|
69 | 24 | if prev != nullrev: |
|
70 | 25 | prev2 = revdata[6] |
|
71 | 26 | if prev2 == nullrev: |
|
72 | 27 | return [prev] |
|
73 | 28 | return [prev, prev2] |
|
74 | 29 | prev2 = revdata[6] |
|
75 | 30 | if prev2 != nullrev: |
|
76 | 31 | return [prev2] |
|
77 | 32 | return [] |
|
78 | 33 | |
|
79 | 34 | def headsetofconnecteds(self, ixs): |
|
80 | 35 | if not ixs: |
|
81 | 36 | return set() |
|
82 | 37 | rlog = self._revlog |
|
83 | 38 | idx = rlog.index |
|
84 | 39 | headrevs = set(ixs) |
|
85 | 40 | for rev in ixs: |
|
86 | 41 | revdata = idx[rev] |
|
87 | 42 | for i in [5, 6]: |
|
88 | 43 | prev = revdata[i] |
|
89 | 44 | if prev != nullrev: |
|
90 | 45 | headrevs.discard(prev) |
|
91 | 46 | assert headrevs |
|
92 | 47 | return headrevs |
|
93 | 48 | |
|
94 | 49 | def linearize(self, ixs): |
|
95 | 50 | '''linearize and topologically sort a list of revisions |
|
96 | 51 | |
|
97 | 52 | The linearization process tries to create long runs of revs where |
|
98 | 53 | a child rev comes immediately after its first parent. This is done by |
|
99 | 54 | visiting the heads of the given revs in inverse topological order, |
|
100 | 55 | and for each visited rev, visiting its second parent, then its first |
|
101 | 56 | parent, then adding the rev itself to the output list. |
|
102 | 57 | ''' |
|
103 | 58 | sorted = [] |
|
104 | 59 | visit = list(self.headsetofconnecteds(ixs)) |
|
105 | 60 | visit.sort(reverse=True) |
|
106 | 61 | finished = set() |
|
107 | 62 | |
|
108 | 63 | while visit: |
|
109 | 64 | cur = visit.pop() |
|
110 | 65 | if cur < 0: |
|
111 | 66 | cur = -cur - 1 |
|
112 | 67 | if cur not in finished: |
|
113 | 68 | sorted.append(cur) |
|
114 | 69 | finished.add(cur) |
|
115 | 70 | else: |
|
116 | 71 | visit.append(-cur - 1) |
|
117 | 72 | visit += [p for p in self.parents(cur) |
|
118 | 73 | if p in ixs and p not in finished] |
|
119 | 74 | assert len(sorted) == len(ixs) |
|
120 | 75 | return sorted |
General Comments 0
You need to be logged in to leave comments.
Login now