108static const int avl_child2balance[2] = {-1, 1};
109static const int avl_balance2child[] = {0, 0, 1};
125avl_walk(
const avl_tree_t *tree,
const void *oldnode,
int left)
127 size_t off = tree->avl_offset;
128 const avl_node_t *node = AVL_DATA2NODE(oldnode, off);
129 int right = 1 - left;
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])
155 was_child = AVL_XCHILD(node);
156 node = AVL_XPARENT(node);
159 if (was_child == right)
164 return (AVL_NODE2DATA(node, off));
174 const avl_node_t *node;
175 const avl_node_t *prev = NULL;
176 size_t off = tree->avl_offset;
178 for (node = tree->avl_root; node != NULL; node = node->avl_child[0])
182 return (AVL_NODE2DATA(prev, off));
193 const avl_node_t *node;
194 const avl_node_t *prev = NULL;
195 size_t off = tree->avl_offset;
197 for (node = tree->avl_root; node != NULL; node = node->avl_child[1])
201 return (AVL_NODE2DATA(prev, off));
217 int child = AVL_INDEX2CHILD(where);
218 avl_node_t *node = AVL_INDEX2NODE(where);
220 size_t off = tree->avl_offset;
223 ASSERT(tree->avl_root == NULL);
226 data = AVL_NODE2DATA(node, off);
227 if (child != direction)
230 return (avl_walk(tree, data, direction));
247 avl_node_t *prev = NULL;
250 size_t off = tree->avl_offset;
252 for (node = tree->avl_root; node != NULL;
253 node = node->avl_child[child]) {
257 diff = tree->avl_compar(value, AVL_NODE2DATA(node, off));
258 ASSERT(-1 <= diff && diff <= 1);
264 return (AVL_NODE2DATA(node, off));
266 child = avl_balance2child[1 + diff];
271 *where = AVL_MKINDEX(prev, child);
292avl_rotation(avl_tree_t *tree, avl_node_t *node,
int balance)
294 int left = !(balance < 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];
304 int which_child = AVL_XCHILD(node);
305 int child_bal = AVL_XBALANCE(child);
334 if (child_bal != right_heavy) {
342 child_bal += right_heavy;
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);
357 child->avl_child[right] = node;
358 AVL_SETBALANCE(node, -child_bal);
359 AVL_SETCHILD(node, right);
360 AVL_SETPARENT(node, child);
365 AVL_SETBALANCE(child, child_bal);
366 AVL_SETCHILD(child, which_child);
367 AVL_SETPARENT(child, parent);
369 parent->avl_child[which_child] = child;
371 tree->avl_root = child;
373 return (child_bal == 0);
409 gchild = child->avl_child[right];
410 gleft = gchild->avl_child[left];
411 gright = gchild->avl_child[right];
418 node->avl_child[left] = gright;
419 if (gright != NULL) {
420 AVL_SETPARENT(gright, node);
421 AVL_SETCHILD(gright, left);
424 child->avl_child[right] = gleft;
426 AVL_SETPARENT(gleft, child);
427 AVL_SETCHILD(gleft, right);
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);
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);
448 AVL_SETBALANCE(gchild, 0);
449 AVL_SETPARENT(gchild, parent);
450 AVL_SETCHILD(gchild, which_child);
452 parent->avl_child[which_child] = gchild;
454 tree->avl_root = gchild;
474 avl_node_t *parent = AVL_INDEX2NODE(where);
477 int which_child = AVL_INDEX2CHILD(where);
478 size_t off = tree->avl_offset;
482 ASSERT(((uintptr_t)new_data & 0x7) == 0);
485 node = AVL_DATA2NODE(new_data, off);
490 ++tree->avl_numnodes;
492 node->avl_child[0] = NULL;
493 node->avl_child[1] = NULL;
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;
502 ASSERT(tree->avl_root == NULL);
503 tree->avl_root = node;
519 old_balance = AVL_XBALANCE(node);
520 new_balance = old_balance + avl_child2balance[which_child];
525 if (new_balance == 0) {
526 AVL_SETBALANCE(node, 0);
534 if (old_balance != 0)
537 AVL_SETBALANCE(node, new_balance);
538 parent = AVL_XPARENT(node);
539 which_child = AVL_XCHILD(node);
545 (void) avl_rotation(tree, node, new_balance);
568 int child = direction;
582 node = AVL_DATA2NODE(here, tree->avl_offset);
585 diff = tree->avl_compar(new_data, here);
586 ASSERT(-1 <= diff && diff <= 1);
588 ASSERT(diff > 0 ? child == 1 : child == 0);
591 if (node->avl_child[child] != NULL) {
592 node = node->avl_child[child];
594 while (node->avl_child[child] != NULL) {
596 diff = tree->avl_compar(new_data,
597 AVL_NODE2DATA(node, tree->avl_offset));
598 ASSERT(-1 <= diff && diff <= 1);
600 ASSERT(diff > 0 ? child == 1 : child == 0);
602 node = node->avl_child[child];
605 diff = tree->avl_compar(new_data,
606 AVL_NODE2DATA(node, tree->avl_offset));
607 ASSERT(-1 <= diff && diff <= 1);
609 ASSERT(diff > 0 ? child == 1 : child == 0);
612 ASSERT(node->avl_child[child] == NULL);
614 avl_insert(tree, new_data, AVL_MKINDEX(node, child));
631 memset(&where, 0,
sizeof (where));
671 size_t off = tree->avl_offset;
675 delete = AVL_DATA2NODE(data, off);
687 if (delete->avl_child[0] != NULL && delete->avl_child[1] != NULL) {
692 old_balance = AVL_XBALANCE(
delete);
693 left = avl_balance2child[old_balance + 1];
700 for (node = delete->avl_child[left];
701 node->avl_child[right] != NULL;
702 node = node->avl_child[right])
712 if (node->avl_child[left] == node)
713 node->avl_child[left] = &tmp;
715 parent = AVL_XPARENT(node);
717 parent->avl_child[AVL_XCHILD(node)] = node;
719 tree->avl_root = node;
720 AVL_SETPARENT(node->avl_child[left], node);
721 AVL_SETPARENT(node->avl_child[right], node);
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);
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];
747 node =
delete->avl_child[1];
753 AVL_SETPARENT(node, parent);
754 AVL_SETCHILD(node, which_child);
756 if (parent == NULL) {
757 tree->avl_root = node;
760 parent->avl_child[which_child] = node;
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);
786 if (old_balance == 0) {
787 AVL_SETBALANCE(node, new_balance);
798 if (new_balance == 0)
799 AVL_SETBALANCE(node, new_balance);
800 else if (!avl_rotation(tree, node, new_balance))
802 }
while (parent != NULL);
805#define AVL_REINSERT(tree, obj) \
806 avl_remove((tree), (obj)); \
807 avl_add((tree), (obj))
815 (t->avl_compar(obj, neighbor) <= 0));
818 if ((neighbor != NULL) && (t->avl_compar(obj, neighbor) < 0)) {
819 AVL_REINSERT(t, obj);
832 (t->avl_compar(obj, neighbor) >= 0));
835 if ((neighbor != NULL) && (t->avl_compar(obj, neighbor) > 0)) {
836 AVL_REINSERT(t, obj);
849 if ((neighbor != NULL) && (t->avl_compar(obj, neighbor) < 0)) {
850 AVL_REINSERT(t, obj);
855 if ((neighbor != NULL) && (t->avl_compar(obj, neighbor) > 0)) {
856 AVL_REINSERT(t, obj);
867avl_create(avl_tree_t *tree,
int (*compar) (
const void *,
const void *),
868 size_t size,
size_t offset)
873 ASSERT(size >= offset +
sizeof (avl_node_t));
875 ASSERT((offset & 0x7) == 0);
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;
894 ASSERT(tree->avl_numnodes == 0);
895 ASSERT(tree->avl_root == NULL);
906 return (tree->avl_numnodes);
913 return (tree->avl_numnodes == 0);
944 size_t off = tree->avl_offset;
949 if (*cookie == NULL) {
956 *cookie = (
void *)CHILDBIT;
960 node = AVL_DATA2NODE(first, off);
961 parent = AVL_XPARENT(node);
962 goto check_right_side;
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;
981 child = (uintptr_t)(*cookie) & CHILDBIT;
982 parent->avl_child[child] = NULL;
983 ASSERT(tree->avl_numnodes > 1);
984 --tree->avl_numnodes;
989 if (child == 1 || parent->avl_child[1] == NULL) {
991 parent = AVL_XPARENT(parent);
998 node = parent->avl_child[1];
999 while (node->avl_child[0] != NULL) {
1001 node = node->avl_child[0];
1009 if (node->avl_child[1] != NULL) {
1010 ASSERT(AVL_XBALANCE(node) == 1);
1012 node = node->avl_child[1];
1013 ASSERT(node->avl_child[0] == NULL &&
1014 node->avl_child[1] == NULL);
1016 ASSERT(AVL_XBALANCE(node) <= 0);
1020 if (parent == NULL) {
1021 *cookie = (
void *)CHILDBIT;
1022 ASSERT(node == tree->avl_root);
1024 *cookie = (
void *)((uintptr_t)parent | AVL_XCHILD(node));
1027 return (AVL_NODE2DATA(node, off));
#define VERIFY3P(x, op, y)
void * avl_destroy_nodes(avl_tree_t *tree, void **cookie)
void * avl_last(const avl_tree_t *tree)
#define AVL_PREV(tree, node)
void avl_insert_here(avl_tree_t *tree, void *new_data, void *here, int direction)
void * avl_first(const avl_tree_t *tree)
bool_t avl_update(avl_tree_t *, void *)
void avl_remove(avl_tree_t *tree, void *node)
bool_t avl_update_lt(avl_tree_t *, void *)
void * avl_nearest(const avl_tree_t *tree, avl_index_t where, int direction)
void avl_add(avl_tree_t *tree, void *node)
bool_t avl_is_empty(avl_tree_t *tree)
void avl_insert(avl_tree_t *tree, void *node, avl_index_t where)
void avl_create(avl_tree_t *tree, int(*compar)(const void *, const void *), size_t size, size_t offset)
#define AVL_NEXT(tree, node)
unsigned long avl_numnodes(const avl_tree_t *tree)
bool_t avl_update_gt(avl_tree_t *, void *)
void avl_destroy(avl_tree_t *tree)
void * avl_find(const avl_tree_t *tree, const void *node, avl_index_t *where)