No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / libbind / dist / isc / heap.c
blobb10bbc7e0f7a2c77c2f2fe80ce53d1301b5be584
1 /* $NetBSD$ */
3 /*
4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1997,1999 by Internet Software Consortium.
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 /*%
21 * Heap implementation of priority queues adapted from the following:
23 * _Introduction to Algorithms_, Cormen, Leiserson, and Rivest,
24 * MIT Press / McGraw Hill, 1990, ISBN 0-262-03141-8, chapter 7.
26 * _Algorithms_, Second Edition, Sedgewick, Addison-Wesley, 1988,
27 * ISBN 0-201-06673-4, chapter 11.
30 #if !defined(LINT) && !defined(CODECENTER)
31 static const char rcsid[] = "Id: heap.c,v 1.4 2006/03/09 23:57:56 marka Exp";
32 #endif /* not lint */
34 #include "port_before.h"
36 #include <stddef.h>
37 #include <stdlib.h>
38 #include <errno.h>
40 #include "port_after.h"
42 #include <isc/heap.h>
44 /*%
45 * Note: to make heap_parent and heap_left easy to compute, the first
46 * element of the heap array is not used; i.e. heap subscripts are 1-based,
47 * not 0-based.
49 #define heap_parent(i) ((i) >> 1)
50 #define heap_left(i) ((i) << 1)
52 #define ARRAY_SIZE_INCREMENT 512
54 heap_context
55 heap_new(heap_higher_priority_func higher_priority, heap_index_func index,
56 int array_size_increment) {
57 heap_context ctx;
59 if (higher_priority == NULL)
60 return (NULL);
62 ctx = (heap_context)malloc(sizeof (struct heap_context));
63 if (ctx == NULL)
64 return (NULL);
66 ctx->array_size = 0;
67 if (array_size_increment == 0)
68 ctx->array_size_increment = ARRAY_SIZE_INCREMENT;
69 else
70 ctx->array_size_increment = array_size_increment;
71 ctx->heap_size = 0;
72 ctx->heap = NULL;
73 ctx->higher_priority = higher_priority;
74 ctx->index = index;
75 return (ctx);
78 int
79 heap_free(heap_context ctx) {
80 if (ctx == NULL) {
81 errno = EINVAL;
82 return (-1);
85 if (ctx->heap != NULL)
86 free(ctx->heap);
87 free(ctx);
89 return (0);
92 static int
93 heap_resize(heap_context ctx) {
94 void **new_heap;
96 ctx->array_size += ctx->array_size_increment;
97 new_heap = (void **)realloc(ctx->heap,
98 (ctx->array_size) * (sizeof (void *)));
99 if (new_heap == NULL) {
100 errno = ENOMEM;
101 return (-1);
103 ctx->heap = new_heap;
104 return (0);
107 static void
108 float_up(heap_context ctx, int i, void *elt) {
109 int p;
111 for ( p = heap_parent(i);
112 i > 1 && ctx->higher_priority(elt, ctx->heap[p]);
113 i = p, p = heap_parent(i) ) {
114 ctx->heap[i] = ctx->heap[p];
115 if (ctx->index != NULL)
116 (ctx->index)(ctx->heap[i], i);
118 ctx->heap[i] = elt;
119 if (ctx->index != NULL)
120 (ctx->index)(ctx->heap[i], i);
123 static void
124 sink_down(heap_context ctx, int i, void *elt) {
125 int j, size, half_size;
127 size = ctx->heap_size;
128 half_size = size / 2;
129 while (i <= half_size) {
130 /* find smallest of the (at most) two children */
131 j = heap_left(i);
132 if (j < size && ctx->higher_priority(ctx->heap[j+1],
133 ctx->heap[j]))
134 j++;
135 if (ctx->higher_priority(elt, ctx->heap[j]))
136 break;
137 ctx->heap[i] = ctx->heap[j];
138 if (ctx->index != NULL)
139 (ctx->index)(ctx->heap[i], i);
140 i = j;
142 ctx->heap[i] = elt;
143 if (ctx->index != NULL)
144 (ctx->index)(ctx->heap[i], i);
148 heap_insert(heap_context ctx, void *elt) {
149 int i;
151 if (ctx == NULL || elt == NULL) {
152 errno = EINVAL;
153 return (-1);
156 i = ++ctx->heap_size;
157 if (ctx->heap_size >= ctx->array_size && heap_resize(ctx) < 0)
158 return (-1);
160 float_up(ctx, i, elt);
162 return (0);
166 heap_delete(heap_context ctx, int i) {
167 void *elt;
168 int less;
170 if (ctx == NULL || i < 1 || i > ctx->heap_size) {
171 errno = EINVAL;
172 return (-1);
175 if (i == ctx->heap_size) {
176 ctx->heap_size--;
177 } else {
178 elt = ctx->heap[ctx->heap_size--];
179 less = ctx->higher_priority(elt, ctx->heap[i]);
180 ctx->heap[i] = elt;
181 if (less)
182 float_up(ctx, i, ctx->heap[i]);
183 else
184 sink_down(ctx, i, ctx->heap[i]);
187 return (0);
191 heap_increased(heap_context ctx, int i) {
192 if (ctx == NULL || i < 1 || i > ctx->heap_size) {
193 errno = EINVAL;
194 return (-1);
197 float_up(ctx, i, ctx->heap[i]);
199 return (0);
203 heap_decreased(heap_context ctx, int i) {
204 if (ctx == NULL || i < 1 || i > ctx->heap_size) {
205 errno = EINVAL;
206 return (-1);
209 sink_down(ctx, i, ctx->heap[i]);
211 return (0);
214 void *
215 heap_element(heap_context ctx, int i) {
216 if (ctx == NULL || i < 1 || i > ctx->heap_size) {
217 errno = EINVAL;
218 return (NULL);
221 return (ctx->heap[i]);
225 heap_for_each(heap_context ctx, heap_for_each_func action, void *uap) {
226 int i;
228 if (ctx == NULL || action == NULL) {
229 errno = EINVAL;
230 return (-1);
233 for (i = 1; i <= ctx->heap_size; i++)
234 (action)(ctx->heap[i], uap);
235 return (0);
238 /*! \file */