summaryrefslogtreecommitdiffstats
path: root/lib/bind/isc/heap.c
blob: bea7678df36282dd89f381b1fd847e3a95489ead (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
 * Copyright (c) 1997,1999 by Internet Software Consortium.
 *
 * 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.
 *
 * 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.
 */

/*%
 * 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.
 */

#if !defined(LINT) && !defined(CODECENTER)
static const char rcsid[] = "$Id: heap.c,v 1.2.18.2 2006/03/10 00:20:08 marka Exp $";
#endif /* not lint */

#include "port_before.h"

#include <stddef.h>
#include <stdlib.h>
#include <errno.h>

#include "port_after.h"

#include <isc/heap.h>

/*%
 * 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.
 */
#define heap_parent(i) ((i) >> 1)
#define heap_left(i) ((i) << 1)

#define ARRAY_SIZE_INCREMENT 512

heap_context
heap_new(heap_higher_priority_func higher_priority, heap_index_func index,
	 int array_size_increment) {
	heap_context ctx;

	if (higher_priority == NULL)
		return (NULL);

	ctx = (heap_context)malloc(sizeof (struct heap_context));
	if (ctx == NULL)
		return (NULL);

	ctx->array_size = 0;
	if (array_size_increment == 0)
		ctx->array_size_increment = ARRAY_SIZE_INCREMENT;
	else
		ctx->array_size_increment = array_size_increment;
	ctx->heap_size = 0;
	ctx->heap = NULL;
	ctx->higher_priority = higher_priority;
	ctx->index = index;
	return (ctx);
}

int
heap_free(heap_context ctx) {
	if (ctx == NULL) {
		errno = EINVAL;
		return (-1);
	}

	if (ctx->heap != NULL)
		free(ctx->heap);
	free(ctx);

	return (0);
}

static int
heap_resize(heap_context ctx) {
	void **new_heap;

	ctx->array_size += ctx->array_size_increment;
	new_heap = (void **)realloc(ctx->heap,
				    (ctx->array_size) * (sizeof (void *)));
	if (new_heap == NULL) {
		errno = ENOMEM;
		return (-1);
	}
	ctx->heap = new_heap;
	return (0);
}

static void
float_up(heap_context ctx, int i, void *elt) {
	int p;

	for ( p = heap_parent(i); 
	      i > 1 && ctx->higher_priority(elt, ctx->heap[p]);
	      i = p, p = heap_parent(i) ) {
		ctx->heap[i] = ctx->heap[p];
		if (ctx->index != NULL)
			(ctx->index)(ctx->heap[i], i);
	}
	ctx->heap[i] = elt;
	if (ctx->index != NULL)
		(ctx->index)(ctx->heap[i], i);
}

static void
sink_down(heap_context ctx, int i, void *elt) {
	int j, size, half_size;

	size = ctx->heap_size;
	half_size = size / 2;
	while (i <= half_size) {
		/* find smallest of the (at most) two children */
		j = heap_left(i);
		if (j < size && ctx->higher_priority(ctx->heap[j+1],
						     ctx->heap[j]))
			j++;
		if (ctx->higher_priority(elt, ctx->heap[j]))
			break;
		ctx->heap[i] = ctx->heap[j];
		if (ctx->index != NULL)
			(ctx->index)(ctx->heap[i], i);
		i = j;
	}
	ctx->heap[i] = elt;
	if (ctx->index != NULL)
		(ctx->index)(ctx->heap[i], i);
}

int
heap_insert(heap_context ctx, void *elt) {
	int i;

	if (ctx == NULL || elt == NULL) {
		errno = EINVAL;
		return (-1);
	}

	i = ++ctx->heap_size;
	if (ctx->heap_size >= ctx->array_size && heap_resize(ctx) < 0)
		return (-1);
	
	float_up(ctx, i, elt);

	return (0);
}

int
heap_delete(heap_context ctx, int i) {
	void *elt;
	int less;

	if (ctx == NULL || i < 1 || i > ctx->heap_size) {
		errno = EINVAL;
		return (-1);
	}

	if (i == ctx->heap_size) {
		ctx->heap_size--;
	} else {
		elt = ctx->heap[ctx->heap_size--];
		less = ctx->higher_priority(elt, ctx->heap[i]);
		ctx->heap[i] = elt;
		if (less)
			float_up(ctx, i, ctx->heap[i]);
		else
			sink_down(ctx, i, ctx->heap[i]);
	}

	return (0);
}

int
heap_increased(heap_context ctx, int i) {
     	if (ctx == NULL || i < 1 || i > ctx->heap_size) {
		errno = EINVAL;
		return (-1);
	}
	
	float_up(ctx, i, ctx->heap[i]);

	return (0);
}

int
heap_decreased(heap_context ctx, int i) {
     	if (ctx == NULL || i < 1 || i > ctx->heap_size) {
		errno = EINVAL;
		return (-1);
	}
	
	sink_down(ctx, i, ctx->heap[i]);

	return (0);
}

void *
heap_element(heap_context ctx, int i) {
	if (ctx == NULL || i < 1 || i > ctx->heap_size) {
		errno = EINVAL;
		return (NULL);
	}

	return (ctx->heap[i]);
}

int
heap_for_each(heap_context ctx, heap_for_each_func action, void *uap) {
	int i;

	if (ctx == NULL || action == NULL) {
		errno = EINVAL;
		return (-1);
	}

	for (i = 1; i <= ctx->heap_size; i++)
		(action)(ctx->heap[i], uap);
	return (0);
}

/*! \file */
OpenPOWER on IntegriCloud