feat(lsm): binary tree iterators

This commit is contained in:
Jef Roosens 2023-12-22 22:07:09 +01:00
parent 8c2a7a640d
commit 5564e23ceb
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
5 changed files with 130 additions and 5 deletions

View file

@ -1,6 +1,8 @@
#ifndef LSM_BT
#define LSM_BT
#include <stdbool.h>
#include "lsm.h"
/**
@ -8,6 +10,11 @@
*/
typedef struct lsm_bt lsm_bt;
/**
* A node inside an `lsm_bt` binary tree.
*/
typedef struct lsm_bt_node lsm_bt_node;
/**
* Initialize a new binary tree
*
@ -68,4 +75,33 @@ lsm_error lsm_bt_remove(void **out, lsm_bt *bt, char key);
*/
lsm_error lsm_bt_replace(void **out, lsm_bt *bt, char key, void *data);
/**
* Struct representing an in-flight iterator over the binary tree
*/
typedef struct lsm_bt_iterator {
lsm_bt_node *next;
} lsm_bt_iterator;
/**
* Initialize the given iterator for the binary tree.
*
* The iterator is explicitely allowed to be allocated by the user, as these are
* commonly used inside functions where they can simply be stored on the stack.
*
* @param out iterator to initialize
* @param bt binary tree to iterate
*/
void lsm_bt_iter(lsm_bt_iterator *out, const lsm_bt *bt);
/**
* Advance the iterator to the next element.
*
* @param out where to store pointer to data; ignored if NULL
* @param key_out where to store key; ignored if NULL
* @param iter iterator to advance
* @return true if a new entry was returned, false if the iterator has no more
* entries to return
*/
bool lsm_bt_iter_next(const void **out, char *key_out, lsm_bt_iterator *iter);
#endif