Mercurial > hg
changeset 24030:828dc8db5515
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.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Wed, 04 Feb 2015 21:25:57 +0900 |
parents | e1dbe0b215ae |
children | fe1abb5d92f6 |
files | mercurial/changelog.py mercurial/revlog.py |
diffstat | 2 files changed, 7 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- 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):