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__(search_root
)(L__(avl
) *tree
);
95 L__SC AVL_HANDLE
L__(remove
)(L__(avl
) *tree
, AVL_KEY k
);
97 L__SC AVL_HANDLE
L__(subst
)(L__(avl
) *tree
, AVL_HANDLE new_node
);
99 #ifdef AVL_BUILD_ITER_TYPE
101 L__SC
int L__(build
)(
102 L__(avl
) *tree
, AVL_BUILD_ITER_TYPE p
, L__SIZE num_nodes
);
106 /* ANSI C/ISO C++ require that a long have at least 32 bits. Set
107 ** L__EST_LONG_BIT to be the greatest multiple of 8 in the range
108 ** 32 - 64 (inclusive) that is less than or equal to the number of
112 #if (((LONG_MAX >> 31) >> 7) == 0)
114 #define L__EST_LONG_BIT 32
116 #elif (((LONG_MAX >> 31) >> 15) == 0)
118 #define L__EST_LONG_BIT 40
120 #elif (((LONG_MAX >> 31) >> 23) == 0)
122 #define L__EST_LONG_BIT 48
124 #elif (((LONG_MAX >> 31) >> 31) == 0)
126 #define L__EST_LONG_BIT 56
130 #define L__EST_LONG_BIT 64
134 /* Number of bits in a long. */
135 #define L__LONG_BIT (sizeof(long) * CHAR_BIT)
137 /* The macro L__BIT_ARR_DEFN defines a bit array whose index is a (0-based)
138 ** node depth. The definition depends on whether the maximum depth is more
139 ** or less than the number of bits in a single long.
142 #if ((AVL_MAX_DEPTH) > L__EST_LONG_BIT)
144 /* Maximum depth may be more than number of bits in a long. */
146 #define L__BIT_ARR_DEFN(NAME) \
147 unsigned long NAME[((AVL_MAX_DEPTH) + L__LONG_BIT - 1) / L__LONG_BIT];
151 /* Maximum depth is definitely less than number of bits in a long. */
153 #define L__BIT_ARR_DEFN(NAME) unsigned long NAME;
157 /* Iterator structure. */
160 /* Tree being iterated over. */
163 /* Records a path into the tree. If bit n is true, indicates
164 ** take greater branch from the nth node in the path, otherwise
165 ** take the less branch. bit 0 gives branch from root, and
167 L__BIT_ARR_DEFN(branch
)
169 /* Zero-based depth of path into tree. */
172 /* Handles of nodes in path from root to current node (returned by *). */
173 AVL_HANDLE path_h
[(AVL_MAX_DEPTH
) - 1];
177 /* Iterator function prototypes. */
179 L__SC
void L__(start_iter
)(
180 L__(avl
) *tree
, L__(iter
) *iter
, AVL_KEY k
, avl_search_type st
);
182 L__SC
void L__(start_iter_least
)(L__(avl
) *tree
, L__(iter
) *iter
);
184 L__SC
void L__(start_iter_greatest
)(L__(avl
) *tree
, L__(iter
) *iter
);
186 L__SC AVL_HANDLE
L__(get_iter
)(L__(iter
) *iter
);
188 L__SC
void L__(incr_iter
)(L__(iter
) *iter
);
190 L__SC
void L__(decr_iter
)(L__(iter
) *iter
);
192 L__SC
void L__(init_iter
)(L__(iter
) *iter
);
194 #define AVL_IMPL_INIT 1
195 #define AVL_IMPL_IS_EMPTY (1 << 1)
196 #define AVL_IMPL_INSERT (1 << 2)
197 #define AVL_IMPL_SEARCH (1 << 3)
198 #define AVL_IMPL_SEARCH_LEAST (1 << 4)
199 #define AVL_IMPL_SEARCH_GREATEST (1 << 5)
200 #define AVL_IMPL_REMOVE (1 << 6)
201 #define AVL_IMPL_BUILD (1 << 7)
202 #define AVL_IMPL_START_ITER (1 << 8)
203 #define AVL_IMPL_START_ITER_LEAST (1 << 9)
204 #define AVL_IMPL_START_ITER_GREATEST (1 << 10)
205 #define AVL_IMPL_GET_ITER (1 << 11)
206 #define AVL_IMPL_INCR_ITER (1 << 12)
207 #define AVL_IMPL_DECR_ITER (1 << 13)
208 #define AVL_IMPL_INIT_ITER (1 << 14)
209 #define AVL_IMPL_SUBST (1 << 15)
211 #define AVL_IMPL_ALL (~0)
214 #undef L__EST_LONG_BIT
218 #undef L__BIT_ARR_DEFN