Patchwork [2,of,2,RFC] revset: added cached generated list on generatorset

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

Comments

Lucas Moscovicz - Feb. 13, 2014, 10:56 p.m.
# HG changeset patch
# User Lucas Moscovicz <lmoscovicz@fb.com>
# Date 1392259549 28800
#      Wed Feb 12 18:45:49 2014 -0800
# Node ID 81523b4b0be9145300bb65f1a3d618904365decc
# Parent  f4d973e19d267efa440d992583a860fc2f8eed98
revset: added cached generated list on generatorset

This allows to iterate the generatorset more than once.

Patch

diff --git a/mercurial/revset.py b/mercurial/revset.py
--- a/mercurial/revset.py
+++ b/mercurial/revset.py
@@ -2150,15 +2150,23 @@ 
         self._gen = gen
         self._iter = iter(gen)
         self._cache = {}
+        self._genlist = baseset([])
+        self._iterated = False
+
+    def _nextitem(self):
+        l = self._iter.next()
+        self._cache[l] = True
+        self._genlist.append(l)
+        return l
 
     def __contains__(self, x):
         if x in self._cache:
             return self._cache[x]
 
+        self._iterated = True
         while True:
             try:
-                l = self._iter.next()
-                self._cache[l] = True
+                l = self._nextitem()
                 if l == x:
                     return True
             except (StopIteration):
@@ -2168,9 +2176,21 @@ 
         return False
 
     def __iter__(self):
-        for item in self._gen:
-            self._cache[item] = True
-            yield item
+        if self._iterated:
+            for l in self._genlist:
+                yield l
+            while True:
+                try:
+                    item = self._nextitem()
+                    yield item
+                except (StopIteration):
+                    break
+        else:
+            self._iterated = True
+            for item in self._gen:
+                self._cache[item] = True
+                self._genlist.append(item)
+                yield item
 
     def set(self):
         return self