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.
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
18 ** Version: 1.5 Author: Walt Karas
25 #undef L_MASK_HIGH_BIT
32 #undef L_BIT_ARR_LONGS
34 #undef L_CHECK_READ_ERROR
35 #undef L_CHECK_READ_ERROR_INV_DEPTH
37 #undef L_BALANCE_PARAM_PREFIX
49 /* Determine correct storage class for functions */
62 #define L_SIZE AVL_SIZE
66 #define L_SIZE unsigned long
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
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
96 #define L_EST_LONG_BIT 64
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
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);
138 #ifdef AVL_READ_ERRORS_HAPPEN
140 #define L_CHECK_READ_ERROR(ERROR_RETURN) \
141 { if (AVL_READ_ERROR) return(ERROR_RETURN); }
145 #define L_CHECK_READ_ERROR(ERROR_RETURN)
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
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,
164 #define L_BALANCE_PARAM_CALL_PREFIX
165 #define L_BALANCE_PARAM_DECL_PREFIX
171 #define L_IMPL_MASK (AVL_IMPL_MASK)
175 /* Define all functions. */
176 #define L_IMPL_MASK AVL_IMPL_ALL
180 #if (L_IMPL_MASK & AVL_IMPL_INIT)
182 L_SC
void L_(init
)(L_(avl
) *l_tree
)
184 l_tree
->root
= AVL_NULL
;
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
);
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
)
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
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)
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
);
238 AVL_SET_BALANCE_FACTOR(old_h
, -1)
239 AVL_SET_BALANCE_FACTOR(deep_h
, 0)
243 AVL_SET_BALANCE_FACTOR(deep_h
, 1)
244 AVL_SET_BALANCE_FACTOR(old_h
, 0)
247 AVL_SET_BALANCE_FACTOR(bal_h
, 0)
251 AVL_SET_BALANCE_FACTOR(old_h
, 0)
252 AVL_SET_BALANCE_FACTOR(deep_h
, 0)
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)
267 AVL_SET_BALANCE_FACTOR(deep_h
, 0)
268 AVL_SET_BALANCE_FACTOR(bal_h
, 0)
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)
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
);
298 AVL_SET_BALANCE_FACTOR(old_h
, 1)
299 AVL_SET_BALANCE_FACTOR(deep_h
, 0)
303 AVL_SET_BALANCE_FACTOR(deep_h
, -1)
304 AVL_SET_BALANCE_FACTOR(old_h
, 0)
307 AVL_SET_BALANCE_FACTOR(bal_h
, 0)
311 AVL_SET_BALANCE_FACTOR(old_h
, 0)
312 AVL_SET_BALANCE_FACTOR(deep_h
, 0)
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)
327 AVL_SET_BALANCE_FACTOR(deep_h
, 0)
328 AVL_SET_BALANCE_FACTOR(bal_h
, 0)
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
)
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. */
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
362 L_BIT_ARR_DEFN(branch
)
364 AVL_HANDLE hh
= l_tree
->root
;
365 AVL_HANDLE parent
= AVL_NULL
;
370 if (AVL_GET_BALANCE_FACTOR(hh
) != 0)
373 parent_unbal
= parent
;
377 cmp
= AVL_COMPARE_NODE_NODE(h
, hh
);
387 hh
= AVL_GET_GREATER(hh
, 1);
388 L_BIT_ARR_1(branch
, depth
)
392 hh
= AVL_GET_LESS(hh
, 1);
393 L_BIT_ARR_0(branch
, depth
)
396 L_CHECK_READ_ERROR(AVL_NULL
)
399 while (hh
!= AVL_NULL
);
401 /* Add node to insert as leaf of tree. */
403 AVL_SET_LESS(parent
, h
)
405 AVL_SET_GREATER(parent
, h
)
409 if (unbal
== AVL_NULL
)
413 cmp
= L_BIT_ARR_VAL(branch
, depth
) ? 1 : -1;
415 unbal_bf
= AVL_GET_BALANCE_FACTOR(unbal
);
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
)
436 cmp
= L_BIT_ARR_VAL(branch
, depth
) ? 1 : -1;
441 AVL_SET_BALANCE_FACTOR(hh
, -1)
442 hh
= AVL_GET_LESS(hh
, 1);
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
;
462 depth
= unbal_depth
- 1;
463 cmp
= L_BIT_ARR_VAL(branch
, depth
) ? 1 : -1;
466 AVL_SET_LESS(parent_unbal
, unbal
)
468 AVL_SET_GREATER(parent_unbal
, unbal
)
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
)
484 AVL_HANDLE match_h
= AVL_NULL
;
485 AVL_HANDLE h
= l_tree
->root
;
489 else if (st
& AVL_GREATER
)
494 while (h
!= AVL_NULL
)
496 cmp
= AVL_COMPARE_KEY_NODE(k
, h
);
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. */
513 h
= cmp
< 0 ? AVL_GET_LESS(h
, 1) : AVL_GET_GREATER(h
, 1);
514 L_CHECK_READ_ERROR(AVL_NULL
)
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
)
532 h
= AVL_GET_LESS(h
, 1);
533 L_CHECK_READ_ERROR(AVL_NULL
)
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
)
551 h
= AVL_GET_GREATER(h
, 1);
552 L_CHECK_READ_ERROR(AVL_NULL
)
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
576 L_BIT_ARR_DEFN(branch
)
578 AVL_HANDLE h
= l_tree
->root
;
579 AVL_HANDLE parent
= AVL_NULL
;
582 int cmp
, cmp_shortened_sub_with_path
;
586 AVL_HANDLE parent_rm
;
591 /* No node in tree with given key. */
594 cmp
= AVL_COMPARE_KEY_NODE(k
, h
);
597 /* Found node to remove. */
604 h
= AVL_GET_GREATER(h
, 1);
605 L_BIT_ARR_1(branch
, depth
)
609 h
= AVL_GET_LESS(h
, 1);
610 L_BIT_ARR_0(branch
, depth
)
613 L_CHECK_READ_ERROR(AVL_NULL
)
615 cmp_shortened_sub_with_path
= cmp
;
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
)
637 child
= AVL_GET_GREATER(h
, 1);
638 L_BIT_ARR_1(branch
, depth
)
642 L_CHECK_READ_ERROR(AVL_NULL
)
645 if (child
!= AVL_NULL
)
656 child
= AVL_GET_LESS(h
, 1);
657 L_BIT_ARR_0(branch
, depth
)
661 child
= AVL_GET_GREATER(h
, 1);
662 L_BIT_ARR_1(branch
, depth
)
665 L_CHECK_READ_ERROR(AVL_NULL
)
668 while (child
!= AVL_NULL
);
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
;
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
)
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
;
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
)
706 depth
= rm_depth
- 1;
708 if (L_BIT_ARR_VAL(branch
, depth
))
709 AVL_SET_GREATER(parent_rm
, h
)
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. */
725 if (L_BIT_ARR_VAL(branch
, depth
))
727 child
= AVL_GET_GREATER(h
, 1);
728 AVL_SET_GREATER(h
, parent
)
732 child
= AVL_GET_LESS(h
, 1);
733 AVL_SET_LESS(h
, parent
)
736 L_CHECK_READ_ERROR(AVL_NULL
)
742 /* Climb from the path node to the root node using the linked
743 ** list, restoring the tree structure and rebalancing as necessary.
746 cmp
= cmp_shortened_sub_with_path
;
752 bf
= AVL_GET_BALANCE_FACTOR(h
);
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
);
766 AVL_SET_BALANCE_FACTOR(h
, bf
)
767 reduced_depth
= (bf
== 0);
770 if (parent
== AVL_NULL
)
776 cmp
= L_BIT_ARR_VAL(branch
, depth
) ? 1 : -1;
780 parent
= AVL_GET_LESS(h
, 1);
781 AVL_SET_LESS(h
, child
)
785 parent
= AVL_GET_GREATER(h
, 1);
786 AVL_SET_GREATER(h
, child
)
789 L_CHECK_READ_ERROR(AVL_NULL
)
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
;
808 /* Search for node already in tree with same key. */
812 /* No node in tree with same key as new node. */
815 cmp
= AVL_COMPARE_NODE_NODE(new_node
, h
);
818 /* Found the node to substitute new one for. */
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
;
837 /* Make parent point to new node. */
839 AVL_SET_LESS(parent
, new_node
)
841 AVL_SET_GREATER(parent
, new_node
)
849 #ifdef AVL_BUILD_ITER_TYPE
851 #if (L_IMPL_MASK & AVL_IMPL_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. */
864 /* Depth of root node of current subtree. */
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. */
884 l_tree
->root
= AVL_NULL
;
892 /* Subtract one for root of subtree. */
896 L_BIT_ARR_1(rem
, depth
)
898 L_BIT_ARR_0(rem
, depth
)
899 L_BIT_ARR_0(branch
, depth
)
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)
941 if (!L_BIT_ARR_VAL(branch
, depth
))
942 /* We've completed a less subtree. */
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. */
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 */
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)
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. */
969 /* The subtree we've completed is the less subtree of the
970 ** next node in the sequence. */
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
)
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;
987 } /* end for ( ; ; ) */
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
)
1011 #ifdef AVL_READ_ERRORS_HAPPEN
1013 #define L_CHECK_READ_ERROR_INV_DEPTH \
1014 { if (AVL_READ_ERROR) { iter->depth = ~0; return; } }
1018 #define L_CHECK_READ_ERROR_INV_DEPTH
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
;
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
;
1038 /* Tree is empty. */
1042 /* Key can be greater than key of starting node. */
1044 else if (st
& AVL_GREATER
)
1045 /* Key can be less than key of starting node. */
1048 /* Key must be same as key of starting node. */
1053 cmp
= AVL_COMPARE_KEY_NODE(k
, h
);
1059 /* Equal node was sought and found as starting node. */
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. */
1071 h
= cmp
< 0 ? AVL_GET_LESS(h
, 1) : AVL_GET_GREATER(h
, 1);
1072 L_CHECK_READ_ERROR_INV_DEPTH
1078 L_BIT_ARR_1(iter
->branch
, d
)
1080 L_BIT_ARR_0(iter
->branch
, d
)
1081 iter
->path_h
[d
++] = h
;
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
;
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
;
1105 h
= AVL_GET_LESS(h
, 1);
1106 L_CHECK_READ_ERROR_INV_DEPTH
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
;
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
;
1130 h
= AVL_GET_GREATER(h
, 1);
1131 L_CHECK_READ_ERROR_INV_DEPTH
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)
1144 return(iter
->depth
== 0 ?
1145 iter
->tree_
->root
: iter
->path_h
[iter
->depth
- 1]);
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)
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
1166 if (iter
->depth
== 0)
1174 while (L_BIT_ARR_VAL(iter
->branch
, iter
->depth
));
1177 L_BIT_ARR_1(iter
->branch
, iter
->depth
)
1178 iter
->path_h
[iter
->depth
++] = h
;
1182 h
= AVL_GET_LESS(h
, 1);
1183 L_CHECK_READ_ERROR_INV_DEPTH
1188 L_BIT_ARR_0(iter
->branch
, iter
->depth
)
1189 iter
->path_h
[iter
->depth
++] = h
;
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)
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
1215 if (iter
->depth
== 0)
1223 while (!L_BIT_ARR_VAL(iter
->branch
, iter
->depth
));
1226 L_BIT_ARR_0(iter
->branch
, iter
->depth
)
1227 iter
->path_h
[iter
->depth
++] = h
;
1231 h
= AVL_GET_GREATER(h
, 1);
1232 L_CHECK_READ_ERROR_INV_DEPTH
1237 L_BIT_ARR_1(iter
->branch
, iter
->depth
)
1238 iter
->path_h
[iter
->depth
++] = h
;
1248 /* Tidy up the preprocessor symbol name space. */
1250 #undef L_EST_LONG_BIT
1252 #undef L_MASK_HIGH_BIT
1254 #undef L_BIT_ARR_DEFN
1255 #undef L_BIT_ARR_VAL
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
1263 #undef L_CHECK_READ_ERROR
1264 #undef L_CHECK_READ_ERROR_INV_DEPTH
1266 #undef L_BALANCE_PARAM_CALL_PREFIX
1267 #undef L_BALANCE_PARAM_DECL_PREFIX