From d98f46ce647846b0aa30b2e16a30fd4e152a1bf5 Mon Sep 17 00:00:00 2001 From: Carlos Maiolino Date: Thu, 10 Jul 2025 22:55:07 +0200 Subject: Add new code Signed-off-by: Carlos Maiolino --- Algorithms/bplus-tree/src/bplus_leaf.c | 71 ++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 Algorithms/bplus-tree/src/bplus_leaf.c (limited to 'Algorithms/bplus-tree/src/bplus_leaf.c') diff --git a/Algorithms/bplus-tree/src/bplus_leaf.c b/Algorithms/bplus-tree/src/bplus_leaf.c new file mode 100644 index 0000000..a6b6ae5 --- /dev/null +++ b/Algorithms/bplus-tree/src/bplus_leaf.c @@ -0,0 +1,71 @@ +/** + * Distributed under the Boost Software License, Version 1.0. + * See accompanying file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt + */ + +#include "bplus_leaf.h" + +BplusLeaf* bplus_leaf_new(BplusTree* tree) +{ + g_return_val_if_fail(tree != NULL, NULL); + + BplusLeaf* leaf = g_slice_new(BplusLeaf); + + bplus_node_init(&leaf->node, TRUE); + leaf->left = NULL; + leaf->right = NULL; + +#ifdef BPLUS_TREE_GATHER_STATS + tree->node_count++; + tree->leaf_count++; + tree->underflow_count++; +#endif /* ifdef BPLUS_TREE_GATHER_STATS */ + + return leaf; +} + +BplusLeaf* bplus_leaf_new_right(BplusTree* tree, BplusLeaf* leaf_left) +{ + g_return_val_if_fail(tree != NULL, NULL); + g_return_val_if_fail(leaf_left != NULL, NULL); + + BplusLeaf* leaf_right = bplus_leaf_new(tree); + + leaf_right->left = leaf_left; + leaf_right->right = leaf_left->right; + leaf_left->right = leaf_right; + + if (leaf_right->right != NULL) + leaf_right->right->left = leaf_right; + + if (tree->last == leaf_left) + tree->last = leaf_right; + + return leaf_right; +} + +void bplus_leaf_destroy(BplusTree* tree, BplusLeaf* leaf) +{ + g_return_if_fail(tree != NULL); + g_return_if_fail(leaf != NULL); + + if (leaf->left != NULL) + leaf->left->right = leaf->right; + + if (leaf->right != NULL) + leaf->right->left = leaf->left; + + if (tree->first == leaf) + tree->first = leaf->right; + + if (tree->last == leaf) + tree->last = leaf->left; + +#ifdef BPLUS_TREE_GATHER_STATS + tree->node_count--; + tree->leaf_count--; + tree->underflow_count--; +#endif /* ifdef BPLUS_TREE_GATHER_STATS */ + + g_slice_free(BplusLeaf, leaf); +} -- cgit v1.2.3