Show More
@@ -129,89 +129,6 b' def ancestors(pfunc, *orignodes):' | |||||
129 | return gca |
|
129 | return gca | |
130 | return deepest(gca) |
|
130 | return deepest(gca) | |
131 |
|
131 | |||
132 | def genericancestor(a, b, pfunc): |
|
|||
133 | """ |
|
|||
134 | Returns the common ancestor of a and b that is furthest from a |
|
|||
135 | root (as measured by longest path) or None if no ancestor is |
|
|||
136 | found. If there are multiple common ancestors at the same |
|
|||
137 | distance, the first one found is returned. |
|
|||
138 |
|
||||
139 | pfunc must return a list of parent vertices for a given vertex |
|
|||
140 | """ |
|
|||
141 |
|
||||
142 | if a == b: |
|
|||
143 | return a |
|
|||
144 |
|
||||
145 | a, b = sorted([a, b]) |
|
|||
146 |
|
||||
147 | # find depth from root of all ancestors |
|
|||
148 | # depth is stored as a negative for heapq |
|
|||
149 | parentcache = {} |
|
|||
150 | visit = [a, b] |
|
|||
151 | depth = {} |
|
|||
152 | while visit: |
|
|||
153 | vertex = visit[-1] |
|
|||
154 | pl = [p for p in pfunc(vertex) if p != nullrev] |
|
|||
155 | parentcache[vertex] = pl |
|
|||
156 | if not pl: |
|
|||
157 | depth[vertex] = 0 |
|
|||
158 | visit.pop() |
|
|||
159 | else: |
|
|||
160 | for p in pl: |
|
|||
161 | if p == a or p == b: # did we find a or b as a parent? |
|
|||
162 | return p # we're done |
|
|||
163 | if p not in depth: |
|
|||
164 | visit.append(p) |
|
|||
165 | if visit[-1] == vertex: |
|
|||
166 | # -(maximum distance of parents + 1) |
|
|||
167 | depth[vertex] = min([depth[p] for p in pl]) - 1 |
|
|||
168 | visit.pop() |
|
|||
169 |
|
||||
170 | # traverse ancestors in order of decreasing distance from root |
|
|||
171 | def ancestors(vertex): |
|
|||
172 | h = [(depth[vertex], vertex)] |
|
|||
173 | seen = set() |
|
|||
174 | while h: |
|
|||
175 | d, n = heapq.heappop(h) |
|
|||
176 | if n not in seen: |
|
|||
177 | seen.add(n) |
|
|||
178 | yield (d, n) |
|
|||
179 | for p in parentcache[n]: |
|
|||
180 | heapq.heappush(h, (depth[p], p)) |
|
|||
181 |
|
||||
182 | def generations(vertex): |
|
|||
183 | sg, s = None, set() |
|
|||
184 | for g, v in ancestors(vertex): |
|
|||
185 | if g != sg: |
|
|||
186 | if sg: |
|
|||
187 | yield sg, s |
|
|||
188 | sg, s = g, set((v,)) |
|
|||
189 | else: |
|
|||
190 | s.add(v) |
|
|||
191 | yield sg, s |
|
|||
192 |
|
||||
193 | x = generations(a) |
|
|||
194 | y = generations(b) |
|
|||
195 | gx = x.next() |
|
|||
196 | gy = y.next() |
|
|||
197 |
|
||||
198 | # increment each ancestor list until it is closer to root than |
|
|||
199 | # the other, or they match |
|
|||
200 | try: |
|
|||
201 | while True: |
|
|||
202 | if gx[0] == gy[0]: |
|
|||
203 | for v in gx[1]: |
|
|||
204 | if v in gy[1]: |
|
|||
205 | return v |
|
|||
206 | gy = y.next() |
|
|||
207 | gx = x.next() |
|
|||
208 | elif gx[0] > gy[0]: |
|
|||
209 | gy = y.next() |
|
|||
210 | else: |
|
|||
211 | gx = x.next() |
|
|||
212 | except StopIteration: |
|
|||
213 | return None |
|
|||
214 |
|
||||
215 | def missingancestors(revs, bases, pfunc): |
|
132 | def missingancestors(revs, bases, pfunc): | |
216 | """Return all the ancestors of revs that are not ancestors of bases. |
|
133 | """Return all the ancestors of revs that are not ancestors of bases. | |
217 |
|
134 |
General Comments 0
You need to be logged in to leave comments.
Login now