[pypy-svn] pypy default: Oups. Sorry dcolish, I also implemented W_Permutations two days ago

arigo commits-noreply at bitbucket.org
Tue Jan 25 17:54:54 CET 2011


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r41313:02c7d1d30c09
Date: 2011-01-25 17:54 +0100
http://bitbucket.org/pypy/pypy/changeset/02c7d1d30c09/

Log:	Oups. Sorry dcolish, I also implemented W_Permutations two days ago
	but was on a train ride and can only check it in now. Sorry, I
	think I will remove your version, because mine is a bit more
	efficient: there is less copying of lists around.

diff --git a/pypy/module/itertools/interp_itertools.py b/pypy/module/itertools/interp_itertools.py
--- a/pypy/module/itertools/interp_itertools.py
+++ b/pypy/module/itertools/interp_itertools.py
@@ -1142,68 +1142,6 @@
 """)
 
 
-class W_Permutations(Wrappable):
-    def __init__(self, space, iterable_w, w_r):
-        self.space = space
-        self.pool_w = iterable_w[:]
-        self.n = n = len(self.pool_w)
-        self.r = r = n if space.is_w(w_r, space.w_None) else space.int_w(w_r)
-        if r < 0:
-            raise OperationError(self.space.w_ValueError,
-                                 self.space.wrap("r cannot be negative"))
-        self.indices = range(n)
-        self.cycles = range(n, n - r, -1)
-        self.first_run = True
-
-    def iter_w(self):
-        return self.space.wrap(self)
-
-    def next_w(self):
-        if self.r > self.n:
-            raise OperationError(self.space.w_StopIteration, self.space.w_None)
-        if self.first_run:
-            self.first_run = False
-        else:
-            # cargo-culted from python docs
-            for i in xrange(self.r - 1, -1, -1):
-                self.cycles[i] -= 1
-                if self.cycles[i] == 0:
-                    new_indices = self.indices[i + 1:] + self.indices[i:i + 1]
-                    for x in xrange(len(new_indices)):
-                        self.indices[i + x] = new_indices[x]
-                    self.cycles[i] = self.n - i
-                else:
-                    j = self.cycles[i]
-                    self.indices[i], self.indices[-j] = (self.indices[-j],
-                                                         self.indices[i])
-                    break
-            else:
-                raise OperationError(self.space.w_StopIteration,
-                                     self.space.w_None)
-        res_w = [self.pool_w[self.indices[x]] for x in range(self.r)]
-        return self.space.newtuple(res_w)
-
-
-def W_Permutations__new__(space, w_subtype, w_iterable, w_r=None):
-    iterable_w = space.listview(w_iterable)
-    return space.wrap(W_Permutations(space, iterable_w, w_r))
-
-
-W_Permutations.typedef = TypeDef(
-    'permutations',
-    __new__ = interp2app(W_Permutations__new__,
-                         unwrap_spec=[ObjSpace, W_Root, W_Root, W_Root]),
-    __iter__ = interp2app(W_Permutations.iter_w, unwrap_spec=['self']),
-    next = interp2app(W_Permutations.next_w, unwrap_spec=['self']),
-    __doc__ = """\
-permutations(iterable[, r]) --> permutations object
-
-Return successive r-length permutations of elements in the iterable.
-
-permutations(range(3), 2) --> (0,1), (0,2), (1,0), (1,2), (2,0), (2,1)
-""")
-
-
 class W_Combinations(Wrappable):
     def __init__(self, space, pool_w, indices, r):
         self.pool_w = pool_w


More information about the Pypy-commit mailing list