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