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