]> git.draconx.ca Git - upkg.git/blob - src/uobject/avl.c
Stop using gnulib's flexmember module.
[upkg.git] / src / uobject / 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 <config.h>
24 #include <assert.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include "avl.h"
29
30 /* Creates and returns a new table
31    with comparison function |compare| using parameter |param|
32    and memory allocator |allocator|.
33    Returns |NULL| if memory allocation failed. */
34 struct avl_table *
35 avl_create (avl_comparison_func *compare, void *param,
36             struct libavl_allocator *allocator)
37 {
38   struct avl_table *tree;
39
40   assert (compare != NULL);
41
42   if (allocator == NULL)
43     allocator = &avl_allocator_default;
44
45   tree = allocator->libavl_malloc (allocator, sizeof *tree);
46   if (tree == NULL)
47     return NULL;
48
49   tree->avl_root = NULL;
50   tree->avl_compare = compare;
51   tree->avl_param = param;
52   tree->avl_alloc = allocator;
53   tree->avl_count = 0;
54   tree->avl_generation = 0;
55
56   return tree;
57 }
58
59 /* Search |tree| for an item matching |item|, and return it if found.
60    Otherwise return |NULL|. */
61 void *
62 avl_find (const struct avl_table *tree, const void *item)
63 {
64   const struct avl_node *p;
65
66   assert (tree != NULL && item != NULL);
67   for (p = tree->avl_root; p != NULL; )
68     {
69       int cmp = tree->avl_compare (item, p->avl_data, tree->avl_param);
70
71       if (cmp < 0)
72         p = p->avl_link[0];
73       else if (cmp > 0)
74         p = p->avl_link[1];
75       else /* |cmp == 0| */
76         return p->avl_data;
77     }
78
79   return NULL;
80 }
81
82 /* Inserts |item| into |tree| and returns a pointer to |item|'s address.
83    If a duplicate item is found in the tree,
84    returns a pointer to the duplicate without inserting |item|.
85    Returns |NULL| in case of memory allocation failure. */
86 void **
87 avl_probe (struct avl_table *tree, void *item)
88 {
89   struct avl_node *y, *z; /* Top node to update balance factor, and parent. */
90   struct avl_node *p, *q; /* Iterator, and parent. */
91   struct avl_node *n;     /* Newly inserted node. */
92   struct avl_node *w;     /* New root of rebalanced subtree. */
93   int dir;                /* Direction to descend. */
94
95   unsigned char da[AVL_MAX_HEIGHT]; /* Cached comparison results. */
96   int k = 0;              /* Number of cached results. */
97
98   assert (tree != NULL && item != NULL);
99
100   z = (struct avl_node *) &tree->avl_root;
101   y = tree->avl_root;
102   dir = 0;
103   for (q = z, p = y; p != NULL; q = p, p = p->avl_link[dir])
104     {
105       int cmp = tree->avl_compare (item, p->avl_data, tree->avl_param);
106       if (cmp == 0)
107         return &p->avl_data;
108
109       if (p->avl_balance != 0)
110         z = q, y = p, k = 0;
111       da[k++] = dir = cmp > 0;
112     }
113
114   n = q->avl_link[dir] =
115     tree->avl_alloc->libavl_malloc (tree->avl_alloc, sizeof *n);
116   if (n == NULL)
117     return NULL;
118
119   tree->avl_count++;
120   n->avl_data = item;
121   n->avl_link[0] = n->avl_link[1] = NULL;
122   n->avl_balance = 0;
123   if (y == NULL)
124     return &n->avl_data;
125
126   for (p = y, k = 0; p != n; p = p->avl_link[da[k]], k++)
127     if (da[k] == 0)
128       p->avl_balance--;
129     else
130       p->avl_balance++;
131
132   if (y->avl_balance == -2)
133     {
134       struct avl_node *x = y->avl_link[0];
135       if (x->avl_balance == -1)
136         {
137           w = x;
138           y->avl_link[0] = x->avl_link[1];
139           x->avl_link[1] = y;
140           x->avl_balance = y->avl_balance = 0;
141         }
142       else
143         {
144           assert (x->avl_balance == +1);
145           w = x->avl_link[1];
146           x->avl_link[1] = w->avl_link[0];
147           w->avl_link[0] = x;
148           y->avl_link[0] = w->avl_link[1];
149           w->avl_link[1] = y;
150           if (w->avl_balance == -1)
151             x->avl_balance = 0, y->avl_balance = +1;
152           else if (w->avl_balance == 0)
153             x->avl_balance = y->avl_balance = 0;
154           else /* |w->avl_balance == +1| */
155             x->avl_balance = -1, y->avl_balance = 0;
156           w->avl_balance = 0;
157         }
158     }
159   else if (y->avl_balance == +2)
160     {
161       struct avl_node *x = y->avl_link[1];
162       if (x->avl_balance == +1)
163         {
164           w = x;
165           y->avl_link[1] = x->avl_link[0];
166           x->avl_link[0] = y;
167           x->avl_balance = y->avl_balance = 0;
168         }
169       else
170         {
171           assert (x->avl_balance == -1);
172           w = x->avl_link[0];
173           x->avl_link[0] = w->avl_link[1];
174           w->avl_link[1] = x;
175           y->avl_link[1] = w->avl_link[0];
176           w->avl_link[0] = y;
177           if (w->avl_balance == +1)
178             x->avl_balance = 0, y->avl_balance = -1;
179           else if (w->avl_balance == 0)
180             x->avl_balance = y->avl_balance = 0;
181           else /* |w->avl_balance == -1| */
182             x->avl_balance = +1, y->avl_balance = 0;
183           w->avl_balance = 0;
184         }
185     }
186   else
187     return &n->avl_data;
188   z->avl_link[y != z->avl_link[0]] = w;
189
190   tree->avl_generation++;
191   return &n->avl_data;
192 }
193
194 /* Inserts |item| into |table|.
195    Returns |NULL| if |item| was successfully inserted
196    or if a memory allocation error occurred.
197    Otherwise, returns the duplicate item. */
198 void *
199 avl_insert (struct avl_table *table, void *item)
200 {
201   void **p = avl_probe (table, item);
202   return p == NULL || *p == item ? NULL : *p;
203 }
204
205 /* Inserts |item| into |table|, replacing any duplicate item.
206    Returns |NULL| if |item| was inserted without replacing a duplicate,
207    or if a memory allocation error occurred.
208    Otherwise, returns the item that was replaced. */
209 void *
210 avl_replace (struct avl_table *table, void *item)
211 {
212   void **p = avl_probe (table, item);
213   if (p == NULL || *p == item)
214     return NULL;
215   else
216     {
217       void *r = *p;
218       *p = item;
219       return r;
220     }
221 }
222
223 /* Deletes from |tree| and returns an item matching |item|.
224    Returns a null pointer if no matching item found. */
225 void *
226 avl_delete (struct avl_table *tree, const void *item)
227 {
228   /* Stack of nodes. */
229   struct avl_node *pa[AVL_MAX_HEIGHT]; /* Nodes. */
230   unsigned char da[AVL_MAX_HEIGHT];    /* |avl_link[]| indexes. */
231   int k;                               /* Stack pointer. */
232
233   struct avl_node *p;   /* Traverses tree to find node to delete. */
234   int cmp;              /* Result of comparison between |item| and |p|. */
235
236   assert (tree != NULL && item != NULL);
237
238   k = 0;
239   p = (struct avl_node *) &tree->avl_root;
240   for (cmp = -1; cmp != 0;
241        cmp = tree->avl_compare (item, p->avl_data, tree->avl_param))
242     {
243       int dir = cmp > 0;
244
245       pa[k] = p;
246       da[k++] = dir;
247
248       p = p->avl_link[dir];
249       if (p == NULL)
250         return NULL;
251     }
252   item = p->avl_data;
253
254   if (p->avl_link[1] == NULL)
255     pa[k - 1]->avl_link[da[k - 1]] = p->avl_link[0];
256   else
257     {
258       struct avl_node *r = p->avl_link[1];
259       if (r->avl_link[0] == NULL)
260         {
261           r->avl_link[0] = p->avl_link[0];
262           r->avl_balance = p->avl_balance;
263           pa[k - 1]->avl_link[da[k - 1]] = r;
264           da[k] = 1;
265           pa[k++] = r;
266         }
267       else
268         {
269           struct avl_node *s;
270           int j = k++;
271
272           for (;;)
273             {
274               da[k] = 0;
275               pa[k++] = r;
276               s = r->avl_link[0];
277               if (s->avl_link[0] == NULL)
278                 break;
279
280               r = s;
281             }
282
283           s->avl_link[0] = p->avl_link[0];
284           r->avl_link[0] = s->avl_link[1];
285           s->avl_link[1] = p->avl_link[1];
286           s->avl_balance = p->avl_balance;
287
288           pa[j - 1]->avl_link[da[j - 1]] = s;
289           da[j] = 1;
290           pa[j] = s;
291         }
292     }
293
294   tree->avl_alloc->libavl_free (tree->avl_alloc, p);
295
296   assert (k > 0);
297   while (--k > 0)
298     {
299       struct avl_node *y = pa[k];
300
301       if (da[k] == 0)
302         {
303           y->avl_balance++;
304           if (y->avl_balance == +1)
305             break;
306           else if (y->avl_balance == +2)
307             {
308               struct avl_node *x = y->avl_link[1];
309               if (x->avl_balance == -1)
310                 {
311                   struct avl_node *w;
312                   assert (x->avl_balance == -1);
313                   w = x->avl_link[0];
314                   x->avl_link[0] = w->avl_link[1];
315                   w->avl_link[1] = x;
316                   y->avl_link[1] = w->avl_link[0];
317                   w->avl_link[0] = y;
318                   if (w->avl_balance == +1)
319                     x->avl_balance = 0, y->avl_balance = -1;
320                   else if (w->avl_balance == 0)
321                     x->avl_balance = y->avl_balance = 0;
322                   else /* |w->avl_balance == -1| */
323                     x->avl_balance = +1, y->avl_balance = 0;
324                   w->avl_balance = 0;
325                   pa[k - 1]->avl_link[da[k - 1]] = w;
326                 }
327               else
328                 {
329                   y->avl_link[1] = x->avl_link[0];
330                   x->avl_link[0] = y;
331                   pa[k - 1]->avl_link[da[k - 1]] = x;
332                   if (x->avl_balance == 0)
333                     {
334                       x->avl_balance = -1;
335                       y->avl_balance = +1;
336                       break;
337                     }
338                   else
339                     x->avl_balance = y->avl_balance = 0;
340                 }
341             }
342         }
343       else
344         {
345           y->avl_balance--;
346           if (y->avl_balance == -1)
347             break;
348           else if (y->avl_balance == -2)
349             {
350               struct avl_node *x = y->avl_link[0];
351               if (x->avl_balance == +1)
352                 {
353                   struct avl_node *w;
354                   assert (x->avl_balance == +1);
355                   w = x->avl_link[1];
356                   x->avl_link[1] = w->avl_link[0];
357                   w->avl_link[0] = x;
358                   y->avl_link[0] = w->avl_link[1];
359                   w->avl_link[1] = y;
360                   if (w->avl_balance == -1)
361                     x->avl_balance = 0, y->avl_balance = +1;
362                   else if (w->avl_balance == 0)
363                     x->avl_balance = y->avl_balance = 0;
364                   else /* |w->avl_balance == +1| */
365                     x->avl_balance = -1, y->avl_balance = 0;
366                   w->avl_balance = 0;
367                   pa[k - 1]->avl_link[da[k - 1]] = w;
368                 }
369               else
370                 {
371                   y->avl_link[0] = x->avl_link[1];
372                   x->avl_link[1] = y;
373                   pa[k - 1]->avl_link[da[k - 1]] = x;
374                   if (x->avl_balance == 0)
375                     {
376                       x->avl_balance = +1;
377                       y->avl_balance = -1;
378                       break;
379                     }
380                   else
381                     x->avl_balance = y->avl_balance = 0;
382                 }
383             }
384         }
385     }
386
387   tree->avl_count--;
388   tree->avl_generation++;
389   return (void *) item;
390 }
391
392 /* Refreshes the stack of parent pointers in |trav|
393    and updates its generation number. */
394 static void
395 trav_refresh (struct avl_traverser *trav)
396 {
397   assert (trav != NULL);
398
399   trav->avl_generation = trav->avl_table->avl_generation;
400
401   if (trav->avl_node != NULL)
402     {
403       avl_comparison_func *cmp = trav->avl_table->avl_compare;
404       void *param = trav->avl_table->avl_param;
405       struct avl_node *node = trav->avl_node;
406       struct avl_node *i;
407
408       trav->avl_height = 0;
409       for (i = trav->avl_table->avl_root; i != node; )
410         {
411           assert (trav->avl_height < AVL_MAX_HEIGHT);
412           assert (i != NULL);
413
414           trav->avl_stack[trav->avl_height++] = i;
415           i = i->avl_link[cmp (node->avl_data, i->avl_data, param) > 0];
416         }
417     }
418 }
419
420 /* Initializes |trav| for use with |tree|
421    and selects the null node. */
422 void
423 avl_t_init (struct avl_traverser *trav, struct avl_table *tree)
424 {
425   trav->avl_table = tree;
426   trav->avl_node = NULL;
427   trav->avl_height = 0;
428   trav->avl_generation = tree->avl_generation;
429 }
430
431 /* Initializes |trav| for |tree|
432    and selects and returns a pointer to its least-valued item.
433    Returns |NULL| if |tree| contains no nodes. */
434 void *
435 avl_t_first (struct avl_traverser *trav, struct avl_table *tree)
436 {
437   struct avl_node *x;
438
439   assert (tree != NULL && trav != NULL);
440
441   trav->avl_table = tree;
442   trav->avl_height = 0;
443   trav->avl_generation = tree->avl_generation;
444
445   x = tree->avl_root;
446   if (x != NULL)
447     while (x->avl_link[0] != NULL)
448       {
449         assert (trav->avl_height < AVL_MAX_HEIGHT);
450         trav->avl_stack[trav->avl_height++] = x;
451         x = x->avl_link[0];
452       }
453   trav->avl_node = x;
454
455   return x != NULL ? x->avl_data : NULL;
456 }
457
458 /* Initializes |trav| for |tree|
459    and selects and returns a pointer to its greatest-valued item.
460    Returns |NULL| if |tree| contains no nodes. */
461 void *
462 avl_t_last (struct avl_traverser *trav, struct avl_table *tree)
463 {
464   struct avl_node *x;
465
466   assert (tree != NULL && trav != NULL);
467
468   trav->avl_table = tree;
469   trav->avl_height = 0;
470   trav->avl_generation = tree->avl_generation;
471
472   x = tree->avl_root;
473   if (x != NULL)
474     while (x->avl_link[1] != NULL)
475       {
476         assert (trav->avl_height < AVL_MAX_HEIGHT);
477         trav->avl_stack[trav->avl_height++] = x;
478         x = x->avl_link[1];
479       }
480   trav->avl_node = x;
481
482   return x != NULL ? x->avl_data : NULL;
483 }
484
485 /* Searches for |item| in |tree|.
486    If found, initializes |trav| to the item found and returns the item
487    as well.
488    If there is no matching item, initializes |trav| to the null item
489    and returns |NULL|. */
490 void *
491 avl_t_find (struct avl_traverser *trav, struct avl_table *tree, void *item)
492 {
493   struct avl_node *p, *q;
494
495   assert (trav != NULL && tree != NULL && item != NULL);
496   trav->avl_table = tree;
497   trav->avl_height = 0;
498   trav->avl_generation = tree->avl_generation;
499   for (p = tree->avl_root; p != NULL; p = q)
500     {
501       int cmp = tree->avl_compare (item, p->avl_data, tree->avl_param);
502
503       if (cmp < 0)
504         q = p->avl_link[0];
505       else if (cmp > 0)
506         q = p->avl_link[1];
507       else /* |cmp == 0| */
508         {
509           trav->avl_node = p;
510           return p->avl_data;
511         }
512
513       assert (trav->avl_height < AVL_MAX_HEIGHT);
514       trav->avl_stack[trav->avl_height++] = p;
515     }
516
517   trav->avl_height = 0;
518   trav->avl_node = NULL;
519   return NULL;
520 }
521
522 /* Attempts to insert |item| into |tree|.
523    If |item| is inserted successfully, it is returned and |trav| is
524    initialized to its location.
525    If a duplicate is found, it is returned and |trav| is initialized to
526    its location.  No replacement of the item occurs.
527    If a memory allocation failure occurs, |NULL| is returned and |trav|
528    is initialized to the null item. */
529 void *
530 avl_t_insert (struct avl_traverser *trav, struct avl_table *tree, void *item)
531 {
532   void **p;
533
534   assert (trav != NULL && tree != NULL && item != NULL);
535
536   p = avl_probe (tree, item);
537   if (p != NULL)
538     {
539       trav->avl_table = tree;
540       trav->avl_node =
541         ((struct avl_node *)
542          ((char *) p - offsetof (struct avl_node, avl_data)));
543       trav->avl_generation = tree->avl_generation - 1;
544       return *p;
545     }
546   else
547     {
548       avl_t_init (trav, tree);
549       return NULL;
550     }
551 }
552
553 /* Initializes |trav| to have the same current node as |src|. */
554 void *
555 avl_t_copy (struct avl_traverser *trav, const struct avl_traverser *src)
556 {
557   assert (trav != NULL && src != NULL);
558
559   if (trav != src)
560     {
561       trav->avl_table = src->avl_table;
562       trav->avl_node = src->avl_node;
563       trav->avl_generation = src->avl_generation;
564       if (trav->avl_generation == trav->avl_table->avl_generation)
565         {
566           trav->avl_height = src->avl_height;
567           memcpy (trav->avl_stack, (const void *) src->avl_stack,
568                   sizeof *trav->avl_stack * trav->avl_height);
569         }
570     }
571
572   return trav->avl_node != NULL ? trav->avl_node->avl_data : NULL;
573 }
574
575 /* Returns the next data item in inorder
576    within the tree being traversed with |trav|,
577    or if there are no more data items returns |NULL|. */
578 void *
579 avl_t_next (struct avl_traverser *trav)
580 {
581   struct avl_node *x;
582
583   assert (trav != NULL);
584
585   if (trav->avl_generation != trav->avl_table->avl_generation)
586     trav_refresh (trav);
587
588   x = trav->avl_node;
589   if (x == NULL)
590     {
591       return avl_t_first (trav, trav->avl_table);
592     }
593   else if (x->avl_link[1] != NULL)
594     {
595       assert (trav->avl_height < AVL_MAX_HEIGHT);
596       trav->avl_stack[trav->avl_height++] = x;
597       x = x->avl_link[1];
598
599       while (x->avl_link[0] != NULL)
600         {
601           assert (trav->avl_height < AVL_MAX_HEIGHT);
602           trav->avl_stack[trav->avl_height++] = x;
603           x = x->avl_link[0];
604         }
605     }
606   else
607     {
608       struct avl_node *y;
609
610       do
611         {
612           if (trav->avl_height == 0)
613             {
614               trav->avl_node = NULL;
615               return NULL;
616             }
617
618           y = x;
619           x = trav->avl_stack[--trav->avl_height];
620         }
621       while (y == x->avl_link[1]);
622     }
623   trav->avl_node = x;
624
625   return x->avl_data;
626 }
627
628 /* Returns the previous data item in inorder
629    within the tree being traversed with |trav|,
630    or if there are no more data items returns |NULL|. */
631 void *
632 avl_t_prev (struct avl_traverser *trav)
633 {
634   struct avl_node *x;
635
636   assert (trav != NULL);
637
638   if (trav->avl_generation != trav->avl_table->avl_generation)
639     trav_refresh (trav);
640
641   x = trav->avl_node;
642   if (x == NULL)
643     {
644       return avl_t_last (trav, trav->avl_table);
645     }
646   else if (x->avl_link[0] != NULL)
647     {
648       assert (trav->avl_height < AVL_MAX_HEIGHT);
649       trav->avl_stack[trav->avl_height++] = x;
650       x = x->avl_link[0];
651
652       while (x->avl_link[1] != NULL)
653         {
654           assert (trav->avl_height < AVL_MAX_HEIGHT);
655           trav->avl_stack[trav->avl_height++] = x;
656           x = x->avl_link[1];
657         }
658     }
659   else
660     {
661       struct avl_node *y;
662
663       do
664         {
665           if (trav->avl_height == 0)
666             {
667               trav->avl_node = NULL;
668               return NULL;
669             }
670
671           y = x;
672           x = trav->avl_stack[--trav->avl_height];
673         }
674       while (y == x->avl_link[0]);
675     }
676   trav->avl_node = x;
677
678   return x->avl_data;
679 }
680
681 /* Returns |trav|'s current item. */
682 void *
683 avl_t_cur (struct avl_traverser *trav)
684 {
685   assert (trav != NULL);
686
687   return trav->avl_node != NULL ? trav->avl_node->avl_data : NULL;
688 }
689
690 /* Replaces the current item in |trav| by |new| and returns the item replaced.
691    |trav| must not have the null item selected.
692    The new item must not upset the ordering of the tree. */
693 void *
694 avl_t_replace (struct avl_traverser *trav, void *new)
695 {
696   void *old;
697
698   assert (trav != NULL && trav->avl_node != NULL && new != NULL);
699   old = trav->avl_node->avl_data;
700   trav->avl_node->avl_data = new;
701   return old;
702 }
703
704 /* Destroys |new| with |avl_destroy (new, destroy)|,
705    first setting right links of nodes in |stack| within |new|
706    to null pointers to avoid touching uninitialized data. */
707 static void
708 copy_error_recovery (struct avl_node **stack, int height,
709                      struct avl_table *new, avl_item_func *destroy)
710 {
711   assert (stack != NULL && height >= 0 && new != NULL);
712
713   for (; height > 2; height -= 2)
714     stack[height - 1]->avl_link[1] = NULL;
715   avl_destroy (new, destroy);
716 }
717
718 /* Copies |org| to a newly created tree, which is returned.
719    If |copy != NULL|, each data item in |org| is first passed to |copy|,
720    and the return values are inserted into the tree,
721    with |NULL| return values taken as indications of failure.
722    On failure, destroys the partially created new tree,
723    applying |destroy|, if non-null, to each item in the new tree so far,
724    and returns |NULL|.
725    If |allocator != NULL|, it is used for allocation in the new tree.
726    Otherwise, the same allocator used for |org| is used. */
727 struct avl_table *
728 avl_copy (const struct avl_table *org, avl_copy_func *copy,
729           avl_item_func *destroy, struct libavl_allocator *allocator)
730 {
731   struct avl_node *stack[2 * (AVL_MAX_HEIGHT + 1)];
732   int height = 0;
733
734   struct avl_table *new;
735   const struct avl_node *x;
736   struct avl_node *y;
737
738   assert (org != NULL);
739   new = avl_create (org->avl_compare, org->avl_param,
740                     allocator != NULL ? allocator : org->avl_alloc);
741   if (new == NULL)
742     return NULL;
743   new->avl_count = org->avl_count;
744   if (new->avl_count == 0)
745     return new;
746
747   x = (const struct avl_node *) &org->avl_root;
748   y = (struct avl_node *) &new->avl_root;
749   for (;;)
750     {
751       while (x->avl_link[0] != NULL)
752         {
753           assert (height < 2 * (AVL_MAX_HEIGHT + 1));
754
755           y->avl_link[0] =
756             new->avl_alloc->libavl_malloc (new->avl_alloc,
757                                            sizeof *y->avl_link[0]);
758           if (y->avl_link[0] == NULL)
759             {
760               if (y != (struct avl_node *) &new->avl_root)
761                 {
762                   y->avl_data = NULL;
763                   y->avl_link[1] = NULL;
764                 }
765
766               copy_error_recovery (stack, height, new, destroy);
767               return NULL;
768             }
769
770           stack[height++] = (struct avl_node *) x;
771           stack[height++] = y;
772           x = x->avl_link[0];
773           y = y->avl_link[0];
774         }
775       y->avl_link[0] = NULL;
776
777       for (;;)
778         {
779           y->avl_balance = x->avl_balance;
780           if (copy == NULL)
781             y->avl_data = x->avl_data;
782           else
783             {
784               y->avl_data = copy (x->avl_data, org->avl_param);
785               if (y->avl_data == NULL)
786                 {
787                   y->avl_link[1] = NULL;
788                   copy_error_recovery (stack, height, new, destroy);
789                   return NULL;
790                 }
791             }
792
793           if (x->avl_link[1] != NULL)
794             {
795               y->avl_link[1] =
796                 new->avl_alloc->libavl_malloc (new->avl_alloc,
797                                                sizeof *y->avl_link[1]);
798               if (y->avl_link[1] == NULL)
799                 {
800                   copy_error_recovery (stack, height, new, destroy);
801                   return NULL;
802                 }
803
804               x = x->avl_link[1];
805               y = y->avl_link[1];
806               break;
807             }
808           else
809             y->avl_link[1] = NULL;
810
811           if (height <= 2)
812             return new;
813
814           y = stack[--height];
815           x = stack[--height];
816         }
817     }
818 }
819
820 /* Frees storage allocated for |tree|.
821    If |destroy != NULL|, applies it to each data item in inorder. */
822 void
823 avl_destroy (struct avl_table *tree, avl_item_func *destroy)
824 {
825   struct avl_node *p, *q;
826
827   assert (tree != NULL);
828
829   for (p = tree->avl_root; p != NULL; p = q)
830     if (p->avl_link[0] == NULL)
831       {
832         q = p->avl_link[1];
833         if (destroy != NULL && p->avl_data != NULL)
834           destroy (p->avl_data, tree->avl_param);
835         tree->avl_alloc->libavl_free (tree->avl_alloc, p);
836       }
837     else
838       {
839         q = p->avl_link[0];
840         p->avl_link[0] = q->avl_link[1];
841         q->avl_link[1] = p;
842       }
843
844   tree->avl_alloc->libavl_free (tree->avl_alloc, tree);
845 }
846
847 /* Allocates |size| bytes of space using |malloc()|.
848    Returns a null pointer if allocation fails. */
849 void *
850 avl_malloc (struct libavl_allocator *allocator, size_t size)
851 {
852   assert (allocator != NULL && size > 0);
853   return malloc (size);
854 }
855
856 /* Frees |block|. */
857 void
858 avl_free (struct libavl_allocator *allocator, void *block)
859 {
860   assert (allocator != NULL && block != NULL);
861   free (block);
862 }
863
864 /* Default memory allocator that uses |malloc()| and |free()|. */
865 struct libavl_allocator avl_allocator_default =
866   {
867     avl_malloc,
868     avl_free
869   };
870
871 #undef NDEBUG
872 #include <assert.h>
873
874 /* Asserts that |avl_insert()| succeeds at inserting |item| into |table|. */
875 void
876 (avl_assert_insert) (struct avl_table *table, void *item)
877 {
878   void **p = avl_probe (table, item);
879   assert (p != NULL && *p == item);
880 }
881
882 /* Asserts that |avl_delete()| really removes |item| from |table|,
883    and returns the removed item. */
884 void *
885 (avl_assert_delete) (struct avl_table *table, void *item)
886 {
887   void *p = avl_delete (table, item);
888   assert (p != NULL);
889   return p;
890 }
891