dagop: unnest inner generator of revancestors()
authorYuya Nishihara <yuya@tcha.org>
Sat, 17 Jun 2017 22:33:23 +0900
changeset 32997 b9e2269aeff8
parent 32996 1c97df5e3b46
child 32998 c7da57bbae96
dagop: unnest inner generator of revancestors() This just moves iterate() to module-level function.
mercurial/dagop.py
--- a/mercurial/dagop.py	Wed Jun 21 17:17:17 2017 +0200
+++ b/mercurial/dagop.py	Sat Jun 17 22:33:23 2017 +0900
@@ -20,43 +20,43 @@
 baseset = smartset.baseset
 generatorset = smartset.generatorset
 
-def revancestors(repo, revs, followfirst):
-    """Like revlog.ancestors(), but supports followfirst."""
+def _genrevancestors(repo, revs, followfirst):
     if followfirst:
         cut = 1
     else:
         cut = None
     cl = repo.changelog
+    revs.sort(reverse=True)
+    irevs = iter(revs)
+    h = []
 
-    def iterate():
-        revs.sort(reverse=True)
-        irevs = iter(revs)
-        h = []
-
-        inputrev = next(irevs, None)
-        if inputrev is not None:
-            heapq.heappush(h, -inputrev)
+    inputrev = next(irevs, None)
+    if inputrev is not None:
+        heapq.heappush(h, -inputrev)
 
-        seen = set()
-        while h:
-            current = -heapq.heappop(h)
-            if current == inputrev:
-                inputrev = next(irevs, None)
-                if inputrev is not None:
-                    heapq.heappush(h, -inputrev)
-            if current not in seen:
-                seen.add(current)
-                yield current
-                try:
-                    for parent in cl.parentrevs(current)[:cut]:
-                        if parent != node.nullrev:
-                            heapq.heappush(h, -parent)
-                except error.WdirUnsupported:
-                    for parent in repo[current].parents()[:cut]:
-                        if parent.rev() != node.nullrev:
-                            heapq.heappush(h, -parent.rev())
+    seen = set()
+    while h:
+        current = -heapq.heappop(h)
+        if current == inputrev:
+            inputrev = next(irevs, None)
+            if inputrev is not None:
+                heapq.heappush(h, -inputrev)
+        if current not in seen:
+            seen.add(current)
+            yield current
+            try:
+                for parent in cl.parentrevs(current)[:cut]:
+                    if parent != node.nullrev:
+                        heapq.heappush(h, -parent)
+            except error.WdirUnsupported:
+                for parent in repo[current].parents()[:cut]:
+                    if parent.rev() != node.nullrev:
+                        heapq.heappush(h, -parent.rev())
 
-    return generatorset(iterate(), iterasc=False)
+def revancestors(repo, revs, followfirst):
+    """Like revlog.ancestors(), but supports followfirst."""
+    gen = _genrevancestors(repo, revs, followfirst)
+    return generatorset(gen, iterasc=False)
 
 def revdescendants(repo, revs, followfirst):
     """Like revlog.descendants() but supports followfirst."""