summaryrefslogtreecommitdiffstats
path: root/Modules/itertoolsmodule.c
diff options
context:
space:
mode:
authorTim Peters <tim@python.org>2013-09-03 16:49:31 (GMT)
committerTim Peters <tim@python.org>2013-09-03 16:49:31 (GMT)
commit9edb168dd7f8fbe2da9874de720c2d00e5a3bf7e (patch)
tree8785f563e36d4e232ba14171578604c81995caaa /Modules/itertoolsmodule.c
parentc554f725a09fc14a08ba2613682a961bd2216d45 (diff)
downloadcpython-9edb168dd7f8fbe2da9874de720c2d00e5a3bf7e.zip
cpython-9edb168dd7f8fbe2da9874de720c2d00e5a3bf7e.tar.gz
cpython-9edb168dd7f8fbe2da9874de720c2d00e5a3bf7e.tar.bz2
cwr_next(): move invariants out of loops.
This simplifies and clarifies the code, and gives a small speedup.
Diffstat (limited to 'Modules/itertoolsmodule.c')
-rw-r--r--Modules/itertoolsmodule.c28
1 files changed, 12 insertions, 16 deletions
diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c
index c751184..4bc9192 100644
--- a/Modules/itertoolsmodule.c
+++ b/Modules/itertoolsmodule.c
@@ -2713,20 +2713,20 @@ cwr_next(cwrobject *co)
PyObject *result = co->result;
Py_ssize_t n = PyTuple_GET_SIZE(pool);
Py_ssize_t r = co->r;
- Py_ssize_t i, j, index;
+ Py_ssize_t i, index;
if (co->stopped)
return NULL;
if (result == NULL) {
- /* On the first pass, initialize result tuple using the indices */
+ /* On the first pass, initialize result tuple with pool[0] */
result = PyTuple_New(r);
if (result == NULL)
goto empty;
co->result = result;
+ elem = PyTuple_GET_ITEM(pool, 0);
for (i=0; i<r ; i++) {
- index = indices[i];
- elem = PyTuple_GET_ITEM(pool, index);
+ assert(indices[i] == 0);
Py_INCREF(elem);
PyTuple_SET_ITEM(result, i, elem);
}
@@ -2749,27 +2749,23 @@ cwr_next(cwrobject *co)
empty tuple is a singleton and cached in PyTuple's freelist. */
assert(r == 0 || Py_REFCNT(result) == 1);
- /* Scan indices right-to-left until finding one that is not
- * at its maximum (n-1). */
+ /* Scan indices right-to-left until finding one that is not
+ * at its maximum (n-1). */
for (i=r-1 ; i >= 0 && indices[i] == n-1; i--)
;
/* If i is negative, then the indices are all at
- their maximum value and we're done. */
+ their maximum value and we're done. */
if (i < 0)
goto empty;
/* Increment the current index which we know is not at its
- maximum. Then set all to the right to the same value. */
- indices[i]++;
- for (j=i+1 ; j<r ; j++)
- indices[j] = indices[j-1];
-
- /* Update the result tuple for the new indices
- starting with i, the leftmost index that changed */
+ maximum. Then set all to the right to the same value. */
+ index = indices[i] + 1;
+ assert(index < n);
+ elem = PyTuple_GET_ITEM(pool, index);
for ( ; i<r ; i++) {
- index = indices[i];
- elem = PyTuple_GET_ITEM(pool, index);
+ indices[i] = index;
Py_INCREF(elem);
oldelem = PyTuple_GET_ITEM(result, i);
PyTuple_SET_ITEM(result, i, elem);