[PATCH 1 of 2 RFC] revset: added generatorset class with cached __contains__ method

Lucas Moscovicz lmoscovicz at fb.com
Thu Feb 13 16:56:17 CST 2014


# HG changeset patch
# User Lucas Moscovicz <lmoscovicz at fb.com>
# Date 1391642591 28800
#      Wed Feb 05 15:23:11 2014 -0800
# Node ID f4d973e19d267efa440d992583a860fc2f8eed98
# Parent  365b233e02ebf05d1b91e48d12f28f0c5ddd990c
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
@@ -2145,6 +2145,36 @@
     def set(self):
         return set([r for r in self])
 
+class generatorset(object):
+    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