Debian Codegraph @

engine/read_only/marpa_tavl.c

raw
1 /* Produced by texiweb from libavl.w. */
2
3 /* libavl - library for manipulation of binary trees.
4 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004 Free Software
5 Foundation, Inc.
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 3 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301 USA.
21 */
22
23
24 #include <assert.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27
28 /* I changed the memory allocator from a parameter to a global. Not
29 * thread-safe, but it is only used in the test, which is not threaded.
30 * In production Marpa's * |my_malloc| is hardwired in, by the following defines,
31 * and that is thread-safe.
32 */
33
34 #ifndef TESTING_TAVL
35 #define TESTING_TAVL 0
36 #endif /* TESTING_TAVL */
37
38 #if TESTING_TAVL
39
40 #include <assert.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43
44 /* First parameter is really not needed */
45 #define MY_AVL_MALLOC(size) \
46 (marpa__tavl_allocator_default->libavl_malloc(marpa__tavl_allocator_default, (size)))
47 #define MY_AVL_FREE(p) \
48 (marpa__tavl_allocator_default->libavl_free(marpa__tavl_allocator_default, (p)))
49
50 #else /* not TESTING_TAVL */
51
52 #include "config.h"
53
54 #include <assert.h>
55 #include <stdio.h>
56
57 #include "marpa.h"
58 #include "marpa_ami.h"
59
60 #define MY_AVL_MALLOC(size) (my_malloc(size))
61 #define MY_AVL_FREE(p) (my_free(p))
62
63 #endif /* TESTING AVL */
64
65 #include "marpa_tavl.h"
66
67 /* Creates and returns a new table
68 with comparison function |compare| using parameter |param|
69 and memory allocator |allocator|.
70 Returns |NULL| if memory allocation failed. */
71 struct tavl_table *
72 marpa__tavl_create (tavl_comparison_func *compare, void *param)
73 {
74 struct tavl_table *tree;
75
76 assert (compare != NULL);
77
78 tree = MY_AVL_MALLOC( sizeof *tree);
79
80 tree->tavl_root = NULL;
81 tree->tavl_compare = compare;
82 tree->tavl_param = param;
83 tree->tavl_count = 0;
84
85 return tree;
86 }
87
88 /* Search |tree| for an item matching |item|, and return it if found.
89 Otherwise return |NULL|. */
90 void *
91 marpa__tavl_find (const struct tavl_table *tree, const void *item)
92 {
93 const struct tavl_node *p;
94
95 assert (tree != NULL && item != NULL);
96
97 p = tree->tavl_root;
98 if (p == NULL)
99 return NULL;
100
101 for (;;)
102 {
103 int cmp, dir;
104
105 cmp = tree->tavl_compare (item, p->tavl_data, tree->tavl_param);
106 if (cmp == 0)
107 return p->tavl_data;
108
109 dir = cmp > 0;
110 if (p->tavl_tag[dir] == TAVL_CHILD)
111 p = p->tavl_link[dir];
112 else
113 return NULL;
114 }
115 }
116
117 /* Inserts |item| into |tree| and returns a pointer to |item|'s address.
118 If a duplicate item is found in the tree,
119 returns a pointer to the duplicate without inserting |item|.
120 Returns |NULL| in case of memory allocation failure. */
121 void **
122 marpa__tavl_probe (struct tavl_table *tree, void *item)
123 {
124 struct tavl_node *y, *z; /* Top node to update balance factor, and parent. */
125 struct tavl_node *p, *q; /* Iterator, and parent. */
126 struct tavl_node *n; /* Newly inserted node. */
127 struct tavl_node *w; /* New root of rebalanced subtree. */
128 int dir; /* Direction to descend. */
129
130 unsigned char da[TAVL_MAX_HEIGHT]; /* Cached comparison results. */
131 int k = 0; /* Number of cached results. */
132
133 assert (tree != NULL && item != NULL);
134 tree->tavl_duplicate_found = 0; /* Default to no duplicate found */
135
136 z = (struct tavl_node *) &tree->tavl_root;
137 y = tree->tavl_root;
138 if (y != NULL)
139 {
140 for (q = z, p = y; ; q = p, p = p->tavl_link[dir])
141 {
142 int cmp = tree->tavl_compare (item, p->tavl_data, tree->tavl_param);
143 if (cmp == 0) {
144 tree->tavl_duplicate_found = 1;
145 return &p->tavl_data;
146 }
147
148 if (p->tavl_balance != 0)
149 z = q, y = p, k = 0;
150 dir = cmp > 0;
151 da[k++] = (unsigned char)dir;
152
153 if (p->tavl_tag[dir] == TAVL_THREAD)
154 break;
155 }
156 }
157 else
158 {
159 p = z;
160 dir = 0;
161 }
162
163 n = MY_AVL_MALLOC( sizeof *n);
164
165 tree->tavl_count++;
166 n->tavl_data = item;
167 n->tavl_tag[0] = n->tavl_tag[1] = TAVL_THREAD;
168 n->tavl_link[dir] = p->tavl_link[dir];
169 if (tree->tavl_root != NULL)
170 {
171 p->tavl_tag[dir] = TAVL_CHILD;
172 n->tavl_link[!dir] = p;
173 }
174 else
175 n->tavl_link[1] = NULL;
176 p->tavl_link[dir] = n;
177 n->tavl_balance = 0;
178 if (tree->tavl_root == n)
179 return &n->tavl_data;
180
181 for (p = y, k = 0; p != n; p = p->tavl_link[da[k]], k++)
182 if (da[k] == 0)
183 p->tavl_balance--;
184 else
185 p->tavl_balance++;
186
187 if (y->tavl_balance == -2)
188 {
189 struct tavl_node *x = y->tavl_link[0];
190 if (x->tavl_balance == -1)
191 {
192 w = x;
193 if (x->tavl_tag[1] == TAVL_THREAD)
194 {
195 x->tavl_tag[1] = TAVL_CHILD;
196 y->tavl_tag[0] = TAVL_THREAD;
197 y->tavl_link[0] = x;
198 }
199 else
200 y->tavl_link[0] = x->tavl_link[1];
201 x->tavl_link[1] = y;
202 x->tavl_balance = y->tavl_balance = 0;
203 }
204 else
205 {
206 assert (x->tavl_balance == +1);
207 w = x->tavl_link[1];
208 x->tavl_link[1] = w->tavl_link[0];
209 w->tavl_link[0] = x;
210 y->tavl_link[0] = w->tavl_link[1];
211 w->tavl_link[1] = y;
212 if (w->tavl_balance == -1)
213 x->tavl_balance = 0, y->tavl_balance = +1;
214 else if (w->tavl_balance == 0)
215 x->tavl_balance = y->tavl_balance = 0;
216 else /* |w->tavl_balance == +1| */
217 x->tavl_balance = -1, y->tavl_balance = 0;
218 w->tavl_balance = 0;
219 if (w->tavl_tag[0] == TAVL_THREAD)
220 {
221 x->tavl_tag[1] = TAVL_THREAD;
222 x->tavl_link[1] = w;
223 w->tavl_tag[0] = TAVL_CHILD;
224 }
225 if (w->tavl_tag[1] == TAVL_THREAD)
226 {
227 y->tavl_tag[0] = TAVL_THREAD;
228 y->tavl_link[0] = w;
229 w->tavl_tag[1] = TAVL_CHILD;
230 }
231 }
232 }
233 else if (y->tavl_balance == +2)
234 {
235 struct tavl_node *x = y->tavl_link[1];
236 if (x->tavl_balance == +1)
237 {
238 w = x;
239 if (x->tavl_tag[0] == TAVL_THREAD)
240 {
241 x->tavl_tag[0] = TAVL_CHILD;
242 y->tavl_tag[1] = TAVL_THREAD;
243 y->tavl_link[1] = x;
244 }
245 else
246 y->tavl_link[1] = x->tavl_link[0];
247 x->tavl_link[0] = y;
248 x->tavl_balance = y->tavl_balance = 0;
249 }
250 else
251 {
252 assert (x->tavl_balance == -1);
253 w = x->tavl_link[0];
254 x->tavl_link[0] = w->tavl_link[1];
255 w->tavl_link[1] = x;
256 y->tavl_link[1] = w->tavl_link[0];
257 w->tavl_link[0] = y;
258 if (w->tavl_balance == +1)
259 x->tavl_balance = 0, y->tavl_balance = -1;
260 else if (w->tavl_balance == 0)
261 x->tavl_balance = y->tavl_balance = 0;
262 else /* |w->tavl_balance == -1| */
263 x->tavl_balance = +1, y->tavl_balance = 0;
264 w->tavl_balance = 0;
265 if (w->tavl_tag[0] == TAVL_THREAD)
266 {
267 y->tavl_tag[1] = TAVL_THREAD;
268 y->tavl_link[1] = w;
269 w->tavl_tag[0] = TAVL_CHILD;
270 }
271 if (w->tavl_tag[1] == TAVL_THREAD)
272 {
273 x->tavl_tag[0] = TAVL_THREAD;
274 x->tavl_link[0] = w;
275 w->tavl_tag[1] = TAVL_CHILD;
276 }
277 }
278 }
279 else
280 return &n->tavl_data;
281 z->tavl_link[y != z->tavl_link[0]] = w;
282
283 return &n->tavl_data;
284 }
285
286 /* Returns the parent of |node| within |tree|,
287 or a pointer to |tavl_root| if |s| is the root of the tree. */
288 static struct tavl_node *
289 find_parent (struct tavl_table *tree, struct tavl_node *node)
290 {
291 if (node != tree->tavl_root)
292 {
293 struct tavl_node *x, *y;
294
295 for (x = y = node; ; x = x->tavl_link[0], y = y->tavl_link[1])
296 if (y->tavl_tag[1] == TAVL_THREAD)
297 {
298 struct tavl_node *p = y->tavl_link[1];
299 if (p == NULL || p->tavl_link[0] != node)
300 {
301 while (x->tavl_tag[0] == TAVL_CHILD)
302 x = x->tavl_link[0];
303 p = x->tavl_link[0];
304 }
305 return p;
306 }
307 else if (x->tavl_tag[0] == TAVL_THREAD)
308 {
309 struct tavl_node *p = x->tavl_link[0];
310 if (p == NULL || p->tavl_link[1] != node)
311 {
312 while (y->tavl_tag[1] == TAVL_CHILD)
313 y = y->tavl_link[1];
314 p = y->tavl_link[1];
315 }
316 return p;
317 }
318 }
319 else
320 return (struct tavl_node *) &tree->tavl_root;
321 }
322
323 /* Deletes from |tree| and returns an item matching |item|.
324 Returns a null pointer if no matching item found. */
325 void *
326 marpa__tavl_delete (struct tavl_table *tree, const void *item)
327 {
328 struct tavl_node *p; /* Traverses tree to find node to delete. */
329 struct tavl_node *q; /* Parent of |p|. */
330 int dir; /* Index into |q->tavl_link[]| to get |p|. */
331 int cmp; /* Result of comparison between |item| and |p|. */
332
333 assert (tree != NULL && item != NULL);
334
335 if (tree->tavl_root == NULL)
336 return NULL;
337
338 q = (struct tavl_node *) &tree->tavl_root;
339 p = tree->tavl_root;
340 dir = 0;
341 for (;;)
342 {
343 cmp = tree->tavl_compare (item, p->tavl_data, tree->tavl_param);
344 if (cmp == 0)
345 break;
346 dir = cmp > 0;
347
348 q = p;
349 if (p->tavl_tag[dir] == TAVL_THREAD)
350 return NULL;
351 p = p->tavl_link[dir];
352 }
353 item = p->tavl_data;
354
355 if (p->tavl_tag[1] == TAVL_THREAD)
356 {
357 if (p->tavl_tag[0] == TAVL_CHILD)
358 {
359 struct tavl_node *t = p->tavl_link[0];
360 while (t->tavl_tag[1] == TAVL_CHILD)
361 t = t->tavl_link[1];
362 t->tavl_link[1] = p->tavl_link[1];
363 q->tavl_link[dir] = p->tavl_link[0];
364 }
365 else
366 {
367 q->tavl_link[dir] = p->tavl_link[dir];
368 if (q != (struct tavl_node *) &tree->tavl_root)
369 q->tavl_tag[dir] = TAVL_THREAD;
370 }
371 }
372 else
373 {
374 struct tavl_node *r = p->tavl_link[1];
375 if (r->tavl_tag[0] == TAVL_THREAD)
376 {
377 r->tavl_link[0] = p->tavl_link[0];
378 r->tavl_tag[0] = p->tavl_tag[0];
379 if (r->tavl_tag[0] == TAVL_CHILD)
380 {
381 struct tavl_node *t = r->tavl_link[0];
382 while (t->tavl_tag[1] == TAVL_CHILD)
383 t = t->tavl_link[1];
384 t->tavl_link[1] = r;
385 }
386 q->tavl_link[dir] = r;
387 r->tavl_balance = p->tavl_balance;
388 q = r;
389 dir = 1;
390 }
391 else
392 {
393 struct tavl_node *s;
394
395 for (;;)
396 {
397 s = r->tavl_link[0];
398 if (s->tavl_tag[0] == TAVL_THREAD)
399 break;
400
401 r = s;
402 }
403
404 if (s->tavl_tag[1] == TAVL_CHILD)
405 r->tavl_link[0] = s->tavl_link[1];
406 else
407 {
408 r->tavl_link[0] = s;
409 r->tavl_tag[0] = TAVL_THREAD;
410 }
411
412 s->tavl_link[0] = p->tavl_link[0];
413 if (p->tavl_tag[0] == TAVL_CHILD)
414 {
415 struct tavl_node *t = p->tavl_link[0];
416 while (t->tavl_tag[1] == TAVL_CHILD)
417 t = t->tavl_link[1];
418 t->tavl_link[1] = s;
419
420 s->tavl_tag[0] = TAVL_CHILD;
421 }
422
423 s->tavl_link[1] = p->tavl_link[1];
424 s->tavl_tag[1] = TAVL_CHILD;
425
426 q->tavl_link[dir] = s;
427 s->tavl_balance = p->tavl_balance;
428 q = r;
429 dir = 0;
430 }
431 }
432
433 MY_AVL_FREE( p);
434
435 while (q != (struct tavl_node *) &tree->tavl_root)
436 {
437 struct tavl_node *y = q;
438
439 q = find_parent (tree, y);
440
441 if (dir == 0)
442 {
443 dir = q->tavl_link[0] != y;
444 y->tavl_balance++;
445 if (y->tavl_balance == +1)
446 break;
447 else if (y->tavl_balance == +2)
448 {
449 struct tavl_node *x = y->tavl_link[1];
450
451 assert (x != NULL);
452 if (x->tavl_balance == -1)
453 {
454 struct tavl_node *w;
455
456 assert (x->tavl_balance == -1);
457 w = x->tavl_link[0];
458 x->tavl_link[0] = w->tavl_link[1];
459 w->tavl_link[1] = x;
460 y->tavl_link[1] = w->tavl_link[0];
461 w->tavl_link[0] = y;
462 if (w->tavl_balance == +1)
463 x->tavl_balance = 0, y->tavl_balance = -1;
464 else if (w->tavl_balance == 0)
465 x->tavl_balance = y->tavl_balance = 0;
466 else /* |w->tavl_balance == -1| */
467 x->tavl_balance = +1, y->tavl_balance = 0;
468 w->tavl_balance = 0;
469 if (w->tavl_tag[0] == TAVL_THREAD)
470 {
471 y->tavl_tag[1] = TAVL_THREAD;
472 y->tavl_link[1] = w;
473 w->tavl_tag[0] = TAVL_CHILD;
474 }
475 if (w->tavl_tag[1] == TAVL_THREAD)
476 {
477 x->tavl_tag[0] = TAVL_THREAD;
478 x->tavl_link[0] = w;
479 w->tavl_tag[1] = TAVL_CHILD;
480 }
481 q->tavl_link[dir] = w;
482 }
483 else
484 {
485 q->tavl_link[dir] = x;
486
487 if (x->tavl_balance == 0)
488 {
489 y->tavl_link[1] = x->tavl_link[0];
490 x->tavl_link[0] = y;
491 x->tavl_balance = -1;
492 y->tavl_balance = +1;
493 break;
494 }
495 else /* |x->tavl_balance == +1| */
496 {
497 if (x->tavl_tag[0] == TAVL_CHILD)
498 y->tavl_link[1] = x->tavl_link[0];
499 else
500 {
501 y->tavl_tag[1] = TAVL_THREAD;
502 x->tavl_tag[0] = TAVL_CHILD;
503 }
504 x->tavl_link[0] = y;
505 y->tavl_balance = x->tavl_balance = 0;
506 }
507 }
508 }
509 }
510 else
511 {
512 dir = q->tavl_link[0] != y;
513 y->tavl_balance--;
514 if (y->tavl_balance == -1)
515 break;
516 else if (y->tavl_balance == -2)
517 {
518 struct tavl_node *x = y->tavl_link[0];
519 assert (x != NULL);
520 if (x->tavl_balance == +1)
521 {
522 struct tavl_node *w;
523
524 assert (x->tavl_balance == +1);
525 w = x->tavl_link[1];
526 x->tavl_link[1] = w->tavl_link[0];
527 w->tavl_link[0] = x;
528 y->tavl_link[0] = w->tavl_link[1];
529 w->tavl_link[1] = y;
530 if (w->tavl_balance == -1)
531 x->tavl_balance = 0, y->tavl_balance = +1;
532 else if (w->tavl_balance == 0)
533 x->tavl_balance = y->tavl_balance = 0;
534 else /* |w->tavl_balance == +1| */
535 x->tavl_balance = -1, y->tavl_balance = 0;
536 w->tavl_balance = 0;
537 if (w->tavl_tag[0] == TAVL_THREAD)
538 {
539 x->tavl_tag[1] = TAVL_THREAD;
540 x->tavl_link[1] = w;
541 w->tavl_tag[0] = TAVL_CHILD;
542 }
543 if (w->tavl_tag[1] == TAVL_THREAD)
544 {
545 y->tavl_tag[0] = TAVL_THREAD;
546 y->tavl_link[0] = w;
547 w->tavl_tag[1] = TAVL_CHILD;
548 }
549 q->tavl_link[dir] = w;
550 }
551 else
552 {
553 q->tavl_link[dir] = x;
554
555 if (x->tavl_balance == 0)
556 {
557 y->tavl_link[0] = x->tavl_link[1];
558 x->tavl_link[1] = y;
559 x->tavl_balance = +1;
560 y->tavl_balance = -1;
561 break;
562 }
563 else /* |x->tavl_balance == -1| */
564 {
565 if (x->tavl_tag[1] == TAVL_CHILD)
566 y->tavl_link[0] = x->tavl_link[1];
567 else
568 {
569 y->tavl_tag[0] = TAVL_THREAD;
570 x->tavl_tag[1] = TAVL_CHILD;
571 }
572 x->tavl_link[1] = y;
573 y->tavl_balance = x->tavl_balance = 0;
574 }
575 }
576 }
577 }
578 }
579
580 tree->tavl_count--;
581 return (void *) item;
582 }
583
584 /* Initializes |trav| for use with |tree|
585 and selects the null node. */
586 void
587 marpa__tavl_t_init (struct tavl_traverser *trav, struct tavl_table *tree)
588 {
589 trav->tavl_table = tree;
590 trav->tavl_node = NULL;
591 }
592
593 /* Initializes |trav| for |tree|.
594 Returns data item in |tree| with the least value,
595 or |NULL| if |tree| is empty. */
596 void *
597 marpa__tavl_t_first (struct tavl_traverser *trav, struct tavl_table *tree)
598 {
599 assert (tree != NULL && trav != NULL);
600
601 trav->tavl_table = tree;
602 trav->tavl_node = tree->tavl_root;
603 if (trav->tavl_node != NULL)
604 {
605 while (trav->tavl_node->tavl_tag[0] == TAVL_CHILD)
606 trav->tavl_node = trav->tavl_node->tavl_link[0];
607 return trav->tavl_node->tavl_data;
608 }
609 else
610 return NULL;
611 }
612
613 /* Initializes |trav| for |tree|.
614 Returns data item in |tree| with the greatest value,
615 or |NULL| if |tree| is empty. */
616 void *
617 marpa__tavl_t_last (struct tavl_traverser *trav, struct tavl_table *tree)
618 {
619 assert (tree != NULL && trav != NULL);
620
621 trav->tavl_table = tree;
622 trav->tavl_node = tree->tavl_root;
623 if (trav->tavl_node != NULL)
624 {
625 while (trav->tavl_node->tavl_tag[1] == TAVL_CHILD)
626 trav->tavl_node = trav->tavl_node->tavl_link[1];
627 return trav->tavl_node->tavl_data;
628 }
629 else
630 return NULL;
631 }
632
633 /* Searches for |item| in |tree|.
634 If found, initializes |trav| to the item found and returns the item
635 as well.
636 If there is no matching item, initializes |trav| to the null item
637 and returns |NULL|. */
638 void *
639 marpa__tavl_t_find (struct tavl_traverser *trav, struct tavl_table *tree, void *item)
640 {
641 struct tavl_node *p;
642
643 assert (trav != NULL && tree != NULL && item != NULL);
644
645 trav->tavl_table = tree;
646 trav->tavl_node = NULL;
647
648 p = tree->tavl_root;
649 if (p == NULL)
650 return NULL;
651
652 for (;;)
653 {
654 int cmp, dir;
655
656 cmp = tree->tavl_compare (item, p->tavl_data, tree->tavl_param);
657 if (cmp == 0)
658 {
659 trav->tavl_node = p;
660 return p->tavl_data;
661 }
662
663 dir = cmp > 0;
664 if (p->tavl_tag[dir] == TAVL_CHILD)
665 p = p->tavl_link[dir];
666 else
667 return NULL;
668 }
669 }
670
671 /* Attempts to insert |item| into |tree|.
672 If |item| is inserted successfully, it is returned and |trav| is
673 initialized to its location.
674 If a duplicate is found, it is returned and |trav| is initialized to
675 its location. No replacement of the item occurs.
676 If a memory allocation failure occurs, |NULL| is returned and |trav|
677 is initialized to the null item. */
678 void *
679 marpa__tavl_t_insert (struct tavl_traverser *trav,
680 struct tavl_table *tree, void *item)
681 {
682 void **p;
683
684 assert (trav != NULL && tree != NULL && item != NULL);
685
686 p = marpa__tavl_probe (tree, item);
687 if (p != NULL)
688 {
689 trav->tavl_table = tree;
690 trav->tavl_node =
691 ((struct tavl_node *)
692 ((char *) p - offsetof (struct tavl_node, tavl_data)));
693 return *p;
694 }
695 else
696 {
697 marpa__tavl_t_init (trav, tree);
698 return NULL;
699 }
700 }
701
702 /* Initializes |trav| to have the same current node as |src|. */
703 void *
704 marpa__tavl_t_copy (struct tavl_traverser *trav, const struct tavl_traverser *src)
705 {
706 assert (trav != NULL && src != NULL);
707
708 trav->tavl_table = src->tavl_table;
709 trav->tavl_node = src->tavl_node;
710
711 return trav->tavl_node != NULL ? trav->tavl_node->tavl_data : NULL;
712 }
713
714 /* Returns the next data item in inorder
715 within the tree being traversed with |trav|,
716 or if there are no more data items returns |NULL|. */
717 void *
718 marpa__tavl_t_next (struct tavl_traverser *trav)
719 {
720 assert (trav != NULL);
721
722 if (trav->tavl_node == NULL)
723 return marpa__tavl_t_first (trav, trav->tavl_table);
724 else if (trav->tavl_node->tavl_tag[1] == TAVL_THREAD)
725 {
726 trav->tavl_node = trav->tavl_node->tavl_link[1];
727 return trav->tavl_node != NULL ? trav->tavl_node->tavl_data : NULL;
728 }
729 else
730 {
731 trav->tavl_node = trav->tavl_node->tavl_link[1];
732 while (trav->tavl_node->tavl_tag[0] == TAVL_CHILD)
733 trav->tavl_node = trav->tavl_node->tavl_link[0];
734 return trav->tavl_node->tavl_data;
735 }
736 }
737
738 /* Returns the previous data item in inorder
739 within the tree being traversed with |trav|,
740 or if there are no more data items returns |NULL|. */
741 void *
742 marpa__tavl_t_prev (struct tavl_traverser *trav)
743 {
744 assert (trav != NULL);
745
746 if (trav->tavl_node == NULL)
747 return marpa__tavl_t_last (trav, trav->tavl_table);
748 else if (trav->tavl_node->tavl_tag[0] == TAVL_THREAD)
749 {
750 trav->tavl_node = trav->tavl_node->tavl_link[0];
751 return trav->tavl_node != NULL ? trav->tavl_node->tavl_data : NULL;
752 }
753 else
754 {
755 trav->tavl_node = trav->tavl_node->tavl_link[0];
756 while (trav->tavl_node->tavl_tag[1] == TAVL_CHILD)
757 trav->tavl_node = trav->tavl_node->tavl_link[1];
758 return trav->tavl_node->tavl_data;
759 }
760 }
761
762 /* Returns |trav|'s current item. */
763 void *
764 marpa__tavl_t_cur (struct tavl_traverser *trav)
765 {
766 assert (trav != NULL);
767
768 return trav->tavl_node != NULL ? trav->tavl_node->tavl_data : NULL;
769 }
770
771 /* Replaces the current item in |trav| by |new| and returns the item replaced.
772 |trav| must not have the null item selected.
773 The new item must not upset the ordering of the tree. */
774 void *
775 marpa__tavl_t_replace (struct tavl_traverser *trav, void *new)
776 {
777 void *old;
778
779 assert (trav != NULL && trav->tavl_node != NULL && new != NULL);
780 old = trav->tavl_node->tavl_data;
781 trav->tavl_node->tavl_data = new;
782 return old;
783 }
784
785 /* Creates a new node as a child of |dst| on side |dir|.
786 Copies data and |tavl_balance| from |src| into the new node,
787 applying |copy()|, if non-null.
788 Returns nonzero only if fully successful.
789 Regardless of success, integrity of the tree structure is assured,
790 though failure may leave a null pointer in a |tavl_data| member. */
791 static int
792 copy_node (struct tavl_table *tree,
793 struct tavl_node *dst, int dir,
794 const struct tavl_node *src, tavl_copy_func *copy)
795 {
796 struct tavl_node *new =
797 MY_AVL_MALLOC( sizeof *new);
798
799 new->tavl_link[dir] = dst->tavl_link[dir];
800 new->tavl_tag[dir] = TAVL_THREAD;
801 new->tavl_link[!dir] = dst;
802 new->tavl_tag[!dir] = TAVL_THREAD;
803 dst->tavl_link[dir] = new;
804 dst->tavl_tag[dir] = TAVL_CHILD;
805
806 new->tavl_balance = src->tavl_balance;
807 if (copy == NULL)
808 new->tavl_data = src->tavl_data;
809 else
810 {
811 new->tavl_data = copy (src->tavl_data, tree->tavl_param);
812 if (new->tavl_data == NULL)
813 return 0;
814 }
815
816 return 1;
817 }
818
819 /* Destroys |new| with |tavl_destroy (new, destroy)|,
820 first initializing the right link in |new| that has
821 not yet been initialized. */
822 static void
823 copy_error_recovery (struct tavl_node *p,
824 struct tavl_table *new, tavl_item_func *destroy)
825 {
826 new->tavl_root = p;
827 if (p != NULL)
828 {
829 while (p->tavl_tag[1] == TAVL_CHILD)
830 p = p->tavl_link[1];
831 p->tavl_link[1] = NULL;
832 }
833 marpa__tavl_destroy (new, destroy);
834 }
835
836 /* Copies |org| to a newly created tree, which is returned.
837 If |copy != NULL|, each data item in |org| is first passed to |copy|,
838 and the return values are inserted into the tree,
839 with |NULL| return values taken as indications of failure.
840 On failure, destroys the partially created new tree,
841 applying |destroy|, if non-null, to each item in the new tree so far,
842 and returns |NULL|.
843 If |allocator != NULL|, it is used for allocation in the new tree.
844 Otherwise, the same allocator used for |org| is used. */
845 struct tavl_table *
846 marpa__tavl_copy (const struct tavl_table *org, tavl_copy_func *copy,
847 tavl_item_func *destroy)
848 {
849 struct tavl_table *new;
850
851 const struct tavl_node *p;
852 struct tavl_node *q;
853 struct tavl_node rp, rq;
854
855 assert (org != NULL);
856 new = marpa__tavl_create (org->tavl_compare, org->tavl_param);
857 if (new == NULL)
858 return NULL;
859
860 new->tavl_count = org->tavl_count;
861 if (new->tavl_count == 0)
862 return new;
863
864 p = &rp;
865 rp.tavl_link[0] = org->tavl_root;
866 rp.tavl_tag[0] = TAVL_CHILD;
867
868 q = &rq;
869 rq.tavl_link[0] = NULL;
870 rq.tavl_tag[0] = TAVL_THREAD;
871
872 for (;;)
873 {
874 if (p->tavl_tag[0] == TAVL_CHILD)
875 {
876 if (!copy_node (new, q, 0, p->tavl_link[0], copy))
877 {
878 copy_error_recovery (rq.tavl_link[0], new, destroy);
879 return NULL;
880 }
881
882 p = p->tavl_link[0];
883 q = q->tavl_link[0];
884 }
885 else
886 {
887 while (p->tavl_tag[1] == TAVL_THREAD)
888 {
889 p = p->tavl_link[1];
890 if (p == NULL)
891 {
892 q->tavl_link[1] = NULL;
893 new->tavl_root = rq.tavl_link[0];
894 return new;
895 }
896
897 q = q->tavl_link[1];
898 }
899
900 p = p->tavl_link[1];
901 q = q->tavl_link[1];
902 }
903
904 if (p->tavl_tag[1] == TAVL_CHILD)
905 if (!copy_node (new, q, 1, p->tavl_link[1], copy))
906 {
907 copy_error_recovery (rq.tavl_link[0], new, destroy);
908 return NULL;
909 }
910 }
911 }
912
913 /* Frees storage allocated for |tree|.
914 If |destroy != NULL|, applies it to each data item in inorder. */
915 void
916 marpa__tavl_destroy (struct tavl_table *tree, tavl_item_func *destroy)
917 {
918 struct tavl_node *p; /* Current node. */
919 struct tavl_node *n; /* Next node. */
920
921 p = tree->tavl_root;
922 if (p != NULL)
923 while (p->tavl_tag[0] == TAVL_CHILD)
924 p = p->tavl_link[0];
925
926 while (p != NULL)
927 {
928 n = p->tavl_link[1];
929 if (p->tavl_tag[1] == TAVL_CHILD)
930 while (n->tavl_tag[0] == TAVL_CHILD)
931 n = n->tavl_link[0];
932
933 if (destroy != NULL && p->tavl_data != NULL)
934 destroy (p->tavl_data, tree->tavl_param);
935 MY_AVL_FREE( p);
936
937 p = n;
938 }
939
940 MY_AVL_FREE( tree);
941 }
942
943 static void fatal(const char* message) {
944 puts(message);
945 abort();
946 }
947
948 /* Allocates |size| bytes of space using |malloc()|.
949 Returns a null pointer if allocation fails. */
950 static void *
951 tavl_malloc (struct libavl_allocator *allocator UNUSED, size_t size UNUSED)
952 {
953 fatal("Internal error: legacy TAVL malloc called");
954 return NULL;
955 }
956
957 /* Frees |block|. */
958 static void tavl_free (struct libavl_allocator *allocator UNUSED, void *block UNUSED)
959 {
960 fatal("Internal error: legacy TAVL free called");
961 }
962
963 /* Default memory allocator that uses |malloc()| and |free()|. */
964 /* Fail allocator -- should not be used and always fails. */
965 struct libavl_allocator default_allocator =
966 {
967 tavl_malloc,
968 tavl_free
969 };
970 struct libavl_allocator *marpa__tavl_allocator_default = &default_allocator;
971
972 #undef NDEBUG
973 #include <assert.h>
974
975 /* Asserts that |tavl_insert()| succeeds at inserting |item| into |table|. */
976 void
977 (marpa__tavl_assert_insert) (struct tavl_table *table, void *item)
978 {
979 void **p = marpa__tavl_probe (table, item);
980 assert (p != NULL && *p == item);
981 }
982
983 /* Asserts that |tavl_delete()| really removes |item| from |table|,
984 and returns the removed item. */
985 void *
986 (marpa__tavl_assert_delete) (struct tavl_table *table, void *item)
987 {
988 void *p = marpa__tavl_delete (table, item);
989 assert (p != NULL);
990 return p;
991 }
992