2 * Copyright (C) 2004-2007, 2010-2012 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1997-2001 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or 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 WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
21 * Heap implementation of priority queues adapted from the following:
23 * \li "Introduction to Algorithms," Cormen, Leiserson, and Rivest,
24 * MIT Press / McGraw Hill, 1990, ISBN 0-262-03141-8, chapter 7.
26 * \li "Algorithms," Second Edition, Sedgewick, Addison-Wesley, 1988,
27 * ISBN 0-201-06673-4, chapter 11.
33 #include <isc/magic.h>
35 #include <isc/string.h> /* Required for memcpy. */
40 * Note: to make heap_parent and heap_left easy to compute, the first
41 * element of the heap array is not used; i.e. heap subscripts are 1-based,
42 * not 0-based. The parent is index/2, and the left-child is index*2.
43 * The right child is index*2+1.
45 #define heap_parent(i) ((i) >> 1)
46 #define heap_left(i) ((i) << 1)
49 #define SIZE_INCREMENT 1024
51 #define HEAP_MAGIC ISC_MAGIC('H', 'E', 'A', 'P')
52 #define VALID_HEAP(h) ISC_MAGIC_VALID(h, HEAP_MAGIC)
55 * When the heap is in a consistent state, the following invariant
56 * holds true: for every element i > 1, heap_parent(i) has a priority
57 * higher than or equal to that of i.
59 #define HEAPCONDITION(i) ((i) == 1 || \
60 ! heap->compare(heap->array[(i)], \
61 heap->array[heap_parent(i)]))
63 /*% ISC heap structure. */
68 unsigned int size_increment;
71 isc_heapcompare_t compare;
72 isc_heapindex_t index;
76 isc_heap_create(isc_mem_t *mctx, isc_heapcompare_t compare,
77 isc_heapindex_t index, unsigned int size_increment,
82 REQUIRE(heapp != NULL && *heapp == NULL);
83 REQUIRE(compare != NULL);
85 heap = isc_mem_get(mctx, sizeof(*heap));
87 return (ISC_R_NOMEMORY);
88 heap->magic = HEAP_MAGIC;
91 isc_mem_attach(mctx, &heap->mctx);
92 if (size_increment == 0)
93 heap->size_increment = SIZE_INCREMENT;
95 heap->size_increment = size_increment;
98 heap->compare = compare;
103 return (ISC_R_SUCCESS);
107 isc_heap_destroy(isc_heap_t **heapp) {
110 REQUIRE(heapp != NULL);
112 REQUIRE(VALID_HEAP(heap));
114 if (heap->array != NULL)
115 isc_mem_put(heap->mctx, heap->array,
116 heap->size * sizeof(void *));
118 isc_mem_putanddetach(&heap->mctx, heap, sizeof(*heap));
124 resize(isc_heap_t *heap) {
128 REQUIRE(VALID_HEAP(heap));
130 new_size = heap->size + heap->size_increment;
131 new_array = isc_mem_get(heap->mctx, new_size * sizeof(void *));
132 if (new_array == NULL)
134 if (heap->array != NULL) {
135 memcpy(new_array, heap->array, heap->size * sizeof(void *));
136 isc_mem_put(heap->mctx, heap->array,
137 heap->size * sizeof(void *));
139 heap->size = new_size;
140 heap->array = new_array;
146 float_up(isc_heap_t *heap, unsigned int i, void *elt) {
149 for (p = heap_parent(i) ;
150 i > 1 && heap->compare(elt, heap->array[p]) ;
151 i = p, p = heap_parent(i)) {
152 heap->array[i] = heap->array[p];
153 if (heap->index != NULL)
154 (heap->index)(heap->array[i], i);
156 heap->array[i] = elt;
157 if (heap->index != NULL)
158 (heap->index)(heap->array[i], i);
160 INSIST(HEAPCONDITION(i));
164 sink_down(isc_heap_t *heap, unsigned int i, void *elt) {
165 unsigned int j, size, half_size;
167 half_size = size / 2;
168 while (i <= half_size) {
169 /* Find the smallest of the (at most) two children. */
171 if (j < size && heap->compare(heap->array[j+1],
174 if (heap->compare(elt, heap->array[j]))
176 heap->array[i] = heap->array[j];
177 if (heap->index != NULL)
178 (heap->index)(heap->array[i], i);
181 heap->array[i] = elt;
182 if (heap->index != NULL)
183 (heap->index)(heap->array[i], i);
185 INSIST(HEAPCONDITION(i));
189 isc_heap_insert(isc_heap_t *heap, void *elt) {
190 unsigned int new_last;
192 REQUIRE(VALID_HEAP(heap));
194 new_last = heap->last + 1;
195 RUNTIME_CHECK(new_last > 0); /* overflow check */
196 if (new_last >= heap->size && !resize(heap))
197 return (ISC_R_NOMEMORY);
198 heap->last = new_last;
200 float_up(heap, new_last, elt);
202 return (ISC_R_SUCCESS);
206 isc_heap_delete(isc_heap_t *heap, unsigned int index) {
210 REQUIRE(VALID_HEAP(heap));
211 REQUIRE(index >= 1 && index <= heap->last);
213 if (index == heap->last) {
214 heap->array[heap->last] = NULL;
217 elt = heap->array[heap->last];
218 heap->array[heap->last] = NULL;
221 less = heap->compare(elt, heap->array[index]);
222 heap->array[index] = elt;
224 float_up(heap, index, heap->array[index]);
226 sink_down(heap, index, heap->array[index]);
231 isc_heap_increased(isc_heap_t *heap, unsigned int index) {
232 REQUIRE(VALID_HEAP(heap));
233 REQUIRE(index >= 1 && index <= heap->last);
235 float_up(heap, index, heap->array[index]);
239 isc_heap_decreased(isc_heap_t *heap, unsigned int index) {
240 REQUIRE(VALID_HEAP(heap));
241 REQUIRE(index >= 1 && index <= heap->last);
243 sink_down(heap, index, heap->array[index]);
247 isc_heap_element(isc_heap_t *heap, unsigned int index) {
248 REQUIRE(VALID_HEAP(heap));
251 if (index <= heap->last)
252 return (heap->array[index]);
257 isc_heap_foreach(isc_heap_t *heap, isc_heapaction_t action, void *uap) {
260 REQUIRE(VALID_HEAP(heap));
261 REQUIRE(action != NULL);
263 for (i = 1 ; i <= heap->last ; i++)
264 (action)(heap->array[i], uap);