Skip to content

Commit 758bf5f

Browse files
Use size_t to store heaps count
1 parent 30e2798 commit 758bf5f

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

ext/spl/spl_heap.c

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,6 @@ static spl_ptr_heap *spl_ptr_heap_init(spl_ptr_heap_cmp_func cmp, spl_ptr_heap_c
267267
/* }}} */
268268

269269
static void spl_ptr_heap_insert(spl_ptr_heap *heap, void *elem, void *cmp_userdata) { /* {{{ */
270-
size_t i;
271-
272270
if (heap->count+1 > heap->max_size) {
273271
size_t alloc_size = heap->max_size * heap->elem_size;
274272
/* 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
280278
heap->flags |= SPL_HEAP_WRITE_LOCKED;
281279

282280
/* 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));
285284
}
286285
heap->count++;
287286

@@ -292,7 +291,7 @@ static void spl_ptr_heap_insert(spl_ptr_heap *heap, void *elem, void *cmp_userda
292291
heap->flags |= SPL_HEAP_CORRUPTED;
293292
}
294293

295-
spl_heap_elem_copy(heap, spl_heap_elem(heap, i), elem);
294+
spl_heap_elem_copy(heap, spl_heap_elem(heap, pos), elem);
296295
}
297296
/* }}} */
298297

@@ -306,7 +305,6 @@ static void *spl_ptr_heap_top(spl_ptr_heap *heap) { /* {{{ */
306305
/* }}} */
307306

308307
static zend_result spl_ptr_heap_delete_top(spl_ptr_heap *heap, void *elem, void *cmp_userdata) { /* {{{ */
309-
size_t i, j;
310308
const size_t limit = (heap->count-1)/2;
311309
void *bottom;
312310

@@ -324,16 +322,17 @@ static zend_result spl_ptr_heap_delete_top(spl_ptr_heap *heap, void *elem, void
324322

325323
bottom = spl_heap_elem(heap, --heap->count);
326324

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) {
328327
/* 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 */
332331
}
333332

334333
/* 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));
337336
} else {
338337
break;
339338
}
@@ -346,7 +345,7 @@ static zend_result spl_ptr_heap_delete_top(spl_ptr_heap *heap, void *elem, void
346345
heap->flags |= SPL_HEAP_CORRUPTED;
347346
}
348347

349-
void *to = spl_heap_elem(heap, i);
348+
void *to = spl_heap_elem(heap, parent_idx);
350349
if (to != bottom) {
351350
spl_heap_elem_copy(heap, to, bottom);
352351
}

0 commit comments

Comments
 (0)