Stop using gnulib's flexmember module.
[upkg.git] / src / uobject / avl.c
blob9ded83b5710d48673e68c02ce1733417580e0f97
1 /* Produced by texiweb from libavl.w. */
3 /* libavl - library for manipulation of binary trees.
4 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004 Free Software
5 Foundation, Inc.
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.
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.
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.
23 #include <config.h>
24 #include <assert.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include "avl.h"
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)
38 struct avl_table *tree;
40 assert (compare != NULL);
42 if (allocator == NULL)
43 allocator = &avl_allocator_default;
45 tree = allocator->libavl_malloc (allocator, sizeof *tree);
46 if (tree == NULL)
47 return NULL;
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;
56 return tree;
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)
64 const struct avl_node *p;
66 assert (tree != NULL && item != NULL);
67 for (p = tree->avl_root; p != NULL; )
69 int cmp = tree->avl_compare (item, p->avl_data, tree->avl_param);
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;
79 return NULL;
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)
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. */
95 unsigned char da[AVL_MAX_HEIGHT]; /* Cached comparison results. */
96 int k = 0; /* Number of cached results. */
98 assert (tree != NULL && item != NULL);
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])
105 int cmp = tree->avl_compare (item, p->avl_data, tree->avl_param);
106 if (cmp == 0)
107 return &p->avl_data;
109 if (p->avl_balance != 0)
110 z = q, y = p, k = 0;
111 da[k++] = dir = cmp > 0;
114 n = q->avl_link[dir] =
115 tree->avl_alloc->libavl_malloc (tree->avl_alloc, sizeof *n);
116 if (n == NULL)
117 return NULL;
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;
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++;
132 if (y->avl_balance == -2)
134 struct avl_node *x = y->avl_link[0];
135 if (x->avl_balance == -1)
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;
142 else
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;
159 else if (y->avl_balance == +2)
161 struct avl_node *x = y->avl_link[1];
162 if (x->avl_balance == +1)
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;
169 else
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;
186 else
187 return &n->avl_data;
188 z->avl_link[y != z->avl_link[0]] = w;
190 tree->avl_generation++;
191 return &n->avl_data;
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)
201 void **p = avl_probe (table, item);
202 return p == NULL || *p == item ? NULL : *p;
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)
212 void **p = avl_probe (table, item);
213 if (p == NULL || *p == item)
214 return NULL;
215 else
217 void *r = *p;
218 *p = item;
219 return r;
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)
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. */
233 struct avl_node *p; /* Traverses tree to find node to delete. */
234 int cmp; /* Result of comparison between |item| and |p|. */
236 assert (tree != NULL && item != NULL);
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))
243 int dir = cmp > 0;
245 pa[k] = p;
246 da[k++] = dir;
248 p = p->avl_link[dir];
249 if (p == NULL)
250 return NULL;
252 item = p->avl_data;
254 if (p->avl_link[1] == NULL)
255 pa[k - 1]->avl_link[da[k - 1]] = p->avl_link[0];
256 else
258 struct avl_node *r = p->avl_link[1];
259 if (r->avl_link[0] == NULL)
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;
267 else
269 struct avl_node *s;
270 int j = k++;
272 for (;;)
274 da[k] = 0;
275 pa[k++] = r;
276 s = r->avl_link[0];
277 if (s->avl_link[0] == NULL)
278 break;
280 r = s;
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;
288 pa[j - 1]->avl_link[da[j - 1]] = s;
289 da[j] = 1;
290 pa[j] = s;
294 tree->avl_alloc->libavl_free (tree->avl_alloc, p);
296 assert (k > 0);
297 while (--k > 0)
299 struct avl_node *y = pa[k];
301 if (da[k] == 0)
303 y->avl_balance++;
304 if (y->avl_balance == +1)
305 break;
306 else if (y->avl_balance == +2)
308 struct avl_node *x = y->avl_link[1];
309 if (x->avl_balance == -1)
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;
327 else
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)
334 x->avl_balance = -1;
335 y->avl_balance = +1;
336 break;
338 else
339 x->avl_balance = y->avl_balance = 0;
343 else
345 y->avl_balance--;
346 if (y->avl_balance == -1)
347 break;
348 else if (y->avl_balance == -2)
350 struct avl_node *x = y->avl_link[0];
351 if (x->avl_balance == +1)
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;
369 else
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)
376 x->avl_balance = +1;
377 y->avl_balance = -1;
378 break;
380 else
381 x->avl_balance = y->avl_balance = 0;
387 tree->avl_count--;
388 tree->avl_generation++;
389 return (void *) item;
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)
397 assert (trav != NULL);
399 trav->avl_generation = trav->avl_table->avl_generation;
401 if (trav->avl_node != NULL)
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;
408 trav->avl_height = 0;
409 for (i = trav->avl_table->avl_root; i != node; )
411 assert (trav->avl_height < AVL_MAX_HEIGHT);
412 assert (i != NULL);
414 trav->avl_stack[trav->avl_height++] = i;
415 i = i->avl_link[cmp (node->avl_data, i->avl_data, param) > 0];
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)
425 trav->avl_table = tree;
426 trav->avl_node = NULL;
427 trav->avl_height = 0;
428 trav->avl_generation = tree->avl_generation;
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)
437 struct avl_node *x;
439 assert (tree != NULL && trav != NULL);
441 trav->avl_table = tree;
442 trav->avl_height = 0;
443 trav->avl_generation = tree->avl_generation;
445 x = tree->avl_root;
446 if (x != NULL)
447 while (x->avl_link[0] != NULL)
449 assert (trav->avl_height < AVL_MAX_HEIGHT);
450 trav->avl_stack[trav->avl_height++] = x;
451 x = x->avl_link[0];
453 trav->avl_node = x;
455 return x != NULL ? x->avl_data : NULL;
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)
464 struct avl_node *x;
466 assert (tree != NULL && trav != NULL);
468 trav->avl_table = tree;
469 trav->avl_height = 0;
470 trav->avl_generation = tree->avl_generation;
472 x = tree->avl_root;
473 if (x != NULL)
474 while (x->avl_link[1] != NULL)
476 assert (trav->avl_height < AVL_MAX_HEIGHT);
477 trav->avl_stack[trav->avl_height++] = x;
478 x = x->avl_link[1];
480 trav->avl_node = x;
482 return x != NULL ? x->avl_data : NULL;
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)
493 struct avl_node *p, *q;
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)
501 int cmp = tree->avl_compare (item, p->avl_data, tree->avl_param);
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| */
509 trav->avl_node = p;
510 return p->avl_data;
513 assert (trav->avl_height < AVL_MAX_HEIGHT);
514 trav->avl_stack[trav->avl_height++] = p;
517 trav->avl_height = 0;
518 trav->avl_node = NULL;
519 return NULL;
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)
532 void **p;
534 assert (trav != NULL && tree != NULL && item != NULL);
536 p = avl_probe (tree, item);
537 if (p != NULL)
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;
546 else
548 avl_t_init (trav, tree);
549 return NULL;
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)
557 assert (trav != NULL && src != NULL);
559 if (trav != src)
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)
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);
572 return trav->avl_node != NULL ? trav->avl_node->avl_data : NULL;
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)
581 struct avl_node *x;
583 assert (trav != NULL);
585 if (trav->avl_generation != trav->avl_table->avl_generation)
586 trav_refresh (trav);
588 x = trav->avl_node;
589 if (x == NULL)
591 return avl_t_first (trav, trav->avl_table);
593 else if (x->avl_link[1] != NULL)
595 assert (trav->avl_height < AVL_MAX_HEIGHT);
596 trav->avl_stack[trav->avl_height++] = x;
597 x = x->avl_link[1];
599 while (x->avl_link[0] != NULL)
601 assert (trav->avl_height < AVL_MAX_HEIGHT);
602 trav->avl_stack[trav->avl_height++] = x;
603 x = x->avl_link[0];
606 else
608 struct avl_node *y;
612 if (trav->avl_height == 0)
614 trav->avl_node = NULL;
615 return NULL;
618 y = x;
619 x = trav->avl_stack[--trav->avl_height];
621 while (y == x->avl_link[1]);
623 trav->avl_node = x;
625 return x->avl_data;
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)
634 struct avl_node *x;
636 assert (trav != NULL);
638 if (trav->avl_generation != trav->avl_table->avl_generation)
639 trav_refresh (trav);
641 x = trav->avl_node;
642 if (x == NULL)
644 return avl_t_last (trav, trav->avl_table);
646 else if (x->avl_link[0] != NULL)
648 assert (trav->avl_height < AVL_MAX_HEIGHT);
649 trav->avl_stack[trav->avl_height++] = x;
650 x = x->avl_link[0];
652 while (x->avl_link[1] != NULL)
654 assert (trav->avl_height < AVL_MAX_HEIGHT);
655 trav->avl_stack[trav->avl_height++] = x;
656 x = x->avl_link[1];
659 else
661 struct avl_node *y;
665 if (trav->avl_height == 0)
667 trav->avl_node = NULL;
668 return NULL;
671 y = x;
672 x = trav->avl_stack[--trav->avl_height];
674 while (y == x->avl_link[0]);
676 trav->avl_node = x;
678 return x->avl_data;
681 /* Returns |trav|'s current item. */
682 void *
683 avl_t_cur (struct avl_traverser *trav)
685 assert (trav != NULL);
687 return trav->avl_node != NULL ? trav->avl_node->avl_data : NULL;
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)
696 void *old;
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;
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)
711 assert (stack != NULL && height >= 0 && new != NULL);
713 for (; height > 2; height -= 2)
714 stack[height - 1]->avl_link[1] = NULL;
715 avl_destroy (new, destroy);
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)
731 struct avl_node *stack[2 * (AVL_MAX_HEIGHT + 1)];
732 int height = 0;
734 struct avl_table *new;
735 const struct avl_node *x;
736 struct avl_node *y;
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;
747 x = (const struct avl_node *) &org->avl_root;
748 y = (struct avl_node *) &new->avl_root;
749 for (;;)
751 while (x->avl_link[0] != NULL)
753 assert (height < 2 * (AVL_MAX_HEIGHT + 1));
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)
760 if (y != (struct avl_node *) &new->avl_root)
762 y->avl_data = NULL;
763 y->avl_link[1] = NULL;
766 copy_error_recovery (stack, height, new, destroy);
767 return NULL;
770 stack[height++] = (struct avl_node *) x;
771 stack[height++] = y;
772 x = x->avl_link[0];
773 y = y->avl_link[0];
775 y->avl_link[0] = NULL;
777 for (;;)
779 y->avl_balance = x->avl_balance;
780 if (copy == NULL)
781 y->avl_data = x->avl_data;
782 else
784 y->avl_data = copy (x->avl_data, org->avl_param);
785 if (y->avl_data == NULL)
787 y->avl_link[1] = NULL;
788 copy_error_recovery (stack, height, new, destroy);
789 return NULL;
793 if (x->avl_link[1] != NULL)
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)
800 copy_error_recovery (stack, height, new, destroy);
801 return NULL;
804 x = x->avl_link[1];
805 y = y->avl_link[1];
806 break;
808 else
809 y->avl_link[1] = NULL;
811 if (height <= 2)
812 return new;
814 y = stack[--height];
815 x = stack[--height];
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)
825 struct avl_node *p, *q;
827 assert (tree != NULL);
829 for (p = tree->avl_root; p != NULL; p = q)
830 if (p->avl_link[0] == NULL)
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);
837 else
839 q = p->avl_link[0];
840 p->avl_link[0] = q->avl_link[1];
841 q->avl_link[1] = p;
844 tree->avl_alloc->libavl_free (tree->avl_alloc, tree);
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)
852 assert (allocator != NULL && size > 0);
853 return malloc (size);
856 /* Frees |block|. */
857 void
858 avl_free (struct libavl_allocator *allocator, void *block)
860 assert (allocator != NULL && block != NULL);
861 free (block);
864 /* Default memory allocator that uses |malloc()| and |free()|. */
865 struct libavl_allocator avl_allocator_default =
867 avl_malloc,
868 avl_free
871 #undef NDEBUG
872 #include <assert.h>
874 /* Asserts that |avl_insert()| succeeds at inserting |item| into |table|. */
875 void
876 (avl_assert_insert) (struct avl_table *table, void *item)
878 void **p = avl_probe (table, item);
879 assert (p != NULL && *p == item);
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)
887 void *p = avl_delete (table, item);
888 assert (p != NULL);
889 return p;