Move the initial start-search out of the main loop so it can be factored-out later.
This commit is contained in:
@@ -1017,11 +1017,12 @@ deque_len(dequeobject *deque)
|
|||||||
static PyObject *
|
static PyObject *
|
||||||
deque_index(dequeobject *deque, PyObject *args)
|
deque_index(dequeobject *deque, PyObject *args)
|
||||||
{
|
{
|
||||||
Py_ssize_t i, start=0, stop=Py_SIZE(deque);
|
Py_ssize_t i, n, start=0, stop=Py_SIZE(deque);
|
||||||
PyObject *v, *item;
|
PyObject *v, *item;
|
||||||
block *b = deque->leftblock;
|
block *b = deque->leftblock;
|
||||||
Py_ssize_t index = deque->leftindex;
|
Py_ssize_t index = deque->leftindex;
|
||||||
size_t start_state = deque->state;
|
size_t start_state = deque->state;
|
||||||
|
int cmp;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
|
if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
|
||||||
_PyEval_SliceIndex, &start,
|
_PyEval_SliceIndex, &start,
|
||||||
@@ -1039,22 +1040,32 @@ deque_index(dequeobject *deque, PyObject *args)
|
|||||||
}
|
}
|
||||||
if (stop > Py_SIZE(deque))
|
if (stop > Py_SIZE(deque))
|
||||||
stop = Py_SIZE(deque);
|
stop = Py_SIZE(deque);
|
||||||
|
if (start > stop)
|
||||||
|
start = stop;
|
||||||
|
assert(0 <= start && start <= stop && stop <= Py_SIZE(deque));
|
||||||
|
|
||||||
for (i=0 ; i<stop ; i++) {
|
/* XXX Replace this loop with faster code from deque_item() */
|
||||||
if (i >= start) {
|
for (i=0 ; i<start ; i++) {
|
||||||
int cmp;
|
index++;
|
||||||
CHECK_NOT_END(b);
|
if (index == BLOCKLEN) {
|
||||||
item = b->data[index];
|
b = b->rightlink;
|
||||||
cmp = PyObject_RichCompareBool(item, v, Py_EQ);
|
index = 0;
|
||||||
if (cmp > 0)
|
}
|
||||||
return PyLong_FromSsize_t(i);
|
}
|
||||||
else if (cmp < 0)
|
|
||||||
return NULL;
|
n = stop - i;
|
||||||
if (start_state != deque->state) {
|
while (n--) {
|
||||||
PyErr_SetString(PyExc_RuntimeError,
|
CHECK_NOT_END(b);
|
||||||
"deque mutated during iteration");
|
item = b->data[index];
|
||||||
return NULL;
|
cmp = PyObject_RichCompareBool(item, v, Py_EQ);
|
||||||
}
|
if (cmp > 0)
|
||||||
|
return PyLong_FromSsize_t(stop - (n + 1));
|
||||||
|
else if (cmp < 0)
|
||||||
|
return NULL;
|
||||||
|
if (start_state != deque->state) {
|
||||||
|
PyErr_SetString(PyExc_RuntimeError,
|
||||||
|
"deque mutated during iteration");
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
index++;
|
index++;
|
||||||
if (index == BLOCKLEN) {
|
if (index == BLOCKLEN) {
|
||||||
|
|||||||
Reference in New Issue
Block a user