1 /* Produced by texiweb from libavl.w on 2003/01/06 at 18:07. */
3 /* libavl - library for manipulation of binary trees.
4 Copyright (C) 1998-2002 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 The author may be contacted at <blp@gnu.org> on the Internet, or
22 write to Ben Pfaff, Stanford University, Computer Science Dept., 353
23 Serra Mall, Stanford CA 94305, USA.
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)
39 struct avl_table *tree;
41 assert (compare != NULL);
43 if (allocator == NULL)
44 allocator = &avl_allocator_default;
46 tree = allocator->libavl_malloc (allocator, sizeof *tree);
50 tree->avl_root = NULL;
51 tree->avl_compare = compare;
52 tree->avl_param = param;
53 tree->avl_alloc = allocator;
55 tree->avl_generation = 0;
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)
64 const struct avl_node *p;
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);
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)
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. */
93 unsigned char da[AVL_MAX_HEIGHT]; /* Cached comparison results. */
94 int k = 0; /* Number of cached results. */
96 assert (tree != NULL && item != NULL);
98 z = (struct avl_node *) &tree->avl_root;
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);
106 if (p->avl_balance != 0)
108 da[k++] = dir = cmp > 0;
111 n = q->avl_link[dir] =
112 tree->avl_alloc->libavl_malloc (tree->avl_alloc, sizeof *n);
118 n->avl_link[0] = n->avl_link[1] = NULL;
123 for (p = y, k = 0; p != n; p = p->avl_link[da[k]], k++)
129 if (y->avl_balance == -2) {
130 struct avl_node *x = y->avl_link[0];
131 if (x->avl_balance == -1) {
133 y->avl_link[0] = x->avl_link[1];
135 x->avl_balance = y->avl_balance = 0;
137 assert (x->avl_balance == +1);
139 x->avl_link[1] = w->avl_link[0];
141 y->avl_link[0] = w->avl_link[1];
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;
151 } else if (y->avl_balance == +2) {
152 struct avl_node *x = y->avl_link[1];
153 if (x->avl_balance == +1) {
155 y->avl_link[1] = x->avl_link[0];
157 x->avl_balance = y->avl_balance = 0;
159 assert (x->avl_balance == -1);
161 x->avl_link[0] = w->avl_link[1];
163 y->avl_link[1] = w->avl_link[0];
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;
175 z->avl_link[y != z->avl_link[0]] = w;
177 tree->avl_generation++;
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)
187 void **p = avl_probe (table, item);
188 return p == NULL || *p == item ? NULL : *p;
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)
197 void **p = avl_probe (table, item);
198 if (p == NULL || *p == item)
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)
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. */
216 struct avl_node *p; /* Traverses tree to find node to delete. */
217 int cmp; /* Result of comparison between |item| and |p|. */
219 assert (tree != NULL && item != NULL);
222 p = (struct avl_node *) &tree->avl_root;
223 for (cmp = -1; cmp != 0;
224 cmp = tree->avl_compare (item, p->avl_data, tree->avl_param)) {
230 p = p->avl_link[dir];
236 if (p->avl_link[1] == NULL)
237 pa[k - 1]->avl_link[da[k - 1]] = p->avl_link[0];
239 struct avl_node *r = p->avl_link[1];
240 if (r->avl_link[0] == NULL) {
241 r->avl_link[0] = p->avl_link[0];
242 r->avl_balance = p->avl_balance;
243 pa[k - 1]->avl_link[da[k - 1]] = r;
254 if (s->avl_link[0] == NULL)
260 s->avl_link[0] = p->avl_link[0];
261 r->avl_link[0] = s->avl_link[1];
262 s->avl_link[1] = p->avl_link[1];
263 s->avl_balance = p->avl_balance;
265 pa[j - 1]->avl_link[da[j - 1]] = s;
271 tree->avl_alloc->libavl_free (tree->avl_alloc, p);
275 struct avl_node *y = pa[k];
279 if (y->avl_balance == +1)
281 else if (y->avl_balance == +2) {
282 struct avl_node *x = y->avl_link[1];
283 if (x->avl_balance == -1) {
285 assert (x->avl_balance == -1);
287 x->avl_link[0] = w->avl_link[1];
289 y->avl_link[1] = w->avl_link[0];
291 if (w->avl_balance == +1)
292 x->avl_balance = 0, y->avl_balance = -1;
293 else if (w->avl_balance == 0)
294 x->avl_balance = y->avl_balance = 0;
295 else /* |w->avl_balance == -1| */
296 x->avl_balance = +1, y->avl_balance = 0;
298 pa[k - 1]->avl_link[da[k - 1]] = w;
300 y->avl_link[1] = x->avl_link[0];
302 pa[k - 1]->avl_link[da[k - 1]] = x;
303 if (x->avl_balance == 0) {
308 x->avl_balance = y->avl_balance = 0;
313 if (y->avl_balance == -1)
315 else if (y->avl_balance == -2) {
316 struct avl_node *x = y->avl_link[0];
317 if (x->avl_balance == +1) {
319 assert (x->avl_balance == +1);
321 x->avl_link[1] = w->avl_link[0];
323 y->avl_link[0] = w->avl_link[1];
325 if (w->avl_balance == -1)
326 x->avl_balance = 0, y->avl_balance = +1;
327 else if (w->avl_balance == 0)
328 x->avl_balance = y->avl_balance = 0;
329 else /* |w->avl_balance == +1| */
330 x->avl_balance = -1, y->avl_balance = 0;
332 pa[k - 1]->avl_link[da[k - 1]] = w;
334 y->avl_link[0] = x->avl_link[1];
336 pa[k - 1]->avl_link[da[k - 1]] = x;
337 if (x->avl_balance == 0) {
342 x->avl_balance = y->avl_balance = 0;
349 tree->avl_generation++;
350 return (void *) item;
353 /* Refreshes the stack of parent pointers in |trav|
354 and updates its generation number. */
355 static void trav_refresh (struct avl_traverser *trav)
357 assert (trav != NULL);
359 trav->avl_generation = trav->avl_table->avl_generation;
361 if (trav->avl_node != NULL) {
362 avl_comparison_func *cmp = trav->avl_table->avl_compare;
363 void *param = trav->avl_table->avl_param;
364 struct avl_node *node = trav->avl_node;
367 trav->avl_height = 0;
368 for (i = trav->avl_table->avl_root; i != node;) {
369 assert (trav->avl_height < AVL_MAX_HEIGHT);
372 trav->avl_stack[trav->avl_height++] = i;
373 i = i->avl_link[cmp (node->avl_data, i->avl_data, param) > 0];
378 /* Initializes |trav| for use with |tree|
379 and selects the null node. */
380 void avl_t_init (struct avl_traverser *trav, struct avl_table *tree)
382 trav->avl_table = tree;
383 trav->avl_node = NULL;
384 trav->avl_height = 0;
385 trav->avl_generation = tree->avl_generation;
388 /* Initializes |trav| for |tree|
389 and selects and returns a pointer to its least-valued item.
390 Returns |NULL| if |tree| contains no nodes. */
391 void *avl_t_first (struct avl_traverser *trav, struct avl_table *tree)
395 assert (tree != NULL && trav != NULL);
397 trav->avl_table = tree;
398 trav->avl_height = 0;
399 trav->avl_generation = tree->avl_generation;
403 while (x->avl_link[0] != NULL) {
404 assert (trav->avl_height < AVL_MAX_HEIGHT);
405 trav->avl_stack[trav->avl_height++] = x;
410 return x != NULL ? x->avl_data : NULL;
413 /* Initializes |trav| for |tree|
414 and selects and returns a pointer to its greatest-valued item.
415 Returns |NULL| if |tree| contains no nodes. */
416 void *avl_t_last (struct avl_traverser *trav, struct avl_table *tree)
420 assert (tree != NULL && trav != NULL);
422 trav->avl_table = tree;
423 trav->avl_height = 0;
424 trav->avl_generation = tree->avl_generation;
428 while (x->avl_link[1] != NULL) {
429 assert (trav->avl_height < AVL_MAX_HEIGHT);
430 trav->avl_stack[trav->avl_height++] = x;
435 return x != NULL ? x->avl_data : NULL;
438 /* Searches for |item| in |tree|.
439 If found, initializes |trav| to the item found and returns the item
441 If there is no matching item, initializes |trav| to the null item
442 and returns |NULL|. */
443 void *avl_t_find (struct avl_traverser *trav, struct avl_table *tree,
446 struct avl_node *p, *q;
448 assert (trav != NULL && tree != NULL && item != NULL);
449 trav->avl_table = tree;
450 trav->avl_height = 0;
451 trav->avl_generation = tree->avl_generation;
452 for (p = tree->avl_root; p != NULL; p = q) {
453 int cmp = tree->avl_compare (item, p->avl_data, tree->avl_param);
459 else { /* |cmp == 0| */
465 assert (trav->avl_height < AVL_MAX_HEIGHT);
466 trav->avl_stack[trav->avl_height++] = p;
469 trav->avl_height = 0;
470 trav->avl_node = NULL;
474 /* Attempts to insert |item| into |tree|.
475 If |item| is inserted successfully, it is returned and |trav| is
476 initialized to its location.
477 If a duplicate is found, it is returned and |trav| is initialized to
478 its location. No replacement of the item occurs.
479 If a memory allocation failure occurs, |NULL| is returned and |trav|
480 is initialized to the null item. */
481 void *avl_t_insert (struct avl_traverser *trav, struct avl_table *tree,
486 assert (trav != NULL && tree != NULL && item != NULL);
488 p = avl_probe (tree, item);
490 trav->avl_table = tree;
491 trav->avl_node = ((struct avl_node *)
492 ((char *) p - offsetof (struct avl_node, avl_data)));
493 trav->avl_generation = tree->avl_generation - 1;
496 avl_t_init (trav, tree);
501 /* Initializes |trav| to have the same current node as |src|. */
502 void *avl_t_copy (struct avl_traverser *trav, const struct avl_traverser *src)
504 assert (trav != NULL && src != NULL);
507 trav->avl_table = src->avl_table;
508 trav->avl_node = src->avl_node;
509 trav->avl_generation = src->avl_generation;
510 if (trav->avl_generation == trav->avl_table->avl_generation) {
511 trav->avl_height = src->avl_height;
512 memcpy (trav->avl_stack, (const void *) src->avl_stack,
513 sizeof *trav->avl_stack * trav->avl_height);
517 return trav->avl_node != NULL ? trav->avl_node->avl_data : NULL;
520 /* Returns the next data item in inorder
521 within the tree being traversed with |trav|,
522 or if there are no more data items returns |NULL|. */
523 void *avl_t_next (struct avl_traverser *trav)
527 assert (trav != NULL);
529 if (trav->avl_generation != trav->avl_table->avl_generation)
534 return avl_t_first (trav, trav->avl_table);
535 } else if (x->avl_link[1] != NULL) {
536 assert (trav->avl_height < AVL_MAX_HEIGHT);
537 trav->avl_stack[trav->avl_height++] = x;
540 while (x->avl_link[0] != NULL) {
541 assert (trav->avl_height < AVL_MAX_HEIGHT);
542 trav->avl_stack[trav->avl_height++] = x;
549 if (trav->avl_height == 0) {
550 trav->avl_node = NULL;
555 x = trav->avl_stack[--trav->avl_height];
557 while (y == x->avl_link[1]);
564 /* Returns the previous data item in inorder
565 within the tree being traversed with |trav|,
566 or if there are no more data items returns |NULL|. */
567 void *avl_t_prev (struct avl_traverser *trav)
571 assert (trav != NULL);
573 if (trav->avl_generation != trav->avl_table->avl_generation)
578 return avl_t_last (trav, trav->avl_table);
579 } else if (x->avl_link[0] != NULL) {
580 assert (trav->avl_height < AVL_MAX_HEIGHT);
581 trav->avl_stack[trav->avl_height++] = x;
584 while (x->avl_link[1] != NULL) {
585 assert (trav->avl_height < AVL_MAX_HEIGHT);
586 trav->avl_stack[trav->avl_height++] = x;
593 if (trav->avl_height == 0) {
594 trav->avl_node = NULL;
599 x = trav->avl_stack[--trav->avl_height];
601 while (y == x->avl_link[0]);
608 /* Returns |trav|'s current item. */
609 void *avl_t_cur (struct avl_traverser *trav)
611 assert (trav != NULL);
613 return trav->avl_node != NULL ? trav->avl_node->avl_data : NULL;
616 /* Replaces the current item in |trav| by |new| and returns the item replaced.
617 |trav| must not have the null item selected.
618 The new item must not upset the ordering of the tree. */
619 void *avl_t_replace (struct avl_traverser *trav, void *new)
623 assert (trav != NULL && trav->avl_node != NULL && new != NULL);
624 old = trav->avl_node->avl_data;
625 trav->avl_node->avl_data = new;
630 copy_error_recovery (struct avl_node **stack, int height,
631 struct avl_table *new, avl_item_func * destroy)
633 assert (stack != NULL && height >= 0 && new != NULL);
635 for (; height > 2; height -= 2)
636 stack[height - 1]->avl_link[1] = NULL;
637 avl_destroy (new, destroy);
640 /* Copies |org| to a newly created tree, which is returned.
641 If |copy != NULL|, each data item in |org| is first passed to |copy|,
642 and the return values are inserted into the tree,
643 with |NULL| return values taken as indications of failure.
644 On failure, destroys the partially created new tree,
645 applying |destroy|, if non-null, to each item in the new tree so far,
647 If |allocator != NULL|, it is used for allocation in the new tree.
648 Otherwise, the same allocator used for |org| is used. */
649 struct avl_table *avl_copy (const struct avl_table *org, avl_copy_func * copy,
650 avl_item_func * destroy,
651 struct libavl_allocator *allocator)
653 struct avl_node *stack[2 * (AVL_MAX_HEIGHT + 1)];
656 struct avl_table *new;
657 const struct avl_node *x;
660 assert (org != NULL);
661 new = avl_create (org->avl_compare, org->avl_param,
662 allocator != NULL ? allocator : org->avl_alloc);
665 new->avl_count = org->avl_count;
666 if (new->avl_count == 0)
669 x = (const struct avl_node *) &org->avl_root;
670 y = (struct avl_node *) &new->avl_root;
672 while (x->avl_link[0] != NULL) {
673 assert (height < 2 * (AVL_MAX_HEIGHT + 1));
676 new->avl_alloc->libavl_malloc (new->avl_alloc,
677 sizeof *y->avl_link[0]);
678 if (y->avl_link[0] == NULL) {
679 if (y != (struct avl_node *) &new->avl_root) {
681 y->avl_link[1] = NULL;
684 copy_error_recovery (stack, height, new, destroy);
688 stack[height++] = (struct avl_node *) x;
693 y->avl_link[0] = NULL;
696 y->avl_balance = x->avl_balance;
698 y->avl_data = x->avl_data;
700 y->avl_data = copy (x->avl_data, org->avl_param);
701 if (y->avl_data == NULL) {
702 y->avl_link[1] = NULL;
703 copy_error_recovery (stack, height, new, destroy);
708 if (x->avl_link[1] != NULL) {
710 new->avl_alloc->libavl_malloc (new->avl_alloc,
711 sizeof *y->avl_link[1]);
712 if (y->avl_link[1] == NULL) {
713 copy_error_recovery (stack, height, new, destroy);
721 y->avl_link[1] = NULL;
732 /* Frees storage allocated for |tree|.
733 If |destroy != NULL|, applies it to each data item in inorder. */
734 void avl_destroy (struct avl_table *tree, avl_item_func * destroy)
736 struct avl_node *p, *q;
738 assert (tree != NULL);
740 for (p = tree->avl_root; p != NULL; p = q)
741 if (p->avl_link[0] == NULL) {
743 if (destroy != NULL && p->avl_data != NULL)
744 destroy (p->avl_data, tree->avl_param);
745 tree->avl_alloc->libavl_free (tree->avl_alloc, p);
748 p->avl_link[0] = q->avl_link[1];
752 tree->avl_alloc->libavl_free (tree->avl_alloc, tree);
755 /* Allocates |size| bytes of space using |malloc()|.
756 Returns a null pointer if allocation fails. */
757 void *avl_malloc (struct libavl_allocator *allocator, size_t size)
759 assert (allocator != NULL && size > 0);
760 return malloc (size);
764 void avl_free (struct libavl_allocator *allocator, void *block)
766 assert (allocator != NULL && block != NULL);
770 /* Default memory allocator that uses |malloc()| and |free()|. */
771 struct libavl_allocator avl_allocator_default = {
779 /* Asserts that |avl_insert()| succeeds at inserting |item| into |table|. */
781 (avl_assert_insert) (struct avl_table * table, void *item) {
782 void **p = avl_probe (table, item);
783 assert (p != NULL && *p == item);
786 /* Asserts that |avl_delete()| really removes |item| from |table|,
787 and returns the removed item. */
788 void *(avl_assert_delete) (struct avl_table * table, void *item) {
789 void *p = avl_delete (table, item);