1 /* Produced by texiweb from libavl.w on 2004/01/08 at 23:19. */
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. */
37 avl_create (avl_comparison_func *compare, void *param,
38 struct libavl_allocator *allocator)
40 struct avl_table *tree;
42 assert (compare != NULL);
44 if (allocator == NULL)
45 allocator = &avl_allocator_default;
47 tree = allocator->libavl_malloc (allocator, sizeof *tree);
51 tree->avl_root = NULL;
52 tree->avl_compare = compare;
53 tree->avl_param = param;
54 tree->avl_alloc = allocator;
56 tree->avl_generation = 0;
61 /* Search |tree| for an item matching |item|, and return it if found.
62 Otherwise return |NULL|. */
64 avl_find (const struct avl_table *tree, const void *item)
66 const struct avl_node *p;
68 assert (tree != NULL && item != NULL);
69 for (p = tree->avl_root; p != NULL; )
71 int cmp = tree->avl_compare (item, p->avl_data, tree->avl_param);
84 /* Inserts |item| into |tree| and returns a pointer to |item|'s address.
85 If a duplicate item is found in the tree,
86 returns a pointer to the duplicate without inserting |item|.
87 Returns |NULL| in case of memory allocation failure. */
89 avl_probe (struct avl_table *tree, void *item)
91 struct avl_node *y, *z; /* Top node to update balance factor, and parent. */
92 struct avl_node *p, *q; /* Iterator, and parent. */
93 struct avl_node *n; /* Newly inserted node. */
94 struct avl_node *w; /* New root of rebalanced subtree. */
95 int dir; /* Direction to descend. */
97 unsigned char da[AVL_MAX_HEIGHT]; /* Cached comparison results. */
98 int k = 0; /* Number of cached results. */
100 assert (tree != NULL && item != NULL);
102 z = (struct avl_node *) &tree->avl_root;
105 for (q = z, p = y; p != NULL; q = p, p = p->avl_link[dir])
107 int cmp = tree->avl_compare (item, p->avl_data, tree->avl_param);
111 if (p->avl_balance != 0)
113 da[k++] = dir = cmp > 0;
116 n = q->avl_link[dir] =
117 tree->avl_alloc->libavl_malloc (tree->avl_alloc, sizeof *n);
123 n->avl_link[0] = n->avl_link[1] = NULL;
128 for (p = y, k = 0; p != n; p = p->avl_link[da[k]], k++)
134 if (y->avl_balance == -2)
136 struct avl_node *x = y->avl_link[0];
137 if (x->avl_balance == -1)
140 y->avl_link[0] = x->avl_link[1];
142 x->avl_balance = y->avl_balance = 0;
146 assert (x->avl_balance == +1);
148 x->avl_link[1] = w->avl_link[0];
150 y->avl_link[0] = w->avl_link[1];
152 if (w->avl_balance == -1)
153 x->avl_balance = 0, y->avl_balance = +1;
154 else if (w->avl_balance == 0)
155 x->avl_balance = y->avl_balance = 0;
156 else /* |w->avl_balance == +1| */
157 x->avl_balance = -1, y->avl_balance = 0;
161 else if (y->avl_balance == +2)
163 struct avl_node *x = y->avl_link[1];
164 if (x->avl_balance == +1)
167 y->avl_link[1] = x->avl_link[0];
169 x->avl_balance = y->avl_balance = 0;
173 assert (x->avl_balance == -1);
175 x->avl_link[0] = w->avl_link[1];
177 y->avl_link[1] = w->avl_link[0];
179 if (w->avl_balance == +1)
180 x->avl_balance = 0, y->avl_balance = -1;
181 else if (w->avl_balance == 0)
182 x->avl_balance = y->avl_balance = 0;
183 else /* |w->avl_balance == -1| */
184 x->avl_balance = +1, y->avl_balance = 0;
190 z->avl_link[y != z->avl_link[0]] = w;
192 tree->avl_generation++;
196 /* Inserts |item| into |table|.
197 Returns |NULL| if |item| was successfully inserted
198 or if a memory allocation error occurred.
199 Otherwise, returns the duplicate item. */
201 avl_insert (struct avl_table *table, void *item)
203 void **p = avl_probe (table, item);
204 return p == NULL || *p == item ? NULL : *p;
207 /* Inserts |item| into |table|, replacing any duplicate item.
208 Returns |NULL| if |item| was inserted without replacing a duplicate,
209 or if a memory allocation error occurred.
210 Otherwise, returns the item that was replaced. */
212 avl_replace (struct avl_table *table, void *item)
214 void **p = avl_probe (table, item);
215 if (p == NULL || *p == item)
225 /* Deletes from |tree| and returns an item matching |item|.
226 Returns a null pointer if no matching item found. */
228 avl_delete (struct avl_table *tree, const void *item)
230 /* Stack of nodes. */
231 struct avl_node *pa[AVL_MAX_HEIGHT]; /* Nodes. */
232 unsigned char da[AVL_MAX_HEIGHT]; /* |avl_link[]| indexes. */
233 int k; /* Stack pointer. */
235 struct avl_node *p; /* Traverses tree to find node to delete. */
236 int cmp; /* Result of comparison between |item| and |p|. */
238 assert (tree != NULL && item != NULL);
241 p = (struct avl_node *) &tree->avl_root;
242 for (cmp = -1; cmp != 0;
243 cmp = tree->avl_compare (item, p->avl_data, tree->avl_param))
250 p = p->avl_link[dir];
256 if (p->avl_link[1] == NULL)
257 pa[k - 1]->avl_link[da[k - 1]] = p->avl_link[0];
260 struct avl_node *r = p->avl_link[1];
261 if (r->avl_link[0] == NULL)
263 r->avl_link[0] = p->avl_link[0];
264 r->avl_balance = p->avl_balance;
265 pa[k - 1]->avl_link[da[k - 1]] = r;
279 if (s->avl_link[0] == NULL)
285 s->avl_link[0] = p->avl_link[0];
286 r->avl_link[0] = s->avl_link[1];
287 s->avl_link[1] = p->avl_link[1];
288 s->avl_balance = p->avl_balance;
290 pa[j - 1]->avl_link[da[j - 1]] = s;
296 tree->avl_alloc->libavl_free (tree->avl_alloc, p);
301 struct avl_node *y = pa[k];
306 if (y->avl_balance == +1)
308 else if (y->avl_balance == +2)
310 struct avl_node *x = y->avl_link[1];
311 if (x->avl_balance == -1)
314 assert (x->avl_balance == -1);
316 x->avl_link[0] = w->avl_link[1];
318 y->avl_link[1] = w->avl_link[0];
320 if (w->avl_balance == +1)
321 x->avl_balance = 0, y->avl_balance = -1;
322 else if (w->avl_balance == 0)
323 x->avl_balance = y->avl_balance = 0;
324 else /* |w->avl_balance == -1| */
325 x->avl_balance = +1, y->avl_balance = 0;
327 pa[k - 1]->avl_link[da[k - 1]] = w;
331 y->avl_link[1] = x->avl_link[0];
333 pa[k - 1]->avl_link[da[k - 1]] = x;
334 if (x->avl_balance == 0)
341 x->avl_balance = y->avl_balance = 0;
348 if (y->avl_balance == -1)
350 else if (y->avl_balance == -2)
352 struct avl_node *x = y->avl_link[0];
353 if (x->avl_balance == +1)
356 assert (x->avl_balance == +1);
358 x->avl_link[1] = w->avl_link[0];
360 y->avl_link[0] = w->avl_link[1];
362 if (w->avl_balance == -1)
363 x->avl_balance = 0, y->avl_balance = +1;
364 else if (w->avl_balance == 0)
365 x->avl_balance = y->avl_balance = 0;
366 else /* |w->avl_balance == +1| */
367 x->avl_balance = -1, y->avl_balance = 0;
369 pa[k - 1]->avl_link[da[k - 1]] = w;
373 y->avl_link[0] = x->avl_link[1];
375 pa[k - 1]->avl_link[da[k - 1]] = x;
376 if (x->avl_balance == 0)
383 x->avl_balance = y->avl_balance = 0;
390 tree->avl_generation++;
391 return (void *) item;
394 /* Refreshes the stack of parent pointers in |trav|
395 and updates its generation number. */
397 trav_refresh (struct avl_traverser *trav)
399 assert (trav != NULL);
401 trav->avl_generation = trav->avl_table->avl_generation;
403 if (trav->avl_node != NULL)
405 avl_comparison_func *cmp = trav->avl_table->avl_compare;
406 void *param = trav->avl_table->avl_param;
407 struct avl_node *node = trav->avl_node;
410 trav->avl_height = 0;
411 for (i = trav->avl_table->avl_root; i != node; )
413 assert (trav->avl_height < AVL_MAX_HEIGHT);
416 trav->avl_stack[trav->avl_height++] = i;
417 i = i->avl_link[cmp (node->avl_data, i->avl_data, param) > 0];
422 /* Initializes |trav| for use with |tree|
423 and selects the null node. */
425 avl_t_init (struct avl_traverser *trav, struct avl_table *tree)
427 trav->avl_table = tree;
428 trav->avl_node = NULL;
429 trav->avl_height = 0;
430 trav->avl_generation = tree->avl_generation;
433 /* Initializes |trav| for |tree|
434 and selects and returns a pointer to its least-valued item.
435 Returns |NULL| if |tree| contains no nodes. */
437 avl_t_first (struct avl_traverser *trav, struct avl_table *tree)
441 assert (tree != NULL && trav != NULL);
443 trav->avl_table = tree;
444 trav->avl_height = 0;
445 trav->avl_generation = tree->avl_generation;
449 while (x->avl_link[0] != NULL)
451 assert (trav->avl_height < AVL_MAX_HEIGHT);
452 trav->avl_stack[trav->avl_height++] = x;
457 return x != NULL ? x->avl_data : NULL;
460 /* Initializes |trav| for |tree|
461 and selects and returns a pointer to its greatest-valued item.
462 Returns |NULL| if |tree| contains no nodes. */
464 avl_t_last (struct avl_traverser *trav, struct avl_table *tree)
468 assert (tree != NULL && trav != NULL);
470 trav->avl_table = tree;
471 trav->avl_height = 0;
472 trav->avl_generation = tree->avl_generation;
476 while (x->avl_link[1] != NULL)
478 assert (trav->avl_height < AVL_MAX_HEIGHT);
479 trav->avl_stack[trav->avl_height++] = x;
484 return x != NULL ? x->avl_data : NULL;
487 /* Searches for |item| in |tree|.
488 If found, initializes |trav| to the item found and returns the item
490 If there is no matching item, initializes |trav| to the null item
491 and returns |NULL|. */
493 avl_t_find (struct avl_traverser *trav, struct avl_table *tree, void *item)
495 struct avl_node *p, *q;
497 assert (trav != NULL && tree != NULL && item != NULL);
498 trav->avl_table = tree;
499 trav->avl_height = 0;
500 trav->avl_generation = tree->avl_generation;
501 for (p = tree->avl_root; p != NULL; p = q)
503 int cmp = tree->avl_compare (item, p->avl_data, tree->avl_param);
509 else /* |cmp == 0| */
515 assert (trav->avl_height < AVL_MAX_HEIGHT);
516 trav->avl_stack[trav->avl_height++] = p;
519 trav->avl_height = 0;
520 trav->avl_node = NULL;
524 /* Attempts to insert |item| into |tree|.
525 If |item| is inserted successfully, it is returned and |trav| is
526 initialized to its location.
527 If a duplicate is found, it is returned and |trav| is initialized to
528 its location. No replacement of the item occurs.
529 If a memory allocation failure occurs, |NULL| is returned and |trav|
530 is initialized to the null item. */
532 avl_t_insert (struct avl_traverser *trav, struct avl_table *tree, void *item)
536 assert (trav != NULL && tree != NULL && item != NULL);
538 p = avl_probe (tree, item);
541 trav->avl_table = tree;
544 ((char *) p - offsetof (struct avl_node, avl_data)));
545 trav->avl_generation = tree->avl_generation - 1;
550 avl_t_init (trav, tree);
555 /* Initializes |trav| to have the same current node as |src|. */
557 avl_t_copy (struct avl_traverser *trav, const struct avl_traverser *src)
559 assert (trav != NULL && src != NULL);
563 trav->avl_table = src->avl_table;
564 trav->avl_node = src->avl_node;
565 trav->avl_generation = src->avl_generation;
566 if (trav->avl_generation == trav->avl_table->avl_generation)
568 trav->avl_height = src->avl_height;
569 memcpy (trav->avl_stack, (const void *) src->avl_stack,
570 sizeof *trav->avl_stack * trav->avl_height);
574 return trav->avl_node != NULL ? trav->avl_node->avl_data : NULL;
577 /* Returns the next data item in inorder
578 within the tree being traversed with |trav|,
579 or if there are no more data items returns |NULL|. */
581 avl_t_next (struct avl_traverser *trav)
585 assert (trav != NULL);
587 if (trav->avl_generation != trav->avl_table->avl_generation)
593 return avl_t_first (trav, trav->avl_table);
595 else if (x->avl_link[1] != NULL)
597 assert (trav->avl_height < AVL_MAX_HEIGHT);
598 trav->avl_stack[trav->avl_height++] = x;
601 while (x->avl_link[0] != NULL)
603 assert (trav->avl_height < AVL_MAX_HEIGHT);
604 trav->avl_stack[trav->avl_height++] = x;
614 if (trav->avl_height == 0)
616 trav->avl_node = NULL;
621 x = trav->avl_stack[--trav->avl_height];
623 while (y == x->avl_link[1]);
630 /* Returns the previous data item in inorder
631 within the tree being traversed with |trav|,
632 or if there are no more data items returns |NULL|. */
634 avl_t_prev (struct avl_traverser *trav)
638 assert (trav != NULL);
640 if (trav->avl_generation != trav->avl_table->avl_generation)
646 return avl_t_last (trav, trav->avl_table);
648 else if (x->avl_link[0] != NULL)
650 assert (trav->avl_height < AVL_MAX_HEIGHT);
651 trav->avl_stack[trav->avl_height++] = x;
654 while (x->avl_link[1] != NULL)
656 assert (trav->avl_height < AVL_MAX_HEIGHT);
657 trav->avl_stack[trav->avl_height++] = x;
667 if (trav->avl_height == 0)
669 trav->avl_node = NULL;
674 x = trav->avl_stack[--trav->avl_height];
676 while (y == x->avl_link[0]);
683 /* Returns |trav|'s current item. */
685 avl_t_cur (struct avl_traverser *trav)
687 assert (trav != NULL);
689 return trav->avl_node != NULL ? trav->avl_node->avl_data : NULL;
692 /* Replaces the current item in |trav| by |new| and returns the item replaced.
693 |trav| must not have the null item selected.
694 The new item must not upset the ordering of the tree. */
696 avl_t_replace (struct avl_traverser *trav, void *new)
700 assert (trav != NULL && trav->avl_node != NULL && new != NULL);
701 old = trav->avl_node->avl_data;
702 trav->avl_node->avl_data = new;
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);