core: btree: implement moving btree nodes

moving a btree node is similar to simply using memmove() or memcpy(), with the added
bonus of updating the pointers in the wider data structure to the new node memory
and zeroing the old node memory so that it isn't used accidentally after the move
is complete.
This commit is contained in:
2026-02-03 14:28:56 +00:00
parent d14a1fc61f
commit 837a42e249
2 changed files with 28 additions and 0 deletions

View File

@@ -736,6 +736,32 @@ b_btree_node *b_btree_prev(const struct b_btree_node *node)
return prev_node(node, &d);
}
void b_btree_move(
struct b_btree *tree, struct b_btree_node *dest, struct b_btree_node *src)
{
if (src->b_parent) {
if (src->b_parent->b_left == src) {
src->b_parent->b_left = dest;
} else {
src->b_parent->b_right = dest;
}
}
if (src->b_left) {
src->b_left->b_parent = dest;
}
if (src->b_right) {
src->b_right->b_parent = dest;
}
if (tree->b_root == src) {
tree->b_root = dest;
}
memmove(dest, src, sizeof *src);
}
b_iterator *b_btree_begin(struct b_btree *tree)
{
b_iterator *it_obj = b_object_create(B_TYPE_BTREE_ITERATOR);

View File

@@ -335,6 +335,8 @@ static inline b_btree_node *b_btree_parent(b_btree_node *node)
return node->b_parent;
}
BLUE_API void b_btree_move(b_btree *tree, b_btree_node *dest, b_btree_node *src);
/* get the height of `node`.
the height of a node is defined as the length of the longest path