1 /* Produced by texiweb from libavl.w. */
3 /* libavl - library for manipulation of binary trees.
4 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004 Free Software
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 3 of the License, or (at your option) any later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
29 /* Creates and returns a new table
30 with comparison function |compare| using parameter |param|
31 and memory allocator |allocator|.
32 Returns |NULL| if memory allocation failed. */
34 avl_create (avl_comparison_func *compare, void *param,
35 struct libavl_allocator *allocator)
37 struct avl_table *tree;
39 assert (compare != NULL);
41 if (allocator == NULL)
42 allocator = &avl_allocator_default;
44 tree = allocator->libavl_malloc (allocator, sizeof *tree);
48 tree->avl_root = NULL;
49 tree->avl_compare = compare;
50 tree->avl_param = param;
51 tree->avl_alloc = allocator;
53 tree->avl_generation = 0;
58 /* Search |tree| for an item matching |item|, and return it if found.
59 Otherwise return |NULL|. */
61 avl_find (const struct avl_table *tree, const void *item)
63 const struct avl_node *p;
65 assert (tree != NULL && item != NULL);
66 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. */
86 avl_probe (struct avl_table *tree, void *item)
88 struct avl_node *y, *z; /* Top node to update balance factor, and parent. */
89 struct avl_node *p, *q; /* Iterator, and parent. */
90 struct avl_node *n; /* Newly inserted node. */
91 struct avl_node *w; /* New root of rebalanced subtree. */
92 int dir; /* Direction to descend. */
94 unsigned char da[AVL_MAX_HEIGHT]; /* Cached comparison results. */
95 int k = 0; /* Number of cached results. */
97 assert (tree != NULL && item != NULL);
99 z = (struct avl_node *) &tree->avl_root;
102 for (q = z, p = y; p != NULL; q = p, p = p->avl_link[dir])
104 int cmp = tree->avl_compare (item, p->avl_data, tree->avl_param);
108 if (p->avl_balance != 0)
110 da[k++] = dir = cmp > 0;
113 n = q->avl_link[dir] =
114 tree->avl_alloc->libavl_malloc (tree->avl_alloc, sizeof *n);
120 n->avl_link[0] = n->avl_link[1] = NULL;
125 for (p = y, k = 0; p != n; p = p->avl_link[da[k]], k++)
131 if (y->avl_balance == -2)
133 struct avl_node *x = y->avl_link[0];
134 if (x->avl_balance == -1)
137 y->avl_link[0] = x->avl_link[1];
139 x->avl_balance = y->avl_balance = 0;
143 assert (x->avl_balance == +1);
145 x->avl_link[1] = w->avl_link[0];
147 y->avl_link[0] = w->avl_link[1];
149 if (w->avl_balance == -1)
150 x->avl_balance = 0, y->avl_balance = +1;
151 else if (w->avl_balance == 0)
152 x->avl_balance = y->avl_balance = 0;
153 else /* |w->avl_balance == +1| */
154 x->avl_balance = -1, y->avl_balance = 0;
158 else if (y->avl_balance == +2)
160 struct avl_node *x = y->avl_link[1];
161 if (x->avl_balance == +1)
164 y->avl_link[1] = x->avl_link[0];
166 x->avl_balance = y->avl_balance = 0;
170 assert (x->avl_balance == -1);
172 x->avl_link[0] = w->avl_link[1];
174 y->avl_link[1] = w->avl_link[0];
176 if (w->avl_balance == +1)
177 x->avl_balance = 0, y->avl_balance = -1;
178 else if (w->avl_balance == 0)
179 x->avl_balance = y->avl_balance = 0;
180 else /* |w->avl_balance == -1| */
181 x->avl_balance = +1, y->avl_balance = 0;
187 z->avl_link[y != z->avl_link[0]] = w;
189 tree->avl_generation++;
193 /* Inserts |item| into |table|.
194 Returns |NULL| if |item| was successfully inserted
195 or if a memory allocation error occurred.
196 Otherwise, returns the duplicate item. */
198 avl_insert (struct avl_table *table, void *item)
200 void **p = avl_probe (table, item);
201 return p == NULL || *p == item ? NULL : *p;
204 /* Inserts |item| into |table|, replacing any duplicate item.
205 Returns |NULL| if |item| was inserted without replacing a duplicate,
206 or if a memory allocation error occurred.
207 Otherwise, returns the item that was replaced. */
209 avl_replace (struct avl_table *table, void *item)
211 void **p = avl_probe (table, item);
212 if (p == NULL || *p == item)
222 /* Deletes from |tree| and returns an item matching |item|.
223 Returns a null pointer if no matching item found. */
225 avl_delete (struct avl_table *tree, const void *item)
227 /* Stack of nodes. */
228 struct avl_node *pa[AVL_MAX_HEIGHT]; /* Nodes. */
229 unsigned char da[AVL_MAX_HEIGHT]; /* |avl_link[]| indexes. */
230 int k; /* Stack pointer. */
232 struct avl_node *p; /* Traverses tree to find node to delete. */
233 int cmp; /* Result of comparison between |item| and |p|. */
235 assert (tree != NULL && item != NULL);
238 p = (struct avl_node *) &tree->avl_root;
239 for (cmp = -1; cmp != 0;
240 cmp = tree->avl_compare (item, p->avl_data, tree->avl_param))
247 p = p->avl_link[dir];
253 if (p->avl_link[1] == NULL)
254 pa[k - 1]->avl_link[da[k - 1]] = p->avl_link[0];
257 struct avl_node *r = p->avl_link[1];
258 if (r->avl_link[0] == NULL)
260 r->avl_link[0] = p->avl_link[0];
261 r->avl_balance = p->avl_balance;
262 pa[k - 1]->avl_link[da[k - 1]] = r;
276 if (s->avl_link[0] == NULL)
282 s->avl_link[0] = p->avl_link[0];
283 r->avl_link[0] = s->avl_link[1];
284 s->avl_link[1] = p->avl_link[1];
285 s->avl_balance = p->avl_balance;
287 pa[j - 1]->avl_link[da[j - 1]] = s;
293 tree->avl_alloc->libavl_free (tree->avl_alloc, p);
298 struct avl_node *y = pa[k];
303 if (y->avl_balance == +1)
305 else if (y->avl_balance == +2)
307 struct avl_node *x = y->avl_link[1];
308 if (x->avl_balance == -1)
311 assert (x->avl_balance == -1);
313 x->avl_link[0] = w->avl_link[1];
315 y->avl_link[1] = w->avl_link[0];
317 if (w->avl_balance == +1)
318 x->avl_balance = 0, y->avl_balance = -1;
319 else if (w->avl_balance == 0)
320 x->avl_balance = y->avl_balance = 0;
321 else /* |w->avl_balance == -1| */
322 x->avl_balance = +1, y->avl_balance = 0;
324 pa[k - 1]->avl_link[da[k - 1]] = w;
328 y->avl_link[1] = x->avl_link[0];
330 pa[k - 1]->avl_link[da[k - 1]] = x;
331 if (x->avl_balance == 0)
338 x->avl_balance = y->avl_balance = 0;
345 if (y->avl_balance == -1)
347 else if (y->avl_balance == -2)
349 struct avl_node *x = y->avl_link[0];
350 if (x->avl_balance == +1)
353 assert (x->avl_balance == +1);
355 x->avl_link[1] = w->avl_link[0];
357 y->avl_link[0] = w->avl_link[1];
359 if (w->avl_balance == -1)
360 x->avl_balance = 0, y->avl_balance = +1;
361 else if (w->avl_balance == 0)
362 x->avl_balance = y->avl_balance = 0;
363 else /* |w->avl_balance == +1| */
364 x->avl_balance = -1, y->avl_balance = 0;
366 pa[k - 1]->avl_link[da[k - 1]] = w;
370 y->avl_link[0] = x->avl_link[1];
372 pa[k - 1]->avl_link[da[k - 1]] = x;
373 if (x->avl_balance == 0)
380 x->avl_balance = y->avl_balance = 0;
387 tree->avl_generation++;
388 return (void *) item;
391 /* Refreshes the stack of parent pointers in |trav|
392 and updates its generation number. */
394 trav_refresh (struct avl_traverser *trav)
396 assert (trav != NULL);
398 trav->avl_generation = trav->avl_table->avl_generation;
400 if (trav->avl_node != NULL)
402 avl_comparison_func *cmp = trav->avl_table->avl_compare;
403 void *param = trav->avl_table->avl_param;
404 struct avl_node *node = trav->avl_node;
407 trav->avl_height = 0;
408 for (i = trav->avl_table->avl_root; i != node; )
410 assert (trav->avl_height < AVL_MAX_HEIGHT);
413 trav->avl_stack[trav->avl_height++] = i;
414 i = i->avl_link[cmp (node->avl_data, i->avl_data, param) > 0];
419 /* Initializes |trav| for use with |tree|
420 and selects the null node. */
422 avl_t_init (struct avl_traverser *trav, struct avl_table *tree)
424 trav->avl_table = tree;
425 trav->avl_node = NULL;
426 trav->avl_height = 0;
427 trav->avl_generation = tree->avl_generation;
430 /* Initializes |trav| for |tree|
431 and selects and returns a pointer to its least-valued item.
432 Returns |NULL| if |tree| contains no nodes. */
434 avl_t_first (struct avl_traverser *trav, struct avl_table *tree)
438 assert (tree != NULL && trav != NULL);
440 trav->avl_table = tree;
441 trav->avl_height = 0;
442 trav->avl_generation = tree->avl_generation;
446 while (x->avl_link[0] != NULL)
448 assert (trav->avl_height < AVL_MAX_HEIGHT);
449 trav->avl_stack[trav->avl_height++] = x;
454 return x != NULL ? x->avl_data : NULL;
457 /* Initializes |trav| for |tree|
458 and selects and returns a pointer to its greatest-valued item.
459 Returns |NULL| if |tree| contains no nodes. */
461 avl_t_last (struct avl_traverser *trav, struct avl_table *tree)
465 assert (tree != NULL && trav != NULL);
467 trav->avl_table = tree;
468 trav->avl_height = 0;
469 trav->avl_generation = tree->avl_generation;
473 while (x->avl_link[1] != NULL)
475 assert (trav->avl_height < AVL_MAX_HEIGHT);
476 trav->avl_stack[trav->avl_height++] = x;
481 return x != NULL ? x->avl_data : NULL;
484 /* Searches for |item| in |tree|.
485 If found, initializes |trav| to the item found and returns the item
487 If there is no matching item, initializes |trav| to the null item
488 and returns |NULL|. */
490 avl_t_find (struct avl_traverser *trav, struct avl_table *tree, void *item)
492 struct avl_node *p, *q;
494 assert (trav != NULL && tree != NULL && item != NULL);
495 trav->avl_table = tree;
496 trav->avl_height = 0;
497 trav->avl_generation = tree->avl_generation;
498 for (p = tree->avl_root; p != NULL; p = q)
500 int cmp = tree->avl_compare (item, p->avl_data, tree->avl_param);
506 else /* |cmp == 0| */
512 assert (trav->avl_height < AVL_MAX_HEIGHT);
513 trav->avl_stack[trav->avl_height++] = p;
516 trav->avl_height = 0;
517 trav->avl_node = NULL;
521 /* Attempts to insert |item| into |tree|.
522 If |item| is inserted successfully, it is returned and |trav| is
523 initialized to its location.
524 If a duplicate is found, it is returned and |trav| is initialized to
525 its location. No replacement of the item occurs.
526 If a memory allocation failure occurs, |NULL| is returned and |trav|
527 is initialized to the null item. */
529 avl_t_insert (struct avl_traverser *trav, struct avl_table *tree, void *item)
533 assert (trav != NULL && tree != NULL && item != NULL);
535 p = avl_probe (tree, item);
538 trav->avl_table = tree;
541 ((char *) p - offsetof (struct avl_node, avl_data)));
542 trav->avl_generation = tree->avl_generation - 1;
547 avl_t_init (trav, tree);
552 /* Initializes |trav| to have the same current node as |src|. */
554 avl_t_copy (struct avl_traverser *trav, const struct avl_traverser *src)
556 assert (trav != NULL && src != NULL);
560 trav->avl_table = src->avl_table;
561 trav->avl_node = src->avl_node;
562 trav->avl_generation = src->avl_generation;
563 if (trav->avl_generation == trav->avl_table->avl_generation)
565 trav->avl_height = src->avl_height;
566 memcpy (trav->avl_stack, (const void *) src->avl_stack,
567 sizeof *trav->avl_stack * trav->avl_height);
571 return trav->avl_node != NULL ? trav->avl_node->avl_data : NULL;
574 /* Returns the next data item in inorder
575 within the tree being traversed with |trav|,
576 or if there are no more data items returns |NULL|. */
578 avl_t_next (struct avl_traverser *trav)
582 assert (trav != NULL);
584 if (trav->avl_generation != trav->avl_table->avl_generation)
590 return avl_t_first (trav, trav->avl_table);
592 else if (x->avl_link[1] != NULL)
594 assert (trav->avl_height < AVL_MAX_HEIGHT);
595 trav->avl_stack[trav->avl_height++] = x;
598 while (x->avl_link[0] != NULL)
600 assert (trav->avl_height < AVL_MAX_HEIGHT);
601 trav->avl_stack[trav->avl_height++] = x;
611 if (trav->avl_height == 0)
613 trav->avl_node = NULL;
618 x = trav->avl_stack[--trav->avl_height];
620 while (y == x->avl_link[1]);
627 /* Returns the previous data item in inorder
628 within the tree being traversed with |trav|,
629 or if there are no more data items returns |NULL|. */
631 avl_t_prev (struct avl_traverser *trav)
635 assert (trav != NULL);
637 if (trav->avl_generation != trav->avl_table->avl_generation)
643 return avl_t_last (trav, trav->avl_table);
645 else if (x->avl_link[0] != NULL)
647 assert (trav->avl_height < AVL_MAX_HEIGHT);
648 trav->avl_stack[trav->avl_height++] = x;
651 while (x->avl_link[1] != NULL)
653 assert (trav->avl_height < AVL_MAX_HEIGHT);
654 trav->avl_stack[trav->avl_height++] = x;
664 if (trav->avl_height == 0)
666 trav->avl_node = NULL;
671 x = trav->avl_stack[--trav->avl_height];
673 while (y == x->avl_link[0]);
680 /* Returns |trav|'s current item. */
682 avl_t_cur (struct avl_traverser *trav)
684 assert (trav != NULL);
686 return trav->avl_node != NULL ? trav->avl_node->avl_data : NULL;
689 /* Replaces the current item in |trav| by |new| and returns the item replaced.
690 |trav| must not have the null item selected.
691 The new item must not upset the ordering of the tree. */
693 avl_t_replace (struct avl_traverser *trav, void *new)
697 assert (trav != NULL && trav->avl_node != NULL && new != NULL);
698 old = trav->avl_node->avl_data;
699 trav->avl_node->avl_data = new;
703 /* Destroys |new| with |avl_destroy (new, destroy)|,
704 first setting right links of nodes in |stack| within |new|
705 to null pointers to avoid touching uninitialized data. */
707 copy_error_recovery (struct avl_node **stack, int height,
708 struct avl_table *new, avl_item_func *destroy)
710 assert (stack != NULL && height >= 0 && new != NULL);
712 for (; height > 2; height -= 2)
713 stack[height - 1]->avl_link[1] = NULL;
714 avl_destroy (new, destroy);
717 /* Copies |org| to a newly created tree, which is returned.
718 If |copy != NULL|, each data item in |org| is first passed to |copy|,
719 and the return values are inserted into the tree,
720 with |NULL| return values taken as indications of failure.
721 On failure, destroys the partially created new tree,
722 applying |destroy|, if non-null, to each item in the new tree so far,
724 If |allocator != NULL|, it is used for allocation in the new tree.
725 Otherwise, the same allocator used for |org| is used. */
727 avl_copy (const struct avl_table *org, avl_copy_func *copy,
728 avl_item_func *destroy, struct libavl_allocator *allocator)
730 struct avl_node *stack[2 * (AVL_MAX_HEIGHT + 1)];
733 struct avl_table *new;
734 const struct avl_node *x;
737 assert (org != NULL);
738 new = avl_create (org->avl_compare, org->avl_param,
739 allocator != NULL ? allocator : org->avl_alloc);
742 new->avl_count = org->avl_count;
743 if (new->avl_count == 0)
746 x = (const struct avl_node *) &org->avl_root;
747 y = (struct avl_node *) &new->avl_root;
750 while (x->avl_link[0] != NULL)
752 assert (height < 2 * (AVL_MAX_HEIGHT + 1));
755 new->avl_alloc->libavl_malloc (new->avl_alloc,
756 sizeof *y->avl_link[0]);
757 if (y->avl_link[0] == NULL)
759 if (y != (struct avl_node *) &new->avl_root)
762 y->avl_link[1] = NULL;
765 copy_error_recovery (stack, height, new, destroy);
769 stack[height++] = (struct avl_node *) x;
774 y->avl_link[0] = NULL;
778 y->avl_balance = x->avl_balance;
780 y->avl_data = x->avl_data;
783 y->avl_data = copy (x->avl_data, org->avl_param);
784 if (y->avl_data == NULL)
786 y->avl_link[1] = NULL;
787 copy_error_recovery (stack, height, new, destroy);
792 if (x->avl_link[1] != NULL)
795 new->avl_alloc->libavl_malloc (new->avl_alloc,
796 sizeof *y->avl_link[1]);
797 if (y->avl_link[1] == NULL)
799 copy_error_recovery (stack, height, new, destroy);
808 y->avl_link[1] = NULL;
819 /* Frees storage allocated for |tree|.
820 If |destroy != NULL|, applies it to each data item in inorder. */
822 avl_destroy (struct avl_table *tree, avl_item_func *destroy)
824 struct avl_node *p, *q;
826 assert (tree != NULL);
828 for (p = tree->avl_root; p != NULL; p = q)
829 if (p->avl_link[0] == NULL)
832 if (destroy != NULL && p->avl_data != NULL)
833 destroy (p->avl_data, tree->avl_param);
834 tree->avl_alloc->libavl_free (tree->avl_alloc, p);
839 p->avl_link[0] = q->avl_link[1];
843 tree->avl_alloc->libavl_free (tree->avl_alloc, tree);
846 /* Allocates |size| bytes of space using |malloc()|.
847 Returns a null pointer if allocation fails. */
849 avl_malloc (struct libavl_allocator *allocator, size_t size)
851 assert (allocator != NULL && size > 0);
852 return malloc (size);
857 avl_free (struct libavl_allocator *allocator, void *block)
859 assert (allocator != NULL && block != NULL);
863 /* Default memory allocator that uses |malloc()| and |free()|. */
864 struct libavl_allocator avl_allocator_default =
873 /* Asserts that |avl_insert()| succeeds at inserting |item| into |table|. */
875 (avl_assert_insert) (struct avl_table *table, void *item)
877 void **p = avl_probe (table, item);
878 assert (p != NULL && *p == item);
881 /* Asserts that |avl_delete()| really removes |item| from |table|,
882 and returns the removed item. */
884 (avl_assert_delete) (struct avl_table *table, void *item)
886 void *p = avl_delete (table, item);