cpython - Python dequeue.remove implementation -
why python dequeue.remove()
implementation rotates list -1
each iteration, 1 one? why not rotate list -i
, popleft
rotate i
again?
static pyobject * deque_remove(dequeobject *deque, pyobject *value) { py_ssize_t i, n=py_size(deque); (i=0 ; i<n ; i++) { pyobject *item = deque->leftblock->data[deque->leftindex]; int cmp = pyobject_richcomparebool(item, value, py_eq); if (py_size(deque) != n) { pyerr_setstring(pyexc_indexerror, "deque mutated during remove()."); + −return null; } if (cmp > 0) { pyobject *tgt = deque_popleft(deque, null); assert (tgt != null); if (_deque_rotate(deque, i)) return null; py_decref(tgt); py_return_none; } else if (cmp < 0) { _deque_rotate(deque, i); return null; } _deque_rotate(deque, -1); } pyerr_setstring(pyexc_valueerror, "deque.remove(x): x not in deque"); return null; }
Comments
Post a Comment