feat(lsm): started string implementation

This commit is contained in:
Jef Roosens 2023-10-13 12:45:48 +02:00
parent 96fc645034
commit c327be80e9
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
11 changed files with 256 additions and 90 deletions

51
lsm/include/lsm/bt.h Normal file
View file

@ -0,0 +1,51 @@
#ifndef LSM_BT
#define LSM_BT
#include "lsm.h"
/**
* A binary tree implementation using char values as keys
*/
typedef struct lsm_bt lsm_bt;
/**
* Initialize a new binary tree
*
* @param ptr where to store newly allocated pointer
*/
lsm_error lsm_bt_init(lsm_bt **ptr);
/**
* Deallocate an entire binary tree, including all its nodes
*/
void lsm_bt_free(lsm_bt *bt);
/**
* Search for the data stored behind the given key.
*
* @param out pointer to store data pointer in
* @param bt binary tree to search
* @param key key to search
*/
lsm_error lsm_bt_search(void **out, lsm_bt *bt, char key);
/**
* Insert a new data value into the tree with the given key.
*
* @param bt binary tree to insert into
* @param key key to insert
* @param data data to store
*/
lsm_error lsm_bt_insert(lsm_bt *bt, char key, void *data);
/**
* Remove the given key from the binary tree. Ownership of the data pointer is
* returned to the caller.
*
* @param out address to write data pointer to
* @param bt binary tree to remove from
* @param key key to remove
*/
lsm_error lsm_bt_remove(void **out, lsm_bt *bt, char key);
#endif

60
lsm/include/lsm/str.h Normal file
View file

@ -0,0 +1,60 @@
#ifndef LSM_STR
#define LSM_STR
#include "lsm.h"
/**
* Represents a string (or really any kind of data) with a known length. Data
* with length 8 or less is stored inside the pointer, and does not allocate
* additional memory.
*/
typedef struct lsm_str lsm_str;
/**
* Allocate a new string struct of length 0.
*
* @param ptr pointer to store newly allocated pointer in
*/
lsm_error lsm_str_init_zero(lsm_str **ptr);
/**
* Update an existing lsm_str so it now represents the new provided string. The
* string pointer of the original object is free'd if needed.
*
* @param str lsm_str object to modify
* @param s string to convert into lsm string; ownership is taken over
*/
void lsm_str_init_prealloc(lsm_str *str, char *s);
/**
* Allocate and initialize a new lsm_str object
*
* @param ptr pointer to store newly allocated pointer
* @param s string to convert into lsm string; ownership is taken over
*/
lsm_error lsm_str_init(lsm_str **ptr, char *s);
/**
* Deallocate the existing internal string if needed and replace the lsm_str
* with a string of length 0, wiping its contents.
*
* @param str string to wipe
*/
void lsm_str_zero(lsm_str *str);
/**
* Deallocate the string and its internal char buffer if needed. Only call this
* on heap-allocated strings.
*
* @param str string to dealloate
*/
void lsm_str_free(lsm_str *str);
/**
* Return the length of the string.
*
* @param str string to return length for.
*/
uint64_t lsm_str_len(lsm_str *str);
#endif

25
lsm/include/lsm/trie.h Normal file
View file

@ -0,0 +1,25 @@
#ifndef LSM_TRIE
#define LSM_TRIE
#include "lsm.h"
/**
* A struct representing a trie
*/
typedef struct lsm_trie lsm_trie;
/**
* Initialize a new trie.
*
* @param ptr where to store the newly allocated pointer
*/
lsm_error lsm_trie_init(lsm_trie **ptr);
/**
* Deallocate an entire trie, including all its nodes
*
* @param trie trie to free
*/
void lsm_trie_free(lsm_trie *trie);
#endif