heap.c revision 70e5a7403f0e0a3bd292b8287c5fed5772c15270
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt/*
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Copyright (C) 2004-2007 Internet Systems Consortium, Inc. ("ISC")
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt * Copyright (C) 1997-2001 Internet Software Consortium.
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews *
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 *
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 */
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt/* $Id: heap.c,v 1.36 2007/06/19 23:47:17 tbox Exp $ */
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt/*! \file
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt * Heap implementation of priority queues adapted from the following:
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt *
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 *
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt * \li "Algorithms," Second Edition, Sedgewick, Addison-Wesley, 1988,
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt * ISBN 0-201-06673-4, chapter 11.
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt */
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt#include <config.h>
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt#include <isc/heap.h>
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt#include <isc/magic.h>
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt#include <isc/mem.h>
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt#include <isc/string.h> /* Required for memcpy. */
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt#include <isc/util.h>
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt/*@{*/
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt/*%
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 */
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt#define heap_parent(i) ((i) >> 1)
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt#define heap_left(i) ((i) << 1)
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt/*@}*/
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt#define SIZE_INCREMENT 1024
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt#define HEAP_MAGIC ISC_MAGIC('H', 'E', 'A', 'P')
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt#define VALID_HEAP(h) ISC_MAGIC_VALID(h, HEAP_MAGIC)
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt/*%
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 */
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt#define HEAPCONDITION(i) ((i) == 1 || \
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt ! heap->compare(heap->array[(i)], \
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt heap->array[heap_parent(i)]))
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt/*% ISC heap structure. */
df925e6c66d45d960fbac0383169763967d2111cEvan Huntstruct isc_heap {
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt unsigned int magic;
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt isc_mem_t * mctx;
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt unsigned int size;
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt unsigned int size_increment;
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt unsigned int last;
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt void **array;
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt isc_heapcompare_t compare;
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt isc_heapindex_t index;
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt};
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt
df925e6c66d45d960fbac0383169763967d2111cEvan Huntisc_result_t
df925e6c66d45d960fbac0383169763967d2111cEvan Huntisc_heap_create(isc_mem_t *mctx, isc_heapcompare_t compare,
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt isc_heapindex_t index, unsigned int size_increment,
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt isc_heap_t **heapp)
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt{
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt isc_heap_t *heap;
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt REQUIRE(heapp != NULL && *heapp == NULL);
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt REQUIRE(compare != NULL);
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt heap = isc_mem_get(mctx, sizeof(*heap));
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt if (heap == NULL)
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt return (ISC_R_NOMEMORY);
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt heap->magic = HEAP_MAGIC;
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt heap->mctx = mctx;
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt heap->size = 0;
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt if (size_increment == 0)
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt heap->size_increment = SIZE_INCREMENT;
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt else
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt heap->size_increment = size_increment;
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt heap->last = 0;
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt heap->array = NULL;
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt heap->compare = compare;
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt heap->index = index;
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt *heapp = heap;
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt return (ISC_R_SUCCESS);
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt}
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt
df925e6c66d45d960fbac0383169763967d2111cEvan Huntvoid
df925e6c66d45d960fbac0383169763967d2111cEvan Huntisc_heap_destroy(isc_heap_t **heapp) {
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt isc_heap_t *heap;
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt REQUIRE(heapp != NULL);
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt heap = *heapp;
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt REQUIRE(VALID_HEAP(heap));
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt if (heap->array != NULL)
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt isc_mem_put(heap->mctx, heap->array,
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt heap->size * sizeof(void *));
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt heap->magic = 0;
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt isc_mem_put(heap->mctx, heap, sizeof(*heap));
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt *heapp = NULL;
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt}
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt
df925e6c66d45d960fbac0383169763967d2111cEvan Huntstatic isc_boolean_t
df925e6c66d45d960fbac0383169763967d2111cEvan Huntresize(isc_heap_t *heap) {
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt void **new_array;
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt size_t new_size;
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt REQUIRE(VALID_HEAP(heap));
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt new_size = heap->size + heap->size_increment;
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt new_array = isc_mem_get(heap->mctx, new_size * sizeof(void *));
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt if (new_array == NULL)
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt return (ISC_FALSE);
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt if (heap->array != NULL) {
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt memcpy(new_array, heap->array, heap->size * sizeof(void *));
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt isc_mem_put(heap->mctx, heap->array,
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt heap->size * sizeof(void *));
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt }
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt heap->size = new_size;
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt heap->array = new_array;
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt return (ISC_TRUE);
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt}
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt
df925e6c66d45d960fbac0383169763967d2111cEvan Huntstatic void
df925e6c66d45d960fbac0383169763967d2111cEvan Huntfloat_up(isc_heap_t *heap, unsigned int i, void *elt) {
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt unsigned int p;
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt for (p = heap_parent(i) ;
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt i > 1 && heap->compare(elt, heap->array[p]) ;
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt i = p, p = heap_parent(i)) {
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt heap->array[i] = heap->array[p];
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt if (heap->index != NULL)
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt (heap->index)(heap->array[i], i);
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt }
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt heap->array[i] = elt;
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt if (heap->index != NULL)
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt (heap->index)(heap->array[i], i);
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt INSIST(HEAPCONDITION(i));
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt}
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt
df925e6c66d45d960fbac0383169763967d2111cEvan Huntstatic void
df925e6c66d45d960fbac0383169763967d2111cEvan Huntsink_down(isc_heap_t *heap, unsigned int i, void *elt) {
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt unsigned int j, size, half_size;
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt size = heap->last;
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt half_size = size / 2;
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt while (i <= half_size) {
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt /* Find the smallest of the (at most) two children. */
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt j = heap_left(i);
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt if (j < size && heap->compare(heap->array[j+1],
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt heap->array[j]))
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt j++;
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt if (heap->compare(elt, heap->array[j]))
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt break;
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt heap->array[i] = heap->array[j];
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt if (heap->index != NULL)
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt (heap->index)(heap->array[i], i);
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt i = j;
}
heap->array[i] = elt;
if (heap->index != NULL)
(heap->index)(heap->array[i], i);
INSIST(HEAPCONDITION(i));
}
isc_result_t
isc_heap_insert(isc_heap_t *heap, void *elt) {
unsigned int i;
REQUIRE(VALID_HEAP(heap));
i = ++heap->last;
if (heap->last >= heap->size && !resize(heap))
return (ISC_R_NOMEMORY);
float_up(heap, i, elt);
return (ISC_R_SUCCESS);
}
void
isc_heap_delete(isc_heap_t *heap, unsigned int index) {
void *elt;
isc_boolean_t less;
REQUIRE(VALID_HEAP(heap));
REQUIRE(index >= 1 && index <= heap->last);
if (index == heap->last) {
heap->last--;
} else {
elt = heap->array[heap->last--];
less = heap->compare(elt, heap->array[index]);
heap->array[index] = elt;
if (less)
float_up(heap, index, heap->array[index]);
else
sink_down(heap, index, heap->array[index]);
}
}
void
isc_heap_increased(isc_heap_t *heap, unsigned int index) {
REQUIRE(VALID_HEAP(heap));
REQUIRE(index >= 1 && index <= heap->last);
float_up(heap, index, heap->array[index]);
}
void
isc_heap_decreased(isc_heap_t *heap, unsigned int index) {
REQUIRE(VALID_HEAP(heap));
REQUIRE(index >= 1 && index <= heap->last);
sink_down(heap, index, heap->array[index]);
}
void *
isc_heap_element(isc_heap_t *heap, unsigned int index) {
REQUIRE(VALID_HEAP(heap));
REQUIRE(index >= 1 && index <= heap->last);
return (heap->array[index]);
}
void
isc_heap_foreach(isc_heap_t *heap, isc_heapaction_t action, void *uap) {
unsigned int i;
REQUIRE(VALID_HEAP(heap));
REQUIRE(action != NULL);
for (i = 1 ; i <= heap->last ; i++)
(action)(heap->array[i], uap);
}