Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c6171e4

Browse files
author
Tim Peters
committed
Merge 3.3 into default.
cwr_next(): move invariants out of loops. This simplifies and clarifies the code, and gives a small speedup.
2 parents 207fe01 + 9edb168 commit c6171e4

1 file changed

Lines changed: 12 additions & 16 deletions

File tree

Modules/itertoolsmodule.c

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2713,20 +2713,20 @@ cwr_next(cwrobject *co)
27132713
PyObject *result = co->result;
27142714
Py_ssize_t n = PyTuple_GET_SIZE(pool);
27152715
Py_ssize_t r = co->r;
2716-
Py_ssize_t i, j, index;
2716+
Py_ssize_t i, index;
27172717

27182718
if (co->stopped)
27192719
return NULL;
27202720

27212721
if (result == NULL) {
2722-
/* On the first pass, initialize result tuple using the indices */
2722+
/* On the first pass, initialize result tuple with pool[0] */
27232723
result = PyTuple_New(r);
27242724
if (result == NULL)
27252725
goto empty;
27262726
co->result = result;
2727+
elem = PyTuple_GET_ITEM(pool, 0);
27272728
for (i=0; i<r ; i++) {
2728-
index = indices[i];
2729-
elem = PyTuple_GET_ITEM(pool, index);
2729+
assert(indices[i] == 0);
27302730
Py_INCREF(elem);
27312731
PyTuple_SET_ITEM(result, i, elem);
27322732
}
@@ -2749,27 +2749,23 @@ cwr_next(cwrobject *co)
27492749
empty tuple is a singleton and cached in PyTuple's freelist. */
27502750
assert(r == 0 || Py_REFCNT(result) == 1);
27512751

2752-
/* Scan indices right-to-left until finding one that is not
2753-
* at its maximum (n-1). */
2752+
/* Scan indices right-to-left until finding one that is not
2753+
* at its maximum (n-1). */
27542754
for (i=r-1 ; i >= 0 && indices[i] == n-1; i--)
27552755
;
27562756

27572757
/* If i is negative, then the indices are all at
2758-
their maximum value and we're done. */
2758+
their maximum value and we're done. */
27592759
if (i < 0)
27602760
goto empty;
27612761

27622762
/* Increment the current index which we know is not at its
2763-
maximum. Then set all to the right to the same value. */
2764-
indices[i]++;
2765-
for (j=i+1 ; j<r ; j++)
2766-
indices[j] = indices[j-1];
2767-
2768-
/* Update the result tuple for the new indices
2769-
starting with i, the leftmost index that changed */
2763+
maximum. Then set all to the right to the same value. */
2764+
index = indices[i] + 1;
2765+
assert(index < n);
2766+
elem = PyTuple_GET_ITEM(pool, index);
27702767
for ( ; i<r ; i++) {
2771-
index = indices[i];
2772-
elem = PyTuple_GET_ITEM(pool, index);
2768+
indices[i] = index;
27732769
Py_INCREF(elem);
27742770
oldelem = PyTuple_GET_ITEM(result, i);
27752771
PyTuple_SET_ITEM(result, i, elem);

0 commit comments

Comments
 (0)