8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / common / avl / avl.c
blob0411afb4c5b4b10c7e54d52f9e47bb128c00dcf1
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
28 * Copyright (c) 2015 by Delphix. All rights reserved.
32 * AVL - generic AVL tree implementation for kernel use
34 * A complete description of AVL trees can be found in many CS textbooks.
36 * Here is a very brief overview. An AVL tree is a binary search tree that is
37 * almost perfectly balanced. By "almost" perfectly balanced, we mean that at
38 * any given node, the left and right subtrees are allowed to differ in height
39 * by at most 1 level.
41 * This relaxation from a perfectly balanced binary tree allows doing
42 * insertion and deletion relatively efficiently. Searching the tree is
43 * still a fast operation, roughly O(log(N)).
45 * The key to insertion and deletion is a set of tree manipulations called
46 * rotations, which bring unbalanced subtrees back into the semi-balanced state.
48 * This implementation of AVL trees has the following peculiarities:
50 * - The AVL specific data structures are physically embedded as fields
51 * in the "using" data structures. To maintain generality the code
52 * must constantly translate between "avl_node_t *" and containing
53 * data structure "void *"s by adding/subtracting the avl_offset.
55 * - Since the AVL data is always embedded in other structures, there is
56 * no locking or memory allocation in the AVL routines. This must be
57 * provided for by the enclosing data structure's semantics. Typically,
58 * avl_insert()/_add()/_remove()/avl_insert_here() require some kind of
59 * exclusive write lock. Other operations require a read lock.
61 * - The implementation uses iteration instead of explicit recursion,
62 * since it is intended to run on limited size kernel stacks. Since
63 * there is no recursion stack present to move "up" in the tree,
64 * there is an explicit "parent" link in the avl_node_t.
66 * - The left/right children pointers of a node are in an array.
67 * In the code, variables (instead of constants) are used to represent
68 * left and right indices. The implementation is written as if it only
69 * dealt with left handed manipulations. By changing the value assigned
70 * to "left", the code also works for right handed trees. The
71 * following variables/terms are frequently used:
73 * int left; // 0 when dealing with left children,
74 * // 1 for dealing with right children
76 * int left_heavy; // -1 when left subtree is taller at some node,
77 * // +1 when right subtree is taller
79 * int right; // will be the opposite of left (0 or 1)
80 * int right_heavy;// will be the opposite of left_heavy (-1 or 1)
82 * int direction; // 0 for "<" (ie. left child); 1 for ">" (right)
84 * Though it is a little more confusing to read the code, the approach
85 * allows using half as much code (and hence cache footprint) for tree
86 * manipulations and eliminates many conditional branches.
88 * - The avl_index_t is an opaque "cookie" used to find nodes at or
89 * adjacent to where a new value would be inserted in the tree. The value
90 * is a modified "avl_node_t *". The bottom bit (normally 0 for a
91 * pointer) is set to indicate if that the new node has a value greater
92 * than the value of the indicated "avl_node_t *".
94 * Note - in addition to userland (e.g. libavl and libutil) and the kernel
95 * (e.g. genunix), avl.c is compiled into ld.so and kmdb's genunix module,
96 * which each have their own compilation environments and subsequent
97 * requirements. Each of these environments must be considered when adding
98 * dependencies from avl.c.
101 #include <sys/types.h>
102 #include <sys/param.h>
103 #include <sys/debug.h>
104 #include <sys/avl.h>
105 #include <sys/cmn_err.h>
108 * Small arrays to translate between balance (or diff) values and child indices.
110 * Code that deals with binary tree data structures will randomly use
111 * left and right children when examining a tree. C "if()" statements
112 * which evaluate randomly suffer from very poor hardware branch prediction.
113 * In this code we avoid some of the branch mispredictions by using the
114 * following translation arrays. They replace random branches with an
115 * additional memory reference. Since the translation arrays are both very
116 * small the data should remain efficiently in cache.
118 static const int avl_child2balance[2] = {-1, 1};
119 static const int avl_balance2child[] = {0, 0, 1};
123 * Walk from one node to the previous valued node (ie. an infix walk
124 * towards the left). At any given node we do one of 2 things:
126 * - If there is a left child, go to it, then to it's rightmost descendant.
128 * - otherwise we return through parent nodes until we've come from a right
129 * child.
131 * Return Value:
132 * NULL - if at the end of the nodes
133 * otherwise next node
135 void *
136 avl_walk(avl_tree_t *tree, void *oldnode, int left)
138 size_t off = tree->avl_offset;
139 avl_node_t *node = AVL_DATA2NODE(oldnode, off);
140 int right = 1 - left;
141 int was_child;
145 * nowhere to walk to if tree is empty
147 if (node == NULL)
148 return (NULL);
151 * Visit the previous valued node. There are two possibilities:
153 * If this node has a left child, go down one left, then all
154 * the way right.
156 if (node->avl_child[left] != NULL) {
157 for (node = node->avl_child[left];
158 node->avl_child[right] != NULL;
159 node = node->avl_child[right])
162 * Otherwise, return thru left children as far as we can.
164 } else {
165 for (;;) {
166 was_child = AVL_XCHILD(node);
167 node = AVL_XPARENT(node);
168 if (node == NULL)
169 return (NULL);
170 if (was_child == right)
171 break;
175 return (AVL_NODE2DATA(node, off));
179 * Return the lowest valued node in a tree or NULL.
180 * (leftmost child from root of tree)
182 void *
183 avl_first(avl_tree_t *tree)
185 avl_node_t *node;
186 avl_node_t *prev = NULL;
187 size_t off = tree->avl_offset;
189 for (node = tree->avl_root; node != NULL; node = node->avl_child[0])
190 prev = node;
192 if (prev != NULL)
193 return (AVL_NODE2DATA(prev, off));
194 return (NULL);
198 * Return the highest valued node in a tree or NULL.
199 * (rightmost child from root of tree)
201 void *
202 avl_last(avl_tree_t *tree)
204 avl_node_t *node;
205 avl_node_t *prev = NULL;
206 size_t off = tree->avl_offset;
208 for (node = tree->avl_root; node != NULL; node = node->avl_child[1])
209 prev = node;
211 if (prev != NULL)
212 return (AVL_NODE2DATA(prev, off));
213 return (NULL);
217 * Access the node immediately before or after an insertion point.
219 * "avl_index_t" is a (avl_node_t *) with the bottom bit indicating a child
221 * Return value:
222 * NULL: no node in the given direction
223 * "void *" of the found tree node
225 void *
226 avl_nearest(avl_tree_t *tree, avl_index_t where, int direction)
228 int child = AVL_INDEX2CHILD(where);
229 avl_node_t *node = AVL_INDEX2NODE(where);
230 void *data;
231 size_t off = tree->avl_offset;
233 if (node == NULL) {
234 ASSERT(tree->avl_root == NULL);
235 return (NULL);
237 data = AVL_NODE2DATA(node, off);
238 if (child != direction)
239 return (data);
241 return (avl_walk(tree, data, direction));
246 * Search for the node which contains "value". The algorithm is a
247 * simple binary tree search.
249 * return value:
250 * NULL: the value is not in the AVL tree
251 * *where (if not NULL) is set to indicate the insertion point
252 * "void *" of the found tree node
254 void *
255 avl_find(avl_tree_t *tree, const void *value, avl_index_t *where)
257 avl_node_t *node;
258 avl_node_t *prev = NULL;
259 int child = 0;
260 int diff;
261 size_t off = tree->avl_offset;
263 for (node = tree->avl_root; node != NULL;
264 node = node->avl_child[child]) {
266 prev = node;
268 diff = tree->avl_compar(value, AVL_NODE2DATA(node, off));
269 ASSERT(-1 <= diff && diff <= 1);
270 if (diff == 0) {
271 #ifdef DEBUG
272 if (where != NULL)
273 *where = 0;
274 #endif
275 return (AVL_NODE2DATA(node, off));
277 child = avl_balance2child[1 + diff];
281 if (where != NULL)
282 *where = AVL_MKINDEX(prev, child);
284 return (NULL);
289 * Perform a rotation to restore balance at the subtree given by depth.
291 * This routine is used by both insertion and deletion. The return value
292 * indicates:
293 * 0 : subtree did not change height
294 * !0 : subtree was reduced in height
296 * The code is written as if handling left rotations, right rotations are
297 * symmetric and handled by swapping values of variables right/left[_heavy]
299 * On input balance is the "new" balance at "node". This value is either
300 * -2 or +2.
302 static int
303 avl_rotation(avl_tree_t *tree, avl_node_t *node, int balance)
305 int left = !(balance < 0); /* when balance = -2, left will be 0 */
306 int right = 1 - left;
307 int left_heavy = balance >> 1;
308 int right_heavy = -left_heavy;
309 avl_node_t *parent = AVL_XPARENT(node);
310 avl_node_t *child = node->avl_child[left];
311 avl_node_t *cright;
312 avl_node_t *gchild;
313 avl_node_t *gright;
314 avl_node_t *gleft;
315 int which_child = AVL_XCHILD(node);
316 int child_bal = AVL_XBALANCE(child);
318 /* BEGIN CSTYLED */
320 * case 1 : node is overly left heavy, the left child is balanced or
321 * also left heavy. This requires the following rotation.
323 * (node bal:-2)
324 * / \
325 * / \
326 * (child bal:0 or -1)
327 * / \
328 * / \
329 * cright
331 * becomes:
333 * (child bal:1 or 0)
334 * / \
335 * / \
336 * (node bal:-1 or 0)
337 * / \
338 * / \
339 * cright
341 * we detect this situation by noting that child's balance is not
342 * right_heavy.
344 /* END CSTYLED */
345 if (child_bal != right_heavy) {
348 * compute new balance of nodes
350 * If child used to be left heavy (now balanced) we reduced
351 * the height of this sub-tree -- used in "return...;" below
353 child_bal += right_heavy; /* adjust towards right */
356 * move "cright" to be node's left child
358 cright = child->avl_child[right];
359 node->avl_child[left] = cright;
360 if (cright != NULL) {
361 AVL_SETPARENT(cright, node);
362 AVL_SETCHILD(cright, left);
366 * move node to be child's right child
368 child->avl_child[right] = node;
369 AVL_SETBALANCE(node, -child_bal);
370 AVL_SETCHILD(node, right);
371 AVL_SETPARENT(node, child);
374 * update the pointer into this subtree
376 AVL_SETBALANCE(child, child_bal);
377 AVL_SETCHILD(child, which_child);
378 AVL_SETPARENT(child, parent);
379 if (parent != NULL)
380 parent->avl_child[which_child] = child;
381 else
382 tree->avl_root = child;
384 return (child_bal == 0);
387 /* BEGIN CSTYLED */
389 * case 2 : When node is left heavy, but child is right heavy we use
390 * a different rotation.
392 * (node b:-2)
393 * / \
394 * / \
395 * / \
396 * (child b:+1)
397 * / \
398 * / \
399 * (gchild b: != 0)
400 * / \
401 * / \
402 * gleft gright
404 * becomes:
406 * (gchild b:0)
407 * / \
408 * / \
409 * / \
410 * (child b:?) (node b:?)
411 * / \ / \
412 * / \ / \
413 * gleft gright
415 * computing the new balances is more complicated. As an example:
416 * if gchild was right_heavy, then child is now left heavy
417 * else it is balanced
419 /* END CSTYLED */
420 gchild = child->avl_child[right];
421 gleft = gchild->avl_child[left];
422 gright = gchild->avl_child[right];
425 * move gright to left child of node and
427 * move gleft to right child of node
429 node->avl_child[left] = gright;
430 if (gright != NULL) {
431 AVL_SETPARENT(gright, node);
432 AVL_SETCHILD(gright, left);
435 child->avl_child[right] = gleft;
436 if (gleft != NULL) {
437 AVL_SETPARENT(gleft, child);
438 AVL_SETCHILD(gleft, right);
442 * move child to left child of gchild and
444 * move node to right child of gchild and
446 * fixup parent of all this to point to gchild
448 balance = AVL_XBALANCE(gchild);
449 gchild->avl_child[left] = child;
450 AVL_SETBALANCE(child, (balance == right_heavy ? left_heavy : 0));
451 AVL_SETPARENT(child, gchild);
452 AVL_SETCHILD(child, left);
454 gchild->avl_child[right] = node;
455 AVL_SETBALANCE(node, (balance == left_heavy ? right_heavy : 0));
456 AVL_SETPARENT(node, gchild);
457 AVL_SETCHILD(node, right);
459 AVL_SETBALANCE(gchild, 0);
460 AVL_SETPARENT(gchild, parent);
461 AVL_SETCHILD(gchild, which_child);
462 if (parent != NULL)
463 parent->avl_child[which_child] = gchild;
464 else
465 tree->avl_root = gchild;
467 return (1); /* the new tree is always shorter */
472 * Insert a new node into an AVL tree at the specified (from avl_find()) place.
474 * Newly inserted nodes are always leaf nodes in the tree, since avl_find()
475 * searches out to the leaf positions. The avl_index_t indicates the node
476 * which will be the parent of the new node.
478 * After the node is inserted, a single rotation further up the tree may
479 * be necessary to maintain an acceptable AVL balance.
481 void
482 avl_insert(avl_tree_t *tree, void *new_data, avl_index_t where)
484 avl_node_t *node;
485 avl_node_t *parent = AVL_INDEX2NODE(where);
486 int old_balance;
487 int new_balance;
488 int which_child = AVL_INDEX2CHILD(where);
489 size_t off = tree->avl_offset;
491 ASSERT(tree);
492 #ifdef _LP64
493 ASSERT(((uintptr_t)new_data & 0x7) == 0);
494 #endif
496 node = AVL_DATA2NODE(new_data, off);
499 * First, add the node to the tree at the indicated position.
501 ++tree->avl_numnodes;
503 node->avl_child[0] = NULL;
504 node->avl_child[1] = NULL;
506 AVL_SETCHILD(node, which_child);
507 AVL_SETBALANCE(node, 0);
508 AVL_SETPARENT(node, parent);
509 if (parent != NULL) {
510 ASSERT(parent->avl_child[which_child] == NULL);
511 parent->avl_child[which_child] = node;
512 } else {
513 ASSERT(tree->avl_root == NULL);
514 tree->avl_root = node;
517 * Now, back up the tree modifying the balance of all nodes above the
518 * insertion point. If we get to a highly unbalanced ancestor, we
519 * need to do a rotation. If we back out of the tree we are done.
520 * If we brought any subtree into perfect balance (0), we are also done.
522 for (;;) {
523 node = parent;
524 if (node == NULL)
525 return;
528 * Compute the new balance
530 old_balance = AVL_XBALANCE(node);
531 new_balance = old_balance + avl_child2balance[which_child];
534 * If we introduced equal balance, then we are done immediately
536 if (new_balance == 0) {
537 AVL_SETBALANCE(node, 0);
538 return;
542 * If both old and new are not zero we went
543 * from -1 to -2 balance, do a rotation.
545 if (old_balance != 0)
546 break;
548 AVL_SETBALANCE(node, new_balance);
549 parent = AVL_XPARENT(node);
550 which_child = AVL_XCHILD(node);
554 * perform a rotation to fix the tree and return
556 (void) avl_rotation(tree, node, new_balance);
560 * Insert "new_data" in "tree" in the given "direction" either after or
561 * before (AVL_AFTER, AVL_BEFORE) the data "here".
563 * Insertions can only be done at empty leaf points in the tree, therefore
564 * if the given child of the node is already present we move to either
565 * the AVL_PREV or AVL_NEXT and reverse the insertion direction. Since
566 * every other node in the tree is a leaf, this always works.
568 * To help developers using this interface, we assert that the new node
569 * is correctly ordered at every step of the way in DEBUG kernels.
571 void
572 avl_insert_here(
573 avl_tree_t *tree,
574 void *new_data,
575 void *here,
576 int direction)
578 avl_node_t *node;
579 int child = direction; /* rely on AVL_BEFORE == 0, AVL_AFTER == 1 */
580 #ifdef DEBUG
581 int diff;
582 #endif
584 ASSERT(tree != NULL);
585 ASSERT(new_data != NULL);
586 ASSERT(here != NULL);
587 ASSERT(direction == AVL_BEFORE || direction == AVL_AFTER);
590 * If corresponding child of node is not NULL, go to the neighboring
591 * node and reverse the insertion direction.
593 node = AVL_DATA2NODE(here, tree->avl_offset);
595 #ifdef DEBUG
596 diff = tree->avl_compar(new_data, here);
597 ASSERT(-1 <= diff && diff <= 1);
598 ASSERT(diff != 0);
599 ASSERT(diff > 0 ? child == 1 : child == 0);
600 #endif
602 if (node->avl_child[child] != NULL) {
603 node = node->avl_child[child];
604 child = 1 - child;
605 while (node->avl_child[child] != NULL) {
606 #ifdef DEBUG
607 diff = tree->avl_compar(new_data,
608 AVL_NODE2DATA(node, tree->avl_offset));
609 ASSERT(-1 <= diff && diff <= 1);
610 ASSERT(diff != 0);
611 ASSERT(diff > 0 ? child == 1 : child == 0);
612 #endif
613 node = node->avl_child[child];
615 #ifdef DEBUG
616 diff = tree->avl_compar(new_data,
617 AVL_NODE2DATA(node, tree->avl_offset));
618 ASSERT(-1 <= diff && diff <= 1);
619 ASSERT(diff != 0);
620 ASSERT(diff > 0 ? child == 1 : child == 0);
621 #endif
623 ASSERT(node->avl_child[child] == NULL);
625 avl_insert(tree, new_data, AVL_MKINDEX(node, child));
629 * Add a new node to an AVL tree.
631 void
632 avl_add(avl_tree_t *tree, void *new_node)
634 avl_index_t where;
637 * This is unfortunate. We want to call panic() here, even for
638 * non-DEBUG kernels. In userland, however, we can't depend on anything
639 * in libc or else the rtld build process gets confused.
640 * Thankfully, rtld provides us with its own assfail() so we can use
641 * that here. We use assfail() directly to get a nice error message
642 * in the core - much like what panic() does for crashdumps.
644 if (avl_find(tree, new_node, &where) != NULL)
645 #ifdef _KERNEL
646 panic("avl_find() succeeded inside avl_add()");
647 #else
648 (void) assfail("avl_find() succeeded inside avl_add()",
649 __FILE__, __LINE__);
650 #endif
651 avl_insert(tree, new_node, where);
655 * Delete a node from the AVL tree. Deletion is similar to insertion, but
656 * with 2 complications.
658 * First, we may be deleting an interior node. Consider the following subtree:
660 * d c c
661 * / \ / \ / \
662 * b e b e b e
663 * / \ / \ /
664 * a c a a
666 * When we are deleting node (d), we find and bring up an adjacent valued leaf
667 * node, say (c), to take the interior node's place. In the code this is
668 * handled by temporarily swapping (d) and (c) in the tree and then using
669 * common code to delete (d) from the leaf position.
671 * Secondly, an interior deletion from a deep tree may require more than one
672 * rotation to fix the balance. This is handled by moving up the tree through
673 * parents and applying rotations as needed. The return value from
674 * avl_rotation() is used to detect when a subtree did not change overall
675 * height due to a rotation.
677 void
678 avl_remove(avl_tree_t *tree, void *data)
680 avl_node_t *delete;
681 avl_node_t *parent;
682 avl_node_t *node;
683 avl_node_t tmp;
684 int old_balance;
685 int new_balance;
686 int left;
687 int right;
688 int which_child;
689 size_t off = tree->avl_offset;
691 ASSERT(tree);
693 delete = AVL_DATA2NODE(data, off);
696 * Deletion is easiest with a node that has at most 1 child.
697 * We swap a node with 2 children with a sequentially valued
698 * neighbor node. That node will have at most 1 child. Note this
699 * has no effect on the ordering of the remaining nodes.
701 * As an optimization, we choose the greater neighbor if the tree
702 * is right heavy, otherwise the left neighbor. This reduces the
703 * number of rotations needed.
705 if (delete->avl_child[0] != NULL && delete->avl_child[1] != NULL) {
708 * choose node to swap from whichever side is taller
710 old_balance = AVL_XBALANCE(delete);
711 left = avl_balance2child[old_balance + 1];
712 right = 1 - left;
715 * get to the previous value'd node
716 * (down 1 left, as far as possible right)
718 for (node = delete->avl_child[left];
719 node->avl_child[right] != NULL;
720 node = node->avl_child[right])
724 * create a temp placeholder for 'node'
725 * move 'node' to delete's spot in the tree
727 tmp = *node;
729 *node = *delete;
730 if (node->avl_child[left] == node)
731 node->avl_child[left] = &tmp;
733 parent = AVL_XPARENT(node);
734 if (parent != NULL)
735 parent->avl_child[AVL_XCHILD(node)] = node;
736 else
737 tree->avl_root = node;
738 AVL_SETPARENT(node->avl_child[left], node);
739 AVL_SETPARENT(node->avl_child[right], node);
742 * Put tmp where node used to be (just temporary).
743 * It always has a parent and at most 1 child.
745 delete = &tmp;
746 parent = AVL_XPARENT(delete);
747 parent->avl_child[AVL_XCHILD(delete)] = delete;
748 which_child = (delete->avl_child[1] != 0);
749 if (delete->avl_child[which_child] != NULL)
750 AVL_SETPARENT(delete->avl_child[which_child], delete);
755 * Here we know "delete" is at least partially a leaf node. It can
756 * be easily removed from the tree.
758 ASSERT(tree->avl_numnodes > 0);
759 --tree->avl_numnodes;
760 parent = AVL_XPARENT(delete);
761 which_child = AVL_XCHILD(delete);
762 if (delete->avl_child[0] != NULL)
763 node = delete->avl_child[0];
764 else
765 node = delete->avl_child[1];
768 * Connect parent directly to node (leaving out delete).
770 if (node != NULL) {
771 AVL_SETPARENT(node, parent);
772 AVL_SETCHILD(node, which_child);
774 if (parent == NULL) {
775 tree->avl_root = node;
776 return;
778 parent->avl_child[which_child] = node;
782 * Since the subtree is now shorter, begin adjusting parent balances
783 * and performing any needed rotations.
785 do {
788 * Move up the tree and adjust the balance
790 * Capture the parent and which_child values for the next
791 * iteration before any rotations occur.
793 node = parent;
794 old_balance = AVL_XBALANCE(node);
795 new_balance = old_balance - avl_child2balance[which_child];
796 parent = AVL_XPARENT(node);
797 which_child = AVL_XCHILD(node);
800 * If a node was in perfect balance but isn't anymore then
801 * we can stop, since the height didn't change above this point
802 * due to a deletion.
804 if (old_balance == 0) {
805 AVL_SETBALANCE(node, new_balance);
806 break;
810 * If the new balance is zero, we don't need to rotate
811 * else
812 * need a rotation to fix the balance.
813 * If the rotation doesn't change the height
814 * of the sub-tree we have finished adjusting.
816 if (new_balance == 0)
817 AVL_SETBALANCE(node, new_balance);
818 else if (!avl_rotation(tree, node, new_balance))
819 break;
820 } while (parent != NULL);
823 #define AVL_REINSERT(tree, obj) \
824 avl_remove((tree), (obj)); \
825 avl_add((tree), (obj))
827 boolean_t
828 avl_update_lt(avl_tree_t *t, void *obj)
830 void *neighbor;
832 ASSERT(((neighbor = AVL_NEXT(t, obj)) == NULL) ||
833 (t->avl_compar(obj, neighbor) <= 0));
835 neighbor = AVL_PREV(t, obj);
836 if ((neighbor != NULL) && (t->avl_compar(obj, neighbor) < 0)) {
837 AVL_REINSERT(t, obj);
838 return (B_TRUE);
841 return (B_FALSE);
844 boolean_t
845 avl_update_gt(avl_tree_t *t, void *obj)
847 void *neighbor;
849 ASSERT(((neighbor = AVL_PREV(t, obj)) == NULL) ||
850 (t->avl_compar(obj, neighbor) >= 0));
852 neighbor = AVL_NEXT(t, obj);
853 if ((neighbor != NULL) && (t->avl_compar(obj, neighbor) > 0)) {
854 AVL_REINSERT(t, obj);
855 return (B_TRUE);
858 return (B_FALSE);
861 boolean_t
862 avl_update(avl_tree_t *t, void *obj)
864 void *neighbor;
866 neighbor = AVL_PREV(t, obj);
867 if ((neighbor != NULL) && (t->avl_compar(obj, neighbor) < 0)) {
868 AVL_REINSERT(t, obj);
869 return (B_TRUE);
872 neighbor = AVL_NEXT(t, obj);
873 if ((neighbor != NULL) && (t->avl_compar(obj, neighbor) > 0)) {
874 AVL_REINSERT(t, obj);
875 return (B_TRUE);
878 return (B_FALSE);
881 void
882 avl_swap(avl_tree_t *tree1, avl_tree_t *tree2)
884 avl_node_t *temp_node;
885 ulong_t temp_numnodes;
887 ASSERT3P(tree1->avl_compar, ==, tree2->avl_compar);
888 ASSERT3U(tree1->avl_offset, ==, tree2->avl_offset);
889 ASSERT3U(tree1->avl_size, ==, tree2->avl_size);
891 temp_node = tree1->avl_root;
892 temp_numnodes = tree1->avl_numnodes;
893 tree1->avl_root = tree2->avl_root;
894 tree1->avl_numnodes = tree2->avl_numnodes;
895 tree2->avl_root = temp_node;
896 tree2->avl_numnodes = temp_numnodes;
900 * initialize a new AVL tree
902 void
903 avl_create(avl_tree_t *tree, int (*compar) (const void *, const void *),
904 size_t size, size_t offset)
906 ASSERT(tree);
907 ASSERT(compar);
908 ASSERT(size > 0);
909 ASSERT(size >= offset + sizeof (avl_node_t));
910 #ifdef _LP64
911 ASSERT((offset & 0x7) == 0);
912 #endif
914 tree->avl_compar = compar;
915 tree->avl_root = NULL;
916 tree->avl_numnodes = 0;
917 tree->avl_size = size;
918 tree->avl_offset = offset;
922 * Delete a tree.
924 /* ARGSUSED */
925 void
926 avl_destroy(avl_tree_t *tree)
928 ASSERT(tree);
929 ASSERT(tree->avl_numnodes == 0);
930 ASSERT(tree->avl_root == NULL);
935 * Return the number of nodes in an AVL tree.
937 ulong_t
938 avl_numnodes(avl_tree_t *tree)
940 ASSERT(tree);
941 return (tree->avl_numnodes);
944 boolean_t
945 avl_is_empty(avl_tree_t *tree)
947 ASSERT(tree);
948 return (tree->avl_numnodes == 0);
951 #define CHILDBIT (1L)
954 * Post-order tree walk used to visit all tree nodes and destroy the tree
955 * in post order. This is used for removing all the nodes from a tree without
956 * paying any cost for rebalancing it.
958 * example:
960 * void *cookie = NULL;
961 * my_data_t *node;
963 * while ((node = avl_destroy_nodes(tree, &cookie)) != NULL)
964 * free(node);
965 * avl_destroy(tree);
967 * The cookie is really an avl_node_t to the current node's parent and
968 * an indication of which child you looked at last.
970 * On input, a cookie value of CHILDBIT indicates the tree is done.
972 void *
973 avl_destroy_nodes(avl_tree_t *tree, void **cookie)
975 avl_node_t *node;
976 avl_node_t *parent;
977 int child;
978 void *first;
979 size_t off = tree->avl_offset;
982 * Initial calls go to the first node or it's right descendant.
984 if (*cookie == NULL) {
985 first = avl_first(tree);
988 * deal with an empty tree
990 if (first == NULL) {
991 *cookie = (void *)CHILDBIT;
992 return (NULL);
995 node = AVL_DATA2NODE(first, off);
996 parent = AVL_XPARENT(node);
997 goto check_right_side;
1001 * If there is no parent to return to we are done.
1003 parent = (avl_node_t *)((uintptr_t)(*cookie) & ~CHILDBIT);
1004 if (parent == NULL) {
1005 if (tree->avl_root != NULL) {
1006 ASSERT(tree->avl_numnodes == 1);
1007 tree->avl_root = NULL;
1008 tree->avl_numnodes = 0;
1010 return (NULL);
1014 * Remove the child pointer we just visited from the parent and tree.
1016 child = (uintptr_t)(*cookie) & CHILDBIT;
1017 parent->avl_child[child] = NULL;
1018 ASSERT(tree->avl_numnodes > 1);
1019 --tree->avl_numnodes;
1022 * If we just did a right child or there isn't one, go up to parent.
1024 if (child == 1 || parent->avl_child[1] == NULL) {
1025 node = parent;
1026 parent = AVL_XPARENT(parent);
1027 goto done;
1031 * Do parent's right child, then leftmost descendent.
1033 node = parent->avl_child[1];
1034 while (node->avl_child[0] != NULL) {
1035 parent = node;
1036 node = node->avl_child[0];
1040 * If here, we moved to a left child. It may have one
1041 * child on the right (when balance == +1).
1043 check_right_side:
1044 if (node->avl_child[1] != NULL) {
1045 ASSERT(AVL_XBALANCE(node) == 1);
1046 parent = node;
1047 node = node->avl_child[1];
1048 ASSERT(node->avl_child[0] == NULL &&
1049 node->avl_child[1] == NULL);
1050 } else {
1051 ASSERT(AVL_XBALANCE(node) <= 0);
1054 done:
1055 if (parent == NULL) {
1056 *cookie = (void *)CHILDBIT;
1057 ASSERT(node == tree->avl_root);
1058 } else {
1059 *cookie = (void *)((uintptr_t)parent | AVL_XCHILD(node));
1062 return (AVL_NODE2DATA(node, off));