/*
* Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
* Copyright (c) 1997,1999 by Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*%
* Heap implementation of priority queues adapted from the following:
*
* _Introduction to Algorithms_, Cormen, Leiserson, and Rivest,
* MIT Press / McGraw Hill, 1990, ISBN 0-262-03141-8, chapter 7.
*
* _Algorithms_, Second Edition, Sedgewick, Addison-Wesley, 1988,
* ISBN 0-201-06673-4, chapter 11.
*/
/*%
* Note: to make heap_parent and heap_left easy to compute, the first
* element of the heap array is not used; i.e. heap subscripts are 1-based,
* not 0-based.
*/
#define heap_parent(i) ((i) >> 1)
#define heap_left(i) ((i) << 1)
#define ARRAY_SIZE_INCREMENT 512
heap_context
heap_new(heap_higher_priority_func higher_priority, heap_index_func index,
int array_size_increment) {
heap_context ctx;
i = ++ctx->heap_size;
if (ctx->heap_size >= ctx->array_size && heap_resize(ctx) < 0)
return (-1);
float_up(ctx, i, elt);
return (0);
}
int
heap_delete(heap_context ctx, int i) {
void *elt;
int less;
if (ctx == NULL || i < 1 || i > ctx->heap_size) {
errno = EINVAL;
return (-1);
}
if (i == ctx->heap_size) {
ctx->heap_size--;
} else {
elt = ctx->heap[ctx->heap_size--];
less = ctx->higher_priority(elt, ctx->heap[i]);
ctx->heap[i] = elt;
if (less)
float_up(ctx, i, ctx->heap[i]);
else
sink_down(ctx, i, ctx->heap[i]);
}
return (0);
}
int
heap_increased(heap_context ctx, int i) {
if (ctx == NULL || i < 1 || i > ctx->heap_size) {
errno = EINVAL;
return (-1);
}
float_up(ctx, i, ctx->heap[i]);
return (0);
}
int
heap_decreased(heap_context ctx, int i) {
if (ctx == NULL || i < 1 || i > ctx->heap_size) {
errno = EINVAL;
return (-1);
}
sink_down(ctx, i, ctx->heap[i]);
return (0);
}
void *
heap_element(heap_context ctx, int i) {
if (ctx == NULL || i < 1 || i > ctx->heap_size) {
errno = EINVAL;
return (NULL);
}
return (ctx->heap[i]);
}
int
heap_for_each(heap_context ctx, heap_for_each_func action, void *uap) {
int i;