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