##// END OF EJS Templates
ancestor: caching the parent list to improve performance...
Nicolas Dumazet -
r7882:8d78fc99 default
parent child Browse files
Show More
@@ -19,11 +19,13 b' def ancestor(a, b, pfunc):'
19 return a
19 return a
20
20
21 # find depth from root of all ancestors
21 # find depth from root of all ancestors
22 parentcache = {}
22 visit = [a, b]
23 visit = [a, b]
23 depth = {}
24 depth = {}
24 while visit:
25 while visit:
25 vertex = visit[-1]
26 vertex = visit[-1]
26 pl = pfunc(vertex)
27 pl = pfunc(vertex)
28 parentcache[vertex] = pl
27 if not pl:
29 if not pl:
28 depth[vertex] = 0
30 depth[vertex] = 0
29 visit.pop()
31 visit.pop()
@@ -46,7 +48,7 b' def ancestor(a, b, pfunc):'
46 if n not in seen:
48 if n not in seen:
47 seen[n] = 1
49 seen[n] = 1
48 yield (d, n)
50 yield (d, n)
49 for p in pfunc(n):
51 for p in parentcache[n]:
50 heapq.heappush(h, (depth[p], p))
52 heapq.heappush(h, (depth[p], p))
51
53
52 def generations(vertex):
54 def generations(vertex):
General Comments 0
You need to be logged in to leave comments. Login now