libacfutils
A general purpose library of utility functions designed to make it easier to develop addons for the X-Plane flight simulator.
Loading...
Searching...
No Matches
avl.c
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25
26/*
27 * AVL - generic AVL tree implementation for kernel use
28 *
29 * A complete description of AVL trees can be found in many CS textbooks.
30 *
31 * Here is a very brief overview. An AVL tree is a binary search tree that is
32 * almost perfectly balanced. By "almost" perfectly balanced, we mean that at
33 * any given node, the left and right subtrees are allowed to differ in height
34 * by at most 1 level.
35 *
36 * This relaxation from a perfectly balanced binary tree allows doing
37 * insertion and deletion relatively efficiently. Searching the tree is
38 * still a fast operation, roughly O(log(N)).
39 *
40 * The key to insertion and deletion is a set of tree maniuplations called
41 * rotations, which bring unbalanced subtrees back into the semi-balanced state.
42 *
43 * This implementation of AVL trees has the following peculiarities:
44 *
45 * - The AVL specific data structures are physically embedded as fields
46 * in the "using" data structures. To maintain generality the code
47 * must constantly translate between "avl_node_t *" and containing
48 * data structure "void *"s by adding/subracting the avl_offset.
49 *
50 * - Since the AVL data is always embedded in other structures, there is
51 * no locking or memory allocation in the AVL routines. This must be
52 * provided for by the enclosing data structure's semantics. Typically,
53 * avl_insert()/_add()/_remove()/avl_insert_here() require some kind of
54 * exclusive write lock. Other operations require a read lock.
55 *
56 * - The implementation uses iteration instead of explicit recursion,
57 * since it is intended to run on limited size kernel stacks. Since
58 * there is no recursion stack present to move "up" in the tree,
59 * there is an explicit "parent" link in the avl_node_t.
60 *
61 * - The left/right children pointers of a node are in an array.
62 * In the code, variables (instead of constants) are used to represent
63 * left and right indices. The implementation is written as if it only
64 * dealt with left handed manipulations. By changing the value assigned
65 * to "left", the code also works for right handed trees. The
66 * following variables/terms are frequently used:
67 *
68 * int left; // 0 when dealing with left children,
69 * // 1 for dealing with right children
70 *
71 * int left_heavy; // -1 when left subtree is taller at some node,
72 * // +1 when right subtree is taller
73 *
74 * int right; // will be the opposite of left (0 or 1)
75 * int right_heavy;// will be the opposite of left_heavy (-1 or 1)
76 *
77 * int direction; // 0 for "<" (ie. left child); 1 for ">" (right)
78 *
79 * Though it is a little more confusing to read the code, the approach
80 * allows using half as much code (and hence cache footprint) for tree
81 * manipulations and eliminates many conditional branches.
82 *
83 * - The avl_index_t is an opaque "cookie" used to find nodes at or
84 * adjacent to where a new value would be inserted in the tree. The value
85 * is a modified "avl_node_t *". The bottom bit (normally 0 for a
86 * pointer) is set to indicate if that the new node has a value greater
87 * than the value of the indicated "avl_node_t *".
88 */
89
90#include <string.h>
91#include <stdlib.h>
92
93#include <acfutils/assert.h>
94#include <acfutils/avl.h>
95#include <acfutils/helpers.h>
96
97/*
98 * Small arrays to translate between balance (or diff) values and child indeces.
99 *
100 * Code that deals with binary tree data structures will randomly use
101 * left and right children when examining a tree. C "if()" statements
102 * which evaluate randomly suffer from very poor hardware branch prediction.
103 * In this code we avoid some of the branch mispredictions by using the
104 * following translation arrays. They replace random branches with an
105 * additional memory reference. Since the translation arrays are both very
106 * small the data should remain efficiently in cache.
107 */
108static const int avl_child2balance[2] = {-1, 1};
109static const int avl_balance2child[] = {0, 0, 1};
110
111
112/*
113 * Walk from one node to the previous valued node (ie. an infix walk
114 * towards the left). At any given node we do one of 2 things:
115 *
116 * - If there is a left child, go to it, then to it's rightmost descendant.
117 *
118 * - otherwise we return thru parent nodes until we've come from a right child.
119 *
120 * Return Value:
121 * NULL - if at the end of the nodes
122 * otherwise next node
123 */
124void *
125avl_walk(const avl_tree_t *tree, const void *oldnode, int left)
126{
127 size_t off = tree->avl_offset;
128 const avl_node_t *node = AVL_DATA2NODE(oldnode, off);
129 int right = 1 - left;
130 int was_child;
131
132
133 /*
134 * nowhere to walk to if tree is empty
135 */
136 if (node == NULL)
137 return (NULL);
138
139 /*
140 * Visit the previous valued node. There are two possibilities:
141 *
142 * If this node has a left child, go down one left, then all
143 * the way right.
144 */
145 if (node->avl_child[left] != NULL) {
146 for (node = node->avl_child[left];
147 node->avl_child[right] != NULL;
148 node = node->avl_child[right])
149 ;
150 /*
151 * Otherwise, return thru left children as far as we can.
152 */
153 } else {
154 for (;;) {
155 was_child = AVL_XCHILD(node);
156 node = AVL_XPARENT(node);
157 if (node == NULL)
158 return (NULL);
159 if (was_child == right)
160 break;
161 }
162 }
163
164 return (AVL_NODE2DATA(node, off));
165}
166
167/*
168 * Return the lowest valued node in a tree or NULL.
169 * (leftmost child from root of tree)
170 */
171void *
172avl_first(const avl_tree_t *tree)
173{
174 const avl_node_t *node;
175 const avl_node_t *prev = NULL;
176 size_t off = tree->avl_offset;
177
178 for (node = tree->avl_root; node != NULL; node = node->avl_child[0])
179 prev = node;
180
181 if (prev != NULL)
182 return (AVL_NODE2DATA(prev, off));
183 return (NULL);
184}
185
186/*
187 * Return the highest valued node in a tree or NULL.
188 * (rightmost child from root of tree)
189 */
190void *
191avl_last(const avl_tree_t *tree)
192{
193 const avl_node_t *node;
194 const avl_node_t *prev = NULL;
195 size_t off = tree->avl_offset;
196
197 for (node = tree->avl_root; node != NULL; node = node->avl_child[1])
198 prev = node;
199
200 if (prev != NULL)
201 return (AVL_NODE2DATA(prev, off));
202 return (NULL);
203}
204
205/*
206 * Access the node immediately before or after an insertion point.
207 *
208 * "avl_index_t" is a (avl_node_t *) with the bottom bit indicating a child
209 *
210 * Return value:
211 * NULL: no node in the given direction
212 * "void *" of the found tree node
213 */
214void *
215avl_nearest(const avl_tree_t *tree, avl_index_t where, int direction)
216{
217 int child = AVL_INDEX2CHILD(where);
218 avl_node_t *node = AVL_INDEX2NODE(where);
219 void *data;
220 size_t off = tree->avl_offset;
221
222 if (node == NULL) {
223 ASSERT(tree->avl_root == NULL);
224 return (NULL);
225 }
226 data = AVL_NODE2DATA(node, off);
227 if (child != direction)
228 return (data);
229
230 return (avl_walk(tree, data, direction));
231}
232
233
234/*
235 * Search for the node which contains "value". The algorithm is a
236 * simple binary tree search.
237 *
238 * return value:
239 * NULL: the value is not in the AVL tree
240 * *where (if not NULL) is set to indicate the insertion point
241 * "void *" of the found tree node
242 */
243void *
244avl_find(const avl_tree_t *tree, const void *value, avl_index_t *where)
245{
246 avl_node_t *node;
247 avl_node_t *prev = NULL;
248 int child = 0;
249 int diff;
250 size_t off = tree->avl_offset;
251
252 for (node = tree->avl_root; node != NULL;
253 node = node->avl_child[child]) {
254
255 prev = node;
256
257 diff = tree->avl_compar(value, AVL_NODE2DATA(node, off));
258 ASSERT(-1 <= diff && diff <= 1);
259 if (diff == 0) {
260#ifdef DEBUG
261 if (where != NULL)
262 *where = 0;
263#endif
264 return (AVL_NODE2DATA(node, off));
265 }
266 child = avl_balance2child[1 + diff];
267
268 }
269
270 if (where != NULL)
271 *where = AVL_MKINDEX(prev, child);
272
273 return (NULL);
274}
275
276
277/*
278 * Perform a rotation to restore balance at the subtree given by depth.
279 *
280 * This routine is used by both insertion and deletion. The return value
281 * indicates:
282 * 0 : subtree did not change height
283 * !0 : subtree was reduced in height
284 *
285 * The code is written as if handling left rotations, right rotations are
286 * symmetric and handled by swapping values of variables right/left[_heavy]
287 *
288 * On input balance is the "new" balance at "node". This value is either
289 * -2 or +2.
290 */
291static int
292avl_rotation(avl_tree_t *tree, avl_node_t *node, int balance)
293{
294 int left = !(balance < 0); /* when balance = -2, left will be 0 */
295 int right = 1 - left;
296 int left_heavy = balance >> 1;
297 int right_heavy = -left_heavy;
298 avl_node_t *parent = AVL_XPARENT(node);
299 avl_node_t *child = node->avl_child[left];
300 avl_node_t *cright;
301 avl_node_t *gchild;
302 avl_node_t *gright;
303 avl_node_t *gleft;
304 int which_child = AVL_XCHILD(node);
305 int child_bal = AVL_XBALANCE(child);
306
307 /* BEGIN CSTYLED */
308 /*
309 * case 1 : node is overly left heavy, the left child is balanced or
310 * also left heavy. This requires the following rotation.
311 *
312 * (node bal:-2)
313 * / \
314 * / \
315 * (child bal:0 or -1)
316 * / \
317 * / \
318 * cright
319 *
320 * becomes:
321 *
322 * (child bal:1 or 0)
323 * / \
324 * / \
325 * (node bal:-1 or 0)
326 * / \
327 * / \
328 * cright
329 *
330 * we detect this situation by noting that child's balance is not
331 * right_heavy.
332 */
333 /* END CSTYLED */
334 if (child_bal != right_heavy) {
335
336 /*
337 * compute new balance of nodes
338 *
339 * If child used to be left heavy (now balanced) we reduced
340 * the height of this sub-tree -- used in "return...;" below
341 */
342 child_bal += right_heavy; /* adjust towards right */
343
344 /*
345 * move "cright" to be node's left child
346 */
347 cright = child->avl_child[right];
348 node->avl_child[left] = cright;
349 if (cright != NULL) {
350 AVL_SETPARENT(cright, node);
351 AVL_SETCHILD(cright, left);
352 }
353
354 /*
355 * move node to be child's right child
356 */
357 child->avl_child[right] = node;
358 AVL_SETBALANCE(node, -child_bal);
359 AVL_SETCHILD(node, right);
360 AVL_SETPARENT(node, child);
361
362 /*
363 * update the pointer into this subtree
364 */
365 AVL_SETBALANCE(child, child_bal);
366 AVL_SETCHILD(child, which_child);
367 AVL_SETPARENT(child, parent);
368 if (parent != NULL)
369 parent->avl_child[which_child] = child;
370 else
371 tree->avl_root = child;
372
373 return (child_bal == 0);
374 }
375
376 /* BEGIN CSTYLED */
377 /*
378 * case 2 : When node is left heavy, but child is right heavy we use
379 * a different rotation.
380 *
381 * (node b:-2)
382 * / \
383 * / \
384 * / \
385 * (child b:+1)
386 * / \
387 * / \
388 * (gchild b: != 0)
389 * / \
390 * / \
391 * gleft gright
392 *
393 * becomes:
394 *
395 * (gchild b:0)
396 * / \
397 * / \
398 * / \
399 * (child b:?) (node b:?)
400 * / \ / \
401 * / \ / \
402 * gleft gright
403 *
404 * computing the new balances is more complicated. As an example:
405 * if gchild was right_heavy, then child is now left heavy
406 * else it is balanced
407 */
408 /* END CSTYLED */
409 gchild = child->avl_child[right];
410 gleft = gchild->avl_child[left];
411 gright = gchild->avl_child[right];
412
413 /*
414 * move gright to left child of node and
415 *
416 * move gleft to right child of node
417 */
418 node->avl_child[left] = gright;
419 if (gright != NULL) {
420 AVL_SETPARENT(gright, node);
421 AVL_SETCHILD(gright, left);
422 }
423
424 child->avl_child[right] = gleft;
425 if (gleft != NULL) {
426 AVL_SETPARENT(gleft, child);
427 AVL_SETCHILD(gleft, right);
428 }
429
430 /*
431 * move child to left child of gchild and
432 *
433 * move node to right child of gchild and
434 *
435 * fixup parent of all this to point to gchild
436 */
437 balance = AVL_XBALANCE(gchild);
438 gchild->avl_child[left] = child;
439 AVL_SETBALANCE(child, (balance == right_heavy ? left_heavy : 0));
440 AVL_SETPARENT(child, gchild);
441 AVL_SETCHILD(child, left);
442
443 gchild->avl_child[right] = node;
444 AVL_SETBALANCE(node, (balance == left_heavy ? right_heavy : 0));
445 AVL_SETPARENT(node, gchild);
446 AVL_SETCHILD(node, right);
447
448 AVL_SETBALANCE(gchild, 0);
449 AVL_SETPARENT(gchild, parent);
450 AVL_SETCHILD(gchild, which_child);
451 if (parent != NULL)
452 parent->avl_child[which_child] = gchild;
453 else
454 tree->avl_root = gchild;
455
456 return (1); /* the new tree is always shorter */
457}
458
459
460/*
461 * Insert a new node into an AVL tree at the specified (from avl_find()) place.
462 *
463 * Newly inserted nodes are always leaf nodes in the tree, since avl_find()
464 * searches out to the leaf positions. The avl_index_t indicates the node
465 * which will be the parent of the new node.
466 *
467 * After the node is inserted, a single rotation further up the tree may
468 * be necessary to maintain an acceptable AVL balance.
469 */
470void
471avl_insert(avl_tree_t *tree, void *new_data, avl_index_t where)
472{
473 avl_node_t *node;
474 avl_node_t *parent = AVL_INDEX2NODE(where);
475 int old_balance;
476 int new_balance;
477 int which_child = AVL_INDEX2CHILD(where);
478 size_t off = tree->avl_offset;
479
480 ASSERT(tree);
481#ifdef _LP64
482 ASSERT(((uintptr_t)new_data & 0x7) == 0);
483#endif
484
485 node = AVL_DATA2NODE(new_data, off);
486
487 /*
488 * First, add the node to the tree at the indicated position.
489 */
490 ++tree->avl_numnodes;
491
492 node->avl_child[0] = NULL;
493 node->avl_child[1] = NULL;
494
495 AVL_SETCHILD(node, which_child);
496 AVL_SETBALANCE(node, 0);
497 AVL_SETPARENT(node, parent);
498 if (parent != NULL) {
499 ASSERT(parent->avl_child[which_child] == NULL);
500 parent->avl_child[which_child] = node;
501 } else {
502 ASSERT(tree->avl_root == NULL);
503 tree->avl_root = node;
504 }
505 /*
506 * Now, back up the tree modifying the balance of all nodes above the
507 * insertion point. If we get to a highly unbalanced ancestor, we
508 * need to do a rotation. If we back out of the tree we are done.
509 * If we brought any subtree into perfect balance (0), we are also done.
510 */
511 for (;;) {
512 node = parent;
513 if (node == NULL)
514 return;
515
516 /*
517 * Compute the new balance
518 */
519 old_balance = AVL_XBALANCE(node);
520 new_balance = old_balance + avl_child2balance[which_child];
521
522 /*
523 * If we introduced equal balance, then we are done immediately
524 */
525 if (new_balance == 0) {
526 AVL_SETBALANCE(node, 0);
527 return;
528 }
529
530 /*
531 * If both old and new are not zero we went
532 * from -1 to -2 balance, do a rotation.
533 */
534 if (old_balance != 0)
535 break;
536
537 AVL_SETBALANCE(node, new_balance);
538 parent = AVL_XPARENT(node);
539 which_child = AVL_XCHILD(node);
540 }
541
542 /*
543 * perform a rotation to fix the tree and return
544 */
545 (void) avl_rotation(tree, node, new_balance);
546}
547
548/*
549 * Insert "new_data" in "tree" in the given "direction" either after or
550 * before (AVL_AFTER, AVL_BEFORE) the data "here".
551 *
552 * Insertions can only be done at empty leaf points in the tree, therefore
553 * if the given child of the node is already present we move to either
554 * the AVL_PREV or AVL_NEXT and reverse the insertion direction. Since
555 * every other node in the tree is a leaf, this always works.
556 *
557 * To help developers using this interface, we assert that the new node
558 * is correctly ordered at every step of the way in DEBUG kernels.
559 */
560void
562 avl_tree_t *tree,
563 void *new_data,
564 void *here,
565 int direction)
566{
567 avl_node_t *node;
568 int child = direction; /* rely on AVL_BEFORE == 0, AVL_AFTER == 1 */
569#ifdef DEBUG
570 int diff;
571#endif
572
573 ASSERT(tree != NULL);
574 ASSERT(new_data != NULL);
575 ASSERT(here != NULL);
576 ASSERT(direction == AVL_BEFORE || direction == AVL_AFTER);
577
578 /*
579 * If corresponding child of node is not NULL, go to the neighboring
580 * node and reverse the insertion direction.
581 */
582 node = AVL_DATA2NODE(here, tree->avl_offset);
583
584#ifdef DEBUG
585 diff = tree->avl_compar(new_data, here);
586 ASSERT(-1 <= diff && diff <= 1);
587 ASSERT(diff != 0);
588 ASSERT(diff > 0 ? child == 1 : child == 0);
589#endif
590
591 if (node->avl_child[child] != NULL) {
592 node = node->avl_child[child];
593 child = 1 - child;
594 while (node->avl_child[child] != NULL) {
595#ifdef DEBUG
596 diff = tree->avl_compar(new_data,
597 AVL_NODE2DATA(node, tree->avl_offset));
598 ASSERT(-1 <= diff && diff <= 1);
599 ASSERT(diff != 0);
600 ASSERT(diff > 0 ? child == 1 : child == 0);
601#endif
602 node = node->avl_child[child];
603 }
604#ifdef DEBUG
605 diff = tree->avl_compar(new_data,
606 AVL_NODE2DATA(node, tree->avl_offset));
607 ASSERT(-1 <= diff && diff <= 1);
608 ASSERT(diff != 0);
609 ASSERT(diff > 0 ? child == 1 : child == 0);
610#endif
611 }
612 ASSERT(node->avl_child[child] == NULL);
613
614 avl_insert(tree, new_data, AVL_MKINDEX(node, child));
615}
616
617/*
618 * Add a new node to an AVL tree.
619 */
620void
621avl_add(avl_tree_t *tree, void *new_node)
622{
623 avl_index_t where;
624
625 /*
626 * This is unfortunate. We want to call panic() here, even for
627 * non-DEBUG kernels. In userland, however, we can't depend on anything
628 * in libc or else the rtld build process gets confused. So, all we can
629 * do in userland is resort to a normal ASSERT().
630 */
631 memset(&where, 0, sizeof (where));
632 VERIFY3P(avl_find(tree, new_node, &where), ==, NULL);
633 avl_insert(tree, new_node, where);
634}
635
636/*
637 * Delete a node from the AVL tree. Deletion is similar to insertion, but
638 * with 2 complications.
639 *
640 * First, we may be deleting an interior node. Consider the following subtree:
641 *
642 * d c c
643 * / \ / \ / \
644 * b e b e b e
645 * / \ / \ /
646 * a c a a
647 *
648 * When we are deleting node (d), we find and bring up an adjacent valued leaf
649 * node, say (c), to take the interior node's place. In the code this is
650 * handled by temporarily swapping (d) and (c) in the tree and then using
651 * common code to delete (d) from the leaf position.
652 *
653 * Secondly, an interior deletion from a deep tree may require more than one
654 * rotation to fix the balance. This is handled by moving up the tree through
655 * parents and applying rotations as needed. The return value from
656 * avl_rotation() is used to detect when a subtree did not change overall
657 * height due to a rotation.
658 */
659void
660avl_remove(avl_tree_t *tree, void *data)
661{
662 avl_node_t *delete;
663 avl_node_t *parent;
664 avl_node_t *node;
665 avl_node_t tmp;
666 int old_balance;
667 int new_balance;
668 int left;
669 int right;
670 int which_child;
671 size_t off = tree->avl_offset;
672
673 ASSERT(tree);
674
675 delete = AVL_DATA2NODE(data, off);
676
677 /*
678 * Deletion is easiest with a node that has at most 1 child.
679 * We swap a node with 2 children with a sequentially valued
680 * neighbor node. That node will have at most 1 child. Note this
681 * has no effect on the ordering of the remaining nodes.
682 *
683 * As an optimization, we choose the greater neighbor if the tree
684 * is right heavy, otherwise the left neighbor. This reduces the
685 * number of rotations needed.
686 */
687 if (delete->avl_child[0] != NULL && delete->avl_child[1] != NULL) {
688
689 /*
690 * choose node to swap from whichever side is taller
691 */
692 old_balance = AVL_XBALANCE(delete);
693 left = avl_balance2child[old_balance + 1];
694 right = 1 - left;
695
696 /*
697 * get to the previous value'd node
698 * (down 1 left, as far as possible right)
699 */
700 for (node = delete->avl_child[left];
701 node->avl_child[right] != NULL;
702 node = node->avl_child[right])
703 ;
704
705 /*
706 * create a temp placeholder for 'node'
707 * move 'node' to delete's spot in the tree
708 */
709 tmp = *node;
710
711 *node = *delete;
712 if (node->avl_child[left] == node)
713 node->avl_child[left] = &tmp;
714
715 parent = AVL_XPARENT(node);
716 if (parent != NULL)
717 parent->avl_child[AVL_XCHILD(node)] = node;
718 else
719 tree->avl_root = node;
720 AVL_SETPARENT(node->avl_child[left], node);
721 AVL_SETPARENT(node->avl_child[right], node);
722
723 /*
724 * Put tmp where node used to be (just temporary).
725 * It always has a parent and at most 1 child.
726 */
727 delete = &tmp;
728 parent = AVL_XPARENT(delete);
729 parent->avl_child[AVL_XCHILD(delete)] = delete;
730 which_child = (delete->avl_child[1] != 0);
731 if (delete->avl_child[which_child] != NULL)
732 AVL_SETPARENT(delete->avl_child[which_child], delete);
733 }
734
735
736 /*
737 * Here we know "delete" is at least partially a leaf node. It can
738 * be easily removed from the tree.
739 */
740 ASSERT(tree->avl_numnodes > 0);
741 --tree->avl_numnodes;
742 parent = AVL_XPARENT(delete);
743 which_child = AVL_XCHILD(delete);
744 if (delete->avl_child[0] != NULL)
745 node = delete->avl_child[0];
746 else
747 node = delete->avl_child[1];
748
749 /*
750 * Connect parent directly to node (leaving out delete).
751 */
752 if (node != NULL) {
753 AVL_SETPARENT(node, parent);
754 AVL_SETCHILD(node, which_child);
755 }
756 if (parent == NULL) {
757 tree->avl_root = node;
758 return;
759 }
760 parent->avl_child[which_child] = node;
761
762
763 /*
764 * Since the subtree is now shorter, begin adjusting parent balances
765 * and performing any needed rotations.
766 */
767 do {
768
769 /*
770 * Move up the tree and adjust the balance
771 *
772 * Capture the parent and which_child values for the next
773 * iteration before any rotations occur.
774 */
775 node = parent;
776 old_balance = AVL_XBALANCE(node);
777 new_balance = old_balance - avl_child2balance[which_child];
778 parent = AVL_XPARENT(node);
779 which_child = AVL_XCHILD(node);
780
781 /*
782 * If a node was in perfect balance but isn't anymore then
783 * we can stop, since the height didn't change above this point
784 * due to a deletion.
785 */
786 if (old_balance == 0) {
787 AVL_SETBALANCE(node, new_balance);
788 break;
789 }
790
791 /*
792 * If the new balance is zero, we don't need to rotate
793 * else
794 * need a rotation to fix the balance.
795 * If the rotation doesn't change the height
796 * of the sub-tree we have finished adjusting.
797 */
798 if (new_balance == 0)
799 AVL_SETBALANCE(node, new_balance);
800 else if (!avl_rotation(tree, node, new_balance))
801 break;
802 } while (parent != NULL);
803}
804
805#define AVL_REINSERT(tree, obj) \
806 avl_remove((tree), (obj)); \
807 avl_add((tree), (obj))
808
809bool_t
810avl_update_lt(avl_tree_t *t, void *obj)
811{
812 void *neighbor;
813
814 ASSERT(((neighbor = AVL_NEXT(t, obj)) == NULL) ||
815 (t->avl_compar(obj, neighbor) <= 0));
816
817 neighbor = AVL_PREV(t, obj);
818 if ((neighbor != NULL) && (t->avl_compar(obj, neighbor) < 0)) {
819 AVL_REINSERT(t, obj);
820 return (B_TRUE);
821 }
822
823 return (B_FALSE);
824}
825
826bool_t
827avl_update_gt(avl_tree_t *t, void *obj)
828{
829 void *neighbor;
830
831 ASSERT(((neighbor = AVL_PREV(t, obj)) == NULL) ||
832 (t->avl_compar(obj, neighbor) >= 0));
833
834 neighbor = AVL_NEXT(t, obj);
835 if ((neighbor != NULL) && (t->avl_compar(obj, neighbor) > 0)) {
836 AVL_REINSERT(t, obj);
837 return (B_TRUE);
838 }
839
840 return (B_FALSE);
841}
842
843bool_t
844avl_update(avl_tree_t *t, void *obj)
845{
846 void *neighbor;
847
848 neighbor = AVL_PREV(t, obj);
849 if ((neighbor != NULL) && (t->avl_compar(obj, neighbor) < 0)) {
850 AVL_REINSERT(t, obj);
851 return (B_TRUE);
852 }
853
854 neighbor = AVL_NEXT(t, obj);
855 if ((neighbor != NULL) && (t->avl_compar(obj, neighbor) > 0)) {
856 AVL_REINSERT(t, obj);
857 return (B_TRUE);
858 }
859
860 return (B_FALSE);
861}
862
863/*
864 * initialize a new AVL tree
865 */
866void
867avl_create(avl_tree_t *tree, int (*compar) (const void *, const void *),
868 size_t size, size_t offset)
869{
870 ASSERT(tree);
871 ASSERT(compar);
872 ASSERT(size > 0);
873 ASSERT(size >= offset + sizeof (avl_node_t));
874#ifdef _LP64
875 ASSERT((offset & 0x7) == 0);
876#endif
877
878 tree->avl_compar = compar;
879 tree->avl_root = NULL;
880 tree->avl_numnodes = 0;
881 tree->avl_size = size;
882 tree->avl_offset = offset;
883}
884
885/*
886 * Delete a tree.
887 */
888/* ARGSUSED */
889void
890avl_destroy(avl_tree_t *tree)
891{
892 LACF_UNUSED(tree);
893 ASSERT(tree);
894 ASSERT(tree->avl_numnodes == 0);
895 ASSERT(tree->avl_root == NULL);
896}
897
898
899/*
900 * Return the number of nodes in an AVL tree.
901 */
902unsigned long
903avl_numnodes(const avl_tree_t *tree)
904{
905 ASSERT(tree);
906 return (tree->avl_numnodes);
907}
908
909bool_t
910avl_is_empty(avl_tree_t *tree)
911{
912 ASSERT(tree);
913 return (tree->avl_numnodes == 0);
914}
915
916#define CHILDBIT (1L)
917
918/*
919 * Post-order tree walk used to visit all tree nodes and destroy the tree
920 * in post order. This is used for destroying a tree w/o paying any cost
921 * for rebalancing it.
922 *
923 * example:
924 *
925 * void *cookie = NULL;
926 * my_data_t *node;
927 *
928 * while ((node = avl_destroy_nodes(tree, &cookie)) != NULL)
929 * free(node);
930 * avl_destroy(tree);
931 *
932 * The cookie is really an avl_node_t to the current node's parent and
933 * an indication of which child you looked at last.
934 *
935 * On input, a cookie value of CHILDBIT indicates the tree is done.
936 */
937void *
938avl_destroy_nodes(avl_tree_t *tree, void **cookie)
939{
940 avl_node_t *node;
941 avl_node_t *parent;
942 int child;
943 void *first;
944 size_t off = tree->avl_offset;
945
946 /*
947 * Initial calls go to the first node or it's right descendant.
948 */
949 if (*cookie == NULL) {
950 first = avl_first(tree);
951
952 /*
953 * deal with an empty tree
954 */
955 if (first == NULL) {
956 *cookie = (void *)CHILDBIT;
957 return (NULL);
958 }
959
960 node = AVL_DATA2NODE(first, off);
961 parent = AVL_XPARENT(node);
962 goto check_right_side;
963 }
964
965 /*
966 * If there is no parent to return to we are done.
967 */
968 parent = (avl_node_t *)((uintptr_t)(*cookie) & ~CHILDBIT);
969 if (parent == NULL) {
970 if (tree->avl_root != NULL) {
971 ASSERT(tree->avl_numnodes == 1);
972 tree->avl_root = NULL;
973 tree->avl_numnodes = 0;
974 }
975 return (NULL);
976 }
977
978 /*
979 * Remove the child pointer we just visited from the parent and tree.
980 */
981 child = (uintptr_t)(*cookie) & CHILDBIT;
982 parent->avl_child[child] = NULL;
983 ASSERT(tree->avl_numnodes > 1);
984 --tree->avl_numnodes;
985
986 /*
987 * If we just did a right child or there isn't one, go up to parent.
988 */
989 if (child == 1 || parent->avl_child[1] == NULL) {
990 node = parent;
991 parent = AVL_XPARENT(parent);
992 goto done;
993 }
994
995 /*
996 * Do parent's right child, then leftmost descendent.
997 */
998 node = parent->avl_child[1];
999 while (node->avl_child[0] != NULL) {
1000 parent = node;
1001 node = node->avl_child[0];
1002 }
1003
1004 /*
1005 * If here, we moved to a left child. It may have one
1006 * child on the right (when balance == +1).
1007 */
1008check_right_side:
1009 if (node->avl_child[1] != NULL) {
1010 ASSERT(AVL_XBALANCE(node) == 1);
1011 parent = node;
1012 node = node->avl_child[1];
1013 ASSERT(node->avl_child[0] == NULL &&
1014 node->avl_child[1] == NULL);
1015 } else {
1016 ASSERT(AVL_XBALANCE(node) <= 0);
1017 }
1018
1019done:
1020 if (parent == NULL) {
1021 *cookie = (void *)CHILDBIT;
1022 ASSERT(node == tree->avl_root);
1023 } else {
1024 *cookie = (void *)((uintptr_t)parent | AVL_XCHILD(node));
1025 }
1026
1027 return (AVL_NODE2DATA(node, off));
1028}
#define VERIFY3P(x, op, y)
Definition assert.h:149
#define ASSERT(x)
Definition assert.h:208
void * avl_destroy_nodes(avl_tree_t *tree, void **cookie)
Definition avl.c:938
void * avl_last(const avl_tree_t *tree)
Definition avl.c:191
#define AVL_PREV(tree, node)
Definition avl.h:218
void avl_insert_here(avl_tree_t *tree, void *new_data, void *here, int direction)
Definition avl.c:561
void * avl_first(const avl_tree_t *tree)
Definition avl.c:172
uintptr_t avl_index_t
Definition avl.h:119
bool_t avl_update(avl_tree_t *, void *)
Definition avl.c:844
void avl_remove(avl_tree_t *tree, void *node)
Definition avl.c:660
bool_t avl_update_lt(avl_tree_t *, void *)
Definition avl.c:810
void * avl_nearest(const avl_tree_t *tree, avl_index_t where, int direction)
Definition avl.c:215
void avl_add(avl_tree_t *tree, void *node)
Definition avl.c:621
#define AVL_AFTER
Definition avl.h:129
bool_t avl_is_empty(avl_tree_t *tree)
Definition avl.c:910
void avl_insert(avl_tree_t *tree, void *node, avl_index_t where)
Definition avl.c:471
void avl_create(avl_tree_t *tree, int(*compar)(const void *, const void *), size_t size, size_t offset)
Definition avl.c:867
#define AVL_NEXT(tree, node)
Definition avl.h:212
unsigned long avl_numnodes(const avl_tree_t *tree)
Definition avl.c:903
bool_t avl_update_gt(avl_tree_t *, void *)
Definition avl.c:827
void avl_destroy(avl_tree_t *tree)
Definition avl.c:890
void * avl_find(const avl_tree_t *tree, const void *node, avl_index_t *where)
Definition avl.c:244
#define AVL_BEFORE
Definition avl.h:125