1 /* Abstract AVL Tree Generic C Package.
2 ** Interface generation header file.
4 ** This code is in the public domain. See cavl_tree.html for interface
7 ** Version: 1.5 Author: Walt Karas
10 /* This header contains the definition of CHAR_BIT (number of bits in a
15 #undef L__EST_LONG_BIT
19 #undef L__BIT_ARR_DEFN
21 #ifndef AVL_SEARCH_TYPE_DEFINED_
22 #define AVL_SEARCH_TYPE_DEFINED_
29 AVL_LESS_EQUAL
= AVL_EQUAL
| AVL_LESS
,
30 AVL_GREATER_EQUAL
= AVL_EQUAL
| AVL_GREATER
38 #define L__ AVL_UNIQUE
46 /* Determine storage class for function prototypes. */
59 #define L__SIZE AVL_SIZE
63 #define L__SIZE unsigned long
69 #ifdef AVL_INSIDE_STRUCT
79 /* Function prototypes. */
81 L__SC
void L__(init
)(L__(avl
) *tree
);
83 L__SC
int L__(is_empty
)(L__(avl
) *tree
);
85 L__SC AVL_HANDLE
L__(insert
)(L__(avl
) *tree
, AVL_HANDLE h
);
87 L__SC AVL_HANDLE
L__(search
)(L__(avl
) *tree
, AVL_KEY k
, avl_search_type st
);
89 L__SC AVL_HANDLE
L__(search_least
)(L__(avl
) *tree
);
91 L__SC AVL_HANDLE
L__(search_greatest
)(L__(avl
) *tree
);
93 L__SC AVL_HANDLE
L__(remove
)(L__(avl
) *tree
, AVL_KEY k
);
95 L__SC AVL_HANDLE
L__(subst
)(L__(avl
) *tree
, AVL_HANDLE new_node
);
97 #ifdef AVL_BUILD_ITER_TYPE
100 L__(avl
) *tree
, AVL_BUILD_ITER_TYPE p
, L__SIZE num_nodes
);
104 /* ANSI C/ISO C++ require that a long have at least 32 bits. Set
105 ** L__EST_LONG_BIT to be the greatest multiple of 8 in the range
106 ** 32 - 64 (inclusive) that is less than or equal to the number of
110 #if (((LONG_MAX >> 31) >> 7) == 0)
112 #define L__EST_LONG_BIT 32
114 #elif (((LONG_MAX >> 31) >> 15) == 0)
116 #define L__EST_LONG_BIT 40
118 #elif (((LONG_MAX >> 31) >> 23) == 0)
120 #define L__EST_LONG_BIT 48
122 #elif (((LONG_MAX >> 31) >> 31) == 0)
124 #define L__EST_LONG_BIT 56
128 #define L__EST_LONG_BIT 64
132 /* Number of bits in a long. */
133 #define L__LONG_BIT (sizeof(long) * CHAR_BIT)
135 /* The macro L__BIT_ARR_DEFN defines a bit array whose index is a (0-based)
136 ** node depth. The definition depends on whether the maximum depth is more
137 ** or less than the number of bits in a single long.
140 #if ((AVL_MAX_DEPTH) > L__EST_LONG_BIT)
142 /* Maximum depth may be more than number of bits in a long. */
144 #define L__BIT_ARR_DEFN(NAME) \
145 unsigned long NAME[((AVL_MAX_DEPTH) + L__LONG_BIT - 1) / L__LONG_BIT];
149 /* Maximum depth is definitely less than number of bits in a long. */
151 #define L__BIT_ARR_DEFN(NAME) unsigned long NAME;
155 /* Iterator structure. */
158 /* Tree being iterated over. */
161 /* Records a path into the tree. If bit n is true, indicates
162 ** take greater branch from the nth node in the path, otherwise
163 ** take the less branch. bit 0 gives branch from root, and
165 L__BIT_ARR_DEFN(branch
)
167 /* Zero-based depth of path into tree. */
170 /* Handles of nodes in path from root to current node (returned by *). */
171 AVL_HANDLE path_h
[(AVL_MAX_DEPTH
) - 1];
175 /* Iterator function prototypes. */
177 L__SC
void L__(start_iter
)(
178 L__(avl
) *tree
, L__(iter
) *iter
, AVL_KEY k
, avl_search_type st
);
180 L__SC
void L__(start_iter_least
)(L__(avl
) *tree
, L__(iter
) *iter
);
182 L__SC
void L__(start_iter_greatest
)(L__(avl
) *tree
, L__(iter
) *iter
);
184 L__SC AVL_HANDLE
L__(get_iter
)(L__(iter
) *iter
);
186 L__SC
void L__(incr_iter
)(L__(iter
) *iter
);
188 L__SC
void L__(decr_iter
)(L__(iter
) *iter
);
190 L__SC
void L__(init_iter
)(L__(iter
) *iter
);
192 #define AVL_IMPL_INIT 1
193 #define AVL_IMPL_IS_EMPTY (1 << 1)
194 #define AVL_IMPL_INSERT (1 << 2)
195 #define AVL_IMPL_SEARCH (1 << 3)
196 #define AVL_IMPL_SEARCH_LEAST (1 << 4)
197 #define AVL_IMPL_SEARCH_GREATEST (1 << 5)
198 #define AVL_IMPL_REMOVE (1 << 6)
199 #define AVL_IMPL_BUILD (1 << 7)
200 #define AVL_IMPL_START_ITER (1 << 8)
201 #define AVL_IMPL_START_ITER_LEAST (1 << 9)
202 #define AVL_IMPL_START_ITER_GREATEST (1 << 10)
203 #define AVL_IMPL_GET_ITER (1 << 11)
204 #define AVL_IMPL_INCR_ITER (1 << 12)
205 #define AVL_IMPL_DECR_ITER (1 << 13)
206 #define AVL_IMPL_INIT_ITER (1 << 14)
207 #define AVL_IMPL_SUBST (1 << 15)
209 #define AVL_IMPL_ALL (~0)
212 #undef L__EST_LONG_BIT
216 #undef L__BIT_ARR_DEFN