Added vp8_update_zbin_extra
[libvpx.git] / vpx_mem / memory_manager / include / cavl_impl.h
blob5e165dd4d4a5ac2cc0f96e6a0daf6bfb89a0e5fa
1 /*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
12 /* Abstract AVL Tree Generic C Package.
13 ** Implementation generation header file.
15 ** This code is in the public domain. See cavl_tree.html for interface
16 ** documentation.
18 ** Version: 1.5 Author: Walt Karas
21 #undef L_
22 #undef L_EST_LONG_BIT
23 #undef L_SIZE
24 #undef l_tree
25 #undef L_MASK_HIGH_BIT
26 #undef L_LONG_BIT
27 #undef L_BIT_ARR_DEFN
28 #undef L_BIT_ARR_VAL
29 #undef L_BIT_ARR_0
30 #undef L_BIT_ARR_1
31 #undef L_BIT_ARR_ALL
32 #undef L_BIT_ARR_LONGS
33 #undef L_IMPL_MASK
34 #undef L_CHECK_READ_ERROR
35 #undef L_CHECK_READ_ERROR_INV_DEPTH
36 #undef L_SC
37 #undef L_BALANCE_PARAM_PREFIX
39 #ifdef AVL_UNIQUE
41 #define L_ AVL_UNIQUE
43 #else
45 #define L_(X) X
47 #endif
49 /* Determine correct storage class for functions */
50 #ifdef AVL_PRIVATE
52 #define L_SC static
54 #else
56 #define L_SC
58 #endif
60 #ifdef AVL_SIZE
62 #define L_SIZE AVL_SIZE
64 #else
66 #define L_SIZE unsigned long
68 #endif
70 #define L_MASK_HIGH_BIT ((int) ~ ((~ (unsigned) 0) >> 1))
72 /* ANSI C/ISO C++ require that a long have at least 32 bits. Set
73 ** L_EST_LONG_BIT to be the greatest multiple of 8 in the range
74 ** 32 - 64 (inclusive) that is less than or equal to the number of
75 ** bits in a long.
78 #if (((LONG_MAX >> 31) >> 7) == 0)
80 #define L_EST_LONG_BIT 32
82 #elif (((LONG_MAX >> 31) >> 15) == 0)
84 #define L_EST_LONG_BIT 40
86 #elif (((LONG_MAX >> 31) >> 23) == 0)
88 #define L_EST_LONG_BIT 48
90 #elif (((LONG_MAX >> 31) >> 31) == 0)
92 #define L_EST_LONG_BIT 56
94 #else
96 #define L_EST_LONG_BIT 64
98 #endif
100 #define L_LONG_BIT (sizeof(long) * CHAR_BIT)
102 #if ((AVL_MAX_DEPTH) > L_EST_LONG_BIT)
104 /* The maximum depth may be greater than the number of bits in a long,
105 ** so multiple longs are needed to hold a bit array indexed by node
106 ** depth. */
108 #define L_BIT_ARR_LONGS (((AVL_MAX_DEPTH) + L_LONG_BIT - 1) / L_LONG_BIT)
110 #define L_BIT_ARR_DEFN(NAME) unsigned long NAME[L_BIT_ARR_LONGS];
112 #define L_BIT_ARR_VAL(BIT_ARR, BIT_NUM) \
113 ((BIT_ARR)[(BIT_NUM) / L_LONG_BIT] & (1L << ((BIT_NUM) % L_LONG_BIT)))
115 #define L_BIT_ARR_0(BIT_ARR, BIT_NUM) \
116 (BIT_ARR)[(BIT_NUM) / L_LONG_BIT] &= ~(1L << ((BIT_NUM) % L_LONG_BIT));
118 #define L_BIT_ARR_1(BIT_ARR, BIT_NUM) \
119 (BIT_ARR)[(BIT_NUM) / L_LONG_BIT] |= 1L << ((BIT_NUM) % L_LONG_BIT);
121 #define L_BIT_ARR_ALL(BIT_ARR, BIT_VAL) \
122 { int i = L_BIT_ARR_LONGS; do (BIT_ARR)[--i] = 0L - (BIT_VAL); while(i); }
124 #else /* The bit array can definitely fit in one long */
126 #define L_BIT_ARR_DEFN(NAME) unsigned long NAME;
128 #define L_BIT_ARR_VAL(BIT_ARR, BIT_NUM) ((BIT_ARR) & (1L << (BIT_NUM)))
130 #define L_BIT_ARR_0(BIT_ARR, BIT_NUM) (BIT_ARR) &= ~(1L << (BIT_NUM));
132 #define L_BIT_ARR_1(BIT_ARR, BIT_NUM) (BIT_ARR) |= 1L << (BIT_NUM);
134 #define L_BIT_ARR_ALL(BIT_ARR, BIT_VAL) (BIT_ARR) = 0L - (BIT_VAL);
136 #endif
138 #ifdef AVL_READ_ERRORS_HAPPEN
140 #define L_CHECK_READ_ERROR(ERROR_RETURN) \
141 { if (AVL_READ_ERROR) return(ERROR_RETURN); }
143 #else
145 #define L_CHECK_READ_ERROR(ERROR_RETURN)
147 #endif
149 /* The presumed reason that an instantiation places additional fields
150 ** inside the AVL tree structure is that the SET_ and GET_ macros
151 ** need these fields. The "balance" function does not explicitly use
152 ** any fields in the AVL tree structure, so only pass an AVL tree
153 ** structure pointer to "balance" if it has instantiation-specific
154 ** fields that are (presumably) needed by the SET_/GET_ calls within
155 ** "balance".
157 #ifdef AVL_INSIDE_STRUCT
159 #define L_BALANCE_PARAM_CALL_PREFIX l_tree,
160 #define L_BALANCE_PARAM_DECL_PREFIX L_(avl) *l_tree,
162 #else
164 #define L_BALANCE_PARAM_CALL_PREFIX
165 #define L_BALANCE_PARAM_DECL_PREFIX
167 #endif
169 #ifdef AVL_IMPL_MASK
171 #define L_IMPL_MASK (AVL_IMPL_MASK)
173 #else
175 /* Define all functions. */
176 #define L_IMPL_MASK AVL_IMPL_ALL
178 #endif
180 #if (L_IMPL_MASK & AVL_IMPL_INIT)
182 L_SC void L_(init)(L_(avl) *l_tree)
184 l_tree->root = AVL_NULL;
187 #endif
189 #if (L_IMPL_MASK & AVL_IMPL_IS_EMPTY)
191 L_SC int L_(is_empty)(L_(avl) *l_tree)
193 return(l_tree->root == AVL_NULL);
196 #endif
198 /* Put the private balance function in the same compilation module as
199 ** the insert function. */
200 #if (L_IMPL_MASK & AVL_IMPL_INSERT)
202 /* Balances subtree, returns handle of root node of subtree after balancing.
204 L_SC AVL_HANDLE L_(balance)(L_BALANCE_PARAM_DECL_PREFIX AVL_HANDLE bal_h)
206 AVL_HANDLE deep_h;
208 /* Either the "greater than" or the "less than" subtree of
209 ** this node has to be 2 levels deeper (or else it wouldn't
210 ** need balancing).
212 if (AVL_GET_BALANCE_FACTOR(bal_h) > 0)
214 /* "Greater than" subtree is deeper. */
216 deep_h = AVL_GET_GREATER(bal_h, 1);
218 L_CHECK_READ_ERROR(AVL_NULL)
220 if (AVL_GET_BALANCE_FACTOR(deep_h) < 0)
222 int bf;
224 AVL_HANDLE old_h = bal_h;
225 bal_h = AVL_GET_LESS(deep_h, 1);
226 L_CHECK_READ_ERROR(AVL_NULL)
227 AVL_SET_GREATER(old_h, AVL_GET_LESS(bal_h, 1))
228 AVL_SET_LESS(deep_h, AVL_GET_GREATER(bal_h, 1))
229 AVL_SET_LESS(bal_h, old_h)
230 AVL_SET_GREATER(bal_h, deep_h)
232 bf = AVL_GET_BALANCE_FACTOR(bal_h);
234 if (bf != 0)
236 if (bf > 0)
238 AVL_SET_BALANCE_FACTOR(old_h, -1)
239 AVL_SET_BALANCE_FACTOR(deep_h, 0)
241 else
243 AVL_SET_BALANCE_FACTOR(deep_h, 1)
244 AVL_SET_BALANCE_FACTOR(old_h, 0)
247 AVL_SET_BALANCE_FACTOR(bal_h, 0)
249 else
251 AVL_SET_BALANCE_FACTOR(old_h, 0)
252 AVL_SET_BALANCE_FACTOR(deep_h, 0)
255 else
257 AVL_SET_GREATER(bal_h, AVL_GET_LESS(deep_h, 0))
258 AVL_SET_LESS(deep_h, bal_h)
260 if (AVL_GET_BALANCE_FACTOR(deep_h) == 0)
262 AVL_SET_BALANCE_FACTOR(deep_h, -1)
263 AVL_SET_BALANCE_FACTOR(bal_h, 1)
265 else
267 AVL_SET_BALANCE_FACTOR(deep_h, 0)
268 AVL_SET_BALANCE_FACTOR(bal_h, 0)
271 bal_h = deep_h;
274 else
276 /* "Less than" subtree is deeper. */
278 deep_h = AVL_GET_LESS(bal_h, 1);
279 L_CHECK_READ_ERROR(AVL_NULL)
281 if (AVL_GET_BALANCE_FACTOR(deep_h) > 0)
283 int bf;
284 AVL_HANDLE old_h = bal_h;
285 bal_h = AVL_GET_GREATER(deep_h, 1);
286 L_CHECK_READ_ERROR(AVL_NULL)
287 AVL_SET_LESS(old_h, AVL_GET_GREATER(bal_h, 0))
288 AVL_SET_GREATER(deep_h, AVL_GET_LESS(bal_h, 0))
289 AVL_SET_GREATER(bal_h, old_h)
290 AVL_SET_LESS(bal_h, deep_h)
292 bf = AVL_GET_BALANCE_FACTOR(bal_h);
294 if (bf != 0)
296 if (bf < 0)
298 AVL_SET_BALANCE_FACTOR(old_h, 1)
299 AVL_SET_BALANCE_FACTOR(deep_h, 0)
301 else
303 AVL_SET_BALANCE_FACTOR(deep_h, -1)
304 AVL_SET_BALANCE_FACTOR(old_h, 0)
307 AVL_SET_BALANCE_FACTOR(bal_h, 0)
309 else
311 AVL_SET_BALANCE_FACTOR(old_h, 0)
312 AVL_SET_BALANCE_FACTOR(deep_h, 0)
315 else
317 AVL_SET_LESS(bal_h, AVL_GET_GREATER(deep_h, 0))
318 AVL_SET_GREATER(deep_h, bal_h)
320 if (AVL_GET_BALANCE_FACTOR(deep_h) == 0)
322 AVL_SET_BALANCE_FACTOR(deep_h, 1)
323 AVL_SET_BALANCE_FACTOR(bal_h, -1)
325 else
327 AVL_SET_BALANCE_FACTOR(deep_h, 0)
328 AVL_SET_BALANCE_FACTOR(bal_h, 0)
331 bal_h = deep_h;
335 return(bal_h);
338 L_SC AVL_HANDLE L_(insert)(L_(avl) *l_tree, AVL_HANDLE h)
340 AVL_SET_LESS(h, AVL_NULL)
341 AVL_SET_GREATER(h, AVL_NULL)
342 AVL_SET_BALANCE_FACTOR(h, 0)
344 if (l_tree->root == AVL_NULL)
345 l_tree->root = h;
346 else
348 /* Last unbalanced node encountered in search for insertion point. */
349 AVL_HANDLE unbal = AVL_NULL;
350 /* Parent of last unbalanced node. */
351 AVL_HANDLE parent_unbal = AVL_NULL;
352 /* Balance factor of last unbalanced node. */
353 int unbal_bf;
355 /* Zero-based depth in tree. */
356 unsigned depth = 0, unbal_depth = 0;
358 /* Records a path into the tree. If bit n is true, indicates
359 ** take greater branch from the nth node in the path, otherwise
360 ** take the less branch. bit 0 gives branch from root, and
361 ** so on. */
362 L_BIT_ARR_DEFN(branch)
364 AVL_HANDLE hh = l_tree->root;
365 AVL_HANDLE parent = AVL_NULL;
366 int cmp;
370 if (AVL_GET_BALANCE_FACTOR(hh) != 0)
372 unbal = hh;
373 parent_unbal = parent;
374 unbal_depth = depth;
377 cmp = AVL_COMPARE_NODE_NODE(h, hh);
379 if (cmp == 0)
380 /* Duplicate key. */
381 return(hh);
383 parent = hh;
385 if (cmp > 0)
387 hh = AVL_GET_GREATER(hh, 1);
388 L_BIT_ARR_1(branch, depth)
390 else
392 hh = AVL_GET_LESS(hh, 1);
393 L_BIT_ARR_0(branch, depth)
396 L_CHECK_READ_ERROR(AVL_NULL)
397 depth++;
399 while (hh != AVL_NULL);
401 /* Add node to insert as leaf of tree. */
402 if (cmp < 0)
403 AVL_SET_LESS(parent, h)
404 else
405 AVL_SET_GREATER(parent, h)
407 depth = unbal_depth;
409 if (unbal == AVL_NULL)
410 hh = l_tree->root;
411 else
413 cmp = L_BIT_ARR_VAL(branch, depth) ? 1 : -1;
414 depth++;
415 unbal_bf = AVL_GET_BALANCE_FACTOR(unbal);
417 if (cmp < 0)
418 unbal_bf--;
419 else /* cmp > 0 */
420 unbal_bf++;
422 hh = cmp < 0 ? AVL_GET_LESS(unbal, 1) : AVL_GET_GREATER(unbal, 1);
423 L_CHECK_READ_ERROR(AVL_NULL)
425 if ((unbal_bf != -2) && (unbal_bf != 2))
427 /* No rebalancing of tree is necessary. */
428 AVL_SET_BALANCE_FACTOR(unbal, unbal_bf)
429 unbal = AVL_NULL;
433 if (hh != AVL_NULL)
434 while (h != hh)
436 cmp = L_BIT_ARR_VAL(branch, depth) ? 1 : -1;
437 depth++;
439 if (cmp < 0)
441 AVL_SET_BALANCE_FACTOR(hh, -1)
442 hh = AVL_GET_LESS(hh, 1);
444 else /* cmp > 0 */
446 AVL_SET_BALANCE_FACTOR(hh, 1)
447 hh = AVL_GET_GREATER(hh, 1);
450 L_CHECK_READ_ERROR(AVL_NULL)
453 if (unbal != AVL_NULL)
455 unbal = L_(balance)(L_BALANCE_PARAM_CALL_PREFIX unbal);
456 L_CHECK_READ_ERROR(AVL_NULL)
458 if (parent_unbal == AVL_NULL)
459 l_tree->root = unbal;
460 else
462 depth = unbal_depth - 1;
463 cmp = L_BIT_ARR_VAL(branch, depth) ? 1 : -1;
465 if (cmp < 0)
466 AVL_SET_LESS(parent_unbal, unbal)
467 else /* cmp > 0 */
468 AVL_SET_GREATER(parent_unbal, unbal)
474 return(h);
477 #endif
479 #if (L_IMPL_MASK & AVL_IMPL_SEARCH)
481 L_SC AVL_HANDLE L_(search)(L_(avl) *l_tree, AVL_KEY k, avl_search_type st)
483 int cmp, target_cmp;
484 AVL_HANDLE match_h = AVL_NULL;
485 AVL_HANDLE h = l_tree->root;
487 if (st & AVL_LESS)
488 target_cmp = 1;
489 else if (st & AVL_GREATER)
490 target_cmp = -1;
491 else
492 target_cmp = 0;
494 while (h != AVL_NULL)
496 cmp = AVL_COMPARE_KEY_NODE(k, h);
498 if (cmp == 0)
500 if (st & AVL_EQUAL)
502 match_h = h;
503 break;
506 cmp = -target_cmp;
508 else if (target_cmp != 0)
509 if (!((cmp ^ target_cmp) & L_MASK_HIGH_BIT))
510 /* cmp and target_cmp are both positive or both negative. */
511 match_h = h;
513 h = cmp < 0 ? AVL_GET_LESS(h, 1) : AVL_GET_GREATER(h, 1);
514 L_CHECK_READ_ERROR(AVL_NULL)
517 return(match_h);
520 #endif
522 #if (L_IMPL_MASK & AVL_IMPL_SEARCH_LEAST)
524 L_SC AVL_HANDLE L_(search_least)(L_(avl) *l_tree)
526 AVL_HANDLE h = l_tree->root;
527 AVL_HANDLE parent = AVL_NULL;
529 while (h != AVL_NULL)
531 parent = h;
532 h = AVL_GET_LESS(h, 1);
533 L_CHECK_READ_ERROR(AVL_NULL)
536 return(parent);
539 #endif
541 #if (L_IMPL_MASK & AVL_IMPL_SEARCH_GREATEST)
543 L_SC AVL_HANDLE L_(search_greatest)(L_(avl) *l_tree)
545 AVL_HANDLE h = l_tree->root;
546 AVL_HANDLE parent = AVL_NULL;
548 while (h != AVL_NULL)
550 parent = h;
551 h = AVL_GET_GREATER(h, 1);
552 L_CHECK_READ_ERROR(AVL_NULL)
555 return(parent);
558 #endif
560 #if (L_IMPL_MASK & AVL_IMPL_REMOVE)
562 /* Prototype of balance function (called by remove) in case not in
563 ** same compilation unit.
565 L_SC AVL_HANDLE L_(balance)(L_BALANCE_PARAM_DECL_PREFIX AVL_HANDLE bal_h);
567 L_SC AVL_HANDLE L_(remove)(L_(avl) *l_tree, AVL_KEY k)
569 /* Zero-based depth in tree. */
570 unsigned depth = 0, rm_depth;
572 /* Records a path into the tree. If bit n is true, indicates
573 ** take greater branch from the nth node in the path, otherwise
574 ** take the less branch. bit 0 gives branch from root, and
575 ** so on. */
576 L_BIT_ARR_DEFN(branch)
578 AVL_HANDLE h = l_tree->root;
579 AVL_HANDLE parent = AVL_NULL;
580 AVL_HANDLE child;
581 AVL_HANDLE path;
582 int cmp, cmp_shortened_sub_with_path;
583 int reduced_depth;
584 int bf;
585 AVL_HANDLE rm;
586 AVL_HANDLE parent_rm;
588 for (; ;)
590 if (h == AVL_NULL)
591 /* No node in tree with given key. */
592 return(AVL_NULL);
594 cmp = AVL_COMPARE_KEY_NODE(k, h);
596 if (cmp == 0)
597 /* Found node to remove. */
598 break;
600 parent = h;
602 if (cmp > 0)
604 h = AVL_GET_GREATER(h, 1);
605 L_BIT_ARR_1(branch, depth)
607 else
609 h = AVL_GET_LESS(h, 1);
610 L_BIT_ARR_0(branch, depth)
613 L_CHECK_READ_ERROR(AVL_NULL)
614 depth++;
615 cmp_shortened_sub_with_path = cmp;
618 rm = h;
619 parent_rm = parent;
620 rm_depth = depth;
622 /* If the node to remove is not a leaf node, we need to get a
623 ** leaf node, or a node with a single leaf as its child, to put
624 ** in the place of the node to remove. We will get the greatest
625 ** node in the less subtree (of the node to remove), or the least
626 ** node in the greater subtree. We take the leaf node from the
627 ** deeper subtree, if there is one. */
629 if (AVL_GET_BALANCE_FACTOR(h) < 0)
631 child = AVL_GET_LESS(h, 1);
632 L_BIT_ARR_0(branch, depth)
633 cmp = -1;
635 else
637 child = AVL_GET_GREATER(h, 1);
638 L_BIT_ARR_1(branch, depth)
639 cmp = 1;
642 L_CHECK_READ_ERROR(AVL_NULL)
643 depth++;
645 if (child != AVL_NULL)
647 cmp = -cmp;
651 parent = h;
652 h = child;
654 if (cmp < 0)
656 child = AVL_GET_LESS(h, 1);
657 L_BIT_ARR_0(branch, depth)
659 else
661 child = AVL_GET_GREATER(h, 1);
662 L_BIT_ARR_1(branch, depth)
665 L_CHECK_READ_ERROR(AVL_NULL)
666 depth++;
668 while (child != AVL_NULL);
670 if (parent == rm)
671 /* Only went through do loop once. Deleted node will be replaced
672 ** in the tree structure by one of its immediate children. */
673 cmp_shortened_sub_with_path = -cmp;
674 else
675 cmp_shortened_sub_with_path = cmp;
677 /* Get the handle of the opposite child, which may not be null. */
678 child = cmp > 0 ? AVL_GET_LESS(h, 0) : AVL_GET_GREATER(h, 0);
681 if (parent == AVL_NULL)
682 /* There were only 1 or 2 nodes in this tree. */
683 l_tree->root = child;
684 else if (cmp_shortened_sub_with_path < 0)
685 AVL_SET_LESS(parent, child)
686 else
687 AVL_SET_GREATER(parent, child)
689 /* "path" is the parent of the subtree being eliminated or reduced
690 ** from a depth of 2 to 1. If "path" is the node to be removed, we
691 ** set path to the node we're about to poke into the position of the
692 ** node to be removed. */
693 path = parent == rm ? h : parent;
695 if (h != rm)
697 /* Poke in the replacement for the node to be removed. */
698 AVL_SET_LESS(h, AVL_GET_LESS(rm, 0))
699 AVL_SET_GREATER(h, AVL_GET_GREATER(rm, 0))
700 AVL_SET_BALANCE_FACTOR(h, AVL_GET_BALANCE_FACTOR(rm))
702 if (parent_rm == AVL_NULL)
703 l_tree->root = h;
704 else
706 depth = rm_depth - 1;
708 if (L_BIT_ARR_VAL(branch, depth))
709 AVL_SET_GREATER(parent_rm, h)
710 else
711 AVL_SET_LESS(parent_rm, h)
715 if (path != AVL_NULL)
717 /* Create a temporary linked list from the parent of the path node
718 ** to the root node. */
719 h = l_tree->root;
720 parent = AVL_NULL;
721 depth = 0;
723 while (h != path)
725 if (L_BIT_ARR_VAL(branch, depth))
727 child = AVL_GET_GREATER(h, 1);
728 AVL_SET_GREATER(h, parent)
730 else
732 child = AVL_GET_LESS(h, 1);
733 AVL_SET_LESS(h, parent)
736 L_CHECK_READ_ERROR(AVL_NULL)
737 depth++;
738 parent = h;
739 h = child;
742 /* Climb from the path node to the root node using the linked
743 ** list, restoring the tree structure and rebalancing as necessary.
745 reduced_depth = 1;
746 cmp = cmp_shortened_sub_with_path;
748 for (; ;)
750 if (reduced_depth)
752 bf = AVL_GET_BALANCE_FACTOR(h);
754 if (cmp < 0)
755 bf++;
756 else /* cmp > 0 */
757 bf--;
759 if ((bf == -2) || (bf == 2))
761 h = L_(balance)(L_BALANCE_PARAM_CALL_PREFIX h);
762 L_CHECK_READ_ERROR(AVL_NULL)
763 bf = AVL_GET_BALANCE_FACTOR(h);
765 else
766 AVL_SET_BALANCE_FACTOR(h, bf)
767 reduced_depth = (bf == 0);
770 if (parent == AVL_NULL)
771 break;
773 child = h;
774 h = parent;
775 depth--;
776 cmp = L_BIT_ARR_VAL(branch, depth) ? 1 : -1;
778 if (cmp < 0)
780 parent = AVL_GET_LESS(h, 1);
781 AVL_SET_LESS(h, child)
783 else
785 parent = AVL_GET_GREATER(h, 1);
786 AVL_SET_GREATER(h, child)
789 L_CHECK_READ_ERROR(AVL_NULL)
792 l_tree->root = h;
795 return(rm);
798 #endif
800 #if (L_IMPL_MASK & AVL_IMPL_SUBST)
802 L_SC AVL_HANDLE L_(subst)(L_(avl) *l_tree, AVL_HANDLE new_node)
804 AVL_HANDLE h = l_tree->root;
805 AVL_HANDLE parent = AVL_NULL;
806 int cmp, last_cmp;
808 /* Search for node already in tree with same key. */
809 for (; ;)
811 if (h == AVL_NULL)
812 /* No node in tree with same key as new node. */
813 return(AVL_NULL);
815 cmp = AVL_COMPARE_NODE_NODE(new_node, h);
817 if (cmp == 0)
818 /* Found the node to substitute new one for. */
819 break;
821 last_cmp = cmp;
822 parent = h;
823 h = cmp < 0 ? AVL_GET_LESS(h, 1) : AVL_GET_GREATER(h, 1);
824 L_CHECK_READ_ERROR(AVL_NULL)
827 /* Copy tree housekeeping fields from node in tree to new node. */
828 AVL_SET_LESS(new_node, AVL_GET_LESS(h, 0))
829 AVL_SET_GREATER(new_node, AVL_GET_GREATER(h, 0))
830 AVL_SET_BALANCE_FACTOR(new_node, AVL_GET_BALANCE_FACTOR(h))
832 if (parent == AVL_NULL)
833 /* New node is also new root. */
834 l_tree->root = new_node;
835 else
837 /* Make parent point to new node. */
838 if (last_cmp < 0)
839 AVL_SET_LESS(parent, new_node)
840 else
841 AVL_SET_GREATER(parent, new_node)
844 return(h);
847 #endif
849 #ifdef AVL_BUILD_ITER_TYPE
851 #if (L_IMPL_MASK & AVL_IMPL_BUILD)
853 L_SC int L_(build)(
854 L_(avl) *l_tree, AVL_BUILD_ITER_TYPE p, L_SIZE num_nodes)
856 /* Gives path to subtree being built. If bit n is false, branch
857 ** less from the node at depth n, if true branch greater. */
858 L_BIT_ARR_DEFN(branch)
860 /* If bit n is true, then for the current subtree at depth n, its
861 ** greater subtree has one more node than its less subtree. */
862 L_BIT_ARR_DEFN(rem)
864 /* Depth of root node of current subtree. */
865 unsigned depth = 0;
867 /* Number of nodes in current subtree. */
868 L_SIZE num_sub = num_nodes;
870 /* The algorithm relies on a stack of nodes whose less subtree has
871 ** been built, but whose greater subtree has not yet been built.
872 ** The stack is implemented as linked list. The nodes are linked
873 ** together by having the "greater" handle of a node set to the
874 ** next node in the list. "less_parent" is the handle of the first
875 ** node in the list. */
876 AVL_HANDLE less_parent = AVL_NULL;
878 /* h is root of current subtree, child is one of its children. */
879 AVL_HANDLE h;
880 AVL_HANDLE child;
882 if (num_nodes == 0)
884 l_tree->root = AVL_NULL;
885 return(1);
888 for (; ;)
890 while (num_sub > 2)
892 /* Subtract one for root of subtree. */
893 num_sub--;
895 if (num_sub & 1)
896 L_BIT_ARR_1(rem, depth)
897 else
898 L_BIT_ARR_0(rem, depth)
899 L_BIT_ARR_0(branch, depth)
900 depth++;
902 num_sub >>= 1;
905 if (num_sub == 2)
907 /* Build a subtree with two nodes, slanting to greater.
908 ** I arbitrarily chose to always have the extra node in the
909 ** greater subtree when there is an odd number of nodes to
910 ** split between the two subtrees. */
912 h = AVL_BUILD_ITER_VAL(p);
913 L_CHECK_READ_ERROR(0)
914 AVL_BUILD_ITER_INCR(p)
915 child = AVL_BUILD_ITER_VAL(p);
916 L_CHECK_READ_ERROR(0)
917 AVL_BUILD_ITER_INCR(p)
918 AVL_SET_LESS(child, AVL_NULL)
919 AVL_SET_GREATER(child, AVL_NULL)
920 AVL_SET_BALANCE_FACTOR(child, 0)
921 AVL_SET_GREATER(h, child)
922 AVL_SET_LESS(h, AVL_NULL)
923 AVL_SET_BALANCE_FACTOR(h, 1)
925 else /* num_sub == 1 */
927 /* Build a subtree with one node. */
929 h = AVL_BUILD_ITER_VAL(p);
930 L_CHECK_READ_ERROR(0)
931 AVL_BUILD_ITER_INCR(p)
932 AVL_SET_LESS(h, AVL_NULL)
933 AVL_SET_GREATER(h, AVL_NULL)
934 AVL_SET_BALANCE_FACTOR(h, 0)
937 while (depth)
939 depth--;
941 if (!L_BIT_ARR_VAL(branch, depth))
942 /* We've completed a less subtree. */
943 break;
945 /* We've completed a greater subtree, so attach it to
946 ** its parent (that is less than it). We pop the parent
947 ** off the stack of less parents. */
948 child = h;
949 h = less_parent;
950 less_parent = AVL_GET_GREATER(h, 1);
951 L_CHECK_READ_ERROR(0)
952 AVL_SET_GREATER(h, child)
953 /* num_sub = 2 * (num_sub - rem[depth]) + rem[depth] + 1 */
954 num_sub <<= 1;
955 num_sub += L_BIT_ARR_VAL(rem, depth) ? 0 : 1;
957 if (num_sub & (num_sub - 1))
958 /* num_sub is not a power of 2. */
959 AVL_SET_BALANCE_FACTOR(h, 0)
960 else
961 /* num_sub is a power of 2. */
962 AVL_SET_BALANCE_FACTOR(h, 1)
965 if (num_sub == num_nodes)
966 /* We've completed the full tree. */
967 break;
969 /* The subtree we've completed is the less subtree of the
970 ** next node in the sequence. */
972 child = h;
973 h = AVL_BUILD_ITER_VAL(p);
974 L_CHECK_READ_ERROR(0)
975 AVL_BUILD_ITER_INCR(p)
976 AVL_SET_LESS(h, child)
978 /* Put h into stack of less parents. */
979 AVL_SET_GREATER(h, less_parent)
980 less_parent = h;
982 /* Proceed to creating greater than subtree of h. */
983 L_BIT_ARR_1(branch, depth)
984 num_sub += L_BIT_ARR_VAL(rem, depth) ? 1 : 0;
985 depth++;
987 } /* end for ( ; ; ) */
989 l_tree->root = h;
991 return(1);
994 #endif
996 #endif
998 #if (L_IMPL_MASK & AVL_IMPL_INIT_ITER)
1000 /* Initialize depth to invalid value, to indicate iterator is
1001 ** invalid. (Depth is zero-base.) It's not necessary to initialize
1002 ** iterators prior to passing them to the "start" function.
1004 L_SC void L_(init_iter)(L_(iter) *iter)
1006 iter->depth = ~0;
1009 #endif
1011 #ifdef AVL_READ_ERRORS_HAPPEN
1013 #define L_CHECK_READ_ERROR_INV_DEPTH \
1014 { if (AVL_READ_ERROR) { iter->depth = ~0; return; } }
1016 #else
1018 #define L_CHECK_READ_ERROR_INV_DEPTH
1020 #endif
1022 #if (L_IMPL_MASK & AVL_IMPL_START_ITER)
1024 L_SC void L_(start_iter)(
1025 L_(avl) *l_tree, L_(iter) *iter, AVL_KEY k, avl_search_type st)
1027 AVL_HANDLE h = l_tree->root;
1028 unsigned d = 0;
1029 int cmp, target_cmp;
1031 /* Save the tree that we're going to iterate through in a
1032 ** member variable. */
1033 iter->tree_ = l_tree;
1035 iter->depth = ~0;
1037 if (h == AVL_NULL)
1038 /* Tree is empty. */
1039 return;
1041 if (st & AVL_LESS)
1042 /* Key can be greater than key of starting node. */
1043 target_cmp = 1;
1044 else if (st & AVL_GREATER)
1045 /* Key can be less than key of starting node. */
1046 target_cmp = -1;
1047 else
1048 /* Key must be same as key of starting node. */
1049 target_cmp = 0;
1051 for (; ;)
1053 cmp = AVL_COMPARE_KEY_NODE(k, h);
1055 if (cmp == 0)
1057 if (st & AVL_EQUAL)
1059 /* Equal node was sought and found as starting node. */
1060 iter->depth = d;
1061 break;
1064 cmp = -target_cmp;
1066 else if (target_cmp != 0)
1067 if (!((cmp ^ target_cmp) & L_MASK_HIGH_BIT))
1068 /* cmp and target_cmp are both negative or both positive. */
1069 iter->depth = d;
1071 h = cmp < 0 ? AVL_GET_LESS(h, 1) : AVL_GET_GREATER(h, 1);
1072 L_CHECK_READ_ERROR_INV_DEPTH
1074 if (h == AVL_NULL)
1075 break;
1077 if (cmp > 0)
1078 L_BIT_ARR_1(iter->branch, d)
1079 else
1080 L_BIT_ARR_0(iter->branch, d)
1081 iter->path_h[d++] = h;
1085 #endif
1087 #if (L_IMPL_MASK & AVL_IMPL_START_ITER_LEAST)
1089 L_SC void L_(start_iter_least)(L_(avl) *l_tree, L_(iter) *iter)
1091 AVL_HANDLE h = l_tree->root;
1093 iter->tree_ = l_tree;
1095 iter->depth = ~0;
1097 L_BIT_ARR_ALL(iter->branch, 0)
1099 while (h != AVL_NULL)
1101 if (iter->depth != ~0)
1102 iter->path_h[iter->depth] = h;
1104 iter->depth++;
1105 h = AVL_GET_LESS(h, 1);
1106 L_CHECK_READ_ERROR_INV_DEPTH
1110 #endif
1112 #if (L_IMPL_MASK & AVL_IMPL_START_ITER_GREATEST)
1114 L_SC void L_(start_iter_greatest)(L_(avl) *l_tree, L_(iter) *iter)
1116 AVL_HANDLE h = l_tree->root;
1118 iter->tree_ = l_tree;
1120 iter->depth = ~0;
1122 L_BIT_ARR_ALL(iter->branch, 1)
1124 while (h != AVL_NULL)
1126 if (iter->depth != ~0)
1127 iter->path_h[iter->depth] = h;
1129 iter->depth++;
1130 h = AVL_GET_GREATER(h, 1);
1131 L_CHECK_READ_ERROR_INV_DEPTH
1135 #endif
1137 #if (L_IMPL_MASK & AVL_IMPL_GET_ITER)
1139 L_SC AVL_HANDLE L_(get_iter)(L_(iter) *iter)
1141 if (iter->depth == ~0)
1142 return(AVL_NULL);
1144 return(iter->depth == 0 ?
1145 iter->tree_->root : iter->path_h[iter->depth - 1]);
1148 #endif
1150 #if (L_IMPL_MASK & AVL_IMPL_INCR_ITER)
1152 L_SC void L_(incr_iter)(L_(iter) *iter)
1154 #define l_tree (iter->tree_)
1156 if (iter->depth != ~0)
1158 AVL_HANDLE h =
1159 AVL_GET_GREATER((iter->depth == 0 ?
1160 iter->tree_->root : iter->path_h[iter->depth - 1]), 1);
1161 L_CHECK_READ_ERROR_INV_DEPTH
1163 if (h == AVL_NULL)
1166 if (iter->depth == 0)
1168 iter->depth = ~0;
1169 break;
1172 iter->depth--;
1174 while (L_BIT_ARR_VAL(iter->branch, iter->depth));
1175 else
1177 L_BIT_ARR_1(iter->branch, iter->depth)
1178 iter->path_h[iter->depth++] = h;
1180 for (; ;)
1182 h = AVL_GET_LESS(h, 1);
1183 L_CHECK_READ_ERROR_INV_DEPTH
1185 if (h == AVL_NULL)
1186 break;
1188 L_BIT_ARR_0(iter->branch, iter->depth)
1189 iter->path_h[iter->depth++] = h;
1194 #undef l_tree
1197 #endif
1199 #if (L_IMPL_MASK & AVL_IMPL_DECR_ITER)
1201 L_SC void L_(decr_iter)(L_(iter) *iter)
1203 #define l_tree (iter->tree_)
1205 if (iter->depth != ~0)
1207 AVL_HANDLE h =
1208 AVL_GET_LESS((iter->depth == 0 ?
1209 iter->tree_->root : iter->path_h[iter->depth - 1]), 1);
1210 L_CHECK_READ_ERROR_INV_DEPTH
1212 if (h == AVL_NULL)
1215 if (iter->depth == 0)
1217 iter->depth = ~0;
1218 break;
1221 iter->depth--;
1223 while (!L_BIT_ARR_VAL(iter->branch, iter->depth));
1224 else
1226 L_BIT_ARR_0(iter->branch, iter->depth)
1227 iter->path_h[iter->depth++] = h;
1229 for (; ;)
1231 h = AVL_GET_GREATER(h, 1);
1232 L_CHECK_READ_ERROR_INV_DEPTH
1234 if (h == AVL_NULL)
1235 break;
1237 L_BIT_ARR_1(iter->branch, iter->depth)
1238 iter->path_h[iter->depth++] = h;
1243 #undef l_tree
1246 #endif
1248 /* Tidy up the preprocessor symbol name space. */
1249 #undef L_
1250 #undef L_EST_LONG_BIT
1251 #undef L_SIZE
1252 #undef L_MASK_HIGH_BIT
1253 #undef L_LONG_BIT
1254 #undef L_BIT_ARR_DEFN
1255 #undef L_BIT_ARR_VAL
1256 #undef L_BIT_ARR_0
1257 #undef L_BIT_ARR_1
1258 #undef L_BIT_ARR_ALL
1259 #undef L_CHECK_READ_ERROR
1260 #undef L_CHECK_READ_ERROR_INV_DEPTH
1261 #undef L_BIT_ARR_LONGS
1262 #undef L_IMPL_MASK
1263 #undef L_CHECK_READ_ERROR
1264 #undef L_CHECK_READ_ERROR_INV_DEPTH
1265 #undef L_SC
1266 #undef L_BALANCE_PARAM_CALL_PREFIX
1267 #undef L_BALANCE_PARAM_DECL_PREFIX