2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-24 11:08:45 +00:00
bind/lib/isc/heap.c

226 lines
5.3 KiB
C
Raw Normal View History

1998-10-15 23:42:56 +00:00
/*
* Copyright (c) 1997, 1998 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 INTERNET SOFTWARE CONSORTIUM DISCLAIMS
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
* CONSORTIUM 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.
*/
#include <stdlib.h>
#include <isc/assertions.h>
#include <isc/boolean.h>
#include <isc/heap.h>
/*
* 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.
*/
1998-10-15 23:51:14 +00:00
#define heap_parent(i) ((i) >> 1)
#define heap_left(i) ((i) << 1)
1998-10-15 23:42:56 +00:00
1998-10-15 23:51:14 +00:00
#define SIZE_INCREMENT 1024
1998-10-15 23:42:56 +00:00
#define HEAP_MAGIC 0x48454150U /* HEAP. */
#define VALID_CONTEXT(ctx) ((ctx) != NULL && \
(ctx)->magic == HEAP_MAGIC)
struct heap_context {
unsigned int magic;
mem_context_t mctx;
1998-10-15 23:51:14 +00:00
unsigned int size;
unsigned int size_increment;
unsigned int last;
void **array;
1998-10-15 23:42:56 +00:00
heap_higher_priority_func higher_priority;
heap_index_func index;
};
isc_result
heap_create(mem_context_t mctx, heap_higher_priority_func higher_priority,
1998-10-15 23:51:14 +00:00
heap_index_func index, unsigned int size_increment,
1998-10-15 23:42:56 +00:00
heap_context_t *ctxp)
{
heap_context_t ctx;
REQUIRE(ctxp != NULL && *ctxp == NULL);
REQUIRE(higher_priority != NULL);
ctx = mem_get(mctx, sizeof *ctx);
if (ctx == NULL)
return (ISC_R_NOMEMORY);
ctx->magic = HEAP_MAGIC;
1998-10-15 23:51:14 +00:00
ctx->size = 0;
if (size_increment == 0)
ctx->size_increment = SIZE_INCREMENT;
1998-10-15 23:42:56 +00:00
else
1998-10-15 23:51:14 +00:00
ctx->size_increment = size_increment;
ctx->last = 0;
ctx->array = NULL;
1998-10-15 23:42:56 +00:00
ctx->higher_priority = higher_priority;
ctx->index = index;
*ctxp = ctx;
return (ISC_R_SUCCESS);
}
void
heap_destroy(heap_context_t *ctxp) {
heap_context_t ctx;
REQUIRE(ctxp != NULL);
ctx = *ctxp;
REQUIRE(VALID_CONTEXT(ctx));
1998-10-15 23:51:14 +00:00
if (ctx->array != NULL)
mem_put(ctx->mctx, ctx->array,
ctx->size * sizeof (void *));
1998-10-15 23:42:56 +00:00
ctx->magic = 0;
mem_put(ctx->mctx, ctx, sizeof *ctx);
*ctxp = NULL;
}
static boolean_t
1998-10-15 23:51:14 +00:00
resize(heap_context_t ctx) {
void **new_array;
1998-10-15 23:42:56 +00:00
size_t new_size;
REQUIRE(VALID_CONTEXT(ctx));
1998-10-15 23:51:14 +00:00
new_size = ctx->size + ctx->size_increment;
new_array = mem_get(ctx->mctx, new_size * sizeof (void *));
if (new_array == NULL)
1998-10-15 23:42:56 +00:00
return (FALSE);
1998-10-15 23:51:14 +00:00
memcpy(new_array, ctx->array, ctx->size);
mem_put(ctx->mctx, ctx->array,
ctx->size * sizeof (void *));
ctx->size = new_size;
ctx->array = new_array;
1998-10-15 23:42:56 +00:00
return (TRUE);
}
static void
float_up(heap_context_t ctx, unsigned int i, void *elt) {
unsigned int p;
for ( p = heap_parent(i);
1998-10-15 23:51:14 +00:00
i > 1 && ctx->higher_priority(elt, ctx->array[p]);
1998-10-15 23:42:56 +00:00
i = p, p = heap_parent(i) ) {
1998-10-15 23:51:14 +00:00
ctx->array[i] = ctx->array[p];
1998-10-15 23:42:56 +00:00
if (ctx->index != NULL)
1998-10-15 23:51:14 +00:00
(ctx->index)(ctx->array[i], i);
1998-10-15 23:42:56 +00:00
}
1998-10-15 23:51:14 +00:00
ctx->array[i] = elt;
1998-10-15 23:42:56 +00:00
if (ctx->index != NULL)
1998-10-15 23:51:14 +00:00
(ctx->index)(ctx->array[i], i);
1998-10-15 23:42:56 +00:00
}
static void
sink_down(heap_context_t ctx, unsigned int i, void *elt) {
unsigned int j, size, half_size;
1998-10-15 23:51:14 +00:00
size = ctx->last;
1998-10-15 23:42:56 +00:00
half_size = size / 2;
while (i <= half_size) {
/* find smallest of the (at most) two children */
j = heap_left(i);
1998-10-15 23:51:14 +00:00
if (j < size && ctx->higher_priority(ctx->array[j+1],
ctx->array[j]))
1998-10-15 23:42:56 +00:00
j++;
1998-10-15 23:51:14 +00:00
if (ctx->higher_priority(elt, ctx->array[j]))
1998-10-15 23:42:56 +00:00
break;
1998-10-15 23:51:14 +00:00
ctx->array[i] = ctx->array[j];
1998-10-15 23:42:56 +00:00
if (ctx->index != NULL)
1998-10-15 23:51:14 +00:00
(ctx->index)(ctx->array[i], i);
1998-10-15 23:42:56 +00:00
i = j;
}
1998-10-15 23:51:14 +00:00
ctx->array[i] = elt;
1998-10-15 23:42:56 +00:00
if (ctx->index != NULL)
1998-10-15 23:51:14 +00:00
(ctx->index)(ctx->array[i], i);
1998-10-15 23:42:56 +00:00
}
isc_result
heap_insert(heap_context_t ctx, void *elt) {
unsigned int i;
REQUIRE(VALID_CONTEXT(ctx));
1998-10-15 23:51:14 +00:00
i = ++ctx->last;
if (ctx->last >= ctx->size && !resize(ctx))
1998-10-15 23:42:56 +00:00
return (ISC_R_NOMEMORY);
float_up(ctx, i, elt);
return (ISC_R_SUCCESS);
}
void
heap_delete(heap_context_t ctx, unsigned int i) {
void *elt;
REQUIRE(VALID_CONTEXT(ctx));
1998-10-15 23:51:14 +00:00
REQUIRE(i >= 1 && i <= ctx->last);
1998-10-15 23:42:56 +00:00
1998-10-15 23:51:14 +00:00
elt = ctx->array[ctx->last];
if (--ctx->last > 0)
1998-10-15 23:42:56 +00:00
sink_down(ctx, i, elt);
}
void
heap_increased(heap_context_t ctx, unsigned int i) {
REQUIRE(VALID_CONTEXT(ctx));
1998-10-15 23:51:14 +00:00
REQUIRE(i >= 1 && i <= ctx->last);
1998-10-15 23:42:56 +00:00
1998-10-15 23:51:14 +00:00
float_up(ctx, i, ctx->array[i]);
1998-10-15 23:42:56 +00:00
}
void
heap_decreased(heap_context_t ctx, unsigned int i) {
REQUIRE(VALID_CONTEXT(ctx));
1998-10-15 23:51:14 +00:00
REQUIRE(i >= 1 && i <= ctx->last);
1998-10-15 23:42:56 +00:00
1998-10-15 23:51:14 +00:00
sink_down(ctx, i, ctx->array[i]);
1998-10-15 23:42:56 +00:00
}
void *
heap_element(heap_context_t ctx, unsigned int i) {
REQUIRE(VALID_CONTEXT(ctx));
1998-10-15 23:51:14 +00:00
REQUIRE(i >= 1 && i <= ctx->last);
1998-10-15 23:42:56 +00:00
1998-10-15 23:51:14 +00:00
return (ctx->array[i]);
1998-10-15 23:42:56 +00:00
}
void
heap_for_each(heap_context_t ctx, heap_for_each_func action, void *uap) {
unsigned int i;
REQUIRE(VALID_CONTEXT(ctx));
REQUIRE(action != NULL);
1998-10-15 23:51:14 +00:00
for (i = 1; i <= ctx->last; i++)
(action)(ctx->array[i], uap);
1998-10-15 23:42:56 +00:00
}