[PATCH 2 of 2] revset: added ordered generatorset classes with __contains__ method

Lucas Moscovicz lmoscovicz at fb.com
Mon Mar 3 15:10:13 CST 2014


# HG changeset patch
# User Lucas Moscovicz <lmoscovicz at fb.com>
# Date 1393550823 28800
#      Thu Feb 27 17:27:03 2014 -0800
# Node ID 3a0b87b24acf69980b020c523ca96f437c162814
# Parent  d4364169b474c6e7a17e3e8656d368998b76ae44
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.

diff --git a/mercurial/revset.py b/mercurial/revset.py
--- a/mercurial/revset.py
+++ b/mercurial/revset.py
@@ -2307,6 +2307,40 @@
     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]
+
+        for l in self:
+            if l == x:
+                return True
+            if l > x:
+                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]
+
+        for l in self:
+            if l == x:
+                return True
+            if l < x:
+                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


More information about the Mercurial-devel mailing list