core: btree: replace legacy iterator interface

This commit is contained in:
2025-10-29 14:30:33 +00:00
parent c2b894ef22
commit 1e4b5bc4e6
2 changed files with 102 additions and 63 deletions

View File

@@ -2,19 +2,21 @@
#define BLUELIB_CORE_BTREE_H_
#include <blue/core/iterator.h>
#include <blue/core/macros.h>
#include <blue/core/misc.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#define B_BTREE_INIT \
{ \
0 \
}
B_DECLS_BEGIN;
#ifdef __cplusplus
extern "C" {
#endif
#define B_BTREE_INIT {0}
#define B_TYPE_BTREE_ITERATOR (b_btree_iterator_get_type())
B_DECLARE_TYPE(b_btree_iterator);
B_TYPE_CLASS_DECLARATION_BEGIN(b_btree_iterator)
B_TYPE_CLASS_DECLARATION_END(b_btree_iterator)
/* defines a simple node insertion function.
this function assumes that your nodes have simple integer keys that can be
@@ -24,18 +26,18 @@ extern "C" {
if you have a tree node type like this:
struct my_tree_node {
int key;
b_btree_node base;
int key;
b_btree_node base;
}
You would use the following call to generate an insert function for a tree
with this node type:
BTREE_DEFINE_SIMPLE_INSERT(
struct my_tree_node,
base,
key,
my_tree_node_insert);
struct my_tree_node,
base,
key,
my_tree_node_insert);
Which would emit a function defined like:
@@ -102,8 +104,8 @@ extern "C" {
if you have a tree node type like this:
struct my_tree_node {
complex_key_t key;
b_btree_node base;
complex_key_t key;
b_btree_node base;
}
You would need to define a comparator function or macro with the following
@@ -191,8 +193,8 @@ extern "C" {
if you have a tree node type like this:
struct my_tree_node {
int key;
b_btree_node base;
int key;
b_btree_node base;
}
You would use the following call to generate a search function for a tree
@@ -257,12 +259,7 @@ typedef struct b_btree {
b_btree_node *b_root;
} b_btree;
typedef struct b_btree_iterator {
b_iterator _base;
size_t i, depth;
b_btree_node *node;
b_btree *_b;
} b_btree_iterator;
BLUE_API b_type b_btree_iterator_get_type(void);
/* re-balance a binary tree after an insertion operation.
@@ -350,10 +347,8 @@ static inline unsigned short b_btree_height(b_btree_node *node)
return node->b_height;
}
BLUE_API int b_btree_iterator_begin(const b_btree *tree, b_btree_iterator *it);
BLUE_API bool b_btree_iterator_next(b_btree_iterator *it);
BLUE_API b_status b_btree_iterator_erase(b_btree_iterator *it);
BLUE_API bool b_btree_iterator_is_valid(const b_btree_iterator *it);
BLUE_API b_iterator *b_btree_begin(b_btree *tree);
BLUE_API const b_iterator *b_btree_cbegin(const b_btree *tree);
#ifdef __cplusplus
}