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.
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";
34 #include "port_before.h"
40 #include "port_after.h"
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,
49 #define heap_parent(i) ((i) >> 1)
50 #define heap_left(i) ((i) << 1)
52 #define ARRAY_SIZE_INCREMENT 512
55 heap_new(heap_higher_priority_func higher_priority
, heap_index_func index
,
56 int array_size_increment
) {
59 if (higher_priority
== NULL
)
62 ctx
= (heap_context
)malloc(sizeof (struct heap_context
));
67 if (array_size_increment
== 0)
68 ctx
->array_size_increment
= ARRAY_SIZE_INCREMENT
;
70 ctx
->array_size_increment
= array_size_increment
;
73 ctx
->higher_priority
= higher_priority
;
79 heap_free(heap_context ctx
) {
85 if (ctx
->heap
!= NULL
)
93 heap_resize(heap_context ctx
) {
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
) {
103 ctx
->heap
= new_heap
;
108 float_up(heap_context ctx
, int i
, void *elt
) {
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
);
119 if (ctx
->index
!= NULL
)
120 (ctx
->index
)(ctx
->heap
[i
], i
);
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 */
132 if (j
< size
&& ctx
->higher_priority(ctx
->heap
[j
+1],
135 if (ctx
->higher_priority(elt
, ctx
->heap
[j
]))
137 ctx
->heap
[i
] = ctx
->heap
[j
];
138 if (ctx
->index
!= NULL
)
139 (ctx
->index
)(ctx
->heap
[i
], i
);
143 if (ctx
->index
!= NULL
)
144 (ctx
->index
)(ctx
->heap
[i
], i
);
148 heap_insert(heap_context ctx
, void *elt
) {
151 if (ctx
== NULL
|| elt
== NULL
) {
156 i
= ++ctx
->heap_size
;
157 if (ctx
->heap_size
>= ctx
->array_size
&& heap_resize(ctx
) < 0)
160 float_up(ctx
, i
, elt
);
166 heap_delete(heap_context ctx
, int i
) {
170 if (ctx
== NULL
|| i
< 1 || i
> ctx
->heap_size
) {
175 if (i
== ctx
->heap_size
) {
178 elt
= ctx
->heap
[ctx
->heap_size
--];
179 less
= ctx
->higher_priority(elt
, ctx
->heap
[i
]);
182 float_up(ctx
, i
, ctx
->heap
[i
]);
184 sink_down(ctx
, i
, ctx
->heap
[i
]);
191 heap_increased(heap_context ctx
, int i
) {
192 if (ctx
== NULL
|| i
< 1 || i
> ctx
->heap_size
) {
197 float_up(ctx
, i
, ctx
->heap
[i
]);
203 heap_decreased(heap_context ctx
, int i
) {
204 if (ctx
== NULL
|| i
< 1 || i
> ctx
->heap_size
) {
209 sink_down(ctx
, i
, ctx
->heap
[i
]);
215 heap_element(heap_context ctx
, int i
) {
216 if (ctx
== NULL
|| i
< 1 || i
> ctx
->heap_size
) {
221 return (ctx
->heap
[i
]);
225 heap_for_each(heap_context ctx
, heap_for_each_func action
, void *uap
) {
228 if (ctx
== NULL
|| action
== NULL
) {
233 for (i
= 1; i
<= ctx
->heap_size
; i
++)
234 (action
)(ctx
->heap
[i
], uap
);