Debian Codegraph @

engine/read_only/marpa_avl.c

raw
1 /* Copyright 2022 Jeffrey Kegler */
2
3 /* This is a modification of avl.c, from Ben Pfaff's libavl,
4 which was under the LGPL 3. Here is the copyright notice
5 from that file:
6
7 libavl - library for manipulation of binary trees.
8 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004 Free Software
9 Foundation, Inc.
10
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Lesser General Public
13 License as published by the Free Software Foundation; either
14 version 3 of the License, or (at your option) any later version.
15
16 This library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
20
21 You should have received a copy of the GNU Lesser General Public
22 License along with this library; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 02110-1301 USA.
25 */
26
27 #include "config.h"
28
29 #include <assert.h>
30 #include <stdio.h>
31 #include "marpa.h"
32
33 #include "marpa_ami.h"
34 #include "marpa_obs.h"
35 #include "marpa_avl.h"
36
37 /* An AVL tree node. */
38 typedef struct marpa_avl_node* NODE;
39
40 /* Creates and returns a new table
41 with comparison function |compare| using parameter |param|.
42 */
43 MARPA_AVL_TREE
44 _marpa_avl_create (marpa_avl_comparison_func *compare, void *param)
45 {
46 MARPA_AVL_TREE tree;
47 struct marpa_obstack *avl_obstack = marpa_obs_init;
48
49 assert (compare != NULL);
50
51 tree = marpa_obs_new( avl_obstack, struct marpa_avl_table, 1);
52 tree->avl_obstack = avl_obstack;
53 tree->avl_root = NULL;
54 tree->avl_compare = compare;
55 tree->avl_param = param;
56 tree->avl_count = 0;
57 tree->avl_generation = 0;
58
59 return tree;
60 }
61
62 /* Search |tree| for an item matching |item|, and return it if found.
63 Otherwise return |NULL|. */
64 void *
65 _marpa_avl_find (const MARPA_AVL_TREE tree, const void *item)
66 {
67 NODE p;
68
69 assert (tree != NULL && item != NULL);
70 for (p = tree->avl_root; p != NULL; )
71 {
72 int cmp = tree->avl_compare (item, p->avl_data, tree->avl_param);
73
74 if (cmp < 0)
75 p = p->avl_link[0];
76 else if (cmp > 0)
77 p = p->avl_link[1];
78 else /* |cmp == 0| */
79 return p->avl_data;
80 }
81
82 return NULL;
83 }
84
85 /* Search |tree| for an item matching or after |item|, and return it if found.
86 Otherwise return |NULL|. */
87 void *
88 _marpa_avl_at_or_after (const MARPA_AVL_TREE tree, const void *item)
89 {
90 NODE p;
91 NODE at_or_after = NULL;
92
93 assert (tree != NULL && item != NULL);
94 for (p = tree->avl_root; p != NULL;)
95 {
96 int cmp = tree->avl_compare (item, p->avl_data, tree->avl_param);
97
98 if (cmp < 0)
99 {
100 at_or_after = p;
101 p = p->avl_link[0];
102 }
103 else if (cmp > 0)
104 {
105 p = p->avl_link[1];
106 }
107 else /* |cmp == 0| */
108 return p->avl_data;
109 }
110
111 return at_or_after ? at_or_after->avl_data : NULL;
112 }
113
114 /* Inserts |item| into |tree| and returns a pointer to |item|'s address.
115 If a duplicate item is found in the tree,
116 returns a pointer to the duplicate without inserting |item|.
117 */
118 void **
119 _marpa_avl_probe (MARPA_AVL_TREE tree, void *item)
120 {
121 NODE y, z; /* Top node to update balance factor, and parent */
122 NODE p, q; /* Iterator, and parent. */
123 NODE n; /* Newly inserted node. */
124 NODE w; /* New root of rebalanced subtree. */
125 int dir; /* Direction to descend. */
126
127 unsigned char da[MARPA_AVL_MAX_HEIGHT]; /* Cached comparison results. */
128 int k = 0; /* Number of cached results. */
129
130 assert (tree != NULL && item != NULL);
131
132 z = (NODE) &tree->avl_root;
133 y = tree->avl_root;
134 dir = 0;
135 for (q = z, p = y; p != NULL; q = p, p = p->avl_link[dir])
136 {
137 int cmp = tree->avl_compare (item, p->avl_data, tree->avl_param);
138 if (cmp == 0)
139 return &p->avl_data;
140
141 if (p->avl_balance != 0)
142 z = q, y = p, k = 0;
143 dir = cmp > 0;
144 da[k++] = (unsigned char)dir;
145 }
146
147 n = q->avl_link[dir] = marpa_obs_new (tree->avl_obstack, struct marpa_avl_node, 1);
148
149 tree->avl_count++;
150 n->avl_data = item;
151 n->avl_link[0] = n->avl_link[1] = NULL;
152 n->avl_balance = 0;
153 if (y == NULL)
154 return &n->avl_data;
155
156 for (p = y, k = 0; p != n; p = p->avl_link[da[k]], k++)
157 if (da[k] == 0)
158 p->avl_balance--;
159 else
160 p->avl_balance++;
161
162 if (y->avl_balance == -2)
163 {
164 NODE x = y->avl_link[0];
165 if (x->avl_balance == -1)
166 {
167 w = x;
168 y->avl_link[0] = x->avl_link[1];
169 x->avl_link[1] = y;
170 x->avl_balance = y->avl_balance = 0;
171 }
172 else
173 {
174 assert (x->avl_balance == +1);
175 w = x->avl_link[1];
176 x->avl_link[1] = w->avl_link[0];
177 w->avl_link[0] = x;
178 y->avl_link[0] = w->avl_link[1];
179 w->avl_link[1] = y;
180 if (w->avl_balance == -1)
181 x->avl_balance = 0, y->avl_balance = +1;
182 else if (w->avl_balance == 0)
183 x->avl_balance = y->avl_balance = 0;
184 else /* |w->avl_balance == +1| */
185 x->avl_balance = -1, y->avl_balance = 0;
186 w->avl_balance = 0;
187 }
188 }
189 else if (y->avl_balance == +2)
190 {
191 NODE x = y->avl_link[1];
192 if (x->avl_balance == +1)
193 {
194 w = x;
195 y->avl_link[1] = x->avl_link[0];
196 x->avl_link[0] = y;
197 x->avl_balance = y->avl_balance = 0;
198 }
199 else
200 {
201 assert (x->avl_balance == -1);
202 w = x->avl_link[0];
203 x->avl_link[0] = w->avl_link[1];
204 w->avl_link[1] = x;
205 y->avl_link[1] = w->avl_link[0];
206 w->avl_link[0] = y;
207 if (w->avl_balance == +1)
208 x->avl_balance = 0, y->avl_balance = -1;
209 else if (w->avl_balance == 0)
210 x->avl_balance = y->avl_balance = 0;
211 else /* |w->avl_balance == -1| */
212 x->avl_balance = +1, y->avl_balance = 0;
213 w->avl_balance = 0;
214 }
215 }
216 else
217 return &n->avl_data;
218 z->avl_link[y != z->avl_link[0]] = w;
219
220 tree->avl_generation++;
221 return &n->avl_data;
222 }
223
224 /* Inserts |item| into |table|.
225 Returns |NULL| if |item| was successfully inserted.
226 Otherwise, returns the duplicate item. */
227 void *
228 _marpa_avl_insert (MARPA_AVL_TREE table, void *item)
229 {
230 void **p = _marpa_avl_probe (table, item);
231 return p == NULL || *p == item ? NULL : *p;
232 }
233
234 /* Inserts |item| into |table|, replacing any duplicate item.
235 Returns |NULL| if |item| was inserted without replacing a duplicate.
236 Otherwise, returns the item that was replaced. */
237 void *
238 _marpa_avl_replace (MARPA_AVL_TREE table, void *item)
239 {
240 void **p = _marpa_avl_probe (table, item);
241 if (p == NULL || *p == item)
242 return NULL;
243 else
244 {
245 void *r = *p;
246 *p = item;
247 return r;
248 }
249 }
250
251 /* Refreshes the stack of parent pointers in |trav|
252 and updates its generation number. */
253 static void
254 trav_refresh (struct marpa_avl_traverser *trav)
255 {
256 assert (trav != NULL);
257
258 trav->avl_generation = trav->avl_table->avl_generation;
259
260 if (trav->avl_node != NULL)
261 {
262 marpa_avl_comparison_func *cmp = trav->avl_table->avl_compare;
263 void *param = trav->avl_table->avl_param;
264 NODE node = trav->avl_node;
265 NODE i;
266
267 trav->avl_height = 0;
268 for (i = trav->avl_table->avl_root; i != node; )
269 {
270 assert (trav->avl_height < MARPA_AVL_MAX_HEIGHT);
271 assert (i != NULL);
272
273 trav->avl_stack[trav->avl_height++] = i;
274 i = i->avl_link[cmp (node->avl_data, i->avl_data, param) > 0];
275 }
276 }
277 }
278
279 /* Assuming that the tree is already set,
280 set the traverser to its initial values */
281 static inline void trav_reset(MARPA_AVL_TRAV trav)
282 {
283 MARPA_AVL_TREE tree = MARPA_TREE_OF_AVL_TRAV(trav);
284 trav->avl_node = NULL;
285 trav->avl_height = 0;
286 trav->avl_generation = tree->avl_generation;
287 }
288
289 /* Initializes |trav| for use with |tree|
290 and selects the null node. */
291 MARPA_AVL_TRAV _marpa_avl_t_init (MARPA_AVL_TREE tree)
292 {
293 const MARPA_AVL_TRAV trav
294 = marpa_obs_new (MARPA_AVL_OBSTACK (tree), struct marpa_avl_traverser, 1);
295 trav->avl_table = tree;
296 trav_reset(trav);
297 return trav;
298 }
299
300 /* Initializes |trav| for use with |tree|
301 and selects the null node. */
302 MARPA_AVL_TRAV _marpa_avl_t_reset (MARPA_AVL_TRAV trav)
303 {
304 trav_reset(trav);
305 return trav;
306 }
307
308 /* Selects and returns a pointer to the least-valued item.
309 Returns |NULL| if |tree| contains no nodes. */
310 void *
311 _marpa_avl_t_first (MARPA_AVL_TRAV trav)
312 {
313 NODE x;
314 MARPA_AVL_TREE tree = MARPA_TREE_OF_AVL_TRAV(trav);
315
316 assert (trav != NULL);
317
318 x = tree->avl_root;
319 if (x != NULL)
320 while (x->avl_link[0] != NULL)
321 {
322 assert (trav->avl_height < MARPA_AVL_MAX_HEIGHT);
323 trav->avl_stack[trav->avl_height++] = x;
324 x = x->avl_link[0];
325 }
326 trav->avl_node = x;
327
328 return x != NULL ? x->avl_data : NULL;
329 }
330
331 /* Selects and returns a pointer to the greatest-valued item.
332 Returns |NULL| if |tree| contains no nodes. */
333 void *
334 _marpa_avl_t_last (MARPA_AVL_TRAV trav)
335 {
336 NODE x;
337 MARPA_AVL_TREE tree = MARPA_TREE_OF_AVL_TRAV(trav);
338
339 assert (trav != NULL);
340
341 x = tree->avl_root;
342 if (x != NULL)
343 while (x->avl_link[1] != NULL)
344 {
345 assert (trav->avl_height < MARPA_AVL_MAX_HEIGHT);
346 trav->avl_stack[trav->avl_height++] = x;
347 x = x->avl_link[1];
348 }
349 trav->avl_node = x;
350
351 return x != NULL ? x->avl_data : NULL;
352 }
353
354 /* Searches for |item| in |tree| of |trav|.
355 If found, sets |trav| to the item found and returns the item
356 as well.
357 If there is no matching item, initializes |trav| to the null item
358 and returns |NULL|. */
359 void *
360 _marpa_avl_t_find (MARPA_AVL_TRAV trav, void *item)
361 {
362 NODE p, q;
363 MARPA_AVL_TREE tree = MARPA_TREE_OF_AVL_TRAV(trav);
364
365 assert (trav != NULL && item != NULL);
366 for (p = tree->avl_root; p != NULL; p = q)
367 {
368 int cmp = tree->avl_compare (item, p->avl_data, tree->avl_param);
369
370 if (cmp < 0)
371 q = p->avl_link[0];
372 else if (cmp > 0)
373 q = p->avl_link[1];
374 else /* |cmp == 0| */
375 {
376 trav->avl_node = p;
377 return p->avl_data;
378 }
379
380 assert (trav->avl_height < MARPA_AVL_MAX_HEIGHT);
381 trav->avl_stack[trav->avl_height++] = p;
382 }
383
384 trav->avl_height = 0;
385 trav->avl_node = NULL;
386 return NULL;
387 }
388
389 /* Searches for |item| in |tree| of |trav|.
390 Sets |trav| to the item, if found,
391 otherwise to the first one after |item|,
392 and returns the item as well.
393 If there is no item after |item|, initializes |trav| to the null item
394 and returns |NULL|.
395
396 The implementation just calls |_marpa_at_or_after| and
397 |_marpa_avl_t_find|. This means the tree is searched twice,
398 but a custom routine would have to build a stack
399 and then possibly back it out, so a double traversal seems
400 inescapable.
401 A custom routine would be faster, but not by much.
402 */
403 void *
404 _marpa_avl_t_at_or_after (MARPA_AVL_TRAV trav, void* sought_item)
405 {
406 const MARPA_AVL_TREE tree = MARPA_TREE_OF_AVL_TRAV(trav);
407 void* found_item = _marpa_avl_at_or_after (tree, sought_item);
408 if (!found_item) return NULL;
409 return _marpa_avl_t_find(trav, found_item);
410 }
411
412 /* Attempts to insert |item| into tree of |trav|.
413 If |item| is inserted successfully, it is returned and |trav| is
414 initialized to its location.
415 If a duplicate is found, it is returned and |trav| is initialized to
416 its location. No replacement of the item occurs.
417 */
418 void *
419 _marpa_avl_t_insert ( MARPA_AVL_TRAV trav, void *item)
420 {
421 void **p;
422 MARPA_AVL_TREE tree = MARPA_TREE_OF_AVL_TRAV(trav);
423
424 assert (trav != NULL && tree != NULL && item != NULL);
425
426 p = _marpa_avl_probe (tree, item);
427 if (p != NULL)
428 {
429 trav->avl_table = tree;
430 trav->avl_node =
431 ((NODE )
432 ((char *) p - offsetof (struct marpa_avl_node, avl_data)));
433 trav->avl_generation = tree->avl_generation - 1;
434 return *p;
435 }
436 else
437 {
438 trav_reset (trav);
439 return NULL;
440 }
441 }
442
443 /* Initializes |trav| to have the same current node as |src|. */
444 void *
445 _marpa_avl_t_copy (struct marpa_avl_traverser *trav, const struct marpa_avl_traverser *src)
446 {
447 assert (trav != NULL && src != NULL);
448
449 if (trav != src)
450 {
451 trav->avl_table = src->avl_table;
452 trav->avl_node = src->avl_node;
453 trav->avl_generation = src->avl_generation;
454 if (trav->avl_generation == trav->avl_table->avl_generation)
455 {
456 trav->avl_height = src->avl_height;
457 memcpy (trav->avl_stack, (const void *) src->avl_stack,
458 sizeof *trav->avl_stack * trav->avl_height);
459 }
460 }
461
462 return trav->avl_node != NULL ? trav->avl_node->avl_data : NULL;
463 }
464
465 /* Returns the next data item in inorder
466 within the tree being traversed with |trav|,
467 or if there are no more data items returns |NULL|. */
468 void *
469 _marpa_avl_t_next (MARPA_AVL_TRAV trav)
470 {
471 NODE x;
472
473 assert (trav != NULL);
474
475 if (trav->avl_generation != trav->avl_table->avl_generation)
476 trav_refresh (trav);
477
478 x = trav->avl_node;
479 if (x == NULL)
480 {
481 return _marpa_avl_t_first (trav);
482 }
483 else if (x->avl_link[1] != NULL)
484 {
485 assert (trav->avl_height < MARPA_AVL_MAX_HEIGHT);
486 trav->avl_stack[trav->avl_height++] = x;
487 x = x->avl_link[1];
488
489 while (x->avl_link[0] != NULL)
490 {
491 assert (trav->avl_height < MARPA_AVL_MAX_HEIGHT);
492 trav->avl_stack[trav->avl_height++] = x;
493 x = x->avl_link[0];
494 }
495 }
496 else
497 {
498 NODE y;
499
500 do
501 {
502 if (trav->avl_height == 0)
503 {
504 trav->avl_node = NULL;
505 return NULL;
506 }
507
508 y = x;
509 x = trav->avl_stack[--trav->avl_height];
510 }
511 while (y == x->avl_link[1]);
512 }
513 trav->avl_node = x;
514
515 return x->avl_data;
516 }
517
518 /* Returns the previous data item in inorder
519 within the tree being traversed with |trav|,
520 or if there are no more data items returns |NULL|. */
521 void *
522 _marpa_avl_t_prev (MARPA_AVL_TRAV trav)
523 {
524 NODE x;
525
526 assert (trav != NULL);
527
528 if (trav->avl_generation != trav->avl_table->avl_generation)
529 trav_refresh (trav);
530
531 x = trav->avl_node;
532 if (x == NULL)
533 {
534 return _marpa_avl_t_last (trav);
535 }
536 else if (x->avl_link[0] != NULL)
537 {
538 assert (trav->avl_height < MARPA_AVL_MAX_HEIGHT);
539 trav->avl_stack[trav->avl_height++] = x;
540 x = x->avl_link[0];
541
542 while (x->avl_link[1] != NULL)
543 {
544 assert (trav->avl_height < MARPA_AVL_MAX_HEIGHT);
545 trav->avl_stack[trav->avl_height++] = x;
546 x = x->avl_link[1];
547 }
548 }
549 else
550 {
551 NODE y;
552
553 do
554 {
555 if (trav->avl_height == 0)
556 {
557 trav->avl_node = NULL;
558 return NULL;
559 }
560
561 y = x;
562 x = trav->avl_stack[--trav->avl_height];
563 }
564 while (y == x->avl_link[0]);
565 }
566 trav->avl_node = x;
567
568 return x->avl_data;
569 }
570
571 /* Returns |trav|'s current item. */
572 void *
573 _marpa_avl_t_cur (MARPA_AVL_TRAV trav)
574 {
575 assert (trav != NULL);
576
577 return trav->avl_node != NULL ? trav->avl_node->avl_data : NULL;
578 }
579
580 /* Replaces the current item in |trav| by |new| and returns the item replaced.
581 |trav| must not have the null item selected.
582 The new item must not upset the ordering of the tree. */
583 void *
584 _marpa_avl_t_replace (struct marpa_avl_traverser *trav, void *new)
585 {
586 void *old;
587
588 assert (trav != NULL && trav->avl_node != NULL && new != NULL);
589 old = trav->avl_node->avl_data;
590 trav->avl_node->avl_data = new;
591 return old;
592 }
593
594 /* Frees storage allocated for |tree|.
595 Everything is on the obstack.
596 */
597 void
598 _marpa_avl_destroy (MARPA_AVL_TREE tree)
599 {
600 if (tree == NULL)
601 return;
602 marpa_obs_free (tree->avl_obstack);
603 }
604
605 #undef NDEBUG
606 #include <assert.h>