revlog: add __contains__ for fast membership test
authorYuya Nishihara <yuya@tcha.org>
Wed, 04 Feb 2015 21:25:57 +0900
changeset 24030 828dc8db5515
parent 24029 e1dbe0b215ae
child 24031 fe1abb5d92f6
revlog: add __contains__ for fast membership test Because revlog implements __iter__, "rev in revlog" works but does silly O(n) lookup unexpectedly. So it seems good to add fast version of __contains__. This allows "rev in repo.changelog" in the next patch.
mercurial/changelog.py
mercurial/revlog.py
--- a/mercurial/changelog.py	Mon Feb 02 19:58:41 2015 -0500
+++ b/mercurial/changelog.py	Wed Feb 04 21:25:57 2015 +0900
@@ -143,6 +143,11 @@
             if i not in self.filteredrevs:
                 return self.node(i)
 
+    def __contains__(self, rev):
+        """filtered version of revlog.__contains__"""
+        return (revlog.revlog.__contains__(self, rev)
+                and rev not in self.filteredrevs)
+
     def __iter__(self):
         """filtered version of revlog.__iter__"""
         if len(self.filteredrevs) == 0:
--- a/mercurial/revlog.py	Mon Feb 02 19:58:41 2015 -0500
+++ b/mercurial/revlog.py	Wed Feb 04 21:25:57 2015 +0900
@@ -277,6 +277,8 @@
 
     def tip(self):
         return self.node(len(self.index) - 2)
+    def __contains__(self, rev):
+        return 0 <= rev < len(self)
     def __len__(self):
         return len(self.index) - 1
     def __iter__(self):