2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-24 19:18:50 +00:00
bind/lib/isc/heap.c

295 lines
6.3 KiB
C
Raw Normal View History

1998-10-15 23:42:56 +00:00
/*
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* See the COPYRIGHT file distributed with this work for additional
* information regarding copyright ownership.
1998-10-15 23:42:56 +00:00
*/
/*! \file
1998-10-15 23:42:56 +00:00
* Heap implementation of priority queues adapted from the following:
*
* \li "Introduction to Algorithms," Cormen, Leiserson, and Rivest,
1998-10-15 23:42:56 +00:00
* MIT Press / McGraw Hill, 1990, ISBN 0-262-03141-8, chapter 7.
*
* \li "Algorithms," Second Edition, Sedgewick, Addison-Wesley, 1988,
1998-10-15 23:42:56 +00:00
* ISBN 0-201-06673-4, chapter 11.
*/
#include <stdbool.h>
1998-10-15 23:42:56 +00:00
#include <isc/heap.h>
#include <isc/magic.h>
#include <isc/mem.h>
#include <isc/string.h> /* Required for memmove. */
2000-04-28 01:12:23 +00:00
#include <isc/util.h>
1998-10-15 23:42:56 +00:00
/*@{*/
/*%
1998-10-15 23:42:56 +00:00
* Note: to make heap_parent and heap_left easy to compute, the first
* element of the heap array is not used; i.e. heap subscripts are 1-based,
* not 0-based. The parent is index/2, and the left-child is index*2.
* The right child is index*2+1.
1998-10-15 23:42:56 +00:00
*/
#define heap_parent(i) ((i) >> 1)
#define heap_left(i) ((i) << 1)
/*@}*/
1998-10-15 23:42:56 +00:00
#define SIZE_INCREMENT 1024
1998-10-15 23:42:56 +00:00
#define HEAP_MAGIC ISC_MAGIC('H', 'E', 'A', 'P')
#define VALID_HEAP(h) ISC_MAGIC_VALID(h, HEAP_MAGIC)
1998-10-15 23:42:56 +00:00
/*%
* When the heap is in a consistent state, the following invariant
* holds true: for every element i > 1, heap_parent(i) has a priority
* higher than or equal to that of i.
*/
#define HEAPCONDITION(i) \
((i) == 1 || \
!heap->compare(heap->array[(i)], heap->array[heap_parent(i)]))
/*% ISC heap structure. */
1998-10-21 01:13:50 +00:00
struct isc_heap {
unsigned int magic;
isc_mem_t * mctx;
unsigned int size;
unsigned int size_increment;
unsigned int last;
void ** array;
isc_heapcompare_t compare;
isc_heapindex_t index;
1998-10-15 23:42:56 +00:00
};
#ifdef ISC_HEAP_CHECK
static void
heap_check(isc_heap_t *heap)
{
unsigned int i;
for (i = 1; i <= heap->last; i++) {
INSIST(HEAPCONDITION(i));
}
}
#else /* ifdef ISC_HEAP_CHECK */
#define heap_check(x) (void)0
#endif /* ifdef ISC_HEAP_CHECK */
1998-10-26 23:08:23 +00:00
isc_result_t
isc_heap_create(isc_mem_t *mctx, isc_heapcompare_t compare, isc_heapindex_t idx,
unsigned int size_increment, isc_heap_t **heapp)
1998-10-15 23:42:56 +00:00
{
1998-12-13 23:45:21 +00:00
isc_heap_t *heap;
1998-10-15 23:42:56 +00:00
1998-10-16 01:18:31 +00:00
REQUIRE(heapp != NULL && *heapp == NULL);
1998-10-21 01:13:50 +00:00
REQUIRE(compare != NULL);
1998-10-15 23:42:56 +00:00
2001-11-27 01:56:32 +00:00
heap = isc_mem_get(mctx, sizeof(*heap));
1998-10-16 01:18:31 +00:00
heap->magic = HEAP_MAGIC;
heap->size = 0;
heap->mctx = NULL;
isc_mem_attach(mctx, &heap->mctx);
if (size_increment == 0) {
1998-10-16 01:18:31 +00:00
heap->size_increment = SIZE_INCREMENT;
} else {
1998-10-16 01:18:31 +00:00
heap->size_increment = size_increment;
}
1998-10-16 01:18:31 +00:00
heap->last = 0;
heap->array = NULL;
1998-10-21 01:13:50 +00:00
heap->compare = compare;
heap->index = idx;
1998-10-15 23:42:56 +00:00
1998-10-16 01:18:31 +00:00
*heapp = heap;
1998-10-15 23:42:56 +00:00
return (ISC_R_SUCCESS);
}
void
isc_heap_destroy(isc_heap_t **heapp)
{
1998-12-13 23:45:21 +00:00
isc_heap_t *heap;
1998-10-15 23:42:56 +00:00
1998-10-16 01:18:31 +00:00
REQUIRE(heapp != NULL);
heap = *heapp;
*heapp = NULL;
1998-10-16 01:18:31 +00:00
REQUIRE(VALID_HEAP(heap));
1998-10-15 23:42:56 +00:00
if (heap->array != NULL) {
1998-10-21 22:01:08 +00:00
isc_mem_put(heap->mctx, heap->array,
2001-11-27 01:56:32 +00:00
heap->size * sizeof(void *));
}
1998-10-16 01:18:31 +00:00
heap->magic = 0;
isc_mem_putanddetach(&heap->mctx, heap, sizeof(*heap));
1998-10-15 23:42:56 +00:00
}
static bool
resize(isc_heap_t *heap)
{
void ** new_array;
unsigned int new_size;
1998-10-15 23:42:56 +00:00
1998-10-16 01:18:31 +00:00
REQUIRE(VALID_HEAP(heap));
1998-10-15 23:42:56 +00:00
1998-10-16 01:18:31 +00:00
new_size = heap->size + heap->size_increment;
2001-11-27 01:56:32 +00:00
new_array = isc_mem_get(heap->mctx, new_size * sizeof(void *));
1998-10-16 01:54:25 +00:00
if (heap->array != NULL) {
memmove(new_array, heap->array, heap->size * sizeof(void *));
isc_mem_put(heap->mctx, heap->array,
2001-11-27 01:56:32 +00:00
heap->size * sizeof(void *));
1998-10-16 01:54:25 +00:00
}
1998-10-16 01:18:31 +00:00
heap->size = new_size;
heap->array = new_array;
1998-10-15 23:42:56 +00:00
return (true);
1998-10-15 23:42:56 +00:00
}
static void
float_up(isc_heap_t *heap, unsigned int i, void *elt)
{
1998-10-15 23:42:56 +00:00
unsigned int p;
for (p = heap_parent(i); i > 1 && heap->compare(elt, heap->array[p]);
i = p, p = heap_parent(i)) {
1998-10-16 01:18:31 +00:00
heap->array[i] = heap->array[p];
if (heap->index != NULL) {
1998-10-16 01:18:31 +00:00
(heap->index)(heap->array[i], i);
}
1998-10-15 23:42:56 +00:00
}
1998-10-16 01:18:31 +00:00
heap->array[i] = elt;
if (heap->index != NULL) {
1998-10-16 01:18:31 +00:00
(heap->index)(heap->array[i], i);
}
INSIST(HEAPCONDITION(i));
heap_check(heap);
1998-10-15 23:42:56 +00:00
}
static void
sink_down(isc_heap_t *heap, unsigned int i, void *elt)
{
1998-10-15 23:42:56 +00:00
unsigned int j, size, half_size;
1998-10-16 01:18:31 +00:00
size = heap->last;
1998-10-15 23:42:56 +00:00
half_size = size / 2;
while (i <= half_size) {
/* Find the smallest of the (at most) two children. */
1998-10-15 23:42:56 +00:00
j = heap_left(i);
if (j < size &&
heap->compare(heap->array[j + 1], heap->array[j])) {
1998-10-15 23:42:56 +00:00
j++;
}
if (heap->compare(elt, heap->array[j])) {
1998-10-15 23:42:56 +00:00
break;
}
1998-10-16 01:18:31 +00:00
heap->array[i] = heap->array[j];
if (heap->index != NULL) {
1998-10-16 01:18:31 +00:00
(heap->index)(heap->array[i], i);
}
1998-10-15 23:42:56 +00:00
i = j;
}
1998-10-16 01:18:31 +00:00
heap->array[i] = elt;
if (heap->index != NULL) {
1998-10-16 01:18:31 +00:00
(heap->index)(heap->array[i], i);
}
INSIST(HEAPCONDITION(i));
heap_check(heap);
1998-10-15 23:42:56 +00:00
}
1998-10-26 23:08:23 +00:00
isc_result_t
isc_heap_insert(isc_heap_t *heap, void *elt)
{
unsigned int new_last;
1998-10-15 23:42:56 +00:00
1998-10-16 01:18:31 +00:00
REQUIRE(VALID_HEAP(heap));
1998-10-15 23:42:56 +00:00
heap_check(heap);
new_last = heap->last + 1;
RUNTIME_CHECK(new_last > 0); /* overflow check */
if (new_last >= heap->size && !resize(heap)) {
1998-10-15 23:42:56 +00:00
return (ISC_R_NOMEMORY);
}
heap->last = new_last;
float_up(heap, new_last, elt);
1998-10-15 23:42:56 +00:00
return (ISC_R_SUCCESS);
}
void
isc_heap_delete(isc_heap_t *heap, unsigned int idx)
{
1998-10-15 23:42:56 +00:00
void *elt;
bool less;
1998-10-15 23:42:56 +00:00
1998-10-16 01:18:31 +00:00
REQUIRE(VALID_HEAP(heap));
REQUIRE(idx >= 1 && idx <= heap->last);
1998-10-15 23:42:56 +00:00
heap_check(heap);
if (heap->index != NULL) {
(heap->index)(heap->array[idx], 0);
}
if (idx == heap->last) {
heap->array[heap->last] = NULL;
heap->last--;
heap_check(heap);
} else {
elt = heap->array[heap->last];
heap->array[heap->last] = NULL;
heap->last--;
less = heap->compare(elt, heap->array[idx]);
heap->array[idx] = elt;
if (less) {
float_up(heap, idx, heap->array[idx]);
} else {
sink_down(heap, idx, heap->array[idx]);
}
}
1998-10-15 23:42:56 +00:00
}
void
isc_heap_increased(isc_heap_t *heap, unsigned int idx)
{
1998-10-16 01:18:31 +00:00
REQUIRE(VALID_HEAP(heap));
REQUIRE(idx >= 1 && idx <= heap->last);
float_up(heap, idx, heap->array[idx]);
1998-10-15 23:42:56 +00:00
}
void
isc_heap_decreased(isc_heap_t *heap, unsigned int idx)
{
1998-10-16 01:18:31 +00:00
REQUIRE(VALID_HEAP(heap));
REQUIRE(idx >= 1 && idx <= heap->last);
sink_down(heap, idx, heap->array[idx]);
1998-10-15 23:42:56 +00:00
}
void *
isc_heap_element(isc_heap_t *heap, unsigned int idx)
{
1998-10-16 01:18:31 +00:00
REQUIRE(VALID_HEAP(heap));
REQUIRE(idx >= 1);
1998-10-15 23:42:56 +00:00
heap_check(heap);
if (idx <= heap->last) {
return (heap->array[idx]);
}
return (NULL);
1998-10-15 23:42:56 +00:00
}
void
isc_heap_foreach(isc_heap_t *heap, isc_heapaction_t action, void *uap)
{
1998-10-15 23:42:56 +00:00
unsigned int i;
1998-10-16 01:18:31 +00:00
REQUIRE(VALID_HEAP(heap));
1998-10-15 23:42:56 +00:00
REQUIRE(action != NULL);
for (i = 1; i <= heap->last; i++) {
1998-10-16 01:18:31 +00:00
(action)(heap->array[i], uap);
}
1998-10-15 23:42:56 +00:00
}