Kaydet (Commit) 90e93383 authored tarafından Raymond Hettinger's avatar Raymond Hettinger

Neaten-up a bit add add missing size change check.

üst 871620d9
...@@ -12,9 +12,8 @@ static int ...@@ -12,9 +12,8 @@ static int
_siftdown(PyListObject *heap, Py_ssize_t startpos, Py_ssize_t pos) _siftdown(PyListObject *heap, Py_ssize_t startpos, Py_ssize_t pos)
{ {
PyObject *newitem, *parent; PyObject *newitem, *parent;
Py_ssize_t parentpos, size;
int cmp; int cmp;
Py_ssize_t parentpos;
Py_ssize_t size;
assert(PyList_Check(heap)); assert(PyList_Check(heap));
size = PyList_GET_SIZE(heap); size = PyList_GET_SIZE(heap);
...@@ -26,7 +25,7 @@ _siftdown(PyListObject *heap, Py_ssize_t startpos, Py_ssize_t pos) ...@@ -26,7 +25,7 @@ _siftdown(PyListObject *heap, Py_ssize_t startpos, Py_ssize_t pos)
/* Follow the path to the root, moving parents down until finding /* Follow the path to the root, moving parents down until finding
a place newitem fits. */ a place newitem fits. */
newitem = PyList_GET_ITEM(heap, pos); newitem = PyList_GET_ITEM(heap, pos);
while (pos > startpos){ while (pos > startpos) {
parentpos = (pos - 1) >> 1; parentpos = (pos - 1) >> 1;
parent = PyList_GET_ITEM(heap, parentpos); parent = PyList_GET_ITEM(heap, parentpos);
cmp = PyObject_RichCompareBool(newitem, parent, Py_LT); cmp = PyObject_RichCompareBool(newitem, parent, Py_LT);
...@@ -355,11 +354,12 @@ static int ...@@ -355,11 +354,12 @@ static int
_siftdownmax(PyListObject *heap, Py_ssize_t startpos, Py_ssize_t pos) _siftdownmax(PyListObject *heap, Py_ssize_t startpos, Py_ssize_t pos)
{ {
PyObject *newitem, *parent; PyObject *newitem, *parent;
Py_ssize_t parentpos, size;
int cmp; int cmp;
Py_ssize_t parentpos;
assert(PyList_Check(heap)); assert(PyList_Check(heap));
if (pos >= PyList_GET_SIZE(heap)) { size = PyList_GET_SIZE(heap);
if (pos >= size) {
PyErr_SetString(PyExc_IndexError, "index out of range"); PyErr_SetString(PyExc_IndexError, "index out of range");
return -1; return -1;
} }
...@@ -367,12 +367,17 @@ _siftdownmax(PyListObject *heap, Py_ssize_t startpos, Py_ssize_t pos) ...@@ -367,12 +367,17 @@ _siftdownmax(PyListObject *heap, Py_ssize_t startpos, Py_ssize_t pos)
/* Follow the path to the root, moving parents down until finding /* Follow the path to the root, moving parents down until finding
a place newitem fits. */ a place newitem fits. */
newitem = PyList_GET_ITEM(heap, pos); newitem = PyList_GET_ITEM(heap, pos);
while (pos > startpos){ while (pos > startpos) {
parentpos = (pos - 1) >> 1; parentpos = (pos - 1) >> 1;
parent = PyList_GET_ITEM(heap, parentpos); parent = PyList_GET_ITEM(heap, parentpos);
cmp = PyObject_RichCompareBool(parent, newitem, Py_LT); cmp = PyObject_RichCompareBool(parent, newitem, Py_LT);
if (cmp == -1) if (cmp == -1)
return -1; return -1;
if (size != PyList_GET_SIZE(heap)) {
PyErr_SetString(PyExc_RuntimeError,
"list changed size during iteration");
return -1;
}
if (cmp == 0) if (cmp == 0)
break; break;
parent = PyList_GET_ITEM(heap, parentpos); parent = PyList_GET_ITEM(heap, parentpos);
......
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