void generic_heap_move_down(generic_heap_t *heap, int32_t x) { uint32_t i; assert(0 <= x && x < heap->idx_size && heap->idx[x] >= 0); i = heap->idx[x]; heap_update_down(heap, x, i); }
/* * Remove x from the heap * - no change if x is not there */ void int_heap_remove(int_heap_t *heap, int32_t x) { assert(x >= 0); if (x >= heap->idx_size || heap->idx[x] < 0) return; // not there heap_update_down(heap, heap->idx[x]); heap->idx[x] = -1; }
/* * Get the smallest element and remove it * - return -1 if the heap is empty */ int32_t int_heap_get_min(int_heap_t *heap) { uint32_t n; int32_t x; n = heap->nelems; if (n == 0) return -1; x = heap->heap[1]; heap_update_down(heap, 1); heap->idx[x] = -1; return x; }
/* * Get the smallest element and remove it * - return -1 if the heap is empty */ int32_t generic_heap_get_min(generic_heap_t *heap) { uint32_t n; int32_t x, y; n = heap->nelems; if (n == 0) return -1; x = heap->heap[1]; y = heap->heap[n]; heap->nelems = n - 1; if (x != y) { heap_update_down(heap, y, 1); } heap->idx[x] = -1; return x; }
/* * Remove x from the heap * - no change if x is not there */ void generic_heap_remove(generic_heap_t *heap, int32_t x) { uint32_t n; int32_t y; assert(x >= 0); if (x >= heap->idx_size || heap->idx[x] < 0) return; // not there // move last element into the subtree of root x n = heap->nelems; y = heap->heap[n]; heap->nelems = n - 1; if (x != y) { heap_update_down(heap, y, heap->idx[x]); } heap->idx[x] = -1; }
void generic_heap_update(generic_heap_t *heap, int32_t x) { uint32_t i, n; int32_t y; assert(0 <= x && x < heap->idx_size && heap->idx[x] >= 0); i = heap->idx[x]; n = heap->nelems; if (i == n) { heap_update_up(heap, x, i); } else { // remove x, replace it by y = last heap element y = heap->heap[n]; heap->nelems = n - 1; heap_update_down(heap, y, i); // put x back in heap->nelems = n; heap_update_up(heap, x, n); } }