revset: added addset class with its basic methods
authorLucas Moscovicz <lmoscovicz@fb.com>
Fri, 07 Mar 2014 13:48:31 -0800
changeset 20694 621c94378d0d
parent 20693 d04aac468bf4
child 20695 d99fcf4483f8
revset: added addset class with its basic methods This class addresses the problem of losing performance on the __contains__ method when adding two smart structures with fast membership testing.
mercurial/revset.py
--- a/mercurial/revset.py	Tue Feb 11 14:03:43 2014 -0800
+++ b/mercurial/revset.py	Fri Mar 07 13:48:31 2014 -0800
@@ -2314,6 +2314,35 @@
         self._subset.reverse()
         self._ascending = not self._ascending
 
+class addset(object):
+    """Wrapper structure for lazily adding two structures without losing much
+    performance on the __contains__ method
+    """
+    def __init__(self, revs1, revs2):
+        self._r1 = revs1
+        self._r2 = revs2
+        self._iter = None
+
+    def _iterator(self):
+        if not self._iter:
+            def gen():
+                for r in self._r1:
+                    yield r
+                s = self._r1.set()
+                for r in self._r2:
+                    if r not in s:
+                        yield r
+            self._iter = generatorset(gen())
+
+        return self._iter
+
+    def __iter__(self):
+        for r in self._iterator():
+            yield r
+
+    def __contains__(self, x):
+        return x in self._r1 or x in self._r2
+
 class generatorset(object):
     """Wrapper structure for generators that provides lazy membership and can
     be iterated more than once.