void *pqueue_dequeue(struct pqueue *queue) { void *data = queue->array[0]; queue->array[0] = queue->array[--queue->size]; trickle_down(0, queue); return data; }
void pqueue_remove_at(int index, struct pqueue *queue) { queue->array[index] = queue->array[--queue->size]; if (index > 0 && (*queue->cmp)(queue->array[index], queue->array[PARENT_OF(index)]) < 0) { trickle_up(index, queue); } else { trickle_down(index, queue); } }
void* heap_pop(struct heap* h) { assert(h != NULL); ASSERT_HEAP(h); if (h->vals->num == 0) return NULL; void* top = array_get(h->vals, 0); void* last = array_get(h->vals, h->vals->num - 1); array_set(h->vals, 0, last); --h->vals->num; trickle_down(h, 0); /* TODO: array does not yet support shrinking */ return top; }