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

login
register
mail settings
Submitter Lucas Moscovicz
Date March 7, 2014, 10:31 p.m.
Message ID <b590b0931be764118f54.1394231516@dev1037.prn2.facebook.com>
Download mbox | patch
Permalink /patch/3886/
State Accepted
Commit 621c94378d0d377e2ff1a6797ed3574e5f6a77ed
Headers show

Comments

Lucas Moscovicz - March 7, 2014, 10:31 p.m.
# HG changeset patch
# User Lucas Moscovicz <lmoscovicz@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.

Patch

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.