Patchwork [1,of,2,RFC] revset: added generatorset class with cached __contains__ method

login
register
mail settings
Submitter Lucas Moscovicz
Date Feb. 13, 2014, 10:56 p.m.
Message ID <f4d973e19d267efa440d.1392332177@dev1037.prn2.facebook.com>
Download mbox | patch
Permalink /patch/3651/
State Superseded
Headers show

Comments

Lucas Moscovicz - Feb. 13, 2014, 10:56 p.m.
# HG changeset patch
# User Lucas Moscovicz <lmoscovicz@fb.com>
# Date 1391642591 28800
#      Wed Feb 05 15:23:11 2014 -0800
# Node ID f4d973e19d267efa440d992583a860fc2f8eed98
# Parent  365b233e02ebf05d1b91e48d12f28f0c5ddd990c
revset: added generatorset class with cached __contains__ method
Pierre-Yves David - Feb. 13, 2014, 10:58 p.m.
On 02/13/2014 02:56 PM, Lucas Moscovicz wrote:
> # HG changeset patch
> # User Lucas Moscovicz <lmoscovicz@fb.com>
> # Date 1391642591 28800
> #      Wed Feb 05 15:23:11 2014 -0800
> # Node ID f4d973e19d267efa440d992583a860fc2f8eed98
> # Parent  365b233e02ebf05d1b91e48d12f28f0c5ddd990c
> revset: added generatorset class with cached __contains__ method
>
> diff --git a/mercurial/revset.py b/mercurial/revset.py
> --- a/mercurial/revset.py
> +++ b/mercurial/revset.py
> @@ -2145,6 +2145,36 @@
>       def set(self):
>           return set([r for r in self])
>
> +class generatorset(object):
> +    def __init__(self, gen):

Now that the big picture is emerging, I believe you should start adding 
docstring to you code. Otherwise we'll end up with a very cool pile of 
code that nobody can read without burning its brain for a day (MQ And 
hgweb, I'm looking at you)
Matt Mackall - Feb. 13, 2014, 11:23 p.m.
On Thu, 2014-02-13 at 14:58 -0800, Pierre-Yves David wrote:
> 
> On 02/13/2014 02:56 PM, Lucas Moscovicz wrote:
> > # HG changeset patch
> > # User Lucas Moscovicz <lmoscovicz@fb.com>
> > # Date 1391642591 28800
> > #      Wed Feb 05 15:23:11 2014 -0800
> > # Node ID f4d973e19d267efa440d992583a860fc2f8eed98
> > # Parent  365b233e02ebf05d1b91e48d12f28f0c5ddd990c
> > revset: added generatorset class with cached __contains__ method
> >
> > diff --git a/mercurial/revset.py b/mercurial/revset.py
> > --- a/mercurial/revset.py
> > +++ b/mercurial/revset.py
> > @@ -2145,6 +2145,36 @@
> >       def set(self):
> >           return set([r for r in self])
> >
> > +class generatorset(object):
> > +    def __init__(self, gen):
> 
> Now that the big picture is emerging, I believe you should start adding 
> docstring to you code. Otherwise we'll end up with a very cool pile of 
> code that nobody can read without burning its brain for a day (MQ And 
> hgweb, I'm looking at you)

Agreed, these look fine with a bit more docs/comments.

Patch

diff --git a/mercurial/revset.py b/mercurial/revset.py
--- a/mercurial/revset.py
+++ b/mercurial/revset.py
@@ -2145,6 +2145,36 @@ 
     def set(self):
         return set([r for r in self])
 
+class generatorset(object):
+    def __init__(self, gen):
+        self._gen = gen
+        self._iter = iter(gen)
+        self._cache = {}
+
+    def __contains__(self, x):
+        if x in self._cache:
+            return self._cache[x]
+
+        while True:
+            try:
+                l = self._iter.next()
+                self._cache[l] = True
+                if l == x:
+                    return True
+            except (StopIteration):
+                break
+
+        self._cache[x] = False
+        return False
+
+    def __iter__(self):
+        for item in self._gen:
+            self._cache[item] = True
+            yield item
+
+    def set(self):
+        return self
+
 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