| 1 |
/* Produced by texiweb from libavl.w. */ |
| 2 |
|
| 3 |
/* libavl - library for manipulation of binary trees. |
| 4 |
Copyright (C) 2004-2014 Han The Thanh, <[email protected]> |
| 5 |
Modified for pdfTeX, contact pdfTeX maintainers with any problems or |
| 6 |
questions, not the original authors. |
| 7 |
|
| 8 |
Copyright (C) 1998-2002, 2004 Free Software Foundation, Inc. |
| 9 |
|
| 10 |
This program is free software; you can redistribute it and/or |
| 11 |
modify it under the terms of the GNU General Public License as |
| 12 |
published by the Free Software Foundation; either version 2 of the |
| 13 |
License, or (at your option) any later version. |
| 14 |
|
| 15 |
This program is distributed in the hope that it will be useful, but |
| 16 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 18 |
See the GNU General Public License for more details. |
| 19 |
|
| 20 |
You should have received a copy of the GNU General Public License |
| 21 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 |
|
| 23 |
Original author: Ben Pfaff. |
| 24 |
*/ |
| 25 |
|
| 26 |
#include <assert.h> |
| 27 |
#include <stdio.h> |
| 28 |
#include <stdlib.h> |
| 29 |
#include <string.h> |
| 30 |
#include "avl.h" |
| 31 |
|
| 32 |
/* Creates and returns a new table |
| 33 |
with comparison function |compare| using parameter |param| |
| 34 |
and memory allocator |allocator|. |
| 35 |
Returns |NULL| if memory allocation failed. */ |
| 36 |
struct avl_table *avl_create(avl_comparison_func * compare, void *param, |
| 37 |
struct libavl_allocator *allocator) |
| 38 |
{ |
| 39 |
struct avl_table *tree; |
| 40 |
|
| 41 |
assert(compare != NULL); |
| 42 |
|
| 43 |
if (allocator == NULL) |
| 44 |
allocator = &avl_allocator_default; |
| 45 |
|
| 46 |
tree = allocator->libavl_malloc(allocator, sizeof *tree); |
| 47 |
if (tree == NULL) |
| 48 |
return NULL; |
| 49 |
|
| 50 |
tree->avl_root = NULL; |
| 51 |
tree->avl_compare = compare; |
| 52 |
tree->avl_param = param; |
| 53 |
tree->avl_alloc = allocator; |
| 54 |
tree->avl_count = 0; |
| 55 |
tree->avl_generation = 0; |
| 56 |
|
| 57 |
return tree; |
| 58 |
} |
| 59 |
|
| 60 |
/* Search |tree| for an item matching |item|, and return it if found. |
| 61 |
Otherwise return |NULL|. */ |
| 62 |
void *avl_find(const struct avl_table *tree, const void *item) |
| 63 |
{ |
| 64 |
const struct avl_node *p; |
| 65 |
|
| 66 |
assert(tree != NULL && item != NULL); |
| 67 |
for (p = tree->avl_root; p != NULL;) { |
| 68 |
int cmp = tree->avl_compare(item, p->avl_data, tree->avl_param); |
| 69 |
|
| 70 |
if (cmp < 0) |
| 71 |
p = p->avl_link[0]; |
| 72 |
else if (cmp > 0) |
| 73 |
p = p->avl_link[1]; |
| 74 |
else /* |cmp == 0| */ |
| 75 |
return p->avl_data; |
| 76 |
} |
| 77 |
|
| 78 |
return NULL; |
| 79 |
} |
| 80 |
|
| 81 |
/* Inserts |item| into |tree| and returns a pointer to |item|'s address. |
| 82 |
If a duplicate item is found in the tree, |
| 83 |
returns a pointer to the duplicate without inserting |item|. |
| 84 |
Returns |NULL| in case of memory allocation failure. */ |
| 85 |
void **avl_probe(struct avl_table *tree, void *item) |
| 86 |
{ |
| 87 |
struct avl_node *y, *z; /* Top node to update balance factor, and parent. */ |
| 88 |
struct avl_node *p, *q; /* Iterator, and parent. */ |
| 89 |
struct avl_node *n; /* Newly inserted node. */ |
| 90 |
struct avl_node *w; /* New root of rebalanced subtree. */ |
| 91 |
int dir; /* Direction to descend. */ |
| 92 |
|
| 93 |
unsigned char da[AVL_MAX_HEIGHT]; /* Cached comparison results. */ |
| 94 |
int k = 0; /* Number of cached results. */ |
| 95 |
|
| 96 |
assert(tree != NULL && item != NULL); |
| 97 |
|
| 98 |
z = (struct avl_node *) &tree->avl_root; |
| 99 |
y = tree->avl_root; |
| 100 |
dir = 0; |
| 101 |
for (q = z, p = y; p != NULL; q = p, p = p->avl_link[dir]) { |
| 102 |
int cmp = tree->avl_compare(item, p->avl_data, tree->avl_param); |
| 103 |
if (cmp == 0) |
| 104 |
return &p->avl_data; |
| 105 |
|
| 106 |
if (p->avl_balance != 0) |
| 107 |
z = q, y = p, k = 0; |
| 108 |
da[k++] = dir = cmp > 0; |
| 109 |
} |
| 110 |
|
| 111 |
n = q->avl_link[dir] = |
| 112 |
tree->avl_alloc->libavl_malloc(tree->avl_alloc, sizeof *n); |
| 113 |
if (n == NULL) |
| 114 |
return NULL; |
| 115 |
|
| 116 |
tree->avl_count++; |
| 117 |
n->avl_data = item; |
| 118 |
n->avl_link[0] = n->avl_link[1] = NULL; |
| 119 |
n->avl_balance = 0; |
| 120 |
if (y == NULL) |
| 121 |
return &n->avl_data; |
| 122 |
|
| 123 |
for (p = y, k = 0; p != n; p = p->avl_link[da[k]], k++) |
| 124 |
if (da[k] == 0) |
| 125 |
p->avl_balance--; |
| 126 |
else |
| 127 |
p->avl_balance++; |
| 128 |
|
| 129 |
if (y->avl_balance == -2) { |
| 130 |
struct avl_node *x = y->avl_link[0]; |
| 131 |
if (x->avl_balance == -1) { |
| 132 |
w = x; |
| 133 |
y->avl_link[0] = x->avl_link[1]; |
| 134 |
x->avl_link[1] = y; |
| 135 |
x->avl_balance = y->avl_balance = 0; |
| 136 |
} else { |
| 137 |
assert(x->avl_balance == +1); |
| 138 |
w = x->avl_link[1]; |
| 139 |
x->avl_link[1] = w->avl_link[0]; |
| 140 |
w->avl_link[0] = x; |
| 141 |
y->avl_link[0] = w->avl_link[1]; |
| 142 |
w->avl_link[1] = y; |
| 143 |
if (w->avl_balance == -1) |
| 144 |
x->avl_balance = 0, y->avl_balance = +1; |
| 145 |
else if (w->avl_balance == 0) |
| 146 |
x->avl_balance = y->avl_balance = 0; |
| 147 |
else /* |w->avl_balance == +1| */ |
| 148 |
x->avl_balance = -1, y->avl_balance = 0; |
| 149 |
w->avl_balance = 0; |
| 150 |
} |
| 151 |
} else if (y->avl_balance == +2) { |
| 152 |
struct avl_node *x = y->avl_link[1]; |
| 153 |
if (x->avl_balance == +1) { |
| 154 |
w = x; |
| 155 |
y->avl_link[1] = x->avl_link[0]; |
| 156 |
x->avl_link[0] = y; |
| 157 |
x->avl_balance = y->avl_balance = 0; |
| 158 |
} else { |
| 159 |
assert(x->avl_balance == -1); |
| 160 |
w = x->avl_link[0]; |
| 161 |
x->avl_link[0] = w->avl_link[1]; |
| 162 |
w->avl_link[1] = x; |
| 163 |
y->avl_link[1] = w->avl_link[0]; |
| 164 |
w->avl_link[0] = y; |
| 165 |
if (w->avl_balance == +1) |
| 166 |
x->avl_balance = 0, y->avl_balance = -1; |
| 167 |
else if (w->avl_balance == 0) |
| 168 |
x->avl_balance = y->avl_balance = 0; |
| 169 |
else /* |w->avl_balance == -1| */ |
| 170 |
x->avl_balance = +1, y->avl_balance = 0; |
| 171 |
w->avl_balance = 0; |
| 172 |
} |
| 173 |
} else |
| 174 |
return &n->avl_data; |
| 175 |
z->avl_link[y != z->avl_link[0]] = w; |
| 176 |
|
| 177 |
tree->avl_generation++; |
| 178 |
return &n->avl_data; |
| 179 |
} |
| 180 |
|
| 181 |
/* Inserts |item| into |table|. |
| 182 |
Returns |NULL| if |item| was successfully inserted |
| 183 |
or if a memory allocation error occurred. |
| 184 |
Otherwise, returns the duplicate item. */ |
| 185 |
void *avl_insert(struct avl_table *table, void *item) |
| 186 |
{ |
| 187 |
void **p = avl_probe(table, item); |
| 188 |
return p == NULL || *p == item ? NULL : *p; |
| 189 |
} |
| 190 |
|
| 191 |
/* Inserts |item| into |table|, replacing any duplicate item. |
| 192 |
Returns |NULL| if |item| was inserted without replacing a duplicate, |
| 193 |
or if a memory allocation error occurred. |
| 194 |
Otherwise, returns the item that was replaced. */ |
| 195 |
void *avl_replace(struct avl_table *table, void *item) |
| 196 |
{ |
| 197 |
void **p = avl_probe(table, item); |
| 198 |
if (p == NULL || *p == item) |
| 199 |
return NULL; |
| 200 |
else { |
| 201 |
void *r = *p; |
| 202 |
*p = item; |
| 203 |
return r; |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
/* Deletes from |tree| and returns an item matching |item|. |
| 208 |
Returns a null pointer if no matching item found. */ |
| 209 |
void *avl_delete(struct avl_table *tree, const void *item) |
| 210 |
{ |
| 211 |
/* Stack of nodes. */ |
| 212 |
struct avl_node *pa[AVL_MAX_HEIGHT]; /* Nodes. */ |
| 213 |
unsigned char da[AVL_MAX_HEIGHT]; /* |avl_link[]| indexes. */ |
| 214 |
int k; /* Stack pointer. */ |
| 215 |
|
| 216 |
struct avl_node *p; /* Traverses tree to find node to delete. */ |
| 217 |
int cmp; /* Result of comparison between |item| and |p|. */ |
| 218 |
|
| 219 |
void *res; |
| 220 |
|
| 221 |
assert(tree != NULL && item != NULL); |
| 222 |
|
| 223 |
k = 0; |
| 224 |
p = (struct avl_node *) &tree->avl_root; |
| 225 |
for (cmp = -1; cmp != 0; |
| 226 |
cmp = tree->avl_compare(item, p->avl_data, tree->avl_param)) { |
| 227 |
int dir = cmp > 0; |
| 228 |
|
| 229 |
pa[k] = p; |
| 230 |
da[k++] = dir; |
| 231 |
|
| 232 |
p = p->avl_link[dir]; |
| 233 |
if (p == NULL) |
| 234 |
return NULL; |
| 235 |
} |
| 236 |
res = p->avl_data; |
| 237 |
|
| 238 |
if (p->avl_link[1] == NULL) |
| 239 |
pa[k - 1]->avl_link[da[k - 1]] = p->avl_link[0]; |
| 240 |
else { |
| 241 |
struct avl_node *r = p->avl_link[1]; |
| 242 |
if (r->avl_link[0] == NULL) { |
| 243 |
r->avl_link[0] = p->avl_link[0]; |
| 244 |
r->avl_balance = p->avl_balance; |
| 245 |
pa[k - 1]->avl_link[da[k - 1]] = r; |
| 246 |
da[k] = 1; |
| 247 |
pa[k++] = r; |
| 248 |
} else { |
| 249 |
struct avl_node *s; |
| 250 |
int j = k++; |
| 251 |
|
| 252 |
for (;;) { |
| 253 |
da[k] = 0; |
| 254 |
pa[k++] = r; |
| 255 |
s = r->avl_link[0]; |
| 256 |
if (s->avl_link[0] == NULL) |
| 257 |
break; |
| 258 |
|
| 259 |
r = s; |
| 260 |
} |
| 261 |
|
| 262 |
s->avl_link[0] = p->avl_link[0]; |
| 263 |
r->avl_link[0] = s->avl_link[1]; |
| 264 |
s->avl_link[1] = p->avl_link[1]; |
| 265 |
s->avl_balance = p->avl_balance; |
| 266 |
|
| 267 |
pa[j - 1]->avl_link[da[j - 1]] = s; |
| 268 |
da[j] = 1; |
| 269 |
pa[j] = s; |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
tree->avl_alloc->libavl_free(tree->avl_alloc, p); |
| 274 |
|
| 275 |
assert(k > 0); |
| 276 |
while (--k > 0) { |
| 277 |
struct avl_node *y = pa[k]; |
| 278 |
|
| 279 |
if (da[k] == 0) { |
| 280 |
y->avl_balance++; |
| 281 |
if (y->avl_balance == +1) |
| 282 |
break; |
| 283 |
else if (y->avl_balance == +2) { |
| 284 |
struct avl_node *x = y->avl_link[1]; |
| 285 |
if (x->avl_balance == -1) { |
| 286 |
struct avl_node *w; |
| 287 |
assert(x->avl_balance == -1); |
| 288 |
w = x->avl_link[0]; |
| 289 |
x->avl_link[0] = w->avl_link[1]; |
| 290 |
w->avl_link[1] = x; |
| 291 |
y->avl_link[1] = w->avl_link[0]; |
| 292 |
w->avl_link[0] = y; |
| 293 |
if (w->avl_balance == +1) |
| 294 |
x->avl_balance = 0, y->avl_balance = -1; |
| 295 |
else if (w->avl_balance == 0) |
| 296 |
x->avl_balance = y->avl_balance = 0; |
| 297 |
else /* |w->avl_balance == -1| */ |
| 298 |
x->avl_balance = +1, y->avl_balance = 0; |
| 299 |
w->avl_balance = 0; |
| 300 |
pa[k - 1]->avl_link[da[k - 1]] = w; |
| 301 |
} else { |
| 302 |
y->avl_link[1] = x->avl_link[0]; |
| 303 |
x->avl_link[0] = y; |
| 304 |
pa[k - 1]->avl_link[da[k - 1]] = x; |
| 305 |
if (x->avl_balance == 0) { |
| 306 |
x->avl_balance = -1; |
| 307 |
y->avl_balance = +1; |
| 308 |
break; |
| 309 |
} else |
| 310 |
x->avl_balance = y->avl_balance = 0; |
| 311 |
} |
| 312 |
} |
| 313 |
} else { |
| 314 |
y->avl_balance--; |
| 315 |
if (y->avl_balance == -1) |
| 316 |
break; |
| 317 |
else if (y->avl_balance == -2) { |
| 318 |
struct avl_node *x = y->avl_link[0]; |
| 319 |
if (x->avl_balance == +1) { |
| 320 |
struct avl_node *w; |
| 321 |
assert(x->avl_balance == +1); |
| 322 |
w = x->avl_link[1]; |
| 323 |
x->avl_link[1] = w->avl_link[0]; |
| 324 |
w->avl_link[0] = x; |
| 325 |
y->avl_link[0] = w->avl_link[1]; |
| 326 |
w->avl_link[1] = y; |
| 327 |
if (w->avl_balance == -1) |
| 328 |
x->avl_balance = 0, y->avl_balance = +1; |
| 329 |
else if (w->avl_balance == 0) |
| 330 |
x->avl_balance = y->avl_balance = 0; |
| 331 |
else /* |w->avl_balance == +1| */ |
| 332 |
x->avl_balance = -1, y->avl_balance = 0; |
| 333 |
w->avl_balance = 0; |
| 334 |
pa[k - 1]->avl_link[da[k - 1]] = w; |
| 335 |
} else { |
| 336 |
y->avl_link[0] = x->avl_link[1]; |
| 337 |
x->avl_link[1] = y; |
| 338 |
pa[k - 1]->avl_link[da[k - 1]] = x; |
| 339 |
if (x->avl_balance == 0) { |
| 340 |
x->avl_balance = +1; |
| 341 |
y->avl_balance = -1; |
| 342 |
break; |
| 343 |
} else |
| 344 |
x->avl_balance = y->avl_balance = 0; |
| 345 |
} |
| 346 |
} |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
tree->avl_count--; |
| 351 |
tree->avl_generation++; |
| 352 |
return res; |
| 353 |
} |
| 354 |
|
| 355 |
/* Refreshes the stack of parent pointers in |trav| |
| 356 |
and updates its generation number. */ |
| 357 |
static void trav_refresh(struct avl_traverser *trav) |
| 358 |
{ |
| 359 |
assert(trav != NULL); |
| 360 |
|
| 361 |
trav->avl_generation = trav->avl_table->avl_generation; |
| 362 |
|
| 363 |
if (trav->avl_node != NULL) { |
| 364 |
avl_comparison_func *cmp = trav->avl_table->avl_compare; |
| 365 |
void *param = trav->avl_table->avl_param; |
| 366 |
struct avl_node *node = trav->avl_node; |
| 367 |
struct avl_node *i; |
| 368 |
|
| 369 |
trav->avl_height = 0; |
| 370 |
for (i = trav->avl_table->avl_root; i != node;) { |
| 371 |
assert(trav->avl_height < AVL_MAX_HEIGHT); |
| 372 |
assert(i != NULL); |
| 373 |
|
| 374 |
trav->avl_stack[trav->avl_height++] = i; |
| 375 |
i = i->avl_link[cmp(node->avl_data, i->avl_data, param) > 0]; |
| 376 |
} |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
/* Initializes |trav| for use with |tree| |
| 381 |
and selects the null node. */ |
| 382 |
void avl_t_init(struct avl_traverser *trav, struct avl_table *tree) |
| 383 |
{ |
| 384 |
trav->avl_table = tree; |
| 385 |
trav->avl_node = NULL; |
| 386 |
trav->avl_height = 0; |
| 387 |
trav->avl_generation = tree->avl_generation; |
| 388 |
} |
| 389 |
|
| 390 |
/* Initializes |trav| for |tree| |
| 391 |
and selects and returns a pointer to its least-valued item. |
| 392 |
Returns |NULL| if |tree| contains no nodes. */ |
| 393 |
void *avl_t_first(struct avl_traverser *trav, struct avl_table *tree) |
| 394 |
{ |
| 395 |
struct avl_node *x; |
| 396 |
|
| 397 |
assert(tree != NULL && trav != NULL); |
| 398 |
|
| 399 |
trav->avl_table = tree; |
| 400 |
trav->avl_height = 0; |
| 401 |
trav->avl_generation = tree->avl_generation; |
| 402 |
|
| 403 |
x = tree->avl_root; |
| 404 |
if (x != NULL) |
| 405 |
while (x->avl_link[0] != NULL) { |
| 406 |
assert(trav->avl_height < AVL_MAX_HEIGHT); |
| 407 |
trav->avl_stack[trav->avl_height++] = x; |
| 408 |
x = x->avl_link[0]; |
| 409 |
} |
| 410 |
trav->avl_node = x; |
| 411 |
|
| 412 |
return x != NULL ? x->avl_data : NULL; |
| 413 |
} |
| 414 |
|
| 415 |
/* Initializes |trav| for |tree| |
| 416 |
and selects and returns a pointer to its greatest-valued item. |
| 417 |
Returns |NULL| if |tree| contains no nodes. */ |
| 418 |
void *avl_t_last(struct avl_traverser *trav, struct avl_table *tree) |
| 419 |
{ |
| 420 |
struct avl_node *x; |
| 421 |
|
| 422 |
assert(tree != NULL && trav != NULL); |
| 423 |
|
| 424 |
trav->avl_table = tree; |
| 425 |
trav->avl_height = 0; |
| 426 |
trav->avl_generation = tree->avl_generation; |
| 427 |
|
| 428 |
x = tree->avl_root; |
| 429 |
if (x != NULL) |
| 430 |
while (x->avl_link[1] != NULL) { |
| 431 |
assert(trav->avl_height < AVL_MAX_HEIGHT); |
| 432 |
trav->avl_stack[trav->avl_height++] = x; |
| 433 |
x = x->avl_link[1]; |
| 434 |
} |
| 435 |
trav->avl_node = x; |
| 436 |
|
| 437 |
return x != NULL ? x->avl_data : NULL; |
| 438 |
} |
| 439 |
|
| 440 |
/* Searches for |item| in |tree|. |
| 441 |
If found, initializes |trav| to the item found and returns the item |
| 442 |
as well. |
| 443 |
If there is no matching item, initializes |trav| to the null item |
| 444 |
and returns |NULL|. */ |
| 445 |
void *avl_t_find(struct avl_traverser *trav, struct avl_table *tree, void *item) |
| 446 |
{ |
| 447 |
struct avl_node *p, *q; |
| 448 |
|
| 449 |
assert(trav != NULL && tree != NULL && item != NULL); |
| 450 |
trav->avl_table = tree; |
| 451 |
trav->avl_height = 0; |
| 452 |
trav->avl_generation = tree->avl_generation; |
| 453 |
for (p = tree->avl_root; p != NULL; p = q) { |
| 454 |
int cmp = tree->avl_compare(item, p->avl_data, tree->avl_param); |
| 455 |
|
| 456 |
if (cmp < 0) |
| 457 |
q = p->avl_link[0]; |
| 458 |
else if (cmp > 0) |
| 459 |
q = p->avl_link[1]; |
| 460 |
else { /* |cmp == 0| */ |
| 461 |
|
| 462 |
trav->avl_node = p; |
| 463 |
return p->avl_data; |
| 464 |
} |
| 465 |
|
| 466 |
assert(trav->avl_height < AVL_MAX_HEIGHT); |
| 467 |
trav->avl_stack[trav->avl_height++] = p; |
| 468 |
} |
| 469 |
|
| 470 |
trav->avl_height = 0; |
| 471 |
trav->avl_node = NULL; |
| 472 |
return NULL; |
| 473 |
} |
| 474 |
|
| 475 |
/* Attempts to insert |item| into |tree|. |
| 476 |
If |item| is inserted successfully, it is returned and |trav| is |
| 477 |
initialized to its location. |
| 478 |
If a duplicate is found, it is returned and |trav| is initialized to |
| 479 |
its location. No replacement of the item occurs. |
| 480 |
If a memory allocation failure occurs, |NULL| is returned and |trav| |
| 481 |
is initialized to the null item. */ |
| 482 |
void *avl_t_insert(struct avl_traverser *trav, struct avl_table *tree, |
| 483 |
void *item) |
| 484 |
{ |
| 485 |
void **p; |
| 486 |
|
| 487 |
assert(trav != NULL && tree != NULL && item != NULL); |
| 488 |
|
| 489 |
p = avl_probe(tree, item); |
| 490 |
if (p != NULL) { |
| 491 |
trav->avl_table = tree; |
| 492 |
trav->avl_node = ((struct avl_node *) |
| 493 |
((char *) p - offsetof(struct avl_node, avl_data))); |
| 494 |
trav->avl_generation = tree->avl_generation - 1; |
| 495 |
return *p; |
| 496 |
} else { |
| 497 |
avl_t_init(trav, tree); |
| 498 |
return NULL; |
| 499 |
} |
| 500 |
} |
| 501 |
|
| 502 |
/* Initializes |trav| to have the same current node as |src|. */ |
| 503 |
void *avl_t_copy(struct avl_traverser *trav, const struct avl_traverser *src) |
| 504 |
{ |
| 505 |
assert(trav != NULL && src != NULL); |
| 506 |
|
| 507 |
if (trav != src) { |
| 508 |
trav->avl_table = src->avl_table; |
| 509 |
trav->avl_node = src->avl_node; |
| 510 |
trav->avl_generation = src->avl_generation; |
| 511 |
if (trav->avl_generation == trav->avl_table->avl_generation) { |
| 512 |
trav->avl_height = src->avl_height; |
| 513 |
memcpy(trav->avl_stack, (const void *) src->avl_stack, |
| 514 |
sizeof *trav->avl_stack * trav->avl_height); |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
return trav->avl_node != NULL ? trav->avl_node->avl_data : NULL; |
| 519 |
} |
| 520 |
|
| 521 |
/* Returns the next data item in inorder |
| 522 |
within the tree being traversed with |trav|, |
| 523 |
or if there are no more data items returns |NULL|. */ |
| 524 |
void *avl_t_next(struct avl_traverser *trav) |
| 525 |
{ |
| 526 |
struct avl_node *x; |
| 527 |
|
| 528 |
assert(trav != NULL); |
| 529 |
|
| 530 |
if (trav->avl_generation != trav->avl_table->avl_generation) |
| 531 |
trav_refresh(trav); |
| 532 |
|
| 533 |
x = trav->avl_node; |
| 534 |
if (x == NULL) { |
| 535 |
return avl_t_first(trav, trav->avl_table); |
| 536 |
} else if (x->avl_link[1] != NULL) { |
| 537 |
assert(trav->avl_height < AVL_MAX_HEIGHT); |
| 538 |
trav->avl_stack[trav->avl_height++] = x; |
| 539 |
x = x->avl_link[1]; |
| 540 |
|
| 541 |
while (x->avl_link[0] != NULL) { |
| 542 |
assert(trav->avl_height < AVL_MAX_HEIGHT); |
| 543 |
trav->avl_stack[trav->avl_height++] = x; |
| 544 |
x = x->avl_link[0]; |
| 545 |
} |
| 546 |
} else { |
| 547 |
struct avl_node *y; |
| 548 |
|
| 549 |
do { |
| 550 |
if (trav->avl_height == 0) { |
| 551 |
trav->avl_node = NULL; |
| 552 |
return NULL; |
| 553 |
} |
| 554 |
|
| 555 |
y = x; |
| 556 |
x = trav->avl_stack[--trav->avl_height]; |
| 557 |
} |
| 558 |
while (y == x->avl_link[1]); |
| 559 |
} |
| 560 |
trav->avl_node = x; |
| 561 |
|
| 562 |
return x->avl_data; |
| 563 |
} |
| 564 |
|
| 565 |
/* Returns the previous data item in inorder |
| 566 |
within the tree being traversed with |trav|, |
| 567 |
or if there are no more data items returns |NULL|. */ |
| 568 |
void *avl_t_prev(struct avl_traverser *trav) |
| 569 |
{ |
| 570 |
struct avl_node *x; |
| 571 |
|
| 572 |
assert(trav != NULL); |
| 573 |
|
| 574 |
if (trav->avl_generation != trav->avl_table->avl_generation) |
| 575 |
trav_refresh(trav); |
| 576 |
|
| 577 |
x = trav->avl_node; |
| 578 |
if (x == NULL) { |
| 579 |
return avl_t_last(trav, trav->avl_table); |
| 580 |
} else if (x->avl_link[0] != NULL) { |
| 581 |
assert(trav->avl_height < AVL_MAX_HEIGHT); |
| 582 |
trav->avl_stack[trav->avl_height++] = x; |
| 583 |
x = x->avl_link[0]; |
| 584 |
|
| 585 |
while (x->avl_link[1] != NULL) { |
| 586 |
assert(trav->avl_height < AVL_MAX_HEIGHT); |
| 587 |
trav->avl_stack[trav->avl_height++] = x; |
| 588 |
x = x->avl_link[1]; |
| 589 |
} |
| 590 |
} else { |
| 591 |
struct avl_node *y; |
| 592 |
|
| 593 |
do { |
| 594 |
if (trav->avl_height == 0) { |
| 595 |
trav->avl_node = NULL; |
| 596 |
return NULL; |
| 597 |
} |
| 598 |
|
| 599 |
y = x; |
| 600 |
x = trav->avl_stack[--trav->avl_height]; |
| 601 |
} |
| 602 |
while (y == x->avl_link[0]); |
| 603 |
} |
| 604 |
trav->avl_node = x; |
| 605 |
|
| 606 |
return x->avl_data; |
| 607 |
} |
| 608 |
|
| 609 |
/* Returns |trav|'s current item. */ |
| 610 |
void *avl_t_cur(struct avl_traverser *trav) |
| 611 |
{ |
| 612 |
assert(trav != NULL); |
| 613 |
|
| 614 |
return trav->avl_node != NULL ? trav->avl_node->avl_data : NULL; |
| 615 |
} |
| 616 |
|
| 617 |
/* Replaces the current item in |trav| by |new| and returns the item replaced. |
| 618 |
|trav| must not have the null item selected. |
| 619 |
The new item must not upset the ordering of the tree. */ |
| 620 |
void *avl_t_replace(struct avl_traverser *trav, void *new) |
| 621 |
{ |
| 622 |
void *old; |
| 623 |
|
| 624 |
assert(trav != NULL && trav->avl_node != NULL && new != NULL); |
| 625 |
old = trav->avl_node->avl_data; |
| 626 |
trav->avl_node->avl_data = new; |
| 627 |
return old; |
| 628 |
} |
| 629 |
|
| 630 |
/* Destroys |new| with |avl_destroy (new, destroy)|, |
| 631 |
first setting right links of nodes in |stack| within |new| |
| 632 |
to null pointers to avoid touching uninitialized data. */ |
| 633 |
static void |
| 634 |
copy_error_recovery(struct avl_node **stack, int height, |
| 635 |
struct avl_table *new, avl_item_func * destroy) |
| 636 |
{ |
| 637 |
assert(stack != NULL && height >= 0 && new != NULL); |
| 638 |
|
| 639 |
for (; height > 2; height -= 2) |
| 640 |
stack[height - 1]->avl_link[1] = NULL; |
| 641 |
avl_destroy(new, destroy); |
| 642 |
} |
| 643 |
|
| 644 |
/* Copies |org| to a newly created tree, which is returned. |
| 645 |
If |copy != NULL|, each data item in |org| is first passed to |copy|, |
| 646 |
and the return values are inserted into the tree, |
| 647 |
with |NULL| return values taken as indications of failure. |
| 648 |
On failure, destroys the partially created new tree, |
| 649 |
applying |destroy|, if non-null, to each item in the new tree so far, |
| 650 |
and returns |NULL|. |
| 651 |
If |allocator != NULL|, it is used for allocation in the new tree. |
| 652 |
Otherwise, the same allocator used for |org| is used. */ |
| 653 |
struct avl_table *avl_copy(const struct avl_table *org, avl_copy_func * copy, |
| 654 |
avl_item_func * destroy, |
| 655 |
struct libavl_allocator *allocator) |
| 656 |
{ |
| 657 |
struct avl_node *stack[2 * (AVL_MAX_HEIGHT + 1)]; |
| 658 |
int height = 0; |
| 659 |
|
| 660 |
struct avl_table *new; |
| 661 |
struct avl_node org_head, *x, *y; |
| 662 |
|
| 663 |
assert(org != NULL); |
| 664 |
new = avl_create(org->avl_compare, org->avl_param, |
| 665 |
allocator != NULL ? allocator : org->avl_alloc); |
| 666 |
if (new == NULL) |
| 667 |
return NULL; |
| 668 |
new->avl_count = org->avl_count; |
| 669 |
if (new->avl_count == 0) |
| 670 |
return new; |
| 671 |
|
| 672 |
org_head.avl_link[0] = (struct avl_node *) org->avl_root; |
| 673 |
x = &org_head; |
| 674 |
y = (struct avl_node *) &new->avl_root; |
| 675 |
for (;;) { |
| 676 |
while (x->avl_link[0] != NULL) { |
| 677 |
assert(height < 2 * (AVL_MAX_HEIGHT + 1)); |
| 678 |
|
| 679 |
y->avl_link[0] = |
| 680 |
new->avl_alloc->libavl_malloc(new->avl_alloc, |
| 681 |
sizeof *y->avl_link[0]); |
| 682 |
if (y->avl_link[0] == NULL) { |
| 683 |
if (y != (struct avl_node *) &new->avl_root) { |
| 684 |
y->avl_data = NULL; |
| 685 |
y->avl_link[1] = NULL; |
| 686 |
} |
| 687 |
|
| 688 |
copy_error_recovery(stack, height, new, destroy); |
| 689 |
return NULL; |
| 690 |
} |
| 691 |
|
| 692 |
stack[height++] = x; |
| 693 |
stack[height++] = y; |
| 694 |
x = x->avl_link[0]; |
| 695 |
y = y->avl_link[0]; |
| 696 |
} |
| 697 |
y->avl_link[0] = NULL; |
| 698 |
|
| 699 |
for (;;) { |
| 700 |
y->avl_balance = x->avl_balance; |
| 701 |
if (copy == NULL) |
| 702 |
y->avl_data = x->avl_data; |
| 703 |
else { |
| 704 |
y->avl_data = copy(x->avl_data, org->avl_param); |
| 705 |
if (y->avl_data == NULL) { |
| 706 |
y->avl_link[1] = NULL; |
| 707 |
copy_error_recovery(stack, height, new, destroy); |
| 708 |
return NULL; |
| 709 |
} |
| 710 |
} |
| 711 |
|
| 712 |
if (x->avl_link[1] != NULL) { |
| 713 |
y->avl_link[1] = |
| 714 |
new->avl_alloc->libavl_malloc(new->avl_alloc, |
| 715 |
sizeof *y->avl_link[1]); |
| 716 |
if (y->avl_link[1] == NULL) { |
| 717 |
copy_error_recovery(stack, height, new, destroy); |
| 718 |
return NULL; |
| 719 |
} |
| 720 |
|
| 721 |
x = x->avl_link[1]; |
| 722 |
y = y->avl_link[1]; |
| 723 |
break; |
| 724 |
} else |
| 725 |
y->avl_link[1] = NULL; |
| 726 |
|
| 727 |
if (height <= 2) |
| 728 |
return new; |
| 729 |
|
| 730 |
y = stack[--height]; |
| 731 |
x = stack[--height]; |
| 732 |
} |
| 733 |
} |
| 734 |
} |
| 735 |
|
| 736 |
/* Frees storage allocated for |tree|. |
| 737 |
If |destroy != NULL|, applies it to each data item in inorder. */ |
| 738 |
void avl_destroy(struct avl_table *tree, avl_item_func * destroy) |
| 739 |
{ |
| 740 |
struct avl_node *p, *q; |
| 741 |
|
| 742 |
assert(tree != NULL); |
| 743 |
|
| 744 |
for (p = tree->avl_root; p != NULL; p = q) |
| 745 |
if (p->avl_link[0] == NULL) { |
| 746 |
q = p->avl_link[1]; |
| 747 |
if (destroy != NULL && p->avl_data != NULL) |
| 748 |
destroy(p->avl_data, tree->avl_param); |
| 749 |
tree->avl_alloc->libavl_free(tree->avl_alloc, p); |
| 750 |
} else { |
| 751 |
q = p->avl_link[0]; |
| 752 |
p->avl_link[0] = q->avl_link[1]; |
| 753 |
q->avl_link[1] = p; |
| 754 |
} |
| 755 |
|
| 756 |
tree->avl_alloc->libavl_free(tree->avl_alloc, tree); |
| 757 |
} |
| 758 |
|
| 759 |
/* Allocates |size| bytes of space using |malloc()|. |
| 760 |
Returns a null pointer if allocation fails. */ |
| 761 |
void *avl_malloc(struct libavl_allocator *allocator, size_t size) |
| 762 |
{ |
| 763 |
assert(allocator != NULL && size > 0); |
| 764 |
return malloc(size); |
| 765 |
} |
| 766 |
|
| 767 |
/* Frees |block|. */ |
| 768 |
void avl_free(struct libavl_allocator *allocator, void *block) |
| 769 |
{ |
| 770 |
assert(allocator != NULL && block != NULL); |
| 771 |
free(block); |
| 772 |
} |
| 773 |
|
| 774 |
/* Default memory allocator that uses |malloc()| and |free()|. */ |
| 775 |
struct libavl_allocator avl_allocator_default = { |
| 776 |
avl_malloc, |
| 777 |
avl_free |
| 778 |
}; |
| 779 |
|
| 780 |
#undef NDEBUG |
| 781 |
#include <assert.h> |
| 782 |
|
| 783 |
/* Asserts that |avl_insert()| succeeds at inserting |item| into |table|. */ |
| 784 |
void |
| 785 |
(avl_assert_insert) (struct avl_table * table, void *item) { |
| 786 |
void **p = avl_probe(table, item); |
| 787 |
assert(p != NULL && *p == item); |
| 788 |
} |
| 789 |
|
| 790 |
/* Asserts that |avl_delete()| really removes |item| from |table|, |
| 791 |
and returns the removed item. */ |
| 792 |
void *(avl_assert_delete) (struct avl_table * table, void *item) { |
| 793 |
void *p = avl_delete(table, item); |
| 794 |
assert(p != NULL); |
| 795 |
return p; |
| 796 |
} |