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