Patchwork revset: added ordered generatorset classes with __contains__ method

login
register
mail settings
Submitter Lucas Moscovicz
Date Feb. 28, 2014, 1:44 a.m.
Message ID <e8aa9bdadfbc713901c0.1393551898@dev1037.prn2.facebook.com>
Download mbox | patch
Permalink /patch/3804/
State Superseded
Commit 7fc371d2e5a38d8d06878ed78da3f6c22364659d
Headers show

Comments

Lucas Moscovicz - Feb. 28, 2014, 1:44 a.m.
# HG changeset patch
# User Lucas Moscovicz <lmoscovicz@fb.com>
# Date 1393550823 28800
#      Thu Feb 27 17:27:03 2014 -0800
# Node ID e8aa9bdadfbc713901c02d96c0149bda50174207
# Parent  d9c1f97dac5f752a9def8f03bcb6604b8ee05c39
revset: added ordered generatorset classes with __contains__ method

They stop iterating as soon as they go past the value they are looking for,
so, for values not in the generator they return faster.

Patch

diff --git a/mercurial/revset.py b/mercurial/revset.py
--- a/mercurial/revset.py
+++ b/mercurial/revset.py
@@ -2284,6 +2284,50 @@ 
     def set(self):
         return self
 
+class ascgeneratorset(generatorset):
+    """ Same structure as generatorset but stops iterating after it goes past
+    the value when asked for membership and the element is not contained
+    """
+    def __contains__(self, x):
+        if x in self._cache:
+            return self._cache[x]
+
+        self._iterated = True
+        while True:
+            try:
+                l = self._nextitem()
+                if l == x:
+                    return True
+                if l > x:
+                    break
+            except (StopIteration):
+                break
+
+        self._cache[x] = False
+        return False
+
+class descgeneratorset(generatorset):
+    """ Same structure as generatorset but stops iterating after it goes past
+    the value when asked for membership and the element is not contained
+    """
+    def __contains__(self, x):
+        if x in self._cache:
+            return self._cache[x]
+
+        self._iterated = True
+        while True:
+            try:
+                l = self._nextitem()
+                if l == x:
+                    return True
+                if l < x:
+                    break
+            except (StopIteration):
+                break
+
+        self._cache[x] = False
+        return False
+
 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