[PATCH] revset: added generatorset class with cached __contains__ method

Lucas Moscovicz lmoscovicz at fb.com
Tue Feb 18 22:29:28 UTC 2014


# HG changeset patch
# User Lucas Moscovicz <lmoscovicz at fb.com>
# Date 1391642591 28800
#      Wed Feb 05 15:23:11 2014 -0800
# Node ID 7dfddd4a822ee7e5a3ce6f39a0bdf9c533cd76ff
# Parent  c29948fed40a2d9755ecaa01ec05bfa542f65670
revset: added generatorset class with cached __contains__ method

diff --git a/mercurial/revset.py b/mercurial/revset.py
--- a/mercurial/revset.py
+++ b/mercurial/revset.py
@@ -2131,6 +2131,37 @@
     def set(self):
         return set([r for r in self])
 
+class generatorset(object):
+    """Wrapper structure for generators that provides lazy membership."""
+    def __init__(self, gen):
+        self._gen = gen
+        self._iter = iter(gen)
+        self._cache = {}
+
+    def __contains__(self, x):
+        if x in self._cache:
+            return self._cache[x]
+
+        while True:
+            try:
+                l = self._iter.next()
+                self._cache[l] = True
+                if l == x:
+                    return True
+            except (StopIteration):
+                break
+
+        self._cache[x] = False
+        return False
+
+    def __iter__(self):
+        for item in self._gen:
+            self._cache[item] = True
+            yield item
+
+    def set(self):
+        return self
+
 class spanset(object):
     """Duck type for baseset class which represents a range of revisions and
     can work lazily and without having all the range in memory



More information about the Mercurial-devel mailing list