Commit e8a28510 authored by Tim Peters's avatar Tim Peters

cwr_next(): move invariants out of loops.

This simplifies and clarifies the code, and gives a small speedup.
parent 87f9da9e
...@@ -2713,20 +2713,20 @@ cwr_next(cwrobject *co) ...@@ -2713,20 +2713,20 @@ cwr_next(cwrobject *co)
PyObject *result = co->result; PyObject *result = co->result;
Py_ssize_t n = PyTuple_GET_SIZE(pool); Py_ssize_t n = PyTuple_GET_SIZE(pool);
Py_ssize_t r = co->r; Py_ssize_t r = co->r;
Py_ssize_t i, j, index; Py_ssize_t i, index;
if (co->stopped) if (co->stopped)
return NULL; return NULL;
if (result == 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); result = PyTuple_New(r);
if (result == NULL) if (result == NULL)
goto empty; goto empty;
co->result = result; co->result = result;
elem = PyTuple_GET_ITEM(pool, 0);
for (i=0; i<r ; i++) { for (i=0; i<r ; i++) {
index = indices[i]; assert(indices[i] == 0);
elem = PyTuple_GET_ITEM(pool, index);
Py_INCREF(elem); Py_INCREF(elem);
PyTuple_SET_ITEM(result, i, elem); PyTuple_SET_ITEM(result, i, elem);
} }
...@@ -2761,15 +2761,11 @@ cwr_next(cwrobject *co) ...@@ -2761,15 +2761,11 @@ cwr_next(cwrobject *co)
/* Increment the current index which we know is not at its /* Increment the current index which we know is not at its
maximum. Then set all to the right to the same value. */ maximum. Then set all to the right to the same value. */
indices[i]++; index = indices[i] + 1;
for (j=i+1 ; j<r ; j++) assert(index < n);
indices[j] = indices[j-1];
/* Update the result tuple for the new indices
starting with i, the leftmost index that changed */
for ( ; i<r ; i++) {
index = indices[i];
elem = PyTuple_GET_ITEM(pool, index); elem = PyTuple_GET_ITEM(pool, index);
for ( ; i<r ; i++) {
indices[i] = index;
Py_INCREF(elem); Py_INCREF(elem);
oldelem = PyTuple_GET_ITEM(result, i); oldelem = PyTuple_GET_ITEM(result, i);
PyTuple_SET_ITEM(result, i, elem); PyTuple_SET_ITEM(result, i, elem);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment