#if !defined(LINT) && !defined(CODECENTER)
static const char rcsid[] = "$Id: heap.c,v 1.2.18.2 2006/03/10 00:20:08 marka Exp $";
#endif
#include "port_before.h"
#include <stddef.h>
#include <stdlib.h>
#include <errno.h>
#include "port_after.h"
#include <isc/heap.h>
#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;
if (higher_priority == NULL)
return (NULL);
ctx = (heap_context)malloc(sizeof (struct heap_context));
if (ctx == NULL)
return (NULL);
ctx->array_size = 0;
if (array_size_increment == 0)
ctx->array_size_increment = ARRAY_SIZE_INCREMENT;
else
ctx->array_size_increment = array_size_increment;
ctx->heap_size = 0;
ctx->heap = NULL;
ctx->higher_priority = higher_priority;
ctx->index = index;
return (ctx);
}
int
heap_free(heap_context ctx) {
if (ctx == NULL) {
errno = EINVAL;
return (-1);
}
if (ctx->heap != NULL)
free(ctx->heap);
free(ctx);
return (0);
}
static int
heap_resize(heap_context ctx) {
void **new_heap;
ctx->array_size += ctx->array_size_increment;
new_heap = (void **)realloc(ctx->heap,
(ctx->array_size) * (sizeof (void *)));
if (new_heap == NULL) {
errno = ENOMEM;
return (-1);
}
ctx->heap = new_heap;
return (0);
}
static void
float_up(heap_context ctx, int i, void *elt) {
int p;
for ( p = heap_parent(i);
i > 1 && ctx->higher_priority(elt, ctx->heap[p]);
i = p, p = heap_parent(i) ) {
ctx->heap[i] = ctx->heap[p];
if (ctx->index != NULL)
(ctx->index)(ctx->heap[i], i);
}
ctx->heap[i] = elt;
if (ctx->index != NULL)
(ctx->index)(ctx->heap[i], i);
}
static void
sink_down(heap_context ctx, int i, void *elt) {
int j, size, half_size;
size = ctx->heap_size;
half_size = size / 2;
while (i <= half_size) {
j = heap_left(i);
if (j < size && ctx->higher_priority(ctx->heap[j+1],
ctx->heap[j]))
j++;
if (ctx->higher_priority(elt, ctx->heap[j]))
break;
ctx->heap[i] = ctx->heap[j];
if (ctx->index != NULL)
(ctx->index)(ctx->heap[i], i);
i = j;
}
ctx->heap[i] = elt;
if (ctx->index != NULL)
(ctx->index)(ctx->heap[i], i);
}
int
heap_insert(heap_context ctx, void *elt) {
int i;
if (ctx == NULL || elt == NULL) {
errno = EINVAL;
return (-1);
}
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;
if (ctx == NULL || action == NULL) {
errno = EINVAL;
return (-1);
}
for (i = 1; i <= ctx->heap_size; i++)
(action)(ctx->heap[i], uap);
return (0);
}