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

253 lines
6.0 KiB
C
Raw Normal View History

1998-10-15 23:42:56 +00:00
/*
2004-03-05 05:14:21 +00:00
* Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
2001-01-09 22:01:04 +00:00
* Copyright (C) 1997-2001 Internet Software Consortium.
*
1998-10-15 23:42:56 +00:00
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
2004-03-05 05:14:21 +00:00
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
1998-10-15 23:42:56 +00:00
*/
2004-03-05 05:14:21 +00:00
/* $Id: heap.c,v 1.30 2004/03/05 05:10:45 marka Exp $ */
2000-06-22 22:00:42 +00:00
1998-10-15 23:42:56 +00:00
/*
* Heap implementation of priority queues adapted from the following:
*
* _Introduction to Algorithms_, Cormen, Leiserson, and Rivest,
* MIT Press / McGraw Hill, 1990, ISBN 0-262-03141-8, chapter 7.
*
* _Algorithms_, Second Edition, Sedgewick, Addison-Wesley, 1988,
* ISBN 0-201-06673-4, chapter 11.
*/
1998-12-12 19:25:20 +00:00
#include <config.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 memcpy. */
2000-04-28 01:12:23 +00:00
#include <isc/util.h>
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.
*/
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)]))
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
};
1998-10-26 23:08:23 +00:00
isc_result_t
1998-12-18 19:14:37 +00:00
isc_heap_create(isc_mem_t *mctx, isc_heapcompare_t compare,
1998-10-21 22:01:08 +00:00
isc_heapindex_t index, unsigned int size_increment,
1998-12-13 23:45:21 +00:00
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
if (heap == NULL)
1998-10-15 23:42:56 +00:00
return (ISC_R_NOMEMORY);
1998-10-16 01:18:31 +00:00
heap->magic = HEAP_MAGIC;
1998-10-16 01:54:25 +00:00
heap->mctx = mctx;
1998-10-16 01:18:31 +00:00
heap->size = 0;
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;
heap->last = 0;
heap->array = NULL;
1998-10-21 01:13:50 +00:00
heap->compare = compare;
1998-10-16 01:18:31 +00:00
heap->index = index;
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
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;
REQUIRE(VALID_HEAP(heap));
1998-10-15 23:42:56 +00:00
1998-10-16 01:18:31 +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;
2001-11-27 01:56:32 +00:00
isc_mem_put(heap->mctx, heap, sizeof(*heap));
1998-10-15 23:42:56 +00:00
1998-10-16 01:18:31 +00:00
*heapp = NULL;
1998-10-15 23:42:56 +00:00
}
1998-10-21 01:13:50 +00:00
static isc_boolean_t
1998-12-13 23:45:21 +00:00
resize(isc_heap_t *heap) {
1998-10-15 23:51:14 +00:00
void **new_array;
1998-10-15 23:42:56 +00:00
size_t new_size;
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-15 23:51:14 +00:00
if (new_array == NULL)
1998-10-21 01:13:50 +00:00
return (ISC_FALSE);
1998-10-16 01:54:25 +00:00
if (heap->array != NULL) {
2001-11-27 01:56:32 +00:00
memcpy(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
1998-10-21 01:13:50 +00:00
return (ISC_TRUE);
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));
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 && 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));
1998-10-15 23:42:56 +00:00
}
1998-10-26 23:08:23 +00:00
isc_result_t
1998-12-13 23:45:21 +00:00
isc_heap_insert(isc_heap_t *heap, void *elt) {
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
1998-10-16 01:18:31 +00:00
i = ++heap->last;
if (heap->last >= heap->size && !resize(heap))
1998-10-15 23:42:56 +00:00
return (ISC_R_NOMEMORY);
1998-10-16 01:18:31 +00:00
float_up(heap, i, elt);
1998-10-15 23:42:56 +00:00
return (ISC_R_SUCCESS);
}
void
1998-12-13 23:45:21 +00:00
isc_heap_delete(isc_heap_t *heap, unsigned int i) {
1998-10-15 23:42:56 +00:00
void *elt;
isc_boolean_t less;
1998-10-15 23:42:56 +00:00
1998-10-16 01:18:31 +00:00
REQUIRE(VALID_HEAP(heap));
REQUIRE(i >= 1 && i <= heap->last);
1998-10-15 23:42:56 +00:00
if (i == heap->last) {
heap->last--;
} else {
elt = heap->array[heap->last--];
less = heap->compare(elt, heap->array[i]);
heap->array[i] = elt;
if (less)
float_up(heap, i, heap->array[i]);
else
sink_down(heap, i, heap->array[i]);
}
1998-10-15 23:42:56 +00:00
}
void
1998-12-13 23:45:21 +00:00
isc_heap_increased(isc_heap_t *heap, unsigned int i) {
1998-10-16 01:18:31 +00:00
REQUIRE(VALID_HEAP(heap));
REQUIRE(i >= 1 && i <= heap->last);
1998-10-16 01:18:31 +00:00
float_up(heap, i, heap->array[i]);
1998-10-15 23:42:56 +00:00
}
void
1998-12-13 23:45:21 +00:00
isc_heap_decreased(isc_heap_t *heap, unsigned int i) {
1998-10-16 01:18:31 +00:00
REQUIRE(VALID_HEAP(heap));
REQUIRE(i >= 1 && i <= heap->last);
1998-10-16 01:18:31 +00:00
sink_down(heap, i, heap->array[i]);
1998-10-15 23:42:56 +00:00
}
void *
1998-12-13 23:45:21 +00:00
isc_heap_element(isc_heap_t *heap, unsigned int i) {
1998-10-16 01:18:31 +00:00
REQUIRE(VALID_HEAP(heap));
REQUIRE(i >= 1 && i <= heap->last);
1998-10-15 23:42:56 +00:00
1998-10-16 01:18:31 +00:00
return (heap->array[i]);
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);
1998-10-16 01:18:31 +00:00
for (i = 1; i <= heap->last; i++)
(action)(heap->array[i], uap);
1998-10-15 23:42:56 +00:00
}