@@ -50,7 +50,7 @@ typedef struct _spl_ptr_heap {
50
50
spl_ptr_heap_ctor_func ctor ;
51
51
spl_ptr_heap_dtor_func dtor ;
52
52
spl_ptr_heap_cmp_func cmp ;
53
- int count ;
53
+ size_t count ;
54
54
int flags ;
55
55
size_t max_size ;
56
56
size_t elem_size ;
@@ -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
- int 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,8 +305,7 @@ 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
- int i , j ;
310
- const int limit = (heap -> count - 1 )/2 ;
308
+ const size_t limit = (heap -> count - 1 )/2 ;
311
309
void * bottom ;
312
310
313
311
if (heap -> count == 0 ) {
@@ -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
}
@@ -355,8 +354,6 @@ static zend_result spl_ptr_heap_delete_top(spl_ptr_heap *heap, void *elem, void
355
354
/* }}} */
356
355
357
356
static spl_ptr_heap * spl_ptr_heap_clone (spl_ptr_heap * from ) { /* {{{ */
358
- int i ;
359
-
360
357
spl_ptr_heap * heap = emalloc (sizeof (spl_ptr_heap ));
361
358
362
359
heap -> dtor = from -> dtor ;
@@ -370,7 +367,7 @@ static spl_ptr_heap *spl_ptr_heap_clone(spl_ptr_heap *from) { /* {{{ */
370
367
heap -> elements = safe_emalloc (from -> elem_size , from -> max_size , 0 );
371
368
memcpy (heap -> elements , from -> elements , from -> elem_size * from -> max_size );
372
369
373
- for (i = 0 ; i < heap -> count ; ++ i ) {
370
+ for (size_t i = 0 ; i < heap -> count ; ++ i ) {
374
371
heap -> ctor (spl_heap_elem (heap , i ));
375
372
}
376
373
@@ -384,11 +381,9 @@ static void spl_ptr_heap_destroy(spl_ptr_heap *heap) { /* {{{ */
384
381
return ;
385
382
}
386
383
387
- int i ;
388
-
389
384
heap -> flags |= SPL_HEAP_WRITE_LOCKED ;
390
385
391
- for (i = 0 ; i < heap -> count ; ++ i ) {
386
+ for (size_t i = 0 ; i < heap -> count ; ++ i ) {
392
387
heap -> dtor (spl_heap_elem (heap , i ));
393
388
}
394
389
@@ -399,7 +394,7 @@ static void spl_ptr_heap_destroy(spl_ptr_heap *heap) { /* {{{ */
399
394
}
400
395
/* }}} */
401
396
402
- static int spl_ptr_heap_count (spl_ptr_heap * heap ) { /* {{{ */
397
+ static size_t spl_ptr_heap_count (spl_ptr_heap * heap ) { /* {{{ */
403
398
return heap -> count ;
404
399
}
405
400
/* }}} */
@@ -534,7 +529,7 @@ static HashTable* spl_heap_object_get_debug_info(const zend_class_entry *ce, zen
534
529
535
530
array_init (& heap_array );
536
531
537
- for (zend_ulong i = 0 ; i < intern -> heap -> count ; ++ i ) {
532
+ for (size_t i = 0 ; i < intern -> heap -> count ; ++ i ) {
538
533
if (ce == spl_ce_SplPriorityQueue ) {
539
534
spl_pqueue_elem * pq_elem = spl_heap_elem (intern -> heap , i );
540
535
zval elem ;
0 commit comments