fix bug when jit_compile is entered recursively
[cacao.git] / toolbox / avl.c
1 /* Produced by texiweb from libavl.w on 2004/01/08 at 23:19. */
2
3 /* libavl - library for manipulation of binary trees.
4    Copyright (C) 1998-2002 Free Software Foundation, Inc.
5
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.
10
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.
15
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
19    02111-1307, USA.
20
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.
24 */
25
26 #include <assert.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include "avl.h"
31
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 *
37 avl_create (avl_comparison_func *compare, void *param,
38             struct libavl_allocator *allocator)
39 {
40   struct avl_table *tree;
41
42   assert (compare != NULL);
43
44   if (allocator == NULL)
45     allocator = &avl_allocator_default;
46
47   tree = allocator->libavl_malloc (allocator, sizeof *tree);
48   if (tree == NULL)
49     return NULL;
50
51   tree->avl_root = NULL;
52   tree->avl_compare = compare;
53   tree->avl_param = param;
54   tree->avl_alloc = allocator;
55   tree->avl_count = 0;
56   tree->avl_generation = 0;
57
58   return tree;
59 }
60
61 /* Search |tree| for an item matching |item|, and return it if found.
62    Otherwise return |NULL|. */
63 void *
64 avl_find (const struct avl_table *tree, const void *item)
65 {
66   const struct avl_node *p;
67
68   assert (tree != NULL && item != NULL);
69   for (p = tree->avl_root; p != NULL; )
70     {
71       int cmp = tree->avl_compare (item, p->avl_data, tree->avl_param);
72
73       if (cmp < 0)
74         p = p->avl_link[0];
75       else if (cmp > 0)
76         p = p->avl_link[1];
77       else /* |cmp == 0| */
78         return p->avl_data;
79     }
80
81   return NULL;
82 }
83
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. */
88 void **
89 avl_probe (struct avl_table *tree, void *item)
90 {
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. */
96
97   unsigned char da[AVL_MAX_HEIGHT]; /* Cached comparison results. */
98   int k = 0;              /* Number of cached results. */
99
100   assert (tree != NULL && item != NULL);
101
102   z = (struct avl_node *) &tree->avl_root;
103   y = tree->avl_root;
104   dir = 0;
105   for (q = z, p = y; p != NULL; q = p, p = p->avl_link[dir])
106     {
107       int cmp = tree->avl_compare (item, p->avl_data, tree->avl_param);
108       if (cmp == 0)
109         return &p->avl_data;
110
111       if (p->avl_balance != 0)
112         z = q, y = p, k = 0;
113       da[k++] = dir = cmp > 0;
114     }
115
116   n = q->avl_link[dir] =
117     tree->avl_alloc->libavl_malloc (tree->avl_alloc, sizeof *n);
118   if (n == NULL)
119     return NULL;
120
121   tree->avl_count++;
122   n->avl_data = item;
123   n->avl_link[0] = n->avl_link[1] = NULL;
124   n->avl_balance = 0;
125   if (y == NULL)
126     return &n->avl_data;
127
128   for (p = y, k = 0; p != n; p = p->avl_link[da[k]], k++)
129     if (da[k] == 0)
130       p->avl_balance--;
131     else
132       p->avl_balance++;
133
134   if (y->avl_balance == -2)
135     {
136       struct avl_node *x = y->avl_link[0];
137       if (x->avl_balance == -1)
138         {
139           w = x;
140           y->avl_link[0] = x->avl_link[1];
141           x->avl_link[1] = y;
142           x->avl_balance = y->avl_balance = 0;
143         }
144       else
145         {
146           assert (x->avl_balance == +1);
147           w = x->avl_link[1];
148           x->avl_link[1] = w->avl_link[0];
149           w->avl_link[0] = x;
150           y->avl_link[0] = w->avl_link[1];
151           w->avl_link[1] = y;
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;
158           w->avl_balance = 0;
159         }
160     }
161   else if (y->avl_balance == +2)
162     {
163       struct avl_node *x = y->avl_link[1];
164       if (x->avl_balance == +1)
165         {
166           w = x;
167           y->avl_link[1] = x->avl_link[0];
168           x->avl_link[0] = y;
169           x->avl_balance = y->avl_balance = 0;
170         }
171       else
172         {
173           assert (x->avl_balance == -1);
174           w = x->avl_link[0];
175           x->avl_link[0] = w->avl_link[1];
176           w->avl_link[1] = x;
177           y->avl_link[1] = w->avl_link[0];
178           w->avl_link[0] = y;
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;
185           w->avl_balance = 0;
186         }
187     }
188   else
189     return &n->avl_data;
190   z->avl_link[y != z->avl_link[0]] = w;
191
192   tree->avl_generation++;
193   return &n->avl_data;
194 }
195
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. */
200 void *
201 avl_insert (struct avl_table *table, void *item)
202 {
203   void **p = avl_probe (table, item);
204   return p == NULL || *p == item ? NULL : *p;
205 }
206
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. */
211 void *
212 avl_replace (struct avl_table *table, void *item)
213 {
214   void **p = avl_probe (table, item);
215   if (p == NULL || *p == item)
216     return NULL;
217   else
218     {
219       void *r = *p;
220       *p = item;
221       return r;
222     }
223 }
224
225 /* Deletes from |tree| and returns an item matching |item|.
226    Returns a null pointer if no matching item found. */
227 void *
228 avl_delete (struct avl_table *tree, const void *item)
229 {
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. */
234
235   struct avl_node *p;   /* Traverses tree to find node to delete. */
236   int cmp;              /* Result of comparison between |item| and |p|. */
237
238   assert (tree != NULL && item != NULL);
239
240   k = 0;
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))
244     {
245       int dir = cmp > 0;
246
247       pa[k] = p;
248       da[k++] = dir;
249
250       p = p->avl_link[dir];
251       if (p == NULL)
252         return NULL;
253     }
254   item = p->avl_data;
255
256   if (p->avl_link[1] == NULL)
257     pa[k - 1]->avl_link[da[k - 1]] = p->avl_link[0];
258   else
259     {
260       struct avl_node *r = p->avl_link[1];
261       if (r->avl_link[0] == NULL)
262         {
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;
266           da[k] = 1;
267           pa[k++] = r;
268         }
269       else
270         {
271           struct avl_node *s;
272           int j = k++;
273
274           for (;;)
275             {
276               da[k] = 0;
277               pa[k++] = r;
278               s = r->avl_link[0];
279               if (s->avl_link[0] == NULL)
280                 break;
281
282               r = s;
283             }
284
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;
289
290           pa[j - 1]->avl_link[da[j - 1]] = s;
291           da[j] = 1;
292           pa[j] = s;
293         }
294     }
295
296   tree->avl_alloc->libavl_free (tree->avl_alloc, p);
297
298   assert (k > 0);
299   while (--k > 0)
300     {
301       struct avl_node *y = pa[k];
302
303       if (da[k] == 0)
304         {
305           y->avl_balance++;
306           if (y->avl_balance == +1)
307             break;
308           else if (y->avl_balance == +2)
309             {
310               struct avl_node *x = y->avl_link[1];
311               if (x->avl_balance == -1)
312                 {
313                   struct avl_node *w;
314                   assert (x->avl_balance == -1);
315                   w = x->avl_link[0];
316                   x->avl_link[0] = w->avl_link[1];
317                   w->avl_link[1] = x;
318                   y->avl_link[1] = w->avl_link[0];
319                   w->avl_link[0] = y;
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;
326                   w->avl_balance = 0;
327                   pa[k - 1]->avl_link[da[k - 1]] = w;
328                 }
329               else
330                 {
331                   y->avl_link[1] = x->avl_link[0];
332                   x->avl_link[0] = y;
333                   pa[k - 1]->avl_link[da[k - 1]] = x;
334                   if (x->avl_balance == 0)
335                     {
336                       x->avl_balance = -1;
337                       y->avl_balance = +1;
338                       break;
339                     }
340                   else
341                     x->avl_balance = y->avl_balance = 0;
342                 }
343             }
344         }
345       else
346         {
347           y->avl_balance--;
348           if (y->avl_balance == -1)
349             break;
350           else if (y->avl_balance == -2)
351             {
352               struct avl_node *x = y->avl_link[0];
353               if (x->avl_balance == +1)
354                 {
355                   struct avl_node *w;
356                   assert (x->avl_balance == +1);
357                   w = x->avl_link[1];
358                   x->avl_link[1] = w->avl_link[0];
359                   w->avl_link[0] = x;
360                   y->avl_link[0] = w->avl_link[1];
361                   w->avl_link[1] = y;
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;
368                   w->avl_balance = 0;
369                   pa[k - 1]->avl_link[da[k - 1]] = w;
370                 }
371               else
372                 {
373                   y->avl_link[0] = x->avl_link[1];
374                   x->avl_link[1] = y;
375                   pa[k - 1]->avl_link[da[k - 1]] = x;
376                   if (x->avl_balance == 0)
377                     {
378                       x->avl_balance = +1;
379                       y->avl_balance = -1;
380                       break;
381                     }
382                   else
383                     x->avl_balance = y->avl_balance = 0;
384                 }
385             }
386         }
387     }
388
389   tree->avl_count--;
390   tree->avl_generation++;
391   return (void *) item;
392 }
393
394 /* Refreshes the stack of parent pointers in |trav|
395    and updates its generation number. */
396 static void
397 trav_refresh (struct avl_traverser *trav)
398 {
399   assert (trav != NULL);
400
401   trav->avl_generation = trav->avl_table->avl_generation;
402
403   if (trav->avl_node != NULL)
404     {
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;
408       struct avl_node *i;
409
410       trav->avl_height = 0;
411       for (i = trav->avl_table->avl_root; i != node; )
412         {
413           assert (trav->avl_height < AVL_MAX_HEIGHT);
414           assert (i != NULL);
415
416           trav->avl_stack[trav->avl_height++] = i;
417           i = i->avl_link[cmp (node->avl_data, i->avl_data, param) > 0];
418         }
419     }
420 }
421
422 /* Initializes |trav| for use with |tree|
423    and selects the null node. */
424 void
425 avl_t_init (struct avl_traverser *trav, struct avl_table *tree)
426 {
427   trav->avl_table = tree;
428   trav->avl_node = NULL;
429   trav->avl_height = 0;
430   trav->avl_generation = tree->avl_generation;
431 }
432
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. */
436 void *
437 avl_t_first (struct avl_traverser *trav, struct avl_table *tree)
438 {
439   struct avl_node *x;
440
441   assert (tree != NULL && trav != NULL);
442
443   trav->avl_table = tree;
444   trav->avl_height = 0;
445   trav->avl_generation = tree->avl_generation;
446
447   x = tree->avl_root;
448   if (x != NULL)
449     while (x->avl_link[0] != NULL)
450       {
451         assert (trav->avl_height < AVL_MAX_HEIGHT);
452         trav->avl_stack[trav->avl_height++] = x;
453         x = x->avl_link[0];
454       }
455   trav->avl_node = x;
456
457   return x != NULL ? x->avl_data : NULL;
458 }
459
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. */
463 void *
464 avl_t_last (struct avl_traverser *trav, struct avl_table *tree)
465 {
466   struct avl_node *x;
467
468   assert (tree != NULL && trav != NULL);
469
470   trav->avl_table = tree;
471   trav->avl_height = 0;
472   trav->avl_generation = tree->avl_generation;
473
474   x = tree->avl_root;
475   if (x != NULL)
476     while (x->avl_link[1] != NULL)
477       {
478         assert (trav->avl_height < AVL_MAX_HEIGHT);
479         trav->avl_stack[trav->avl_height++] = x;
480         x = x->avl_link[1];
481       }
482   trav->avl_node = x;
483
484   return x != NULL ? x->avl_data : NULL;
485 }
486
487 /* Searches for |item| in |tree|.
488    If found, initializes |trav| to the item found and returns the item
489    as well.
490    If there is no matching item, initializes |trav| to the null item
491    and returns |NULL|. */
492 void *
493 avl_t_find (struct avl_traverser *trav, struct avl_table *tree, void *item)
494 {
495   struct avl_node *p, *q;
496
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)
502     {
503       int cmp = tree->avl_compare (item, p->avl_data, tree->avl_param);
504
505       if (cmp < 0)
506         q = p->avl_link[0];
507       else if (cmp > 0)
508         q = p->avl_link[1];
509       else /* |cmp == 0| */
510         {
511           trav->avl_node = p;
512           return p->avl_data;
513         }
514
515       assert (trav->avl_height < AVL_MAX_HEIGHT);
516       trav->avl_stack[trav->avl_height++] = p;
517     }
518
519   trav->avl_height = 0;
520   trav->avl_node = NULL;
521   return NULL;
522 }
523
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. */
531 void *
532 avl_t_insert (struct avl_traverser *trav, struct avl_table *tree, void *item)
533 {
534   void **p;
535
536   assert (trav != NULL && tree != NULL && item != NULL);
537
538   p = avl_probe (tree, item);
539   if (p != NULL)
540     {
541       trav->avl_table = tree;
542       trav->avl_node =
543         ((struct avl_node *)
544          ((char *) p - offsetof (struct avl_node, avl_data)));
545       trav->avl_generation = tree->avl_generation - 1;
546       return *p;
547     }
548   else
549     {
550       avl_t_init (trav, tree);
551       return NULL;
552     }
553 }
554
555 /* Initializes |trav| to have the same current node as |src|. */
556 void *
557 avl_t_copy (struct avl_traverser *trav, const struct avl_traverser *src)
558 {
559   assert (trav != NULL && src != NULL);
560
561   if (trav != src)
562     {
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)
567         {
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);
571         }
572     }
573
574   return trav->avl_node != NULL ? trav->avl_node->avl_data : NULL;
575 }
576
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|. */
580 void *
581 avl_t_next (struct avl_traverser *trav)
582 {
583   struct avl_node *x;
584
585   assert (trav != NULL);
586
587   if (trav->avl_generation != trav->avl_table->avl_generation)
588     trav_refresh (trav);
589
590   x = trav->avl_node;
591   if (x == NULL)
592     {
593       return avl_t_first (trav, trav->avl_table);
594     }
595   else if (x->avl_link[1] != NULL)
596     {
597       assert (trav->avl_height < AVL_MAX_HEIGHT);
598       trav->avl_stack[trav->avl_height++] = x;
599       x = x->avl_link[1];
600
601       while (x->avl_link[0] != NULL)
602         {
603           assert (trav->avl_height < AVL_MAX_HEIGHT);
604           trav->avl_stack[trav->avl_height++] = x;
605           x = x->avl_link[0];
606         }
607     }
608   else
609     {
610       struct avl_node *y;
611
612       do
613         {
614           if (trav->avl_height == 0)
615             {
616               trav->avl_node = NULL;
617               return NULL;
618             }
619
620           y = x;
621           x = trav->avl_stack[--trav->avl_height];
622         }
623       while (y == x->avl_link[1]);
624     }
625   trav->avl_node = x;
626
627   return x->avl_data;
628 }
629
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|. */
633 void *
634 avl_t_prev (struct avl_traverser *trav)
635 {
636   struct avl_node *x;
637
638   assert (trav != NULL);
639
640   if (trav->avl_generation != trav->avl_table->avl_generation)
641     trav_refresh (trav);
642
643   x = trav->avl_node;
644   if (x == NULL)
645     {
646       return avl_t_last (trav, trav->avl_table);
647     }
648   else if (x->avl_link[0] != NULL)
649     {
650       assert (trav->avl_height < AVL_MAX_HEIGHT);
651       trav->avl_stack[trav->avl_height++] = x;
652       x = x->avl_link[0];
653
654       while (x->avl_link[1] != NULL)
655         {
656           assert (trav->avl_height < AVL_MAX_HEIGHT);
657           trav->avl_stack[trav->avl_height++] = x;
658           x = x->avl_link[1];
659         }
660     }
661   else
662     {
663       struct avl_node *y;
664
665       do
666         {
667           if (trav->avl_height == 0)
668             {
669               trav->avl_node = NULL;
670               return NULL;
671             }
672
673           y = x;
674           x = trav->avl_stack[--trav->avl_height];
675         }
676       while (y == x->avl_link[0]);
677     }
678   trav->avl_node = x;
679
680   return x->avl_data;
681 }
682
683 /* Returns |trav|'s current item. */
684 void *
685 avl_t_cur (struct avl_traverser *trav)
686 {
687   assert (trav != NULL);
688
689   return trav->avl_node != NULL ? trav->avl_node->avl_data : NULL;
690 }
691
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. */
695 void *
696 avl_t_replace (struct avl_traverser *trav, void *new)
697 {
698   void *old;
699
700   assert (trav != NULL && trav->avl_node != NULL && new != NULL);
701   old = trav->avl_node->avl_data;
702   trav->avl_node->avl_data = new;
703   return old;
704 }
705
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