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