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 ** Interface generation header file.
15 ** This code is in the public domain. See cavl_tree.html for interface
18 ** Version: 1.5 Author: Walt Karas
21 /* This header contains the definition of CHAR_BIT (number of bits in a
32 #ifndef AVL_SEARCH_TYPE_DEFINED_
33 #define AVL_SEARCH_TYPE_DEFINED_
40 AVL_LESS_EQUAL
= AVL_EQUAL
| AVL_LESS
,
41 AVL_GREATER_EQUAL
= AVL_EQUAL
| AVL_GREATER
57 /* Determine storage class for function prototypes. */
70 #define L_SIZE AVL_SIZE
74 #define L_SIZE unsigned long
80 #ifdef AVL_INSIDE_STRUCT
90 /* Function prototypes. */
92 L_SC
void L_(init
)(L_(avl
) *tree
);
94 L_SC
int L_(is_empty
)(L_(avl
) *tree
);
96 L_SC AVL_HANDLE
L_(insert
)(L_(avl
) *tree
, AVL_HANDLE h
);
98 L_SC AVL_HANDLE
L_(search
)(L_(avl
) *tree
, AVL_KEY k
, avl_search_type st
);
100 L_SC AVL_HANDLE
L_(search_least
)(L_(avl
) *tree
);
102 L_SC AVL_HANDLE
L_(search_greatest
)(L_(avl
) *tree
);
104 L_SC AVL_HANDLE
L_(remove
)(L_(avl
) *tree
, AVL_KEY k
);
106 L_SC AVL_HANDLE
L_(subst
)(L_(avl
) *tree
, AVL_HANDLE new_node
);
108 #ifdef AVL_BUILD_ITER_TYPE
111 L_(avl
) *tree
, AVL_BUILD_ITER_TYPE p
, L_SIZE num_nodes
);
115 /* ANSI C/ISO C++ require that a long have at least 32 bits. Set
116 ** L_EST_LONG_BIT to be the greatest multiple of 8 in the range
117 ** 32 - 64 (inclusive) that is less than or equal to the number of
121 #if (((LONG_MAX >> 31) >> 7) == 0)
123 #define L_EST_LONG_BIT 32
125 #elif (((LONG_MAX >> 31) >> 15) == 0)
127 #define L_EST_LONG_BIT 40
129 #elif (((LONG_MAX >> 31) >> 23) == 0)
131 #define L_EST_LONG_BIT 48
133 #elif (((LONG_MAX >> 31) >> 31) == 0)
135 #define L_EST_LONG_BIT 56
139 #define L_EST_LONG_BIT 64
143 /* Number of bits in a long. */
144 #define L_LONG_BIT (sizeof(long) * CHAR_BIT)
146 /* The macro L_BIT_ARR_DEFN defines a bit array whose index is a (0-based)
147 ** node depth. The definition depends on whether the maximum depth is more
148 ** or less than the number of bits in a single long.
151 #if ((AVL_MAX_DEPTH) > L_EST_LONG_BIT)
153 /* Maximum depth may be more than number of bits in a long. */
155 #define L_BIT_ARR_DEFN(NAME) \
156 unsigned long NAME[((AVL_MAX_DEPTH) + L_LONG_BIT - 1) / L_LONG_BIT];
160 /* Maximum depth is definitely less than number of bits in a long. */
162 #define L_BIT_ARR_DEFN(NAME) unsigned long NAME;
166 /* Iterator structure. */
169 /* Tree being iterated over. */
172 /* Records a path into the tree. If bit n is true, indicates
173 ** take greater branch from the nth node in the path, otherwise
174 ** take the less branch. bit 0 gives branch from root, and
176 L_BIT_ARR_DEFN(branch
)
178 /* Zero-based depth of path into tree. */
181 /* Handles of nodes in path from root to current node (returned by *). */
182 AVL_HANDLE path_h
[(AVL_MAX_DEPTH
) - 1];
186 /* Iterator function prototypes. */
188 L_SC
void L_(start_iter
)(
189 L_(avl
) *tree
, L_(iter
) *iter
, AVL_KEY k
, avl_search_type st
);
191 L_SC
void L_(start_iter_least
)(L_(avl
) *tree
, L_(iter
) *iter
);
193 L_SC
void L_(start_iter_greatest
)(L_(avl
) *tree
, L_(iter
) *iter
);
195 L_SC AVL_HANDLE
L_(get_iter
)(L_(iter
) *iter
);
197 L_SC
void L_(incr_iter
)(L_(iter
) *iter
);
199 L_SC
void L_(decr_iter
)(L_(iter
) *iter
);
201 L_SC
void L_(init_iter
)(L_(iter
) *iter
);
203 #define AVL_IMPL_INIT 1
204 #define AVL_IMPL_IS_EMPTY (1 << 1)
205 #define AVL_IMPL_INSERT (1 << 2)
206 #define AVL_IMPL_SEARCH (1 << 3)
207 #define AVL_IMPL_SEARCH_LEAST (1 << 4)
208 #define AVL_IMPL_SEARCH_GREATEST (1 << 5)
209 #define AVL_IMPL_REMOVE (1 << 6)
210 #define AVL_IMPL_BUILD (1 << 7)
211 #define AVL_IMPL_START_ITER (1 << 8)
212 #define AVL_IMPL_START_ITER_LEAST (1 << 9)
213 #define AVL_IMPL_START_ITER_GREATEST (1 << 10)
214 #define AVL_IMPL_GET_ITER (1 << 11)
215 #define AVL_IMPL_INCR_ITER (1 << 12)
216 #define AVL_IMPL_DECR_ITER (1 << 13)
217 #define AVL_IMPL_INIT_ITER (1 << 14)
218 #define AVL_IMPL_SUBST (1 << 15)
220 #define AVL_IMPL_ALL (~0)
223 #undef L_EST_LONG_BIT
227 #undef L_BIT_ARR_DEFN