@@ -62,7 +62,7 @@ _siftdown(PyListObject *heap, Py_ssize_t startpos, Py_ssize_t pos)
62
62
static int
63
63
_siftup (PyListObject * heap , Py_ssize_t pos )
64
64
{
65
- Py_ssize_t startpos , endpos , childpos , rightpos ;
65
+ Py_ssize_t startpos , endpos , childpos , rightpos , limit ;
66
66
int cmp ;
67
67
PyObject * newitem , * tmp , * olditem ;
68
68
Py_ssize_t size ;
@@ -79,9 +79,10 @@ _siftup(PyListObject *heap, Py_ssize_t pos)
79
79
Py_INCREF (newitem );
80
80
81
81
/* Bubble up the smaller child until hitting a leaf. */
82
- childpos = 2 * pos + 1 ; /* leftmost child position */
83
- while (childpos < endpos ) {
82
+ limit = endpos / 2 ; /* smallest pos that has no child */
83
+ while (pos < limit ) {
84
84
/* Set childpos to index of smaller child. */
85
+ childpos = 2 * pos + 1 ; /* leftmost child position */
85
86
rightpos = childpos + 1 ;
86
87
if (rightpos < endpos ) {
87
88
cmp = PyObject_RichCompareBool (
@@ -108,7 +109,6 @@ _siftup(PyListObject *heap, Py_ssize_t pos)
108
109
PyList_SET_ITEM (heap , pos , tmp );
109
110
Py_DECREF (olditem );
110
111
pos = childpos ;
111
- childpos = 2 * pos + 1 ;
112
112
if (size != PyList_GET_SIZE (heap )) {
113
113
PyErr_SetString (PyExc_RuntimeError ,
114
114
"list changed size during iteration" );
@@ -419,7 +419,7 @@ _siftdownmax(PyListObject *heap, Py_ssize_t startpos, Py_ssize_t pos)
419
419
static int
420
420
_siftupmax (PyListObject * heap , Py_ssize_t pos )
421
421
{
422
- Py_ssize_t startpos , endpos , childpos , rightpos ;
422
+ Py_ssize_t startpos , endpos , childpos , rightpos , limit ;
423
423
int cmp ;
424
424
PyObject * newitem , * tmp ;
425
425
@@ -434,9 +434,10 @@ _siftupmax(PyListObject *heap, Py_ssize_t pos)
434
434
Py_INCREF (newitem );
435
435
436
436
/* Bubble up the smaller child until hitting a leaf. */
437
- childpos = 2 * pos + 1 ; /* leftmost child position */
438
- while (childpos < endpos ) {
437
+ limit = endpos / 2 ; /* smallest pos that has no child */
438
+ while (pos < limit ) {
439
439
/* Set childpos to index of smaller child. */
440
+ childpos = 2 * pos + 1 ; /* leftmost child position */
440
441
rightpos = childpos + 1 ;
441
442
if (rightpos < endpos ) {
442
443
cmp = PyObject_RichCompareBool (
@@ -456,7 +457,6 @@ _siftupmax(PyListObject *heap, Py_ssize_t pos)
456
457
Py_DECREF (PyList_GET_ITEM (heap , pos ));
457
458
PyList_SET_ITEM (heap , pos , tmp );
458
459
pos = childpos ;
459
- childpos = 2 * pos + 1 ;
460
460
}
461
461
462
462
/* The leaf at pos is empty now. Put newitem there, and bubble
0 commit comments