2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1997,1999 by Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 * Heap implementation of priority queues adapted from the following:
21 * _Introduction to Algorithms_, Cormen, Leiserson, and Rivest,
22 * MIT Press / McGraw Hill, 1990, ISBN 0-262-03141-8, chapter 7.
24 * _Algorithms_, Second Edition, Sedgewick, Addison-Wesley, 1988,
25 * ISBN 0-201-06673-4, chapter 11.
28 #if !defined(LINT) && !defined(CODECENTER)
29 static const char rcsid[] = "$Id: heap.c,v 1.1.2.1 2004/03/09 09:17:35 marka Exp $";
32 #include "port_before.h"
38 #include "port_after.h"
43 * Note: to make heap_parent and heap_left easy to compute, the first
44 * element of the heap array is not used; i.e. heap subscripts are 1-based,
47 #define heap_parent(i) ((i) >> 1)
48 #define heap_left(i) ((i) << 1)
50 #define ARRAY_SIZE_INCREMENT 512
53 heap_new(heap_higher_priority_func higher_priority, heap_index_func index,
54 int array_size_increment) {
57 ctx = (heap_context)malloc(sizeof (struct heap_context));
58 if (ctx == NULL || higher_priority == NULL)
61 if (array_size_increment == 0)
62 ctx->array_size_increment = ARRAY_SIZE_INCREMENT;
64 ctx->array_size_increment = array_size_increment;
67 ctx->higher_priority = higher_priority;
73 heap_free(heap_context ctx) {
79 if (ctx->heap != NULL)
87 heap_resize(heap_context ctx) {
90 ctx->array_size += ctx->array_size_increment;
91 new_heap = (void **)realloc(ctx->heap,
92 (ctx->array_size) * (sizeof (void *)));
93 if (new_heap == NULL) {
102 float_up(heap_context ctx, int i, void *elt) {
105 for ( p = heap_parent(i);
106 i > 1 && ctx->higher_priority(elt, ctx->heap[p]);
107 i = p, p = heap_parent(i) ) {
108 ctx->heap[i] = ctx->heap[p];
109 if (ctx->index != NULL)
110 (ctx->index)(ctx->heap[i], i);
113 if (ctx->index != NULL)
114 (ctx->index)(ctx->heap[i], i);
118 sink_down(heap_context ctx, int i, void *elt) {
119 int j, size, half_size;
121 size = ctx->heap_size;
122 half_size = size / 2;
123 while (i <= half_size) {
124 /* find smallest of the (at most) two children */
126 if (j < size && ctx->higher_priority(ctx->heap[j+1],
129 if (ctx->higher_priority(elt, ctx->heap[j]))
131 ctx->heap[i] = ctx->heap[j];
132 if (ctx->index != NULL)
133 (ctx->index)(ctx->heap[i], i);
137 if (ctx->index != NULL)
138 (ctx->index)(ctx->heap[i], i);
142 heap_insert(heap_context ctx, void *elt) {
145 if (ctx == NULL || elt == NULL) {
150 i = ++ctx->heap_size;
151 if (ctx->heap_size >= ctx->array_size && heap_resize(ctx) < 0)
154 float_up(ctx, i, elt);
160 heap_delete(heap_context ctx, int i) {
164 if (ctx == NULL || i < 1 || i > ctx->heap_size) {
169 if (i == ctx->heap_size) {
172 elt = ctx->heap[ctx->heap_size--];
173 less = ctx->higher_priority(elt, ctx->heap[i]);
176 float_up(ctx, i, ctx->heap[i]);
178 sink_down(ctx, i, ctx->heap[i]);
185 heap_increased(heap_context ctx, int i) {
186 if (ctx == NULL || i < 1 || i > ctx->heap_size) {
191 float_up(ctx, i, ctx->heap[i]);
197 heap_decreased(heap_context ctx, int i) {
198 if (ctx == NULL || i < 1 || i > ctx->heap_size) {
203 sink_down(ctx, i, ctx->heap[i]);
209 heap_element(heap_context ctx, int i) {
210 if (ctx == NULL || i < 1 || i > ctx->heap_size) {
215 return (ctx->heap[i]);
219 heap_for_each(heap_context ctx, heap_for_each_func action, void *uap) {
222 if (ctx == NULL || action == NULL) {
227 for (i = 1; i <= ctx->heap_size; i++)
228 (action)(ctx->heap[i], uap);