heap.c revision 89085bebd38357d7953f3217a220a57c1c9fcff7
69fe9aaafdd6a141610e86a777d325db75422070Mark Andrews * Copyright (C) 1997-2001, 2004-2007, 2010-2016 Internet Systems Consortium, Inc. ("ISC")
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * This Source Code Form is subject to the terms of the Mozilla Public
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * License, v. 2.0. If a copy of the MPL was not distributed with this
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * file, You can obtain one at http://mozilla.org/MPL/2.0/.
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * Heap implementation of priority queues adapted from the following:
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * \li "Introduction to Algorithms," Cormen, Leiserson, and Rivest,
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * MIT Press / McGraw Hill, 1990, ISBN 0-262-03141-8, chapter 7.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * \li "Algorithms," Second Edition, Sedgewick, Addison-Wesley, 1988,
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews * ISBN 0-201-06673-4, chapter 11.
96754ed7b400ce080279de2f92111ad868105290Bob Halley#include <isc/string.h> /* Required for memmove. */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * Note: to make heap_parent and heap_left easy to compute, the first
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * element of the heap array is not used; i.e. heap subscripts are 1-based,
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * not 0-based. The parent is index/2, and the left-child is index*2.
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews * The right child is index*2+1.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#define VALID_HEAP(h) ISC_MAGIC_VALID(h, HEAP_MAGIC)
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * When the heap is in a consistent state, the following invariant
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * holds true: for every element i > 1, heap_parent(i) has a priority
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * higher than or equal to that of i.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*% ISC heap structure. */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein unsigned int magic;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein unsigned int size;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein unsigned int last;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein unsigned int i;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#define heap_check(x) (void)0
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinisc_heap_create(isc_mem_t *mctx, isc_heapcompare_t compare,
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein isc_heapindex_t idx, unsigned int size_increment,
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein isc_mem_putanddetach(&heap->mctx, heap, sizeof(*heap));
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein unsigned int new_size;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein new_array = isc_mem_get(heap->mctx, new_size * sizeof(void *));
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein memmove(new_array, heap->array, heap->size * sizeof(void *));
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinfloat_up(isc_heap_t *heap, unsigned int i, void *elt) {
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein unsigned int p;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein i = p, p = heap_parent(i)) {
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinsink_down(isc_heap_t *heap, unsigned int i, void *elt) {
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein while (i <= half_size) {
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein /* Find the smallest of the (at most) two children. */
25e43e68b7431d5e4ff8b5427108cd7f5f9bcf3eBob Halley unsigned int new_last;
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley RUNTIME_CHECK(new_last > 0); /* overflow check */
e4e071ae12aee942fefc2c0a3280e402938669deBob Halleyisc_heap_delete(isc_heap_t *heap, unsigned int idx) {
542189f21b3ea9b27b0fbc047d832a34dcaf75bcAndreas Gustafssonisc_heap_increased(isc_heap_t *heap, unsigned int idx) {
25e43e68b7431d5e4ff8b5427108cd7f5f9bcf3eBob Halleyisc_heap_decreased(isc_heap_t *heap, unsigned int idx) {
34b394b43e2207e8f8f3703f0402422121455638David Lawrenceisc_heap_element(isc_heap_t *heap, unsigned int idx) {
04b8111f2137a9cf9b0b71228f76b3e40ffa1173Brian Wellingtonisc_heap_foreach(isc_heap_t *heap, isc_heapaction_t action, void *uap) {
95b604c5e972a5e9eb713bf45cf0b2d9b98da27eMark Andrews unsigned int i;