| 76 | } |
| 77 | |
| 78 | static void __pushdown |
| 79 | ( |
| 80 | heap_t *hp, |
| 81 | unsigned int idx |
| 82 | ) { |
| 83 | while (1) { |
| 84 | unsigned int childl, childr, child; |
| 85 | |
| 86 | // get left and right child indices |
| 87 | childl = __child_left(idx); |
| 88 | childr = __child_right(idx); |
| 89 | |
| 90 | // see if right child index is within bounds |
| 91 | if(childr >= hp->count) { |
| 92 | // see if left child index is within bounds |
| 93 | if(childl >= hp->count) { |
| 94 | // we've reached a leaf node |
| 95 | // can't push down any further |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | // only left child should be considered |
| 100 | child = childl; |
| 101 | } |
| 102 | // find smallest child |
| 103 | else if(hp->cmp(hp->array[childl], hp->array[childr], hp->udata) > 0) { |
| 104 | child = childl; |
| 105 | } else { |
| 106 | child = childr; |
| 107 | } |
| 108 | |
| 109 | // idx is smaller than child |
| 110 | if(hp->cmp(hp->array[idx], hp->array[child], hp->udata) < 0) { |
| 111 | __swap(hp, idx, child); |
| 112 | idx = child; |
| 113 | } else { |
| 114 | // parent is smaller than its children we can stop |
| 115 | return; |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // return item's index on the heap's array; otherwise -1 |
| 121 | static int __item_get_idx |
no test coverage detected