[PATCH 1 of 2] revset: added addset class with its basic methods

Lucas Moscovicz lmoscovicz at fb.com
Fri Mar 7 16:31:56 CST 2014


# HG changeset patch
# User Lucas Moscovicz <lmoscovicz at fb.com>
# Date 1394228911 28800
#      Fri Mar 07 13:48:31 2014 -0800
# Node ID b590b0931be764118f54a00e1316d19b4b4573ed
# Parent  b04f4e319df23fc242161f7df8c6182989501433
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.

diff --git a/mercurial/revset.py b/mercurial/revset.py
--- a/mercurial/revset.py
+++ b/mercurial/revset.py
@@ -2281,6 +2281,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.


More information about the Mercurial-devel mailing list