2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-23 10:39:16 +00:00
bind/lib/isc/heap.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

279 lines
6.2 KiB
C
Raw Permalink Normal View History

1998-10-15 23:42:56 +00:00
/*
2013-12-05 15:04:53 +11:00
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
1998-12-12 20:48:14 +00:00
*
1998-10-15 23:42:56 +00:00
* SPDX-License-Identifier: MPL-2.0
*
1998-10-15 23:42:56 +00:00
* 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 https://mozilla.org/MPL/2.0/.
*
1998-10-15 23:42:56 +00:00
* 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/overflow.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
*/
1998-10-15 23:51:14 +00:00
#define heap_parent(i) ((i) >> 1)
#define heap_left(i) ((i) << 1)
/*@}*/
1998-10-15 23:42:56 +00:00
1998-10-15 23:51:14 +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 {
1998-10-15 23:42:56 +00:00
unsigned int magic;
1998-12-18 19:14:37 +00:00
isc_mem_t *mctx;
1998-10-15 23:51:14 +00:00
unsigned int size;
unsigned int size_increment;
unsigned int last;
void **array;
1998-10-21 01:13:50 +00:00
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 */
void
1998-12-18 19:14:37 +00:00
isc_heap_create(isc_mem_t *mctx, isc_heapcompare_t compare, isc_heapindex_t idx,
unsigned int size_increment, 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 && *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);
1998-10-15 23:51:14 +00:00
if (size_increment == 0) {
1998-10-16 01:18:31 +00:00
heap->size_increment = SIZE_INCREMENT;
1998-10-15 23:42:56 +00:00
} 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
}
void
1998-12-13 23:45:21 +00:00
isc_heap_destroy(isc_heap_t **heapp) {
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
1998-10-16 01:18:31 +00:00
if (heap->array != NULL) {
2023-08-23 08:56:31 +02:00
isc_mem_cput(heap->mctx, heap->array, 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 void
1998-12-13 23:45:21 +00:00
resize(isc_heap_t *heap) {
unsigned int new_size, new_bytes, old_bytes;
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
new_size = ISC_CHECKED_ADD(heap->size, heap->size_increment);
new_bytes = ISC_CHECKED_MUL(new_size, sizeof(void *));
old_bytes = ISC_CHECKED_MUL(heap->size, sizeof(void *));
1998-10-16 01:18:31 +00:00
heap->size = new_size;
2023-08-23 08:56:31 +02:00
heap->array = isc_mem_creget(heap->mctx, heap->array, old_bytes,
new_bytes, sizeof(char));
1998-10-15 23:42:56 +00:00
}
static void
1998-12-13 23:45:21 +00:00
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) {
(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) {
(heap->index)(heap->array[i], i);
}
INSIST(HEAPCONDITION(i));
heap_check(heap);
1998-10-15 23:42:56 +00:00
}
static void
1998-12-13 23:45:21 +00:00
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);
1998-10-21 01:13:50 +00:00
if (j < size &&
2022-11-02 19:33:14 +01:00
heap->compare(heap->array[j + 1], heap->array[j]))
{
1998-10-15 23:42:56 +00:00
j++;
}
1998-10-21 01:13:50 +00:00
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) {
(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) {
(heap->index)(heap->array[i], i);
}
INSIST(HEAPCONDITION(i));
heap_check(heap);
1998-10-15 23:42:56 +00:00
}
void
1998-12-13 23:45:21 +00:00
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);
}
heap->last = new_last;
float_up(heap, new_last, elt);
1998-10-15 23:42:56 +00:00
}
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
1998-12-13 23:45:21 +00:00
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
}