heap.c revision 70e5a7403f0e0a3bd292b8287c5fed5772c15270
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Copyright (C) 2004-2007 Internet Systems Consortium, Inc. ("ISC")
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt * Copyright (C) 1997-2001 Internet Software Consortium.
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Permission to use, copy, modify, and/or distribute this software for any
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * purpose with or without fee is hereby granted, provided that the above
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt * copyright notice and this permission notice appear in all copies.
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt * PERFORMANCE OF THIS SOFTWARE.
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt/* $Id: heap.c,v 1.36 2007/06/19 23:47:17 tbox Exp $ */
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt * Heap implementation of priority queues adapted from the following:
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt * \li "Introduction to Algorithms," Cormen, Leiserson, and Rivest,
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt * MIT Press / McGraw Hill, 1990, ISBN 0-262-03141-8, chapter 7.
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt * \li "Algorithms," Second Edition, Sedgewick, Addison-Wesley, 1988,
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt * ISBN 0-201-06673-4, chapter 11.
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt * Note: to make heap_parent and heap_left easy to compute, the first
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt * element of the heap array is not used; i.e. heap subscripts are 1-based,
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt * not 0-based. The parent is index/2, and the left-child is index*2.
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt * The right child is index*2+1.
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt#define VALID_HEAP(h) ISC_MAGIC_VALID(h, HEAP_MAGIC)
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt * When the heap is in a consistent state, the following invariant
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt * holds true: for every element i > 1, heap_parent(i) has a priority
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt * higher than or equal to that of i.
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt/*% ISC heap structure. */
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt unsigned int magic;
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt unsigned int size;
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt unsigned int last;
df925e6c66d45d960fbac0383169763967d2111cEvan Huntisc_heap_create(isc_mem_t *mctx, isc_heapcompare_t compare,
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt isc_heapindex_t index, unsigned int size_increment,
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt new_array = isc_mem_get(heap->mctx, new_size * sizeof(void *));
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt memcpy(new_array, heap->array, heap->size * sizeof(void *));
df925e6c66d45d960fbac0383169763967d2111cEvan Huntfloat_up(isc_heap_t *heap, unsigned int i, void *elt) {
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt unsigned int p;
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt for (p = heap_parent(i) ;
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt i = p, p = heap_parent(i)) {
df925e6c66d45d960fbac0383169763967d2111cEvan Huntsink_down(isc_heap_t *heap, unsigned int i, void *elt) {
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt while (i <= half_size) {
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt /* Find the smallest of the (at most) two children. */
return (ISC_R_NOMEMORY);
return (ISC_R_SUCCESS);
void *elt;
if (less)