OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. | 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 | 103 |
104 /* The maximum depth may be greater than the number of bits in a long, | 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 | 105 ** so multiple longs are needed to hold a bit array indexed by node |
106 ** depth. */ | 106 ** depth. */ |
107 | 107 |
108 #define L_BIT_ARR_LONGS (((AVL_MAX_DEPTH) + L_LONG_BIT - 1) / L_LONG_BIT) | 108 #define L_BIT_ARR_LONGS (((AVL_MAX_DEPTH) + L_LONG_BIT - 1) / L_LONG_BIT) |
109 | 109 |
110 #define L_BIT_ARR_DEFN(NAME) unsigned long NAME[L_BIT_ARR_LONGS]; | 110 #define L_BIT_ARR_DEFN(NAME) unsigned long NAME[L_BIT_ARR_LONGS]; |
111 | 111 |
112 #define L_BIT_ARR_VAL(BIT_ARR, BIT_NUM) \ | 112 #define L_BIT_ARR_VAL(BIT_ARR, BIT_NUM) \ |
113 ((BIT_ARR)[(BIT_NUM) / L_LONG_BIT] & (1L << ((BIT_NUM) % L_LONG_BIT))) | 113 ((BIT_ARR)[(BIT_NUM) / L_LONG_BIT] & (1L << ((BIT_NUM) % L_LONG_BIT))) |
114 | 114 |
115 #define L_BIT_ARR_0(BIT_ARR, BIT_NUM) \ | 115 #define L_BIT_ARR_0(BIT_ARR, BIT_NUM) \ |
116 (BIT_ARR)[(BIT_NUM) / L_LONG_BIT] &= ~(1L << ((BIT_NUM) % L_LONG_BIT)); | 116 (BIT_ARR)[(BIT_NUM) / L_LONG_BIT] &= ~(1L << ((BIT_NUM) % L_LONG_BIT)); |
117 | 117 |
118 #define L_BIT_ARR_1(BIT_ARR, BIT_NUM) \ | 118 #define L_BIT_ARR_1(BIT_ARR, BIT_NUM) \ |
119 (BIT_ARR)[(BIT_NUM) / L_LONG_BIT] |= 1L << ((BIT_NUM) % L_LONG_BIT); | 119 (BIT_ARR)[(BIT_NUM) / L_LONG_BIT] |= 1L << ((BIT_NUM) % L_LONG_BIT); |
120 | 120 |
121 #define L_BIT_ARR_ALL(BIT_ARR, BIT_VAL) \ | 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); } | 122 { int i = L_BIT_ARR_LONGS; do (BIT_ARR)[--i] = 0L - (BIT_VAL); while(i); } |
123 | 123 |
124 #else /* The bit array can definitely fit in one long */ | 124 #else /* The bit array can definitely fit in one long */ |
125 | 125 |
126 #define L_BIT_ARR_DEFN(NAME) unsigned long NAME; | 126 #define L_BIT_ARR_DEFN(NAME) unsigned long NAME; |
127 | 127 |
128 #define L_BIT_ARR_VAL(BIT_ARR, BIT_NUM) ((BIT_ARR) & (1L << (BIT_NUM))) | 128 #define L_BIT_ARR_VAL(BIT_ARR, BIT_NUM) ((BIT_ARR) & (1L << (BIT_NUM))) |
129 | 129 |
130 #define L_BIT_ARR_0(BIT_ARR, BIT_NUM) (BIT_ARR) &= ~(1L << (BIT_NUM)); | 130 #define L_BIT_ARR_0(BIT_ARR, BIT_NUM) (BIT_ARR) &= ~(1L << (BIT_NUM)); |
131 | 131 |
132 #define L_BIT_ARR_1(BIT_ARR, BIT_NUM) (BIT_ARR) |= 1L << (BIT_NUM); | 132 #define L_BIT_ARR_1(BIT_ARR, BIT_NUM) (BIT_ARR) |= 1L << (BIT_NUM); |
133 | 133 |
134 #define L_BIT_ARR_ALL(BIT_ARR, BIT_VAL) (BIT_ARR) = 0L - (BIT_VAL); | 134 #define L_BIT_ARR_ALL(BIT_ARR, BIT_VAL) (BIT_ARR) = 0L - (BIT_VAL); |
135 | 135 |
136 #endif | 136 #endif |
137 | 137 |
138 #ifdef AVL_READ_ERRORS_HAPPEN | 138 #ifdef AVL_READ_ERRORS_HAPPEN |
139 | 139 |
140 #define L_CHECK_READ_ERROR(ERROR_RETURN) \ | 140 #define L_CHECK_READ_ERROR(ERROR_RETURN) \ |
141 { if (AVL_READ_ERROR) return(ERROR_RETURN); } | 141 { if (AVL_READ_ERROR) return(ERROR_RETURN); } |
142 | 142 |
143 #else | 143 #else |
144 | 144 |
145 #define L_CHECK_READ_ERROR(ERROR_RETURN) | 145 #define L_CHECK_READ_ERROR(ERROR_RETURN) |
146 | 146 |
147 #endif | 147 #endif |
148 | 148 |
149 /* The presumed reason that an instantiation places additional fields | 149 /* The presumed reason that an instantiation places additional fields |
150 ** inside the AVL tree structure is that the SET_ and GET_ macros | 150 ** inside the AVL tree structure is that the SET_ and GET_ macros |
151 ** need these fields. The "balance" function does not explicitly use | 151 ** need these fields. The "balance" function does not explicitly use |
(...skipping 20 matching lines...) Expand all Loading... |
172 | 172 |
173 #else | 173 #else |
174 | 174 |
175 /* Define all functions. */ | 175 /* Define all functions. */ |
176 #define L_IMPL_MASK AVL_IMPL_ALL | 176 #define L_IMPL_MASK AVL_IMPL_ALL |
177 | 177 |
178 #endif | 178 #endif |
179 | 179 |
180 #if (L_IMPL_MASK & AVL_IMPL_INIT) | 180 #if (L_IMPL_MASK & AVL_IMPL_INIT) |
181 | 181 |
182 L_SC void L_(init)(L_(avl) *l_tree) | 182 L_SC void L_(init)(L_(avl) *l_tree) { |
183 { | 183 l_tree->root = AVL_NULL; |
184 l_tree->root = AVL_NULL; | |
185 } | 184 } |
186 | 185 |
187 #endif | 186 #endif |
188 | 187 |
189 #if (L_IMPL_MASK & AVL_IMPL_IS_EMPTY) | 188 #if (L_IMPL_MASK & AVL_IMPL_IS_EMPTY) |
190 | 189 |
191 L_SC int L_(is_empty)(L_(avl) *l_tree) | 190 L_SC int L_(is_empty)(L_(avl) *l_tree) { |
192 { | 191 return(l_tree->root == AVL_NULL); |
193 return(l_tree->root == AVL_NULL); | |
194 } | 192 } |
195 | 193 |
196 #endif | 194 #endif |
197 | 195 |
198 /* Put the private balance function in the same compilation module as | 196 /* Put the private balance function in the same compilation module as |
199 ** the insert function. */ | 197 ** the insert function. */ |
200 #if (L_IMPL_MASK & AVL_IMPL_INSERT) | 198 #if (L_IMPL_MASK & AVL_IMPL_INSERT) |
201 | 199 |
202 /* Balances subtree, returns handle of root node of subtree after balancing. | 200 /* Balances subtree, returns handle of root node of subtree after balancing. |
203 */ | 201 */ |
204 L_SC AVL_HANDLE L_(balance)(L_BALANCE_PARAM_DECL_PREFIX AVL_HANDLE bal_h) | 202 L_SC AVL_HANDLE L_(balance)(L_BALANCE_PARAM_DECL_PREFIX AVL_HANDLE bal_h) { |
205 { | 203 AVL_HANDLE deep_h; |
206 AVL_HANDLE deep_h; | 204 |
207 | 205 /* Either the "greater than" or the "less than" subtree of |
208 /* Either the "greater than" or the "less than" subtree of | 206 ** this node has to be 2 levels deeper (or else it wouldn't |
209 ** this node has to be 2 levels deeper (or else it wouldn't | 207 ** need balancing). |
210 ** need balancing). | 208 */ |
211 */ | 209 if (AVL_GET_BALANCE_FACTOR(bal_h) > 0) { |
212 if (AVL_GET_BALANCE_FACTOR(bal_h) > 0) | 210 /* "Greater than" subtree is deeper. */ |
213 { | 211 |
214 /* "Greater than" subtree is deeper. */ | 212 deep_h = AVL_GET_GREATER(bal_h, 1); |
215 | 213 |
216 deep_h = AVL_GET_GREATER(bal_h, 1); | 214 L_CHECK_READ_ERROR(AVL_NULL) |
| 215 |
| 216 if (AVL_GET_BALANCE_FACTOR(deep_h) < 0) { |
| 217 int bf; |
| 218 |
| 219 AVL_HANDLE old_h = bal_h; |
| 220 bal_h = AVL_GET_LESS(deep_h, 1); |
| 221 L_CHECK_READ_ERROR(AVL_NULL) |
| 222 AVL_SET_GREATER(old_h, AVL_GET_LESS(bal_h, 1)) |
| 223 AVL_SET_LESS(deep_h, AVL_GET_GREATER(bal_h, 1)) |
| 224 AVL_SET_LESS(bal_h, old_h) |
| 225 AVL_SET_GREATER(bal_h, deep_h) |
| 226 |
| 227 bf = AVL_GET_BALANCE_FACTOR(bal_h); |
| 228 |
| 229 if (bf != 0) { |
| 230 if (bf > 0) { |
| 231 AVL_SET_BALANCE_FACTOR(old_h, -1) |
| 232 AVL_SET_BALANCE_FACTOR(deep_h, 0) |
| 233 } else { |
| 234 AVL_SET_BALANCE_FACTOR(deep_h, 1) |
| 235 AVL_SET_BALANCE_FACTOR(old_h, 0) |
| 236 } |
| 237 |
| 238 AVL_SET_BALANCE_FACTOR(bal_h, 0) |
| 239 } else { |
| 240 AVL_SET_BALANCE_FACTOR(old_h, 0) |
| 241 AVL_SET_BALANCE_FACTOR(deep_h, 0) |
| 242 } |
| 243 } else { |
| 244 AVL_SET_GREATER(bal_h, AVL_GET_LESS(deep_h, 0)) |
| 245 AVL_SET_LESS(deep_h, bal_h) |
| 246 |
| 247 if (AVL_GET_BALANCE_FACTOR(deep_h) == 0) { |
| 248 AVL_SET_BALANCE_FACTOR(deep_h, -1) |
| 249 AVL_SET_BALANCE_FACTOR(bal_h, 1) |
| 250 } else { |
| 251 AVL_SET_BALANCE_FACTOR(deep_h, 0) |
| 252 AVL_SET_BALANCE_FACTOR(bal_h, 0) |
| 253 } |
| 254 |
| 255 bal_h = deep_h; |
| 256 } |
| 257 } else { |
| 258 /* "Less than" subtree is deeper. */ |
| 259 |
| 260 deep_h = AVL_GET_LESS(bal_h, 1); |
| 261 L_CHECK_READ_ERROR(AVL_NULL) |
| 262 |
| 263 if (AVL_GET_BALANCE_FACTOR(deep_h) > 0) { |
| 264 int bf; |
| 265 AVL_HANDLE old_h = bal_h; |
| 266 bal_h = AVL_GET_GREATER(deep_h, 1); |
| 267 L_CHECK_READ_ERROR(AVL_NULL) |
| 268 AVL_SET_LESS(old_h, AVL_GET_GREATER(bal_h, 0)) |
| 269 AVL_SET_GREATER(deep_h, AVL_GET_LESS(bal_h, 0)) |
| 270 AVL_SET_GREATER(bal_h, old_h) |
| 271 AVL_SET_LESS(bal_h, deep_h) |
| 272 |
| 273 bf = AVL_GET_BALANCE_FACTOR(bal_h); |
| 274 |
| 275 if (bf != 0) { |
| 276 if (bf < 0) { |
| 277 AVL_SET_BALANCE_FACTOR(old_h, 1) |
| 278 AVL_SET_BALANCE_FACTOR(deep_h, 0) |
| 279 } else { |
| 280 AVL_SET_BALANCE_FACTOR(deep_h, -1) |
| 281 AVL_SET_BALANCE_FACTOR(old_h, 0) |
| 282 } |
| 283 |
| 284 AVL_SET_BALANCE_FACTOR(bal_h, 0) |
| 285 } else { |
| 286 AVL_SET_BALANCE_FACTOR(old_h, 0) |
| 287 AVL_SET_BALANCE_FACTOR(deep_h, 0) |
| 288 } |
| 289 } else { |
| 290 AVL_SET_LESS(bal_h, AVL_GET_GREATER(deep_h, 0)) |
| 291 AVL_SET_GREATER(deep_h, bal_h) |
| 292 |
| 293 if (AVL_GET_BALANCE_FACTOR(deep_h) == 0) { |
| 294 AVL_SET_BALANCE_FACTOR(deep_h, 1) |
| 295 AVL_SET_BALANCE_FACTOR(bal_h, -1) |
| 296 } else { |
| 297 AVL_SET_BALANCE_FACTOR(deep_h, 0) |
| 298 AVL_SET_BALANCE_FACTOR(bal_h, 0) |
| 299 } |
| 300 |
| 301 bal_h = deep_h; |
| 302 } |
| 303 } |
| 304 |
| 305 return(bal_h); |
| 306 } |
| 307 |
| 308 L_SC AVL_HANDLE L_(insert)(L_(avl) *l_tree, AVL_HANDLE h) { |
| 309 AVL_SET_LESS(h, AVL_NULL) |
| 310 AVL_SET_GREATER(h, AVL_NULL) |
| 311 AVL_SET_BALANCE_FACTOR(h, 0) |
| 312 |
| 313 if (l_tree->root == AVL_NULL) |
| 314 l_tree->root = h; |
| 315 else { |
| 316 /* Last unbalanced node encountered in search for insertion point. */ |
| 317 AVL_HANDLE unbal = AVL_NULL; |
| 318 /* Parent of last unbalanced node. */ |
| 319 AVL_HANDLE parent_unbal = AVL_NULL; |
| 320 /* Balance factor of last unbalanced node. */ |
| 321 int unbal_bf; |
| 322 |
| 323 /* Zero-based depth in tree. */ |
| 324 unsigned depth = 0, unbal_depth = 0; |
| 325 |
| 326 /* Records a path into the tree. If bit n is true, indicates |
| 327 ** take greater branch from the nth node in the path, otherwise |
| 328 ** take the less branch. bit 0 gives branch from root, and |
| 329 ** so on. */ |
| 330 L_BIT_ARR_DEFN(branch) |
| 331 |
| 332 AVL_HANDLE hh = l_tree->root; |
| 333 AVL_HANDLE parent = AVL_NULL; |
| 334 int cmp; |
| 335 |
| 336 do { |
| 337 if (AVL_GET_BALANCE_FACTOR(hh) != 0) { |
| 338 unbal = hh; |
| 339 parent_unbal = parent; |
| 340 unbal_depth = depth; |
| 341 } |
| 342 |
| 343 cmp = AVL_COMPARE_NODE_NODE(h, hh); |
| 344 |
| 345 if (cmp == 0) |
| 346 /* Duplicate key. */ |
| 347 return(hh); |
| 348 |
| 349 parent = hh; |
| 350 |
| 351 if (cmp > 0) { |
| 352 hh = AVL_GET_GREATER(hh, 1); |
| 353 L_BIT_ARR_1(branch, depth) |
| 354 } else { |
| 355 hh = AVL_GET_LESS(hh, 1); |
| 356 L_BIT_ARR_0(branch, depth) |
| 357 } |
| 358 |
| 359 L_CHECK_READ_ERROR(AVL_NULL) |
| 360 depth++; |
| 361 } while (hh != AVL_NULL); |
| 362 |
| 363 /* Add node to insert as leaf of tree. */ |
| 364 if (cmp < 0) |
| 365 AVL_SET_LESS(parent, h) |
| 366 else |
| 367 AVL_SET_GREATER(parent, h) |
| 368 |
| 369 depth = unbal_depth; |
| 370 |
| 371 if (unbal == AVL_NULL) |
| 372 hh = l_tree->root; |
| 373 else { |
| 374 cmp = L_BIT_ARR_VAL(branch, depth) ? 1 : -1; |
| 375 depth++; |
| 376 unbal_bf = AVL_GET_BALANCE_FACTOR(unbal); |
| 377 |
| 378 if (cmp < 0) |
| 379 unbal_bf--; |
| 380 else /* cmp > 0 */ |
| 381 unbal_bf++; |
| 382 |
| 383 hh = cmp < 0 ? AVL_GET_LESS(unbal, 1) : AVL_GET_GREATER(unbal, 1); |
| 384 L_CHECK_READ_ERROR(AVL_NULL) |
| 385 |
| 386 if ((unbal_bf != -2) && (unbal_bf != 2)) { |
| 387 /* No rebalancing of tree is necessary. */ |
| 388 AVL_SET_BALANCE_FACTOR(unbal, unbal_bf) |
| 389 unbal = AVL_NULL; |
| 390 } |
| 391 } |
| 392 |
| 393 if (hh != AVL_NULL) |
| 394 while (h != hh) { |
| 395 cmp = L_BIT_ARR_VAL(branch, depth) ? 1 : -1; |
| 396 depth++; |
| 397 |
| 398 if (cmp < 0) { |
| 399 AVL_SET_BALANCE_FACTOR(hh, -1) |
| 400 hh = AVL_GET_LESS(hh, 1); |
| 401 } else { /* cmp > 0 */ |
| 402 AVL_SET_BALANCE_FACTOR(hh, 1) |
| 403 hh = AVL_GET_GREATER(hh, 1); |
| 404 } |
217 | 405 |
218 L_CHECK_READ_ERROR(AVL_NULL) | 406 L_CHECK_READ_ERROR(AVL_NULL) |
219 | 407 } |
220 if (AVL_GET_BALANCE_FACTOR(deep_h) < 0) | 408 |
221 { | 409 if (unbal != AVL_NULL) { |
222 int bf; | 410 unbal = L_(balance)(L_BALANCE_PARAM_CALL_PREFIX unbal); |
223 | 411 L_CHECK_READ_ERROR(AVL_NULL) |
224 AVL_HANDLE old_h = bal_h; | 412 |
225 bal_h = AVL_GET_LESS(deep_h, 1); | 413 if (parent_unbal == AVL_NULL) |
226 L_CHECK_READ_ERROR(AVL_NULL) | 414 l_tree->root = unbal; |
227 AVL_SET_GREATER(old_h, AVL_GET_LESS(bal_h, 1)) | 415 else { |
228 AVL_SET_LESS(deep_h, AVL_GET_GREATER(bal_h, 1)) | 416 depth = unbal_depth - 1; |
229 AVL_SET_LESS(bal_h, old_h) | 417 cmp = L_BIT_ARR_VAL(branch, depth) ? 1 : -1; |
230 AVL_SET_GREATER(bal_h, deep_h) | 418 |
231 | 419 if (cmp < 0) |
232 bf = AVL_GET_BALANCE_FACTOR(bal_h); | 420 AVL_SET_LESS(parent_unbal, unbal) |
233 | 421 else /* cmp > 0 */ |
234 if (bf != 0) | 422 AVL_SET_GREATER(parent_unbal, unbal) |
235 { | 423 } |
236 if (bf > 0) | |
237 { | |
238 AVL_SET_BALANCE_FACTOR(old_h, -1) | |
239 AVL_SET_BALANCE_FACTOR(deep_h, 0) | |
240 } | |
241 else | |
242 { | |
243 AVL_SET_BALANCE_FACTOR(deep_h, 1) | |
244 AVL_SET_BALANCE_FACTOR(old_h, 0) | |
245 } | |
246 | |
247 AVL_SET_BALANCE_FACTOR(bal_h, 0) | |
248 } | |
249 else | |
250 { | |
251 AVL_SET_BALANCE_FACTOR(old_h, 0) | |
252 AVL_SET_BALANCE_FACTOR(deep_h, 0) | |
253 } | |
254 } | |
255 else | |
256 { | |
257 AVL_SET_GREATER(bal_h, AVL_GET_LESS(deep_h, 0)) | |
258 AVL_SET_LESS(deep_h, bal_h) | |
259 | |
260 if (AVL_GET_BALANCE_FACTOR(deep_h) == 0) | |
261 { | |
262 AVL_SET_BALANCE_FACTOR(deep_h, -1) | |
263 AVL_SET_BALANCE_FACTOR(bal_h, 1) | |
264 } | |
265 else | |
266 { | |
267 AVL_SET_BALANCE_FACTOR(deep_h, 0) | |
268 AVL_SET_BALANCE_FACTOR(bal_h, 0) | |
269 } | |
270 | |
271 bal_h = deep_h; | |
272 } | |
273 } | 424 } |
274 else | 425 |
275 { | 426 } |
276 /* "Less than" subtree is deeper. */ | 427 |
277 | 428 return(h); |
278 deep_h = AVL_GET_LESS(bal_h, 1); | |
279 L_CHECK_READ_ERROR(AVL_NULL) | |
280 | |
281 if (AVL_GET_BALANCE_FACTOR(deep_h) > 0) | |
282 { | |
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) | |
291 | |
292 bf = AVL_GET_BALANCE_FACTOR(bal_h); | |
293 | |
294 if (bf != 0) | |
295 { | |
296 if (bf < 0) | |
297 { | |
298 AVL_SET_BALANCE_FACTOR(old_h, 1) | |
299 AVL_SET_BALANCE_FACTOR(deep_h, 0) | |
300 } | |
301 else | |
302 { | |
303 AVL_SET_BALANCE_FACTOR(deep_h, -1) | |
304 AVL_SET_BALANCE_FACTOR(old_h, 0) | |
305 } | |
306 | |
307 AVL_SET_BALANCE_FACTOR(bal_h, 0) | |
308 } | |
309 else | |
310 { | |
311 AVL_SET_BALANCE_FACTOR(old_h, 0) | |
312 AVL_SET_BALANCE_FACTOR(deep_h, 0) | |
313 } | |
314 } | |
315 else | |
316 { | |
317 AVL_SET_LESS(bal_h, AVL_GET_GREATER(deep_h, 0)) | |
318 AVL_SET_GREATER(deep_h, bal_h) | |
319 | |
320 if (AVL_GET_BALANCE_FACTOR(deep_h) == 0) | |
321 { | |
322 AVL_SET_BALANCE_FACTOR(deep_h, 1) | |
323 AVL_SET_BALANCE_FACTOR(bal_h, -1) | |
324 } | |
325 else | |
326 { | |
327 AVL_SET_BALANCE_FACTOR(deep_h, 0) | |
328 AVL_SET_BALANCE_FACTOR(bal_h, 0) | |
329 } | |
330 | |
331 bal_h = deep_h; | |
332 } | |
333 } | |
334 | |
335 return(bal_h); | |
336 } | |
337 | |
338 L_SC AVL_HANDLE L_(insert)(L_(avl) *l_tree, AVL_HANDLE h) | |
339 { | |
340 AVL_SET_LESS(h, AVL_NULL) | |
341 AVL_SET_GREATER(h, AVL_NULL) | |
342 AVL_SET_BALANCE_FACTOR(h, 0) | |
343 | |
344 if (l_tree->root == AVL_NULL) | |
345 l_tree->root = h; | |
346 else | |
347 { | |
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; | |
354 | |
355 /* Zero-based depth in tree. */ | |
356 unsigned depth = 0, unbal_depth = 0; | |
357 | |
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) | |
363 | |
364 AVL_HANDLE hh = l_tree->root; | |
365 AVL_HANDLE parent = AVL_NULL; | |
366 int cmp; | |
367 | |
368 do | |
369 { | |
370 if (AVL_GET_BALANCE_FACTOR(hh) != 0) | |
371 { | |
372 unbal = hh; | |
373 parent_unbal = parent; | |
374 unbal_depth = depth; | |
375 } | |
376 | |
377 cmp = AVL_COMPARE_NODE_NODE(h, hh); | |
378 | |
379 if (cmp == 0) | |
380 /* Duplicate key. */ | |
381 return(hh); | |
382 | |
383 parent = hh; | |
384 | |
385 if (cmp > 0) | |
386 { | |
387 hh = AVL_GET_GREATER(hh, 1); | |
388 L_BIT_ARR_1(branch, depth) | |
389 } | |
390 else | |
391 { | |
392 hh = AVL_GET_LESS(hh, 1); | |
393 L_BIT_ARR_0(branch, depth) | |
394 } | |
395 | |
396 L_CHECK_READ_ERROR(AVL_NULL) | |
397 depth++; | |
398 } | |
399 while (hh != AVL_NULL); | |
400 | |
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) | |
406 | |
407 depth = unbal_depth; | |
408 | |
409 if (unbal == AVL_NULL) | |
410 hh = l_tree->root; | |
411 else | |
412 { | |
413 cmp = L_BIT_ARR_VAL(branch, depth) ? 1 : -1; | |
414 depth++; | |
415 unbal_bf = AVL_GET_BALANCE_FACTOR(unbal); | |
416 | |
417 if (cmp < 0) | |
418 unbal_bf--; | |
419 else /* cmp > 0 */ | |
420 unbal_bf++; | |
421 | |
422 hh = cmp < 0 ? AVL_GET_LESS(unbal, 1) : AVL_GET_GREATER(unbal, 1); | |
423 L_CHECK_READ_ERROR(AVL_NULL) | |
424 | |
425 if ((unbal_bf != -2) && (unbal_bf != 2)) | |
426 { | |
427 /* No rebalancing of tree is necessary. */ | |
428 AVL_SET_BALANCE_FACTOR(unbal, unbal_bf) | |
429 unbal = AVL_NULL; | |
430 } | |
431 } | |
432 | |
433 if (hh != AVL_NULL) | |
434 while (h != hh) | |
435 { | |
436 cmp = L_BIT_ARR_VAL(branch, depth) ? 1 : -1; | |
437 depth++; | |
438 | |
439 if (cmp < 0) | |
440 { | |
441 AVL_SET_BALANCE_FACTOR(hh, -1) | |
442 hh = AVL_GET_LESS(hh, 1); | |
443 } | |
444 else /* cmp > 0 */ | |
445 { | |
446 AVL_SET_BALANCE_FACTOR(hh, 1) | |
447 hh = AVL_GET_GREATER(hh, 1); | |
448 } | |
449 | |
450 L_CHECK_READ_ERROR(AVL_NULL) | |
451 } | |
452 | |
453 if (unbal != AVL_NULL) | |
454 { | |
455 unbal = L_(balance)(L_BALANCE_PARAM_CALL_PREFIX unbal); | |
456 L_CHECK_READ_ERROR(AVL_NULL) | |
457 | |
458 if (parent_unbal == AVL_NULL) | |
459 l_tree->root = unbal; | |
460 else | |
461 { | |
462 depth = unbal_depth - 1; | |
463 cmp = L_BIT_ARR_VAL(branch, depth) ? 1 : -1; | |
464 | |
465 if (cmp < 0) | |
466 AVL_SET_LESS(parent_unbal, unbal) | |
467 else /* cmp > 0 */ | |
468 AVL_SET_GREATER(parent_unbal, unbal) | |
469 } | |
470 } | |
471 | |
472 } | |
473 | |
474 return(h); | |
475 } | 429 } |
476 | 430 |
477 #endif | 431 #endif |
478 | 432 |
479 #if (L_IMPL_MASK & AVL_IMPL_SEARCH) | 433 #if (L_IMPL_MASK & AVL_IMPL_SEARCH) |
480 | 434 |
481 L_SC AVL_HANDLE L_(search)(L_(avl) *l_tree, AVL_KEY k, avl_search_type st) | 435 L_SC AVL_HANDLE L_(search)(L_(avl) *l_tree, AVL_KEY k, avl_search_type st) { |
482 { | 436 int cmp, target_cmp; |
483 int cmp, target_cmp; | 437 AVL_HANDLE match_h = AVL_NULL; |
484 AVL_HANDLE match_h = AVL_NULL; | 438 AVL_HANDLE h = l_tree->root; |
485 AVL_HANDLE h = l_tree->root; | 439 |
486 | 440 if (st & AVL_LESS) |
487 if (st & AVL_LESS) | 441 target_cmp = 1; |
488 target_cmp = 1; | 442 else if (st & AVL_GREATER) |
489 else if (st & AVL_GREATER) | 443 target_cmp = -1; |
490 target_cmp = -1; | 444 else |
491 else | 445 target_cmp = 0; |
492 target_cmp = 0; | 446 |
493 | 447 while (h != AVL_NULL) { |
494 while (h != AVL_NULL) | 448 cmp = AVL_COMPARE_KEY_NODE(k, h); |
495 { | 449 |
496 cmp = AVL_COMPARE_KEY_NODE(k, h); | 450 if (cmp == 0) { |
497 | 451 if (st & AVL_EQUAL) { |
498 if (cmp == 0) | 452 match_h = h; |
499 { | 453 break; |
500 if (st & AVL_EQUAL) | 454 } |
501 { | 455 |
502 match_h = h; | 456 cmp = -target_cmp; |
503 break; | 457 } else if (target_cmp != 0) |
504 } | 458 if (!((cmp ^ target_cmp) & L_MASK_HIGH_BIT)) |
505 | 459 /* cmp and target_cmp are both positive or both negative. */ |
506 cmp = -target_cmp; | 460 match_h = h; |
507 } | 461 |
508 else if (target_cmp != 0) | 462 h = cmp < 0 ? AVL_GET_LESS(h, 1) : AVL_GET_GREATER(h, 1); |
509 if (!((cmp ^ target_cmp) & L_MASK_HIGH_BIT)) | 463 L_CHECK_READ_ERROR(AVL_NULL) |
510 /* cmp and target_cmp are both positive or both negative. */ | 464 } |
511 match_h = h; | 465 |
512 | 466 return(match_h); |
513 h = cmp < 0 ? AVL_GET_LESS(h, 1) : AVL_GET_GREATER(h, 1); | |
514 L_CHECK_READ_ERROR(AVL_NULL) | |
515 } | |
516 | |
517 return(match_h); | |
518 } | 467 } |
519 | 468 |
520 #endif | 469 #endif |
521 | 470 |
522 #if (L_IMPL_MASK & AVL_IMPL_SEARCH_LEAST) | 471 #if (L_IMPL_MASK & AVL_IMPL_SEARCH_LEAST) |
523 | 472 |
524 L_SC AVL_HANDLE L_(search_least)(L_(avl) *l_tree) | 473 L_SC AVL_HANDLE L_(search_least)(L_(avl) *l_tree) { |
525 { | 474 AVL_HANDLE h = l_tree->root; |
526 AVL_HANDLE h = l_tree->root; | 475 AVL_HANDLE parent = AVL_NULL; |
527 AVL_HANDLE parent = AVL_NULL; | 476 |
528 | 477 while (h != AVL_NULL) { |
529 while (h != AVL_NULL) | 478 parent = h; |
530 { | 479 h = AVL_GET_LESS(h, 1); |
531 parent = h; | 480 L_CHECK_READ_ERROR(AVL_NULL) |
532 h = AVL_GET_LESS(h, 1); | 481 } |
533 L_CHECK_READ_ERROR(AVL_NULL) | 482 |
534 } | 483 return(parent); |
535 | |
536 return(parent); | |
537 } | 484 } |
538 | 485 |
539 #endif | 486 #endif |
540 | 487 |
541 #if (L_IMPL_MASK & AVL_IMPL_SEARCH_GREATEST) | 488 #if (L_IMPL_MASK & AVL_IMPL_SEARCH_GREATEST) |
542 | 489 |
543 L_SC AVL_HANDLE L_(search_greatest)(L_(avl) *l_tree) | 490 L_SC AVL_HANDLE L_(search_greatest)(L_(avl) *l_tree) { |
544 { | 491 AVL_HANDLE h = l_tree->root; |
545 AVL_HANDLE h = l_tree->root; | 492 AVL_HANDLE parent = AVL_NULL; |
546 AVL_HANDLE parent = AVL_NULL; | 493 |
547 | 494 while (h != AVL_NULL) { |
548 while (h != AVL_NULL) | 495 parent = h; |
549 { | 496 h = AVL_GET_GREATER(h, 1); |
550 parent = h; | 497 L_CHECK_READ_ERROR(AVL_NULL) |
551 h = AVL_GET_GREATER(h, 1); | 498 } |
552 L_CHECK_READ_ERROR(AVL_NULL) | 499 |
553 } | 500 return(parent); |
554 | |
555 return(parent); | |
556 } | 501 } |
557 | 502 |
558 #endif | 503 #endif |
559 | 504 |
560 #if (L_IMPL_MASK & AVL_IMPL_REMOVE) | 505 #if (L_IMPL_MASK & AVL_IMPL_REMOVE) |
561 | 506 |
562 /* Prototype of balance function (called by remove) in case not in | 507 /* Prototype of balance function (called by remove) in case not in |
563 ** same compilation unit. | 508 ** same compilation unit. |
564 */ | 509 */ |
565 L_SC AVL_HANDLE L_(balance)(L_BALANCE_PARAM_DECL_PREFIX AVL_HANDLE bal_h); | 510 L_SC AVL_HANDLE L_(balance)(L_BALANCE_PARAM_DECL_PREFIX AVL_HANDLE bal_h); |
566 | 511 |
567 L_SC AVL_HANDLE L_(remove)(L_(avl) *l_tree, AVL_KEY k) | 512 L_SC AVL_HANDLE L_(remove)(L_(avl) *l_tree, AVL_KEY k) { |
568 { | 513 /* Zero-based depth in tree. */ |
569 /* Zero-based depth in tree. */ | 514 unsigned depth = 0, rm_depth; |
570 unsigned depth = 0, rm_depth; | 515 |
571 | 516 /* Records a path into the tree. If bit n is true, indicates |
572 /* Records a path into the tree. If bit n is true, indicates | 517 ** take greater branch from the nth node in the path, otherwise |
573 ** take greater branch from the nth node in the path, otherwise | 518 ** take the less branch. bit 0 gives branch from root, and |
574 ** take the less branch. bit 0 gives branch from root, and | 519 ** so on. */ |
575 ** so on. */ | 520 L_BIT_ARR_DEFN(branch) |
576 L_BIT_ARR_DEFN(branch) | 521 |
577 | 522 AVL_HANDLE h = l_tree->root; |
578 AVL_HANDLE h = l_tree->root; | 523 AVL_HANDLE parent = AVL_NULL; |
579 AVL_HANDLE parent = AVL_NULL; | 524 AVL_HANDLE child; |
580 AVL_HANDLE child; | 525 AVL_HANDLE path; |
581 AVL_HANDLE path; | 526 int cmp, cmp_shortened_sub_with_path; |
582 int cmp, cmp_shortened_sub_with_path; | 527 int reduced_depth; |
583 int reduced_depth; | 528 int bf; |
584 int bf; | 529 AVL_HANDLE rm; |
585 AVL_HANDLE rm; | 530 AVL_HANDLE parent_rm; |
586 AVL_HANDLE parent_rm; | 531 |
587 | 532 for (;;) { |
588 for (; ;) | 533 if (h == AVL_NULL) |
589 { | 534 /* No node in tree with given key. */ |
590 if (h == AVL_NULL) | 535 return(AVL_NULL); |
591 /* No node in tree with given key. */ | 536 |
592 return(AVL_NULL); | 537 cmp = AVL_COMPARE_KEY_NODE(k, h); |
593 | 538 |
594 cmp = AVL_COMPARE_KEY_NODE(k, h); | 539 if (cmp == 0) |
595 | 540 /* Found node to remove. */ |
596 if (cmp == 0) | 541 break; |
597 /* Found node to remove. */ | 542 |
598 break; | 543 parent = h; |
599 | 544 |
600 parent = h; | 545 if (cmp > 0) { |
601 | 546 h = AVL_GET_GREATER(h, 1); |
602 if (cmp > 0) | 547 L_BIT_ARR_1(branch, depth) |
603 { | 548 } else { |
604 h = AVL_GET_GREATER(h, 1); | 549 h = AVL_GET_LESS(h, 1); |
605 L_BIT_ARR_1(branch, depth) | 550 L_BIT_ARR_0(branch, depth) |
606 } | |
607 else | |
608 { | |
609 h = AVL_GET_LESS(h, 1); | |
610 L_BIT_ARR_0(branch, depth) | |
611 } | |
612 | |
613 L_CHECK_READ_ERROR(AVL_NULL) | |
614 depth++; | |
615 cmp_shortened_sub_with_path = cmp; | |
616 } | 551 } |
617 | 552 |
618 rm = h; | 553 L_CHECK_READ_ERROR(AVL_NULL) |
619 parent_rm = parent; | 554 depth++; |
620 rm_depth = depth; | 555 cmp_shortened_sub_with_path = cmp; |
621 | 556 } |
622 /* If the node to remove is not a leaf node, we need to get a | 557 |
623 ** leaf node, or a node with a single leaf as its child, to put | 558 rm = h; |
624 ** in the place of the node to remove. We will get the greatest | 559 parent_rm = parent; |
625 ** node in the less subtree (of the node to remove), or the least | 560 rm_depth = depth; |
626 ** node in the greater subtree. We take the leaf node from the | 561 |
627 ** deeper subtree, if there is one. */ | 562 /* If the node to remove is not a leaf node, we need to get a |
628 | 563 ** leaf node, or a node with a single leaf as its child, to put |
629 if (AVL_GET_BALANCE_FACTOR(h) < 0) | 564 ** in the place of the node to remove. We will get the greatest |
630 { | 565 ** node in the less subtree (of the node to remove), or the least |
| 566 ** node in the greater subtree. We take the leaf node from the |
| 567 ** deeper subtree, if there is one. */ |
| 568 |
| 569 if (AVL_GET_BALANCE_FACTOR(h) < 0) { |
| 570 child = AVL_GET_LESS(h, 1); |
| 571 L_BIT_ARR_0(branch, depth) |
| 572 cmp = -1; |
| 573 } else { |
| 574 child = AVL_GET_GREATER(h, 1); |
| 575 L_BIT_ARR_1(branch, depth) |
| 576 cmp = 1; |
| 577 } |
| 578 |
| 579 L_CHECK_READ_ERROR(AVL_NULL) |
| 580 depth++; |
| 581 |
| 582 if (child != AVL_NULL) { |
| 583 cmp = -cmp; |
| 584 |
| 585 do { |
| 586 parent = h; |
| 587 h = child; |
| 588 |
| 589 if (cmp < 0) { |
631 child = AVL_GET_LESS(h, 1); | 590 child = AVL_GET_LESS(h, 1); |
632 L_BIT_ARR_0(branch, depth) | 591 L_BIT_ARR_0(branch, depth) |
633 cmp = -1; | 592 } else { |
634 } | |
635 else | |
636 { | |
637 child = AVL_GET_GREATER(h, 1); | 593 child = AVL_GET_GREATER(h, 1); |
638 L_BIT_ARR_1(branch, depth) | 594 L_BIT_ARR_1(branch, depth) |
639 cmp = 1; | 595 } |
| 596 |
| 597 L_CHECK_READ_ERROR(AVL_NULL) |
| 598 depth++; |
| 599 } while (child != AVL_NULL); |
| 600 |
| 601 if (parent == rm) |
| 602 /* Only went through do loop once. Deleted node will be replaced |
| 603 ** in the tree structure by one of its immediate children. */ |
| 604 cmp_shortened_sub_with_path = -cmp; |
| 605 else |
| 606 cmp_shortened_sub_with_path = cmp; |
| 607 |
| 608 /* Get the handle of the opposite child, which may not be null. */ |
| 609 child = cmp > 0 ? AVL_GET_LESS(h, 0) : AVL_GET_GREATER(h, 0); |
| 610 } |
| 611 |
| 612 if (parent == AVL_NULL) |
| 613 /* There were only 1 or 2 nodes in this tree. */ |
| 614 l_tree->root = child; |
| 615 else if (cmp_shortened_sub_with_path < 0) |
| 616 AVL_SET_LESS(parent, child) |
| 617 else |
| 618 AVL_SET_GREATER(parent, child) |
| 619 |
| 620 /* "path" is the parent of the subtree being eliminated or reduced |
| 621 ** from a depth of 2 to 1. If "path" is the node to be removed, we |
| 622 ** set path to the node we're about to poke into the position of the |
| 623 ** node to be removed. */ |
| 624 path = parent == rm ? h : parent; |
| 625 |
| 626 if (h != rm) { |
| 627 /* Poke in the replacement for the node to be removed. */ |
| 628 AVL_SET_LESS(h, AVL_GET_LESS(rm, 0)) |
| 629 AVL_SET_GREATER(h, AVL_GET_GREATER(rm, 0)) |
| 630 AVL_SET_BALANCE_FACTOR(h, AVL_GET_BALANCE_FACTOR(rm)) |
| 631 |
| 632 if (parent_rm == AVL_NULL) |
| 633 l_tree->root = h; |
| 634 else { |
| 635 depth = rm_depth - 1; |
| 636 |
| 637 if (L_BIT_ARR_VAL(branch, depth)) |
| 638 AVL_SET_GREATER(parent_rm, h) |
| 639 else |
| 640 AVL_SET_LESS(parent_rm, h) |
| 641 } |
| 642 } |
| 643 |
| 644 if (path != AVL_NULL) { |
| 645 /* Create a temporary linked list from the parent of the path node |
| 646 ** to the root node. */ |
| 647 h = l_tree->root; |
| 648 parent = AVL_NULL; |
| 649 depth = 0; |
| 650 |
| 651 while (h != path) { |
| 652 if (L_BIT_ARR_VAL(branch, depth)) { |
| 653 child = AVL_GET_GREATER(h, 1); |
| 654 AVL_SET_GREATER(h, parent) |
| 655 } else { |
| 656 child = AVL_GET_LESS(h, 1); |
| 657 AVL_SET_LESS(h, parent) |
| 658 } |
| 659 |
| 660 L_CHECK_READ_ERROR(AVL_NULL) |
| 661 depth++; |
| 662 parent = h; |
| 663 h = child; |
640 } | 664 } |
641 | 665 |
| 666 /* Climb from the path node to the root node using the linked |
| 667 ** list, restoring the tree structure and rebalancing as necessary. |
| 668 */ |
| 669 reduced_depth = 1; |
| 670 cmp = cmp_shortened_sub_with_path; |
| 671 |
| 672 for (;;) { |
| 673 if (reduced_depth) { |
| 674 bf = AVL_GET_BALANCE_FACTOR(h); |
| 675 |
| 676 if (cmp < 0) |
| 677 bf++; |
| 678 else /* cmp > 0 */ |
| 679 bf--; |
| 680 |
| 681 if ((bf == -2) || (bf == 2)) { |
| 682 h = L_(balance)(L_BALANCE_PARAM_CALL_PREFIX h); |
| 683 L_CHECK_READ_ERROR(AVL_NULL) |
| 684 bf = AVL_GET_BALANCE_FACTOR(h); |
| 685 } else |
| 686 AVL_SET_BALANCE_FACTOR(h, bf) |
| 687 reduced_depth = (bf == 0); |
| 688 } |
| 689 |
| 690 if (parent == AVL_NULL) |
| 691 break; |
| 692 |
| 693 child = h; |
| 694 h = parent; |
| 695 depth--; |
| 696 cmp = L_BIT_ARR_VAL(branch, depth) ? 1 : -1; |
| 697 |
| 698 if (cmp < 0) { |
| 699 parent = AVL_GET_LESS(h, 1); |
| 700 AVL_SET_LESS(h, child) |
| 701 } else { |
| 702 parent = AVL_GET_GREATER(h, 1); |
| 703 AVL_SET_GREATER(h, child) |
| 704 } |
| 705 |
| 706 L_CHECK_READ_ERROR(AVL_NULL) |
| 707 } |
| 708 |
| 709 l_tree->root = h; |
| 710 } |
| 711 |
| 712 return(rm); |
| 713 } |
| 714 |
| 715 #endif |
| 716 |
| 717 #if (L_IMPL_MASK & AVL_IMPL_SUBST) |
| 718 |
| 719 L_SC AVL_HANDLE L_(subst)(L_(avl) *l_tree, AVL_HANDLE new_node) { |
| 720 AVL_HANDLE h = l_tree->root; |
| 721 AVL_HANDLE parent = AVL_NULL; |
| 722 int cmp, last_cmp; |
| 723 |
| 724 /* Search for node already in tree with same key. */ |
| 725 for (;;) { |
| 726 if (h == AVL_NULL) |
| 727 /* No node in tree with same key as new node. */ |
| 728 return(AVL_NULL); |
| 729 |
| 730 cmp = AVL_COMPARE_NODE_NODE(new_node, h); |
| 731 |
| 732 if (cmp == 0) |
| 733 /* Found the node to substitute new one for. */ |
| 734 break; |
| 735 |
| 736 last_cmp = cmp; |
| 737 parent = h; |
| 738 h = cmp < 0 ? AVL_GET_LESS(h, 1) : AVL_GET_GREATER(h, 1); |
642 L_CHECK_READ_ERROR(AVL_NULL) | 739 L_CHECK_READ_ERROR(AVL_NULL) |
643 depth++; | 740 } |
644 | 741 |
645 if (child != AVL_NULL) | 742 /* Copy tree housekeeping fields from node in tree to new node. */ |
646 { | 743 AVL_SET_LESS(new_node, AVL_GET_LESS(h, 0)) |
647 cmp = -cmp; | 744 AVL_SET_GREATER(new_node, AVL_GET_GREATER(h, 0)) |
648 | 745 AVL_SET_BALANCE_FACTOR(new_node, AVL_GET_BALANCE_FACTOR(h)) |
649 do | 746 |
650 { | 747 if (parent == AVL_NULL) |
651 parent = h; | 748 /* New node is also new root. */ |
652 h = child; | 749 l_tree->root = new_node; |
653 | 750 else { |
654 if (cmp < 0) | 751 /* Make parent point to new node. */ |
655 { | 752 if (last_cmp < 0) |
656 child = AVL_GET_LESS(h, 1); | 753 AVL_SET_LESS(parent, new_node) |
657 L_BIT_ARR_0(branch, depth) | 754 else |
658 } | 755 AVL_SET_GREATER(parent, new_node) |
659 else | 756 } |
660 { | 757 |
661 child = AVL_GET_GREATER(h, 1); | 758 return(h); |
662 L_BIT_ARR_1(branch, depth) | |
663 } | |
664 | |
665 L_CHECK_READ_ERROR(AVL_NULL) | |
666 depth++; | |
667 } | |
668 while (child != AVL_NULL); | |
669 | |
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; | |
676 | |
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); | |
679 } | |
680 | |
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) | |
688 | |
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; | |
694 | |
695 if (h != rm) | |
696 { | |
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)) | |
701 | |
702 if (parent_rm == AVL_NULL) | |
703 l_tree->root = h; | |
704 else | |
705 { | |
706 depth = rm_depth - 1; | |
707 | |
708 if (L_BIT_ARR_VAL(branch, depth)) | |
709 AVL_SET_GREATER(parent_rm, h) | |
710 else | |
711 AVL_SET_LESS(parent_rm, h) | |
712 } | |
713 } | |
714 | |
715 if (path != AVL_NULL) | |
716 { | |
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; | |
722 | |
723 while (h != path) | |
724 { | |
725 if (L_BIT_ARR_VAL(branch, depth)) | |
726 { | |
727 child = AVL_GET_GREATER(h, 1); | |
728 AVL_SET_GREATER(h, parent) | |
729 } | |
730 else | |
731 { | |
732 child = AVL_GET_LESS(h, 1); | |
733 AVL_SET_LESS(h, parent) | |
734 } | |
735 | |
736 L_CHECK_READ_ERROR(AVL_NULL) | |
737 depth++; | |
738 parent = h; | |
739 h = child; | |
740 } | |
741 | |
742 /* Climb from the path node to the root node using the linked | |
743 ** list, restoring the tree structure and rebalancing as necessary. | |
744 */ | |
745 reduced_depth = 1; | |
746 cmp = cmp_shortened_sub_with_path; | |
747 | |
748 for (; ;) | |
749 { | |
750 if (reduced_depth) | |
751 { | |
752 bf = AVL_GET_BALANCE_FACTOR(h); | |
753 | |
754 if (cmp < 0) | |
755 bf++; | |
756 else /* cmp > 0 */ | |
757 bf--; | |
758 | |
759 if ((bf == -2) || (bf == 2)) | |
760 { | |
761 h = L_(balance)(L_BALANCE_PARAM_CALL_PREFIX h); | |
762 L_CHECK_READ_ERROR(AVL_NULL) | |
763 bf = AVL_GET_BALANCE_FACTOR(h); | |
764 } | |
765 else | |
766 AVL_SET_BALANCE_FACTOR(h, bf) | |
767 reduced_depth = (bf == 0); | |
768 } | |
769 | |
770 if (parent == AVL_NULL) | |
771 break; | |
772 | |
773 child = h; | |
774 h = parent; | |
775 depth--; | |
776 cmp = L_BIT_ARR_VAL(branch, depth) ? 1 : -1; | |
777 | |
778 if (cmp < 0) | |
779 { | |
780 parent = AVL_GET_LESS(h, 1); | |
781 AVL_SET_LESS(h, child) | |
782 } | |
783 else | |
784 { | |
785 parent = AVL_GET_GREATER(h, 1); | |
786 AVL_SET_GREATER(h, child) | |
787 } | |
788 | |
789 L_CHECK_READ_ERROR(AVL_NULL) | |
790 } | |
791 | |
792 l_tree->root = h; | |
793 } | |
794 | |
795 return(rm); | |
796 } | 759 } |
797 | 760 |
798 #endif | 761 #endif |
799 | |
800 #if (L_IMPL_MASK & AVL_IMPL_SUBST) | |
801 | |
802 L_SC AVL_HANDLE L_(subst)(L_(avl) *l_tree, AVL_HANDLE new_node) | |
803 { | |
804 AVL_HANDLE h = l_tree->root; | |
805 AVL_HANDLE parent = AVL_NULL; | |
806 int cmp, last_cmp; | |
807 | |
808 /* Search for node already in tree with same key. */ | |
809 for (; ;) | |
810 { | |
811 if (h == AVL_NULL) | |
812 /* No node in tree with same key as new node. */ | |
813 return(AVL_NULL); | |
814 | |
815 cmp = AVL_COMPARE_NODE_NODE(new_node, h); | |
816 | |
817 if (cmp == 0) | |
818 /* Found the node to substitute new one for. */ | |
819 break; | |
820 | |
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) | |
825 } | |
826 | |
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)) | |
831 | |
832 if (parent == AVL_NULL) | |
833 /* New node is also new root. */ | |
834 l_tree->root = new_node; | |
835 else | |
836 { | |
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) | |
842 } | |
843 | |
844 return(h); | |
845 } | |
846 | |
847 #endif | |
848 | 762 |
849 #ifdef AVL_BUILD_ITER_TYPE | 763 #ifdef AVL_BUILD_ITER_TYPE |
850 | 764 |
851 #if (L_IMPL_MASK & AVL_IMPL_BUILD) | 765 #if (L_IMPL_MASK & AVL_IMPL_BUILD) |
852 | 766 |
853 L_SC int L_(build)( | 767 L_SC int L_(build)( |
854 L_(avl) *l_tree, AVL_BUILD_ITER_TYPE p, L_SIZE num_nodes) | 768 L_(avl) *l_tree, AVL_BUILD_ITER_TYPE p, L_SIZE num_nodes) { |
855 { | 769 /* Gives path to subtree being built. If bit n is false, branch |
856 /* Gives path to subtree being built. If bit n is false, branch | 770 ** less from the node at depth n, if true branch greater. */ |
857 ** less from the node at depth n, if true branch greater. */ | 771 L_BIT_ARR_DEFN(branch) |
858 L_BIT_ARR_DEFN(branch) | |
859 | 772 |
860 /* If bit n is true, then for the current subtree at depth n, its | 773 /* 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. */ | 774 ** greater subtree has one more node than its less subtree. */ |
862 L_BIT_ARR_DEFN(rem) | 775 L_BIT_ARR_DEFN(rem) |
863 | 776 |
864 /* Depth of root node of current subtree. */ | 777 /* Depth of root node of current subtree. */ |
865 unsigned depth = 0; | 778 unsigned depth = 0; |
866 | 779 |
867 /* Number of nodes in current subtree. */ | 780 /* Number of nodes in current subtree. */ |
868 L_SIZE num_sub = num_nodes; | 781 L_SIZE num_sub = num_nodes; |
869 | 782 |
870 /* The algorithm relies on a stack of nodes whose less subtree has | 783 /* The algorithm relies on a stack of nodes whose less subtree has |
871 ** been built, but whose greater subtree has not yet been built. | 784 ** been built, but whose greater subtree has not yet been built. |
872 ** The stack is implemented as linked list. The nodes are linked | 785 ** The stack is implemented as linked list. The nodes are linked |
873 ** together by having the "greater" handle of a node set to the | 786 ** 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 | 787 ** next node in the list. "less_parent" is the handle of the first |
875 ** node in the list. */ | 788 ** node in the list. */ |
876 AVL_HANDLE less_parent = AVL_NULL; | 789 AVL_HANDLE less_parent = AVL_NULL; |
877 | 790 |
878 /* h is root of current subtree, child is one of its children. */ | 791 /* h is root of current subtree, child is one of its children. */ |
879 AVL_HANDLE h; | 792 AVL_HANDLE h; |
880 AVL_HANDLE child; | 793 AVL_HANDLE child; |
881 | 794 |
882 if (num_nodes == 0) | 795 if (num_nodes == 0) { |
883 { | 796 l_tree->root = AVL_NULL; |
884 l_tree->root = AVL_NULL; | 797 return(1); |
885 return(1); | 798 } |
| 799 |
| 800 for (;;) { |
| 801 while (num_sub > 2) { |
| 802 /* Subtract one for root of subtree. */ |
| 803 num_sub--; |
| 804 |
| 805 if (num_sub & 1) |
| 806 L_BIT_ARR_1(rem, depth) |
| 807 else |
| 808 L_BIT_ARR_0(rem, depth) |
| 809 L_BIT_ARR_0(branch, depth) |
| 810 depth++; |
| 811 |
| 812 num_sub >>= 1; |
886 } | 813 } |
887 | 814 |
888 for (; ;) | 815 if (num_sub == 2) { |
889 { | 816 /* Build a subtree with two nodes, slanting to greater. |
890 while (num_sub > 2) | 817 ** I arbitrarily chose to always have the extra node in the |
891 { | 818 ** greater subtree when there is an odd number of nodes to |
892 /* Subtract one for root of subtree. */ | 819 ** split between the two subtrees. */ |
893 num_sub--; | |
894 | 820 |
895 if (num_sub & 1) | 821 h = AVL_BUILD_ITER_VAL(p); |
896 L_BIT_ARR_1(rem, depth) | 822 L_CHECK_READ_ERROR(0) |
897 else | 823 AVL_BUILD_ITER_INCR(p) |
898 L_BIT_ARR_0(rem, depth) | 824 child = AVL_BUILD_ITER_VAL(p); |
899 L_BIT_ARR_0(branch, depth) | 825 L_CHECK_READ_ERROR(0) |
900 depth++; | 826 AVL_BUILD_ITER_INCR(p) |
| 827 AVL_SET_LESS(child, AVL_NULL) |
| 828 AVL_SET_GREATER(child, AVL_NULL) |
| 829 AVL_SET_BALANCE_FACTOR(child, 0) |
| 830 AVL_SET_GREATER(h, child) |
| 831 AVL_SET_LESS(h, AVL_NULL) |
| 832 AVL_SET_BALANCE_FACTOR(h, 1) |
| 833 } else { /* num_sub == 1 */ |
| 834 /* Build a subtree with one node. */ |
901 | 835 |
902 num_sub >>= 1; | 836 h = AVL_BUILD_ITER_VAL(p); |
| 837 L_CHECK_READ_ERROR(0) |
| 838 AVL_BUILD_ITER_INCR(p) |
| 839 AVL_SET_LESS(h, AVL_NULL) |
| 840 AVL_SET_GREATER(h, AVL_NULL) |
| 841 AVL_SET_BALANCE_FACTOR(h, 0) |
| 842 } |
| 843 |
| 844 while (depth) { |
| 845 depth--; |
| 846 |
| 847 if (!L_BIT_ARR_VAL(branch, depth)) |
| 848 /* We've completed a less subtree. */ |
| 849 break; |
| 850 |
| 851 /* We've completed a greater subtree, so attach it to |
| 852 ** its parent (that is less than it). We pop the parent |
| 853 ** off the stack of less parents. */ |
| 854 child = h; |
| 855 h = less_parent; |
| 856 less_parent = AVL_GET_GREATER(h, 1); |
| 857 L_CHECK_READ_ERROR(0) |
| 858 AVL_SET_GREATER(h, child) |
| 859 /* num_sub = 2 * (num_sub - rem[depth]) + rem[depth] + 1 */ |
| 860 num_sub <<= 1; |
| 861 num_sub += L_BIT_ARR_VAL(rem, depth) ? 0 : 1; |
| 862 |
| 863 if (num_sub & (num_sub - 1)) |
| 864 /* num_sub is not a power of 2. */ |
| 865 AVL_SET_BALANCE_FACTOR(h, 0) |
| 866 else |
| 867 /* num_sub is a power of 2. */ |
| 868 AVL_SET_BALANCE_FACTOR(h, 1) |
903 } | 869 } |
904 | 870 |
905 if (num_sub == 2) | 871 if (num_sub == num_nodes) |
906 { | 872 /* We've completed the full tree. */ |
907 /* Build a subtree with two nodes, slanting to greater. | 873 break; |
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. */ | |
911 | 874 |
912 h = AVL_BUILD_ITER_VAL(p); | 875 /* The subtree we've completed is the less subtree of the |
913 L_CHECK_READ_ERROR(0) | 876 ** next node in the sequence. */ |
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) | |
924 } | |
925 else /* num_sub == 1 */ | |
926 { | |
927 /* Build a subtree with one node. */ | |
928 | 877 |
929 h = AVL_BUILD_ITER_VAL(p); | 878 child = h; |
930 L_CHECK_READ_ERROR(0) | 879 h = AVL_BUILD_ITER_VAL(p); |
931 AVL_BUILD_ITER_INCR(p) | 880 L_CHECK_READ_ERROR(0) |
932 AVL_SET_LESS(h, AVL_NULL) | 881 AVL_BUILD_ITER_INCR(p) |
933 AVL_SET_GREATER(h, AVL_NULL) | 882 AVL_SET_LESS(h, child) |
934 AVL_SET_BALANCE_FACTOR(h, 0) | |
935 } | |
936 | 883 |
937 while (depth) | 884 /* Put h into stack of less parents. */ |
938 { | 885 AVL_SET_GREATER(h, less_parent) |
939 depth--; | 886 less_parent = h; |
940 | 887 |
941 if (!L_BIT_ARR_VAL(branch, depth)) | 888 /* Proceed to creating greater than subtree of h. */ |
942 /* We've completed a less subtree. */ | 889 L_BIT_ARR_1(branch, depth) |
943 break; | 890 num_sub += L_BIT_ARR_VAL(rem, depth) ? 1 : 0; |
| 891 depth++; |
944 | 892 |
945 /* We've completed a greater subtree, so attach it to | 893 } /* end for (;; ) */ |
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; | |
956 | 894 |
957 if (num_sub & (num_sub - 1)) | 895 l_tree->root = h; |
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) | |
963 } | |
964 | 896 |
965 if (num_sub == num_nodes) | 897 return(1); |
966 /* We've completed the full tree. */ | |
967 break; | |
968 | |
969 /* The subtree we've completed is the less subtree of the | |
970 ** next node in the sequence. */ | |
971 | |
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) | |
977 | |
978 /* Put h into stack of less parents. */ | |
979 AVL_SET_GREATER(h, less_parent) | |
980 less_parent = h; | |
981 | |
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++; | |
986 | |
987 } /* end for ( ; ; ) */ | |
988 | |
989 l_tree->root = h; | |
990 | |
991 return(1); | |
992 } | 898 } |
993 | 899 |
994 #endif | 900 #endif |
995 | 901 |
996 #endif | 902 #endif |
997 | 903 |
998 #if (L_IMPL_MASK & AVL_IMPL_INIT_ITER) | 904 #if (L_IMPL_MASK & AVL_IMPL_INIT_ITER) |
999 | 905 |
1000 /* Initialize depth to invalid value, to indicate iterator is | 906 /* Initialize depth to invalid value, to indicate iterator is |
1001 ** invalid. (Depth is zero-base.) It's not necessary to initialize | 907 ** invalid. (Depth is zero-base.) It's not necessary to initialize |
1002 ** iterators prior to passing them to the "start" function. | 908 ** iterators prior to passing them to the "start" function. |
1003 */ | 909 */ |
1004 L_SC void L_(init_iter)(L_(iter) *iter) | 910 L_SC void L_(init_iter)(L_(iter) *iter) { |
1005 { | 911 iter->depth = ~0; |
1006 iter->depth = ~0; | |
1007 } | 912 } |
1008 | 913 |
1009 #endif | 914 #endif |
1010 | 915 |
1011 #ifdef AVL_READ_ERRORS_HAPPEN | 916 #ifdef AVL_READ_ERRORS_HAPPEN |
1012 | 917 |
1013 #define L_CHECK_READ_ERROR_INV_DEPTH \ | 918 #define L_CHECK_READ_ERROR_INV_DEPTH \ |
1014 { if (AVL_READ_ERROR) { iter->depth = ~0; return; } } | 919 { if (AVL_READ_ERROR) { iter->depth = ~0; return; } } |
1015 | 920 |
1016 #else | 921 #else |
1017 | 922 |
1018 #define L_CHECK_READ_ERROR_INV_DEPTH | 923 #define L_CHECK_READ_ERROR_INV_DEPTH |
1019 | 924 |
1020 #endif | 925 #endif |
1021 | 926 |
1022 #if (L_IMPL_MASK & AVL_IMPL_START_ITER) | 927 #if (L_IMPL_MASK & AVL_IMPL_START_ITER) |
1023 | 928 |
1024 L_SC void L_(start_iter)( | 929 L_SC void L_(start_iter)( |
1025 L_(avl) *l_tree, L_(iter) *iter, AVL_KEY k, avl_search_type st) | 930 L_(avl) *l_tree, L_(iter) *iter, AVL_KEY k, avl_search_type st) { |
1026 { | 931 AVL_HANDLE h = l_tree->root; |
1027 AVL_HANDLE h = l_tree->root; | 932 unsigned d = 0; |
1028 unsigned d = 0; | 933 int cmp, target_cmp; |
1029 int cmp, target_cmp; | |
1030 | 934 |
1031 /* Save the tree that we're going to iterate through in a | 935 /* Save the tree that we're going to iterate through in a |
1032 ** member variable. */ | 936 ** member variable. */ |
1033 iter->tree_ = l_tree; | 937 iter->tree_ = l_tree; |
1034 | 938 |
1035 iter->depth = ~0; | 939 iter->depth = ~0; |
| 940 |
| 941 if (h == AVL_NULL) |
| 942 /* Tree is empty. */ |
| 943 return; |
| 944 |
| 945 if (st & AVL_LESS) |
| 946 /* Key can be greater than key of starting node. */ |
| 947 target_cmp = 1; |
| 948 else if (st & AVL_GREATER) |
| 949 /* Key can be less than key of starting node. */ |
| 950 target_cmp = -1; |
| 951 else |
| 952 /* Key must be same as key of starting node. */ |
| 953 target_cmp = 0; |
| 954 |
| 955 for (;;) { |
| 956 cmp = AVL_COMPARE_KEY_NODE(k, h); |
| 957 |
| 958 if (cmp == 0) { |
| 959 if (st & AVL_EQUAL) { |
| 960 /* Equal node was sought and found as starting node. */ |
| 961 iter->depth = d; |
| 962 break; |
| 963 } |
| 964 |
| 965 cmp = -target_cmp; |
| 966 } else if (target_cmp != 0) |
| 967 if (!((cmp ^ target_cmp) & L_MASK_HIGH_BIT)) |
| 968 /* cmp and target_cmp are both negative or both positive. */ |
| 969 iter->depth = d; |
| 970 |
| 971 h = cmp < 0 ? AVL_GET_LESS(h, 1) : AVL_GET_GREATER(h, 1); |
| 972 L_CHECK_READ_ERROR_INV_DEPTH |
1036 | 973 |
1037 if (h == AVL_NULL) | 974 if (h == AVL_NULL) |
1038 /* Tree is empty. */ | 975 break; |
1039 return; | |
1040 | 976 |
1041 if (st & AVL_LESS) | 977 if (cmp > 0) |
1042 /* Key can be greater than key of starting node. */ | 978 L_BIT_ARR_1(iter->branch, d) |
1043 target_cmp = 1; | 979 else |
1044 else if (st & AVL_GREATER) | 980 L_BIT_ARR_0(iter->branch, d) |
1045 /* Key can be less than key of starting node. */ | 981 iter->path_h[d++] = h; |
1046 target_cmp = -1; | 982 } |
1047 else | |
1048 /* Key must be same as key of starting node. */ | |
1049 target_cmp = 0; | |
1050 | |
1051 for (; ;) | |
1052 { | |
1053 cmp = AVL_COMPARE_KEY_NODE(k, h); | |
1054 | |
1055 if (cmp == 0) | |
1056 { | |
1057 if (st & AVL_EQUAL) | |
1058 { | |
1059 /* Equal node was sought and found as starting node. */ | |
1060 iter->depth = d; | |
1061 break; | |
1062 } | |
1063 | |
1064 cmp = -target_cmp; | |
1065 } | |
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; | |
1070 | |
1071 h = cmp < 0 ? AVL_GET_LESS(h, 1) : AVL_GET_GREATER(h, 1); | |
1072 L_CHECK_READ_ERROR_INV_DEPTH | |
1073 | |
1074 if (h == AVL_NULL) | |
1075 break; | |
1076 | |
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; | |
1082 } | |
1083 } | 983 } |
1084 | 984 |
1085 #endif | 985 #endif |
1086 | 986 |
1087 #if (L_IMPL_MASK & AVL_IMPL_START_ITER_LEAST) | 987 #if (L_IMPL_MASK & AVL_IMPL_START_ITER_LEAST) |
1088 | 988 |
1089 L_SC void L_(start_iter_least)(L_(avl) *l_tree, L_(iter) *iter) | 989 L_SC void L_(start_iter_least)(L_(avl) *l_tree, L_(iter) *iter) { |
1090 { | 990 AVL_HANDLE h = l_tree->root; |
1091 AVL_HANDLE h = l_tree->root; | |
1092 | 991 |
1093 iter->tree_ = l_tree; | 992 iter->tree_ = l_tree; |
1094 | 993 |
1095 iter->depth = ~0; | 994 iter->depth = ~0; |
1096 | 995 |
1097 L_BIT_ARR_ALL(iter->branch, 0) | 996 L_BIT_ARR_ALL(iter->branch, 0) |
1098 | 997 |
1099 while (h != AVL_NULL) | 998 while (h != AVL_NULL) { |
1100 { | 999 if (iter->depth != ~0) |
1101 if (iter->depth != ~0) | 1000 iter->path_h[iter->depth] = h; |
1102 iter->path_h[iter->depth] = h; | |
1103 | 1001 |
1104 iter->depth++; | 1002 iter->depth++; |
1105 h = AVL_GET_LESS(h, 1); | 1003 h = AVL_GET_LESS(h, 1); |
1106 L_CHECK_READ_ERROR_INV_DEPTH | 1004 L_CHECK_READ_ERROR_INV_DEPTH |
1107 } | 1005 } |
1108 } | 1006 } |
1109 | 1007 |
1110 #endif | 1008 #endif |
1111 | 1009 |
1112 #if (L_IMPL_MASK & AVL_IMPL_START_ITER_GREATEST) | 1010 #if (L_IMPL_MASK & AVL_IMPL_START_ITER_GREATEST) |
1113 | 1011 |
1114 L_SC void L_(start_iter_greatest)(L_(avl) *l_tree, L_(iter) *iter) | 1012 L_SC void L_(start_iter_greatest)(L_(avl) *l_tree, L_(iter) *iter) { |
1115 { | 1013 AVL_HANDLE h = l_tree->root; |
1116 AVL_HANDLE h = l_tree->root; | |
1117 | 1014 |
1118 iter->tree_ = l_tree; | 1015 iter->tree_ = l_tree; |
1119 | 1016 |
1120 iter->depth = ~0; | 1017 iter->depth = ~0; |
1121 | 1018 |
1122 L_BIT_ARR_ALL(iter->branch, 1) | 1019 L_BIT_ARR_ALL(iter->branch, 1) |
1123 | 1020 |
1124 while (h != AVL_NULL) | 1021 while (h != AVL_NULL) { |
1125 { | 1022 if (iter->depth != ~0) |
1126 if (iter->depth != ~0) | 1023 iter->path_h[iter->depth] = h; |
1127 iter->path_h[iter->depth] = h; | |
1128 | 1024 |
1129 iter->depth++; | 1025 iter->depth++; |
1130 h = AVL_GET_GREATER(h, 1); | 1026 h = AVL_GET_GREATER(h, 1); |
1131 L_CHECK_READ_ERROR_INV_DEPTH | 1027 L_CHECK_READ_ERROR_INV_DEPTH |
1132 } | 1028 } |
1133 } | 1029 } |
1134 | 1030 |
1135 #endif | 1031 #endif |
1136 | 1032 |
1137 #if (L_IMPL_MASK & AVL_IMPL_GET_ITER) | 1033 #if (L_IMPL_MASK & AVL_IMPL_GET_ITER) |
1138 | 1034 |
1139 L_SC AVL_HANDLE L_(get_iter)(L_(iter) *iter) | 1035 L_SC AVL_HANDLE L_(get_iter)(L_(iter) *iter) { |
1140 { | 1036 if (iter->depth == ~0) |
1141 if (iter->depth == ~0) | 1037 return(AVL_NULL); |
1142 return(AVL_NULL); | |
1143 | 1038 |
1144 return(iter->depth == 0 ? | 1039 return(iter->depth == 0 ? |
1145 iter->tree_->root : iter->path_h[iter->depth - 1]); | 1040 iter->tree_->root : iter->path_h[iter->depth - 1]); |
1146 } | 1041 } |
1147 | 1042 |
1148 #endif | 1043 #endif |
1149 | 1044 |
1150 #if (L_IMPL_MASK & AVL_IMPL_INCR_ITER) | 1045 #if (L_IMPL_MASK & AVL_IMPL_INCR_ITER) |
1151 | 1046 |
1152 L_SC void L_(incr_iter)(L_(iter) *iter) | 1047 L_SC void L_(incr_iter)(L_(iter) *iter) { |
1153 { | |
1154 #define l_tree (iter->tree_) | 1048 #define l_tree (iter->tree_) |
1155 | 1049 |
1156 if (iter->depth != ~0) | 1050 if (iter->depth != ~0) { |
1157 { | 1051 AVL_HANDLE h = |
1158 AVL_HANDLE h = | 1052 AVL_GET_GREATER((iter->depth == 0 ? |
1159 AVL_GET_GREATER((iter->depth == 0 ? | 1053 iter->tree_->root : iter->path_h[iter->depth - 1]), 1); |
1160 iter->tree_->root : iter->path_h[iter->depth - 1]),
1); | 1054 L_CHECK_READ_ERROR_INV_DEPTH |
| 1055 |
| 1056 if (h == AVL_NULL) |
| 1057 do { |
| 1058 if (iter->depth == 0) { |
| 1059 iter->depth = ~0; |
| 1060 break; |
| 1061 } |
| 1062 |
| 1063 iter->depth--; |
| 1064 } while (L_BIT_ARR_VAL(iter->branch, iter->depth)); |
| 1065 else { |
| 1066 L_BIT_ARR_1(iter->branch, iter->depth) |
| 1067 iter->path_h[iter->depth++] = h; |
| 1068 |
| 1069 for (;;) { |
| 1070 h = AVL_GET_LESS(h, 1); |
1161 L_CHECK_READ_ERROR_INV_DEPTH | 1071 L_CHECK_READ_ERROR_INV_DEPTH |
1162 | 1072 |
1163 if (h == AVL_NULL) | 1073 if (h == AVL_NULL) |
1164 do | 1074 break; |
1165 { | |
1166 if (iter->depth == 0) | |
1167 { | |
1168 iter->depth = ~0; | |
1169 break; | |
1170 } | |
1171 | 1075 |
1172 iter->depth--; | 1076 L_BIT_ARR_0(iter->branch, iter->depth) |
1173 } | 1077 iter->path_h[iter->depth++] = h; |
1174 while (L_BIT_ARR_VAL(iter->branch, iter->depth)); | 1078 } |
1175 else | |
1176 { | |
1177 L_BIT_ARR_1(iter->branch, iter->depth) | |
1178 iter->path_h[iter->depth++] = h; | |
1179 | |
1180 for (; ;) | |
1181 { | |
1182 h = AVL_GET_LESS(h, 1); | |
1183 L_CHECK_READ_ERROR_INV_DEPTH | |
1184 | |
1185 if (h == AVL_NULL) | |
1186 break; | |
1187 | |
1188 L_BIT_ARR_0(iter->branch, iter->depth) | |
1189 iter->path_h[iter->depth++] = h; | |
1190 } | |
1191 } | |
1192 } | 1079 } |
| 1080 } |
1193 | 1081 |
1194 #undef l_tree | 1082 #undef l_tree |
1195 } | 1083 } |
1196 | 1084 |
1197 #endif | 1085 #endif |
1198 | 1086 |
1199 #if (L_IMPL_MASK & AVL_IMPL_DECR_ITER) | 1087 #if (L_IMPL_MASK & AVL_IMPL_DECR_ITER) |
1200 | 1088 |
1201 L_SC void L_(decr_iter)(L_(iter) *iter) | 1089 L_SC void L_(decr_iter)(L_(iter) *iter) { |
1202 { | |
1203 #define l_tree (iter->tree_) | 1090 #define l_tree (iter->tree_) |
1204 | 1091 |
1205 if (iter->depth != ~0) | 1092 if (iter->depth != ~0) { |
1206 { | 1093 AVL_HANDLE h = |
1207 AVL_HANDLE h = | 1094 AVL_GET_LESS((iter->depth == 0 ? |
1208 AVL_GET_LESS((iter->depth == 0 ? | 1095 iter->tree_->root : iter->path_h[iter->depth - 1]), 1); |
1209 iter->tree_->root : iter->path_h[iter->depth - 1]), 1)
; | 1096 L_CHECK_READ_ERROR_INV_DEPTH |
| 1097 |
| 1098 if (h == AVL_NULL) |
| 1099 do { |
| 1100 if (iter->depth == 0) { |
| 1101 iter->depth = ~0; |
| 1102 break; |
| 1103 } |
| 1104 |
| 1105 iter->depth--; |
| 1106 } while (!L_BIT_ARR_VAL(iter->branch, iter->depth)); |
| 1107 else { |
| 1108 L_BIT_ARR_0(iter->branch, iter->depth) |
| 1109 iter->path_h[iter->depth++] = h; |
| 1110 |
| 1111 for (;;) { |
| 1112 h = AVL_GET_GREATER(h, 1); |
1210 L_CHECK_READ_ERROR_INV_DEPTH | 1113 L_CHECK_READ_ERROR_INV_DEPTH |
1211 | 1114 |
1212 if (h == AVL_NULL) | 1115 if (h == AVL_NULL) |
1213 do | 1116 break; |
1214 { | |
1215 if (iter->depth == 0) | |
1216 { | |
1217 iter->depth = ~0; | |
1218 break; | |
1219 } | |
1220 | 1117 |
1221 iter->depth--; | 1118 L_BIT_ARR_1(iter->branch, iter->depth) |
1222 } | 1119 iter->path_h[iter->depth++] = h; |
1223 while (!L_BIT_ARR_VAL(iter->branch, iter->depth)); | 1120 } |
1224 else | |
1225 { | |
1226 L_BIT_ARR_0(iter->branch, iter->depth) | |
1227 iter->path_h[iter->depth++] = h; | |
1228 | |
1229 for (; ;) | |
1230 { | |
1231 h = AVL_GET_GREATER(h, 1); | |
1232 L_CHECK_READ_ERROR_INV_DEPTH | |
1233 | |
1234 if (h == AVL_NULL) | |
1235 break; | |
1236 | |
1237 L_BIT_ARR_1(iter->branch, iter->depth) | |
1238 iter->path_h[iter->depth++] = h; | |
1239 } | |
1240 } | |
1241 } | 1121 } |
| 1122 } |
1242 | 1123 |
1243 #undef l_tree | 1124 #undef l_tree |
1244 } | 1125 } |
1245 | 1126 |
1246 #endif | 1127 #endif |
1247 | 1128 |
1248 /* Tidy up the preprocessor symbol name space. */ | 1129 /* Tidy up the preprocessor symbol name space. */ |
1249 #undef L_ | 1130 #undef L_ |
1250 #undef L_EST_LONG_BIT | 1131 #undef L_EST_LONG_BIT |
1251 #undef L_SIZE | 1132 #undef L_SIZE |
1252 #undef L_MASK_HIGH_BIT | 1133 #undef L_MASK_HIGH_BIT |
1253 #undef L_LONG_BIT | 1134 #undef L_LONG_BIT |
1254 #undef L_BIT_ARR_DEFN | 1135 #undef L_BIT_ARR_DEFN |
1255 #undef L_BIT_ARR_VAL | 1136 #undef L_BIT_ARR_VAL |
1256 #undef L_BIT_ARR_0 | 1137 #undef L_BIT_ARR_0 |
1257 #undef L_BIT_ARR_1 | 1138 #undef L_BIT_ARR_1 |
1258 #undef L_BIT_ARR_ALL | 1139 #undef L_BIT_ARR_ALL |
1259 #undef L_CHECK_READ_ERROR | 1140 #undef L_CHECK_READ_ERROR |
1260 #undef L_CHECK_READ_ERROR_INV_DEPTH | 1141 #undef L_CHECK_READ_ERROR_INV_DEPTH |
1261 #undef L_BIT_ARR_LONGS | 1142 #undef L_BIT_ARR_LONGS |
1262 #undef L_IMPL_MASK | 1143 #undef L_IMPL_MASK |
1263 #undef L_CHECK_READ_ERROR | 1144 #undef L_CHECK_READ_ERROR |
1264 #undef L_CHECK_READ_ERROR_INV_DEPTH | 1145 #undef L_CHECK_READ_ERROR_INV_DEPTH |
1265 #undef L_SC | 1146 #undef L_SC |
1266 #undef L_BALANCE_PARAM_CALL_PREFIX | 1147 #undef L_BALANCE_PARAM_CALL_PREFIX |
1267 #undef L_BALANCE_PARAM_DECL_PREFIX | 1148 #undef L_BALANCE_PARAM_DECL_PREFIX |
OLD | NEW |