]> git.draconx.ca Git - upkg.git/blob - src/avl.c
module: add missing return value to module_exit.
[upkg.git] / src / avl.c
1 /* Produced by texiweb from libavl.w. */
2
3 /* libavl - library for manipulation of binary trees.
4    Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004 Free Software
5    Foundation, Inc.
6
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.
11
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.
16
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
20    02110-1301 USA.
21 */
22
23 #include <assert.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include "avl.h"
28
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. */
33 struct avl_table *
34 avl_create (avl_comparison_func *compare, void *param,
35             struct libavl_allocator *allocator)
36 {
37   struct avl_table *tree;
38
39   assert (compare != NULL);
40
41   if (allocator == NULL)
42     allocator = &avl_allocator_default;
43
44   tree = allocator->libavl_malloc (allocator, sizeof *tree);
45   if (tree == NULL)
46     return NULL;
47
48   tree->avl_root = NULL;
49   tree->avl_compare = compare;
50   tree->avl_param = param;
51   tree->avl_alloc = allocator;
52   tree->avl_count = 0;
53   tree->avl_generation = 0;
54
55   return tree;
56 }
57
58 /* Search |tree| for an item matching |item|, and return it if found.
59    Otherwise return |NULL|. */
60 void *
61 avl_find (const struct avl_table *tree, const void *item)
62 {
63   const struct avl_node *p;
64
65   assert (tree != NULL && item != NULL);
66   for (p = tree->avl_root; p != NULL; )
67     {
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 **
86 avl_probe (struct avl_table *tree, void *item)
87 {
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. */
93
94   unsigned char da[AVL_MAX_HEIGHT]; /* Cached comparison results. */
95   int k = 0;              /* Number of cached results. */
96
97   assert (tree != NULL && item != NULL);
98
99   z = (struct avl_node *) &tree->avl_root;
100   y = tree->avl_root;
101   dir = 0;
102   for (q = z, p = y; p != NULL; q = p, p = p->avl_link[dir])
103     {
104       int cmp = tree->avl_compare (item, p->avl_data, tree->avl_param);
105       if (cmp == 0)
106         return &p->avl_data;
107
108       if (p->avl_balance != 0)
109         z = q, y = p, k = 0;
110       da[k++] = dir = cmp > 0;
111     }
112
113   n = q->avl_link[dir] =
114     tree->avl_alloc->libavl_malloc (tree->avl_alloc, sizeof *n);
115   if (n == NULL)
116     return NULL;
117
118   tree->avl_count++;
119   n->avl_data = item;
120   n->avl_link[0] = n->avl_link[1] = NULL;
121   n->avl_balance = 0;
122   if (y == NULL)
123     return &n->avl_data;
124
125   for (p = y, k = 0; p != n; p = p->avl_link[da[k]], k++)
126     if (da[k] == 0)
127       p->avl_balance--;
128     else
129       p->avl_balance++;
130
131   if (y->avl_balance == -2)
132     {
133       struct avl_node *x = y->avl_link[0];
134       if (x->avl_balance == -1)
135         {
136           w = x;
137           y->avl_link[0] = x->avl_link[1];
138           x->avl_link[1] = y;
139           x->avl_balance = y->avl_balance = 0;
140         }
141       else
142         {
143           assert (x->avl_balance == +1);
144           w = x->avl_link[1];
145           x->avl_link[1] = w->avl_link[0];
146           w->avl_link[0] = x;
147           y->avl_link[0] = w->avl_link[1];
148           w->avl_link[1] = y;
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;
155           w->avl_balance = 0;
156         }
157     }
158   else if (y->avl_balance == +2)
159     {
160       struct avl_node *x = y->avl_link[1];
161       if (x->avl_balance == +1)
162         {
163           w = x;
164           y->avl_link[1] = x->avl_link[0];
165           x->avl_link[0] = y;
166           x->avl_balance = y->avl_balance = 0;
167         }
168       else
169         {
170           assert (x->avl_balance == -1);
171           w = x->avl_link[0];
172           x->avl_link[0] = w->avl_link[1];
173           w->avl_link[1] = x;
174           y->avl_link[1] = w->avl_link[0];
175           w->avl_link[0] = y;
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;
182           w->avl_balance = 0;
183         }
184     }
185   else
186     return &n->avl_data;
187   z->avl_link[y != z->avl_link[0]] = w;
188
189   tree->avl_generation++;
190   return &n->avl_data;
191 }
192
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. */
197 void *
198 avl_insert (struct avl_table *table, void *item)
199 {
200   void **p = avl_probe (table, item);
201   return p == NULL || *p == item ? NULL : *p;
202 }
203
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. */
208 void *
209 avl_replace (struct avl_table *table, void *item)
210 {
211   void **p = avl_probe (table, item);
212   if (p == NULL || *p == item)
213     return NULL;
214   else
215     {
216       void *r = *p;
217       *p = item;
218       return r;
219     }
220 }
221
222 /* Deletes from |tree| and returns an item matching |item|.
223    Returns a null pointer if no matching item found. */
224 void *
225 avl_delete (struct avl_table *tree, const void *item)
226 {
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. */
231
232   struct avl_node *p;   /* Traverses tree to find node to delete. */
233   int cmp;              /* Result of comparison between |item| and |p|. */
234
235   assert (tree != NULL && item != NULL);
236
237   k = 0;
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))
241     {
242       int dir = cmp > 0;
243
244       pa[k] = p;
245       da[k++] = dir;
246
247       p = p->avl_link[dir];
248       if (p == NULL)
249         return NULL;
250     }
251   item = p->avl_data;
252
253   if (p->avl_link[1] == NULL)
254     pa[k - 1]->avl_link[da[k - 1]] = p->avl_link[0];
255   else
256     {
257       struct avl_node *r = p->avl_link[1];
258       if (r->avl_link[0] == NULL)
259         {
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;
263           da[k] = 1;
264           pa[k++] = r;
265         }
266       else
267         {
268           struct avl_node *s;
269           int j = k++;
270
271           for (;;)
272             {
273               da[k] = 0;
274               pa[k++] = r;
275               s = r->avl_link[0];
276               if (s->avl_link[0] == NULL)
277                 break;
278
279               r = s;
280             }
281
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;
286
287           pa[j - 1]->avl_link[da[j - 1]] = s;
288           da[j] = 1;
289           pa[j] = s;
290         }
291     }
292
293   tree->avl_alloc->libavl_free (tree->avl_alloc, p);
294
295   assert (k > 0);
296   while (--k > 0)
297     {
298       struct avl_node *y = pa[k];
299
300       if (da[k] == 0)
301         {
302           y->avl_balance++;
303           if (y->avl_balance == +1)
304             break;
305           else if (y->avl_balance == +2)
306             {
307               struct avl_node *x = y->avl_link[1];
308               if (x->avl_balance == -1)
309                 {
310                   struct avl_node *w;
311                   assert (x->avl_balance == -1);
312                   w = x->avl_link[0];
313                   x->avl_link[0] = w->avl_link[1];
314                   w->avl_link[1] = x;
315                   y->avl_link[1] = w->avl_link[0];
316                   w->avl_link[0] = y;
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;
323                   w->avl_balance = 0;
324                   pa[k - 1]->avl_link[da[k - 1]] = w;
325                 }
326               else
327                 {
328                   y->avl_link[1] = x->avl_link[0];
329                   x->avl_link[0] = y;
330                   pa[k - 1]->avl_link[da[k - 1]] = x;
331                   if (x->avl_balance == 0)
332                     {
333                       x->avl_balance = -1;
334                       y->avl_balance = +1;
335                       break;
336                     }
337                   else
338                     x->avl_balance = y->avl_balance = 0;
339                 }
340             }
341         }
342       else
343         {
344           y->avl_balance--;
345           if (y->avl_balance == -1)
346             break;
347           else if (y->avl_balance == -2)
348             {
349               struct avl_node *x = y->avl_link[0];
350               if (x->avl_balance == +1)
351                 {
352                   struct avl_node *w;
353                   assert (x->avl_balance == +1);
354                   w = x->avl_link[1];
355                   x->avl_link[1] = w->avl_link[0];
356                   w->avl_link[0] = x;
357                   y->avl_link[0] = w->avl_link[1];
358                   w->avl_link[1] = y;
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;
365                   w->avl_balance = 0;
366                   pa[k - 1]->avl_link[da[k - 1]] = w;
367                 }
368               else
369                 {
370                   y->avl_link[0] = x->avl_link[1];
371                   x->avl_link[1] = y;
372                   pa[k - 1]->avl_link[da[k - 1]] = x;
373                   if (x->avl_balance == 0)
374                     {
375                       x->avl_balance = +1;
376                       y->avl_balance = -1;
377                       break;
378                     }
379                   else
380                     x->avl_balance = y->avl_balance = 0;
381                 }
382             }
383         }
384     }
385
386   tree->avl_count--;
387   tree->avl_generation++;
388   return (void *) item;
389 }
390
391 /* Refreshes the stack of parent pointers in |trav|
392    and updates its generation number. */
393 static void
394 trav_refresh (struct avl_traverser *trav)
395 {
396   assert (trav != NULL);
397
398   trav->avl_generation = trav->avl_table->avl_generation;
399
400   if (trav->avl_node != NULL)
401     {
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;
405       struct avl_node *i;
406
407       trav->avl_height = 0;
408       for (i = trav->avl_table->avl_root; i != node; )
409         {
410           assert (trav->avl_height < AVL_MAX_HEIGHT);
411           assert (i != NULL);
412
413           trav->avl_stack[trav->avl_height++] = i;
414           i = i->avl_link[cmp (node->avl_data, i->avl_data, param) > 0];
415         }
416     }
417 }
418
419 /* Initializes |trav| for use with |tree|
420    and selects the null node. */
421 void
422 avl_t_init (struct avl_traverser *trav, struct avl_table *tree)
423 {
424   trav->avl_table = tree;
425   trav->avl_node = NULL;
426   trav->avl_height = 0;
427   trav->avl_generation = tree->avl_generation;
428 }
429
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. */
433 void *
434 avl_t_first (struct avl_traverser *trav, struct avl_table *tree)
435 {
436   struct avl_node *x;
437
438   assert (tree != NULL && trav != NULL);
439
440   trav->avl_table = tree;
441   trav->avl_height = 0;
442   trav->avl_generation = tree->avl_generation;
443
444   x = tree->avl_root;
445   if (x != NULL)
446     while (x->avl_link[0] != NULL)
447       {
448         assert (trav->avl_height < AVL_MAX_HEIGHT);
449         trav->avl_stack[trav->avl_height++] = x;
450         x = x->avl_link[0];
451       }
452   trav->avl_node = x;
453
454   return x != NULL ? x->avl_data : NULL;
455 }
456
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. */
460 void *
461 avl_t_last (struct avl_traverser *trav, struct avl_table *tree)
462 {
463   struct avl_node *x;
464
465   assert (tree != NULL && trav != NULL);
466
467   trav->avl_table = tree;
468   trav->avl_height = 0;
469   trav->avl_generation = tree->avl_generation;
470
471   x = tree->avl_root;
472   if (x != NULL)
473     while (x->avl_link[1] != NULL)
474       {
475         assert (trav->avl_height < AVL_MAX_HEIGHT);
476         trav->avl_stack[trav->avl_height++] = x;
477         x = x->avl_link[1];
478       }
479   trav->avl_node = x;
480
481   return x != NULL ? x->avl_data : NULL;
482 }
483
484 /* Searches for |item| in |tree|.
485    If found, initializes |trav| to the item found and returns the item
486    as well.
487    If there is no matching item, initializes |trav| to the null item
488    and returns |NULL|. */
489 void *
490 avl_t_find (struct avl_traverser *trav, struct avl_table *tree, void *item)
491 {
492   struct avl_node *p, *q;
493
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)
499     {
500       int cmp = tree->avl_compare (item, p->avl_data, tree->avl_param);
501
502       if (cmp < 0)
503         q = p->avl_link[0];
504       else if (cmp > 0)
505         q = p->avl_link[1];
506       else /* |cmp == 0| */
507         {
508           trav->avl_node = p;
509           return p->avl_data;
510         }
511
512       assert (trav->avl_height < AVL_MAX_HEIGHT);
513       trav->avl_stack[trav->avl_height++] = p;
514     }
515
516   trav->avl_height = 0;
517   trav->avl_node = NULL;
518   return NULL;
519 }
520
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. */
528 void *
529 avl_t_insert (struct avl_traverser *trav, struct avl_table *tree, void *item)
530 {
531   void **p;
532
533   assert (trav != NULL && tree != NULL && item != NULL);
534
535   p = avl_probe (tree, item);
536   if (p != NULL)
537     {
538       trav->avl_table = tree;
539       trav->avl_node =
540         ((struct avl_node *)
541          ((char *) p - offsetof (struct avl_node, avl_data)));
542       trav->avl_generation = tree->avl_generation - 1;
543       return *p;
544     }
545   else
546     {
547       avl_t_init (trav, tree);
548       return NULL;
549     }
550 }
551
552 /* Initializes |trav| to have the same current node as |src|. */
553 void *
554 avl_t_copy (struct avl_traverser *trav, const struct avl_traverser *src)
555 {
556   assert (trav != NULL && src != NULL);
557
558   if (trav != src)
559     {
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)
564         {
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);
568         }
569     }
570
571   return trav->avl_node != NULL ? trav->avl_node->avl_data : NULL;
572 }
573
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|. */
577 void *
578 avl_t_next (struct avl_traverser *trav)
579 {
580   struct avl_node *x;
581
582   assert (trav != NULL);
583
584   if (trav->avl_generation != trav->avl_table->avl_generation)
585     trav_refresh (trav);
586
587   x = trav->avl_node;
588   if (x == NULL)
589     {
590       return avl_t_first (trav, trav->avl_table);
591     }
592   else if (x->avl_link[1] != NULL)
593     {
594       assert (trav->avl_height < AVL_MAX_HEIGHT);
595       trav->avl_stack[trav->avl_height++] = x;
596       x = x->avl_link[1];
597
598       while (x->avl_link[0] != NULL)
599         {
600           assert (trav->avl_height < AVL_MAX_HEIGHT);
601           trav->avl_stack[trav->avl_height++] = x;
602           x = x->avl_link[0];
603         }
604     }
605   else
606     {
607       struct avl_node *y;
608
609       do
610         {
611           if (trav->avl_height == 0)
612             {
613               trav->avl_node = NULL;
614               return NULL;
615             }
616
617           y = x;
618           x = trav->avl_stack[--trav->avl_height];
619         }
620       while (y == x->avl_link[1]);
621     }
622   trav->avl_node = x;
623
624   return x->avl_data;
625 }
626
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|. */
630 void *
631 avl_t_prev (struct avl_traverser *trav)
632 {
633   struct avl_node *x;
634
635   assert (trav != NULL);
636
637   if (trav->avl_generation != trav->avl_table->avl_generation)
638     trav_refresh (trav);
639
640   x = trav->avl_node;
641   if (x == NULL)
642     {
643       return avl_t_last (trav, trav->avl_table);
644     }
645   else if (x->avl_link[0] != NULL)
646     {
647       assert (trav->avl_height < AVL_MAX_HEIGHT);
648       trav->avl_stack[trav->avl_height++] = x;
649       x = x->avl_link[0];
650
651       while (x->avl_link[1] != NULL)
652         {
653           assert (trav->avl_height < AVL_MAX_HEIGHT);
654           trav->avl_stack[trav->avl_height++] = x;
655           x = x->avl_link[1];
656         }
657     }
658   else
659     {
660       struct avl_node *y;
661
662       do
663         {
664           if (trav->avl_height == 0)
665             {
666               trav->avl_node = NULL;
667               return NULL;
668             }
669
670           y = x;
671           x = trav->avl_stack[--trav->avl_height];
672         }
673       while (y == x->avl_link[0]);
674     }
675   trav->avl_node = x;
676
677   return x->avl_data;
678 }
679
680 /* Returns |trav|'s current item. */
681 void *
682 avl_t_cur (struct avl_traverser *trav)
683 {
684   assert (trav != NULL);
685
686   return trav->avl_node != NULL ? trav->avl_node->avl_data : NULL;
687 }
688
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. */
692 void *
693 avl_t_replace (struct avl_traverser *trav, void *new)
694 {
695   void *old;
696
697   assert (trav != NULL && trav->avl_node != NULL && new != NULL);
698   old = trav->avl_node->avl_data;
699   trav->avl_node->avl_data = new;
700   return old;
701 }
702
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. */
706 static void
707 copy_error_recovery (struct avl_node **stack, int height,
708                      struct avl_table *new, avl_item_func *destroy)
709 {
710   assert (stack != NULL && height >= 0 && new != NULL);
711
712   for (; height > 2; height -= 2)
713     stack[height - 1]->avl_link[1] = NULL;
714   avl_destroy (new, destroy);
715 }
716
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,
723    and returns |NULL|.
724    If |allocator != NULL|, it is used for allocation in the new tree.
725    Otherwise, the same allocator used for |org| is used. */
726 struct avl_table *
727 avl_copy (const struct avl_table *org, avl_copy_func *copy,
728           avl_item_func *destroy, struct libavl_allocator *allocator)
729 {
730   struct avl_node *stack[2 * (AVL_MAX_HEIGHT + 1)];
731   int height = 0;
732
733   struct avl_table *new;
734   const struct avl_node *x;
735   struct avl_node *y;
736
737   assert (org != NULL);
738   new = avl_create (org->avl_compare, org->avl_param,
739                     allocator != NULL ? allocator : org->avl_alloc);
740   if (new == NULL)
741     return NULL;
742   new->avl_count = org->avl_count;
743   if (new->avl_count == 0)
744     return new;
745
746   x = (const struct avl_node *) &org->avl_root;
747   y = (struct avl_node *) &new->avl_root;
748   for (;;)
749     {
750       while (x->avl_link[0] != NULL)
751         {
752           assert (height < 2 * (AVL_MAX_HEIGHT + 1));
753
754           y->avl_link[0] =
755             new->avl_alloc->libavl_malloc (new->avl_alloc,
756                                            sizeof *y->avl_link[0]);
757           if (y->avl_link[0] == NULL)
758             {
759               if (y != (struct avl_node *) &new->avl_root)
760                 {
761                   y->avl_data = NULL;
762                   y->avl_link[1] = NULL;
763                 }
764
765               copy_error_recovery (stack, height, new, destroy);
766               return NULL;
767             }
768
769           stack[height++] = (struct avl_node *) x;
770           stack[height++] = y;
771           x = x->avl_link[0];
772           y = y->avl_link[0];
773         }
774       y->avl_link[0] = NULL;
775
776       for (;;)
777         {
778           y->avl_balance = x->avl_balance;
779           if (copy == NULL)
780             y->avl_data = x->avl_data;
781           else
782             {
783               y->avl_data = copy (x->avl_data, org->avl_param);
784               if (y->avl_data == NULL)
785                 {
786                   y->avl_link[1] = NULL;
787                   copy_error_recovery (stack, height, new, destroy);
788                   return NULL;
789                 }
790             }
791
792           if (x->avl_link[1] != NULL)
793             {
794               y->avl_link[1] =
795                 new->avl_alloc->libavl_malloc (new->avl_alloc,
796                                                sizeof *y->avl_link[1]);
797               if (y->avl_link[1] == NULL)
798                 {
799                   copy_error_recovery (stack, height, new, destroy);
800                   return NULL;
801                 }
802
803               x = x->avl_link[1];
804               y = y->avl_link[1];
805               break;
806             }
807           else
808             y->avl_link[1] = NULL;
809
810           if (height <= 2)
811             return new;
812
813           y = stack[--height];
814           x = stack[--height];
815         }
816     }
817 }
818
819 /* Frees storage allocated for |tree|.
820    If |destroy != NULL|, applies it to each data item in inorder. */
821 void
822 avl_destroy (struct avl_table *tree, avl_item_func *destroy)
823 {
824   struct avl_node *p, *q;
825
826   assert (tree != NULL);
827
828   for (p = tree->avl_root; p != NULL; p = q)
829     if (p->avl_link[0] == NULL)
830       {
831         q = p->avl_link[1];
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);
835       }
836     else
837       {
838         q = p->avl_link[0];
839         p->avl_link[0] = q->avl_link[1];
840         q->avl_link[1] = p;
841       }
842
843   tree->avl_alloc->libavl_free (tree->avl_alloc, tree);
844 }
845
846 /* Allocates |size| bytes of space using |malloc()|.
847    Returns a null pointer if allocation fails. */
848 void *
849 avl_malloc (struct libavl_allocator *allocator, size_t size)
850 {
851   assert (allocator != NULL && size > 0);
852   return malloc (size);
853 }
854
855 /* Frees |block|. */
856 void
857 avl_free (struct libavl_allocator *allocator, void *block)
858 {
859   assert (allocator != NULL && block != NULL);
860   free (block);
861 }
862
863 /* Default memory allocator that uses |malloc()| and |free()|. */
864 struct libavl_allocator avl_allocator_default =
865   {
866     avl_malloc,
867     avl_free
868   };
869
870 #undef NDEBUG
871 #include <assert.h>
872
873 /* Asserts that |avl_insert()| succeeds at inserting |item| into |table|. */
874 void
875 (avl_assert_insert) (struct avl_table *table, void *item)
876 {
877   void **p = avl_probe (table, item);
878   assert (p != NULL && *p == item);
879 }
880
881 /* Asserts that |avl_delete()| really removes |item| from |table|,
882    and returns the removed item. */
883 void *
884 (avl_assert_delete) (struct avl_table *table, void *item)
885 {
886   void *p = avl_delete (table, item);
887   assert (p != NULL);
888   return p;
889 }
890