[PATCH] revset: added stopiterating condition to generatorset

Lucas Moscovicz lmoscovicz at fb.com
Tue Feb 25 15:16:47 CST 2014


# HG changeset patch
# User Lucas Moscovicz <lmoscovicz at fb.com>
# Date 1391796950 28800
#      Fri Feb 07 10:15:50 2014 -0800
# Node ID e1b82805679d24f3de891162d780971da4e8e151
# Parent  f4d57e463ca01c39ea60703c378025ff86c6ab18
revset: added stopiterating condition to generatorset

This will be used for when the generated content is known to be sorted and a
condition can be set to stop iterating without going through all the values.

diff --git a/mercurial/revset.py b/mercurial/revset.py
--- a/mercurial/revset.py
+++ b/mercurial/revset.py
@@ -2191,14 +2191,16 @@
     """Wrapper structure for generators that provides lazy membership and can
     be iterated more than once.
     When asked for membership it generates values until either it finds the
-    requested one or has gone through all the elements in the generator
+    requested one, the stopiter condition returns true or it has gone through
+    all the elements in the generator
     """
-    def __init__(self, gen):
+    def __init__(self, gen, stopiter=None):
         self._gen = gen
         self._iter = iter(gen)
         self._cache = {}
         self._genlist = baseset([])
         self._iterated = False
+        self._stopiter = stopiter
 
     def _nextitem(self):
         l = self._iter.next()
@@ -2211,13 +2213,26 @@
             return self._cache[x]
 
         self._iterated = True
-        while True:
-            try:
-                l = self._nextitem()
-                if l == x:
-                    return True
-            except (StopIteration):
-                break
+        if self._stopiter:
+            stop = self._stopiter
+            while True:
+                try:
+                    l = self._nextitem()
+                    if l == x:
+                        return True
+                    if stop(l, x):
+                        self._cache[x] = False
+                        return False
+                except (StopIteration):
+                    break
+        else:
+            while True:
+                try:
+                    l = self._nextitem()
+                    if l == x:
+                        return True
+                except (StopIteration):
+                    break
 
         self._cache[x] = False
         return False


More information about the Mercurial-devel mailing list