static void remove_item(Prioq *q, struct prioq_item *i) { struct prioq_item *l; assert(q); assert(i); l = q->items + q->n_items - 1; if (i == l) /* Last entry, let's just remove it */ q->n_items--; else { unsigned k; /* Not last entry, let's replace the last entry with * this one, and reshuffle */ k = i - q->items; i->data = l->data; i->idx = l->idx; if (i->idx) *i->idx = k; q->n_items--; k = shuffle_down(q, k); shuffle_up(q, k); } }
void* pa_prioq_remove(pa_prioq *q, pa_prioq_item *i) { void *p; pa_assert(q); pa_assert(i); pa_assert(q->n_items >= 1); p = i->value; if (q->n_items-1 == i->idx) { /* We are the last entry, so let's just remove us and good */ q->n_items--; } else { /* We are not the last entry, we need to replace ourselves * with the last node and reshuffle */ q->items[i->idx] = q->items[q->n_items-1]; q->items[i->idx]->idx = i->idx; q->n_items--; shuffle_down(q, i->idx); } if (pa_flist_push(PA_STATIC_FLIST_GET(items), i) < 0) pa_xfree(i); return p; }
void pa_prioq_reshuffle(pa_prioq *q, pa_prioq_item *i) { pa_assert(q); pa_assert(i); /* This will move the entry down as far as necessary */ shuffle_down(q, i->idx); /* And this will move the entry up as far as necessary */ shuffle_up(q, i); }
int prioq_reshuffle(Prioq *q, void *data, unsigned *idx) { struct prioq_item *i; unsigned k; assert(q); i = find_item(q, data, idx); if (!i) return 0; k = i - q->items; k = shuffle_down(q, k); shuffle_up(q, k); return 1; }