summaryrefslogtreecommitdiff
path: root/Modules/_heapqmodule.c
diff options
context:
space:
mode:
Diffstat (limited to 'Modules/_heapqmodule.c')
-rw-r--r--Modules/_heapqmodule.c10
1 files changed, 5 insertions, 5 deletions
diff --git a/Modules/_heapqmodule.c b/Modules/_heapqmodule.c
index 136abf5985..b499e1f668 100644
--- a/Modules/_heapqmodule.c
+++ b/Modules/_heapqmodule.c
@@ -66,7 +66,7 @@ siftup(PyListObject *heap, Py_ssize_t pos)
/* Bubble up the smaller child until hitting a leaf. */
arr = _PyList_ITEMS(heap);
- limit = endpos / 2; /* smallest pos that has no child */
+ limit = endpos >> 1; /* smallest pos that has no child */
while (pos < limit) {
/* Set childpos to index of smaller child. */
childpos = 2*pos + 1; /* leftmost child position */
@@ -78,6 +78,7 @@ siftup(PyListObject *heap, Py_ssize_t pos)
if (cmp < 0)
return -1;
childpos += ((unsigned)cmp ^ 1); /* increment when cmp==0 */
+ arr = _PyList_ITEMS(heap); /* arr may have changed */
if (endpos != PyList_GET_SIZE(heap)) {
PyErr_SetString(PyExc_RuntimeError,
"list changed size during iteration");
@@ -85,7 +86,6 @@ siftup(PyListObject *heap, Py_ssize_t pos)
}
}
/* Move the smaller child up. */
- arr = _PyList_ITEMS(heap);
tmp1 = arr[childpos];
tmp2 = arr[pos];
arr[childpos] = tmp2;
@@ -347,7 +347,7 @@ heapify_internal(PyObject *heap, int siftup_func(PyListObject *, Py_ssize_t))
n is odd = 2*j+1, this is (2*j+1-1)/2 = j so j-1 is the largest,
and that's again n//2-1.
*/
- for (i = n/2 - 1 ; i >= 0 ; i--)
+ for (i = (n >> 1) - 1 ; i >= 0 ; i--)
if (siftup_func((PyListObject *)heap, i))
return NULL;
Py_RETURN_NONE;
@@ -420,7 +420,7 @@ siftup_max(PyListObject *heap, Py_ssize_t pos)
/* Bubble up the smaller child until hitting a leaf. */
arr = _PyList_ITEMS(heap);
- limit = endpos / 2; /* smallest pos that has no child */
+ limit = endpos >> 1; /* smallest pos that has no child */
while (pos < limit) {
/* Set childpos to index of smaller child. */
childpos = 2*pos + 1; /* leftmost child position */
@@ -432,6 +432,7 @@ siftup_max(PyListObject *heap, Py_ssize_t pos)
if (cmp < 0)
return -1;
childpos += ((unsigned)cmp ^ 1); /* increment when cmp==0 */
+ arr = _PyList_ITEMS(heap); /* arr may have changed */
if (endpos != PyList_GET_SIZE(heap)) {
PyErr_SetString(PyExc_RuntimeError,
"list changed size during iteration");
@@ -439,7 +440,6 @@ siftup_max(PyListObject *heap, Py_ssize_t pos)
}
}
/* Move the smaller child up. */
- arr = _PyList_ITEMS(heap);
tmp1 = arr[childpos];
tmp2 = arr[pos];
arr[childpos] = tmp2;