@@ -267,8 +267,6 @@ static spl_ptr_heap *spl_ptr_heap_init(spl_ptr_heap_cmp_func cmp, spl_ptr_heap_c
267
267
/* }}} */
268
268
269
269
static void spl_ptr_heap_insert (spl_ptr_heap * heap , void * elem , void * cmp_userdata ) { /* {{{ */
270
- size_t i ;
271
-
272
270
if (heap -> count + 1 > heap -> max_size ) {
273
271
size_t alloc_size = heap -> max_size * heap -> elem_size ;
274
272
/* we need to allocate more memory */
@@ -280,8 +278,9 @@ static void spl_ptr_heap_insert(spl_ptr_heap *heap, void *elem, void *cmp_userda
280
278
heap -> flags |= SPL_HEAP_WRITE_LOCKED ;
281
279
282
280
/* sifting up */
283
- for (i = heap -> count ; i > 0 && heap -> cmp (spl_heap_elem (heap , (i - 1 )/2 ), elem , cmp_userdata ) < 0 ; i = (i - 1 )/2 ) {
284
- spl_heap_elem_copy (heap , spl_heap_elem (heap , i ), spl_heap_elem (heap , (i - 1 )/2 ));
281
+ size_t pos ;
282
+ for (pos = heap -> count ; pos > 0 && heap -> cmp (spl_heap_elem (heap , (pos - 1 )/2 ), elem , cmp_userdata ) < 0 ; pos = (pos - 1 )/2 ) {
283
+ spl_heap_elem_copy (heap , spl_heap_elem (heap , pos ), spl_heap_elem (heap , (pos - 1 )/2 ));
285
284
}
286
285
heap -> count ++ ;
287
286
@@ -292,7 +291,7 @@ static void spl_ptr_heap_insert(spl_ptr_heap *heap, void *elem, void *cmp_userda
292
291
heap -> flags |= SPL_HEAP_CORRUPTED ;
293
292
}
294
293
295
- spl_heap_elem_copy (heap , spl_heap_elem (heap , i ), elem );
294
+ spl_heap_elem_copy (heap , spl_heap_elem (heap , pos ), elem );
296
295
}
297
296
/* }}} */
298
297
@@ -306,7 +305,6 @@ static void *spl_ptr_heap_top(spl_ptr_heap *heap) { /* {{{ */
306
305
/* }}} */
307
306
308
307
static zend_result spl_ptr_heap_delete_top (spl_ptr_heap * heap , void * elem , void * cmp_userdata ) { /* {{{ */
309
- size_t i , j ;
310
308
const size_t limit = (heap -> count - 1 )/2 ;
311
309
void * bottom ;
312
310
@@ -324,16 +322,17 @@ static zend_result spl_ptr_heap_delete_top(spl_ptr_heap *heap, void *elem, void
324
322
325
323
bottom = spl_heap_elem (heap , -- heap -> count );
326
324
327
- for (i = 0 ; i < limit ; i = j ) {
325
+ size_t parent_idx , child_idx ;
326
+ for (parent_idx = 0 ; parent_idx < limit ; parent_idx = child_idx ) {
328
327
/* Find smaller child */
329
- j = i * 2 + 1 ;
330
- if (j != heap -> count && heap -> cmp (spl_heap_elem (heap , j + 1 ), spl_heap_elem (heap , j ), cmp_userdata ) > 0 ) {
331
- j ++ ; /* next child is bigger */
328
+ child_idx = parent_idx * 2 + 1 ;
329
+ if (child_idx != heap -> count && heap -> cmp (spl_heap_elem (heap , child_idx + 1 ), spl_heap_elem (heap , child_idx ), cmp_userdata ) > 0 ) {
330
+ child_idx ++ ; /* next child is bigger */
332
331
}
333
332
334
333
/* swap elements between two levels */
335
- if (heap -> cmp (bottom , spl_heap_elem (heap , j ), cmp_userdata ) < 0 ) {
336
- spl_heap_elem_copy (heap , spl_heap_elem (heap , i ), spl_heap_elem (heap , j ));
334
+ if (heap -> cmp (bottom , spl_heap_elem (heap , child_idx ), cmp_userdata ) < 0 ) {
335
+ spl_heap_elem_copy (heap , spl_heap_elem (heap , parent_idx ), spl_heap_elem (heap , child_idx ));
337
336
} else {
338
337
break ;
339
338
}
@@ -346,7 +345,7 @@ static zend_result spl_ptr_heap_delete_top(spl_ptr_heap *heap, void *elem, void
346
345
heap -> flags |= SPL_HEAP_CORRUPTED ;
347
346
}
348
347
349
- void * to = spl_heap_elem (heap , i );
348
+ void * to = spl_heap_elem (heap , parent_idx );
350
349
if (to != bottom ) {
351
350
spl_heap_elem_copy (heap , to , bottom );
352
351
}
0 commit comments