From 837a42e249aab60169cc4055347b8d4ad75d8dcf Mon Sep 17 00:00:00 2001 From: Max Wash Date: Tue, 3 Feb 2026 14:28:56 +0000 Subject: [PATCH] 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. --- core/btree.c | 26 ++++++++++++++++++++++++++ core/include/blue/core/btree.h | 2 ++ 2 files changed, 28 insertions(+) diff --git a/core/btree.c b/core/btree.c index 56dea8c..fdc2a50 100644 --- a/core/btree.c +++ b/core/btree.c @@ -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); diff --git a/core/include/blue/core/btree.h b/core/include/blue/core/btree.h index b5cf925..c055cdd 100644 --- a/core/include/blue/core/btree.h +++ b/core/include/blue/core/btree.h @@ -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