Show More
@@ -1,88 +1,91 | |||
|
1 | 1 | # ancestor.py - generic DAG ancestor algorithm for mercurial |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2006 Matt Mackall <mpm@selenic.com> |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms of the |
|
6 | 6 | # GNU General Public License version 2 or any later version. |
|
7 | 7 | |
|
8 | 8 | import heapq |
|
9 | 9 | |
|
10 | 10 | def ancestor(a, b, pfunc): |
|
11 | 11 | """ |
|
12 | return a minimal-distance ancestor of nodes a and b, or None if there is no | |
|
13 | such ancestor. Note that there can be several ancestors with the same | |
|
14 | (minimal) distance, and the one returned is arbitrary. | |
|
12 | Returns the common ancestor of a and b that is furthest from a | |
|
13 | root (as measured by longest path) or None if no ancestor is | |
|
14 | found. If there are multiple common ancestors at the same | |
|
15 | distance, the first one found is returned. | |
|
15 | 16 | |
|
16 | 17 | pfunc must return a list of parent vertices for a given vertex |
|
17 | 18 | """ |
|
18 | 19 | |
|
19 | 20 | if a == b: |
|
20 | 21 | return a |
|
21 | 22 | |
|
22 | 23 | a, b = sorted([a, b]) |
|
23 | 24 | |
|
24 | 25 | # find depth from root of all ancestors |
|
26 | # depth is stored as a negative for heapq | |
|
25 | 27 | parentcache = {} |
|
26 | 28 | visit = [a, b] |
|
27 | 29 | depth = {} |
|
28 | 30 | while visit: |
|
29 | 31 | vertex = visit[-1] |
|
30 | 32 | pl = pfunc(vertex) |
|
31 | 33 | parentcache[vertex] = pl |
|
32 | 34 | if not pl: |
|
33 | 35 | depth[vertex] = 0 |
|
34 | 36 | visit.pop() |
|
35 | 37 | else: |
|
36 | 38 | for p in pl: |
|
37 | 39 | if p == a or p == b: # did we find a or b as a parent? |
|
38 | 40 | return p # we're done |
|
39 | 41 | if p not in depth: |
|
40 | 42 | visit.append(p) |
|
41 | 43 | if visit[-1] == vertex: |
|
44 | # -(maximum distance of parents + 1) | |
|
42 | 45 | depth[vertex] = min([depth[p] for p in pl]) - 1 |
|
43 | 46 | visit.pop() |
|
44 | 47 | |
|
45 | 48 | # traverse ancestors in order of decreasing distance from root |
|
46 | 49 | def ancestors(vertex): |
|
47 | 50 | h = [(depth[vertex], vertex)] |
|
48 | 51 | seen = set() |
|
49 | 52 | while h: |
|
50 | 53 | d, n = heapq.heappop(h) |
|
51 | 54 | if n not in seen: |
|
52 | 55 | seen.add(n) |
|
53 | 56 | yield (d, n) |
|
54 | 57 | for p in parentcache[n]: |
|
55 | 58 | heapq.heappush(h, (depth[p], p)) |
|
56 | 59 | |
|
57 | 60 | def generations(vertex): |
|
58 | 61 | sg, s = None, set() |
|
59 | 62 | for g, v in ancestors(vertex): |
|
60 | 63 | if g != sg: |
|
61 | 64 | if sg: |
|
62 | 65 | yield sg, s |
|
63 | 66 | sg, s = g, set((v,)) |
|
64 | 67 | else: |
|
65 | 68 | s.add(v) |
|
66 | 69 | yield sg, s |
|
67 | 70 | |
|
68 | 71 | x = generations(a) |
|
69 | 72 | y = generations(b) |
|
70 | 73 | gx = x.next() |
|
71 | 74 | gy = y.next() |
|
72 | 75 | |
|
73 | 76 | # increment each ancestor list until it is closer to root than |
|
74 | 77 | # the other, or they match |
|
75 | 78 | try: |
|
76 | 79 | while 1: |
|
77 | 80 | if gx[0] == gy[0]: |
|
78 | 81 | for v in gx[1]: |
|
79 | 82 | if v in gy[1]: |
|
80 | 83 | return v |
|
81 | 84 | gy = y.next() |
|
82 | 85 | gx = x.next() |
|
83 | 86 | elif gx[0] > gy[0]: |
|
84 | 87 | gy = y.next() |
|
85 | 88 | else: |
|
86 | 89 | gx = x.next() |
|
87 | 90 | except StopIteration: |
|
88 | 91 | return None |
General Comments 0
You need to be logged in to leave comments.
Login now