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.
31 #include "toolbox/avl.h"
34 /* Creates and returns a new table
35 with comparison function |compare| using parameter |param|
36 and memory allocator |allocator|.
37 Returns |NULL| if memory allocation failed. */
39 avl_create (avl_comparison_func *compare, void *param,
40 struct libavl_allocator *allocator)
42 struct avl_table *tree;
44 assert (compare != NULL);
46 if (allocator == NULL)
47 allocator = &avl_allocator_default;
49 tree = allocator->libavl_malloc (allocator, sizeof *tree);
53 tree->avl_root = NULL;
54 tree->avl_compare = compare;
55 tree->avl_param = param;
56 tree->avl_alloc = allocator;
58 tree->avl_generation = 0;
63 /* Search |tree| for an item matching |item|, and return it if found.
64 Otherwise return |NULL|. */
66 avl_find (const struct avl_table *tree, const void *item)
68 const struct avl_node *p;
70 assert (tree != NULL && item != NULL);
71 for (p = tree->avl_root; p != NULL; )
73 int cmp = tree->avl_compare (item, p->avl_data, tree->avl_param);
86 /* Inserts |item| into |tree| and returns a pointer to |item|'s address.
87 If a duplicate item is found in the tree,
88 returns a pointer to the duplicate without inserting |item|.
89 Returns |NULL| in case of memory allocation failure. */
91 avl_probe (struct avl_table *tree, void *item)
93 struct avl_node *y, *z; /* Top node to update balance factor, and parent. */
94 struct avl_node *p, *q; /* Iterator, and parent. */
95 struct avl_node *n; /* Newly inserted node. */
96 struct avl_node *w; /* New root of rebalanced subtree. */
97 int dir; /* Direction to descend. */
99 unsigned char da[AVL_MAX_HEIGHT]; /* Cached comparison results. */
100 int k = 0; /* Number of cached results. */
102 assert (tree != NULL && item != NULL);
104 z = (struct avl_node *) &tree->avl_root;
107 for (q = z, p = y; p != NULL; q = p, p = p->avl_link[dir])
109 int cmp = tree->avl_compare (item, p->avl_data, tree->avl_param);
113 if (p->avl_balance != 0)
115 da[k++] = dir = cmp > 0;
118 n = q->avl_link[dir] =
119 tree->avl_alloc->libavl_malloc (tree->avl_alloc, sizeof *n);
125 n->avl_link[0] = n->avl_link[1] = NULL;
130 for (p = y, k = 0; p != n; p = p->avl_link[da[k]], k++)
136 if (y->avl_balance == -2)
138 struct avl_node *x = y->avl_link[0];
139 if (x->avl_balance == -1)
142 y->avl_link[0] = x->avl_link[1];
144 x->avl_balance = y->avl_balance = 0;
148 assert (x->avl_balance == +1);
150 x->avl_link[1] = w->avl_link[0];
152 y->avl_link[0] = w->avl_link[1];
154 if (w->avl_balance == -1)
155 x->avl_balance = 0, y->avl_balance = +1;
156 else if (w->avl_balance == 0)
157 x->avl_balance = y->avl_balance = 0;
158 else /* |w->avl_balance == +1| */
159 x->avl_balance = -1, y->avl_balance = 0;
163 else if (y->avl_balance == +2)
165 struct avl_node *x = y->avl_link[1];
166 if (x->avl_balance == +1)
169 y->avl_link[1] = x->avl_link[0];
171 x->avl_balance = y->avl_balance = 0;
175 assert (x->avl_balance == -1);
177 x->avl_link[0] = w->avl_link[1];
179 y->avl_link[1] = w->avl_link[0];
181 if (w->avl_balance == +1)
182 x->avl_balance = 0, y->avl_balance = -1;
183 else if (w->avl_balance == 0)
184 x->avl_balance = y->avl_balance = 0;
185 else /* |w->avl_balance == -1| */
186 x->avl_balance = +1, y->avl_balance = 0;
192 z->avl_link[y != z->avl_link[0]] = w;
194 tree->avl_generation++;
198 /* Inserts |item| into |table|.
199 Returns |NULL| if |item| was successfully inserted
200 or if a memory allocation error occurred.
201 Otherwise, returns the duplicate item. */
203 avl_insert (struct avl_table *table, void *item)
205 void **p = avl_probe (table, item);
206 return p == NULL || *p == item ? NULL : *p;
209 /* Inserts |item| into |table|, replacing any duplicate item.
210 Returns |NULL| if |item| was inserted without replacing a duplicate,
211 or if a memory allocation error occurred.
212 Otherwise, returns the item that was replaced. */
214 avl_replace (struct avl_table *table, void *item)
216 void **p = avl_probe (table, item);
217 if (p == NULL || *p == item)
227 /* Deletes from |tree| and returns an item matching |item|.
228 Returns a null pointer if no matching item found. */
230 avl_delete (struct avl_table *tree, const void *item)
232 /* Stack of nodes. */
233 struct avl_node *pa[AVL_MAX_HEIGHT]; /* Nodes. */
234 unsigned char da[AVL_MAX_HEIGHT]; /* |avl_link[]| indexes. */
235 int k; /* Stack pointer. */
237 struct avl_node *p; /* Traverses tree to find node to delete. */
238 int cmp; /* Result of comparison between |item| and |p|. */
240 assert (tree != NULL && item != NULL);
243 p = (struct avl_node *) &tree->avl_root;
244 for (cmp = -1; cmp != 0;
245 cmp = tree->avl_compare (item, p->avl_data, tree->avl_param))
252 p = p->avl_link[dir];
258 if (p->avl_link[1] == NULL)
259 pa[k - 1]->avl_link[da[k - 1]] = p->avl_link[0];
262 struct avl_node *r = p->avl_link[1];
263 if (r->avl_link[0] == NULL)
265 r->avl_link[0] = p->avl_link[0];
266 r->avl_balance = p->avl_balance;
267 pa[k - 1]->avl_link[da[k - 1]] = r;
281 if (s->avl_link[0] == NULL)
287 s->avl_link[0] = p->avl_link[0];
288 r->avl_link[0] = s->avl_link[1];
289 s->avl_link[1] = p->avl_link[1];
290 s->avl_balance = p->avl_balance;
292 pa[j - 1]->avl_link[da[j - 1]] = s;
298 tree->avl_alloc->libavl_free (tree->avl_alloc, p);
303 struct avl_node *y = pa[k];
308 if (y->avl_balance == +1)
310 else if (y->avl_balance == +2)
312 struct avl_node *x = y->avl_link[1];
313 if (x->avl_balance == -1)
316 assert (x->avl_balance == -1);
318 x->avl_link[0] = w->avl_link[1];
320 y->avl_link[1] = w->avl_link[0];
322 if (w->avl_balance == +1)
323 x->avl_balance = 0, y->avl_balance = -1;
324 else if (w->avl_balance == 0)
325 x->avl_balance = y->avl_balance = 0;
326 else /* |w->avl_balance == -1| */
327 x->avl_balance = +1, y->avl_balance = 0;
329 pa[k - 1]->avl_link[da[k - 1]] = w;
333 y->avl_link[1] = x->avl_link[0];
335 pa[k - 1]->avl_link[da[k - 1]] = x;
336 if (x->avl_balance == 0)
343 x->avl_balance = y->avl_balance = 0;
350 if (y->avl_balance == -1)
352 else if (y->avl_balance == -2)
354 struct avl_node *x = y->avl_link[0];
355 if (x->avl_balance == +1)
358 assert (x->avl_balance == +1);
360 x->avl_link[1] = w->avl_link[0];
362 y->avl_link[0] = w->avl_link[1];
364 if (w->avl_balance == -1)
365 x->avl_balance = 0, y->avl_balance = +1;
366 else if (w->avl_balance == 0)
367 x->avl_balance = y->avl_balance = 0;
368 else /* |w->avl_balance == +1| */
369 x->avl_balance = -1, y->avl_balance = 0;
371 pa[k - 1]->avl_link[da[k - 1]] = w;
375 y->avl_link[0] = x->avl_link[1];
377 pa[k - 1]->avl_link[da[k - 1]] = x;
378 if (x->avl_balance == 0)
385 x->avl_balance = y->avl_balance = 0;
392 tree->avl_generation++;
393 return (void *) item;
396 /* Refreshes the stack of parent pointers in |trav|
397 and updates its generation number. */
399 trav_refresh (struct avl_traverser *trav)
401 assert (trav != NULL);
403 trav->avl_generation = trav->avl_table->avl_generation;
405 if (trav->avl_node != NULL)
407 avl_comparison_func *cmp = trav->avl_table->avl_compare;
408 void *param = trav->avl_table->avl_param;
409 struct avl_node *node = trav->avl_node;
412 trav->avl_height = 0;
413 for (i = trav->avl_table->avl_root; i != node; )
415 assert (trav->avl_height < AVL_MAX_HEIGHT);
418 trav->avl_stack[trav->avl_height++] = i;
419 i = i->avl_link[cmp (node->avl_data, i->avl_data, param) > 0];
424 /* Initializes |trav| for use with |tree|
425 and selects the null node. */
427 avl_t_init (struct avl_traverser *trav, struct avl_table *tree)
429 trav->avl_table = tree;
430 trav->avl_node = NULL;
431 trav->avl_height = 0;
432 trav->avl_generation = tree->avl_generation;
435 /* Initializes |trav| for |tree|
436 and selects and returns a pointer to its least-valued item.
437 Returns |NULL| if |tree| contains no nodes. */
439 avl_t_first (struct avl_traverser *trav, struct avl_table *tree)
443 assert (tree != NULL && trav != NULL);
445 trav->avl_table = tree;
446 trav->avl_height = 0;
447 trav->avl_generation = tree->avl_generation;
451 while (x->avl_link[0] != NULL)
453 assert (trav->avl_height < AVL_MAX_HEIGHT);
454 trav->avl_stack[trav->avl_height++] = x;
459 return x != NULL ? x->avl_data : NULL;
462 /* Initializes |trav| for |tree|
463 and selects and returns a pointer to its greatest-valued item.
464 Returns |NULL| if |tree| contains no nodes. */
466 avl_t_last (struct avl_traverser *trav, struct avl_table *tree)
470 assert (tree != NULL && trav != NULL);
472 trav->avl_table = tree;
473 trav->avl_height = 0;
474 trav->avl_generation = tree->avl_generation;
478 while (x->avl_link[1] != NULL)
480 assert (trav->avl_height < AVL_MAX_HEIGHT);
481 trav->avl_stack[trav->avl_height++] = x;
486 return x != NULL ? x->avl_data : NULL;
489 /* Searches for |item| in |tree|.
490 If found, initializes |trav| to the item found and returns the item
492 If there is no matching item, initializes |trav| to the null item
493 and returns |NULL|. */
495 avl_t_find (struct avl_traverser *trav, struct avl_table *tree, void *item)
497 struct avl_node *p, *q;
499 assert (trav != NULL && tree != NULL && item != NULL);
500 trav->avl_table = tree;
501 trav->avl_height = 0;
502 trav->avl_generation = tree->avl_generation;
503 for (p = tree->avl_root; p != NULL; p = q)
505 int cmp = tree->avl_compare (item, p->avl_data, tree->avl_param);
511 else /* |cmp == 0| */
517 assert (trav->avl_height < AVL_MAX_HEIGHT);
518 trav->avl_stack[trav->avl_height++] = p;
521 trav->avl_height = 0;
522 trav->avl_node = NULL;
526 /* Attempts to insert |item| into |tree|.
527 If |item| is inserted successfully, it is returned and |trav| is
528 initialized to its location.
529 If a duplicate is found, it is returned and |trav| is initialized to
530 its location. No replacement of the item occurs.
531 If a memory allocation failure occurs, |NULL| is returned and |trav|
532 is initialized to the null item. */
534 avl_t_insert (struct avl_traverser *trav, struct avl_table *tree, void *item)
538 assert (trav != NULL && tree != NULL && item != NULL);
540 p = avl_probe (tree, item);
543 trav->avl_table = tree;
546 ((char *) p - offsetof (struct avl_node, avl_data)));
547 trav->avl_generation = tree->avl_generation - 1;
552 avl_t_init (trav, tree);
557 /* Initializes |trav| to have the same current node as |src|. */
559 avl_t_copy (struct avl_traverser *trav, const struct avl_traverser *src)
561 assert (trav != NULL && src != NULL);
565 trav->avl_table = src->avl_table;
566 trav->avl_node = src->avl_node;
567 trav->avl_generation = src->avl_generation;
568 if (trav->avl_generation == trav->avl_table->avl_generation)
570 trav->avl_height = src->avl_height;
571 memcpy (trav->avl_stack, (const void *) src->avl_stack,
572 sizeof *trav->avl_stack * trav->avl_height);
576 return trav->avl_node != NULL ? trav->avl_node->avl_data : NULL;
579 /* Returns the next data item in inorder
580 within the tree being traversed with |trav|,
581 or if there are no more data items returns |NULL|. */
583 avl_t_next (struct avl_traverser *trav)
587 assert (trav != NULL);
589 if (trav->avl_generation != trav->avl_table->avl_generation)
595 return avl_t_first (trav, trav->avl_table);
597 else if (x->avl_link[1] != NULL)
599 assert (trav->avl_height < AVL_MAX_HEIGHT);
600 trav->avl_stack[trav->avl_height++] = x;
603 while (x->avl_link[0] != NULL)
605 assert (trav->avl_height < AVL_MAX_HEIGHT);
606 trav->avl_stack[trav->avl_height++] = x;
616 if (trav->avl_height == 0)
618 trav->avl_node = NULL;
623 x = trav->avl_stack[--trav->avl_height];
625 while (y == x->avl_link[1]);
632 /* Returns the previous data item in inorder
633 within the tree being traversed with |trav|,
634 or if there are no more data items returns |NULL|. */
636 avl_t_prev (struct avl_traverser *trav)
640 assert (trav != NULL);
642 if (trav->avl_generation != trav->avl_table->avl_generation)
648 return avl_t_last (trav, trav->avl_table);
650 else if (x->avl_link[0] != NULL)
652 assert (trav->avl_height < AVL_MAX_HEIGHT);
653 trav->avl_stack[trav->avl_height++] = x;
656 while (x->avl_link[1] != NULL)
658 assert (trav->avl_height < AVL_MAX_HEIGHT);
659 trav->avl_stack[trav->avl_height++] = x;
669 if (trav->avl_height == 0)
671 trav->avl_node = NULL;
676 x = trav->avl_stack[--trav->avl_height];
678 while (y == x->avl_link[0]);
685 /* Returns |trav|'s current item. */
687 avl_t_cur (struct avl_traverser *trav)
689 assert (trav != NULL);
691 return trav->avl_node != NULL ? trav->avl_node->avl_data : NULL;
694 /* Replaces the current item in |trav| by |new| and returns the item replaced.
695 |trav| must not have the null item selected.
696 The new item must not upset the ordering of the tree. */
698 avl_t_replace (struct avl_traverser *trav, void *new)
702 assert (trav != NULL && trav->avl_node != NULL && new != NULL);
703 old = trav->avl_node->avl_data;
704 trav->avl_node->avl_data = new;
709 copy_error_recovery (struct avl_node **stack, int height,
710 struct avl_table *new, avl_item_func *destroy)
712 assert (stack != NULL && height >= 0 && new != NULL);
714 for (; height > 2; height -= 2)
715 stack[height - 1]->avl_link[1] = NULL;
716 avl_destroy (new, destroy);
719 /* Copies |org| to a newly created tree, which is returned.
720 If |copy != NULL|, each data item in |org| is first passed to |copy|,
721 and the return values are inserted into the tree,
722 with |NULL| return values taken as indications of failure.
723 On failure, destroys the partially created new tree,
724 applying |destroy|, if non-null, to each item in the new tree so far,
726 If |allocator != NULL|, it is used for allocation in the new tree.
727 Otherwise, the same allocator used for |org| is used. */
729 avl_copy (const struct avl_table *org, avl_copy_func *copy,
730 avl_item_func *destroy, struct libavl_allocator *allocator)
732 struct avl_node *stack[2 * (AVL_MAX_HEIGHT + 1)];
735 struct avl_table *new;
736 const struct avl_node *x;
739 assert (org != NULL);
740 new = avl_create (org->avl_compare, org->avl_param,
741 allocator != NULL ? allocator : org->avl_alloc);
744 new->avl_count = org->avl_count;
745 if (new->avl_count == 0)
748 x = (const struct avl_node *) &org->avl_root;
749 y = (struct avl_node *) &new->avl_root;
752 while (x->avl_link[0] != NULL)
754 assert (height < 2 * (AVL_MAX_HEIGHT + 1));
757 new->avl_alloc->libavl_malloc (new->avl_alloc,
758 sizeof *y->avl_link[0]);
759 if (y->avl_link[0] == NULL)
761 if (y != (struct avl_node *) &new->avl_root)
764 y->avl_link[1] = NULL;
767 copy_error_recovery (stack, height, new, destroy);
771 stack[height++] = (struct avl_node *) x;
776 y->avl_link[0] = NULL;
780 y->avl_balance = x->avl_balance;
782 y->avl_data = x->avl_data;
785 y->avl_data = copy (x->avl_data, org->avl_param);
786 if (y->avl_data == NULL)
788 y->avl_link[1] = NULL;
789 copy_error_recovery (stack, height, new, destroy);
794 if (x->avl_link[1] != NULL)
797 new->avl_alloc->libavl_malloc (new->avl_alloc,
798 sizeof *y->avl_link[1]);
799 if (y->avl_link[1] == NULL)
801 copy_error_recovery (stack, height, new, destroy);
810 y->avl_link[1] = NULL;
821 /* Frees storage allocated for |tree|.
822 If |destroy != NULL|, applies it to each data item in inorder. */
824 avl_destroy (struct avl_table *tree, avl_item_func *destroy)
826 struct avl_node *p, *q;
828 assert (tree != NULL);
830 for (p = tree->avl_root; p != NULL; p = q)
831 if (p->avl_link[0] == NULL)
834 if (destroy != NULL && p->avl_data != NULL)
835 destroy (p->avl_data, tree->avl_param);
836 tree->avl_alloc->libavl_free (tree->avl_alloc, p);
841 p->avl_link[0] = q->avl_link[1];
845 tree->avl_alloc->libavl_free (tree->avl_alloc, tree);
848 /* Allocates |size| bytes of space using |malloc()|.
849 Returns a null pointer if allocation fails. */
851 avl_malloc (struct libavl_allocator *allocator, size_t size)
853 assert (allocator != NULL && size > 0);
854 return malloc (size);
859 avl_free (struct libavl_allocator *allocator, void *block)
861 assert (allocator != NULL && block != NULL);
865 /* Default memory allocator that uses |malloc()| and |free()|. */
866 struct libavl_allocator avl_allocator_default =
875 /* Asserts that |avl_insert()| succeeds at inserting |item| into |table|. */
877 (avl_assert_insert) (struct avl_table *table, void *item)
879 void **p = avl_probe (table, item);
880 assert (p != NULL && *p == item);
883 /* Asserts that |avl_delete()| really removes |item| from |table|,
884 and returns the removed item. */
886 (avl_assert_delete) (struct avl_table *table, void *item)
888 void *p = avl_delete (table, item);