(libretro-db) Cleanups

This commit is contained in:
libretroadmin 2023-02-18 18:13:52 +01:00
parent 2d48cbaec6
commit 91b7445bda
3 changed files with 31 additions and 52 deletions

View file

@ -27,21 +27,6 @@
#include "bintree.h" #include "bintree.h"
struct bintree_node
{
void *value;
struct bintree_node *parent;
struct bintree_node *left;
struct bintree_node *right;
};
struct bintree
{
struct bintree_node *root;
void *ctx;
bintree_cmp_func cmp;
};
static void * const NIL_NODE = (void*)&NIL_NODE; static void * const NIL_NODE = (void*)&NIL_NODE;
static struct bintree_node *bintree_new_nil_node( static struct bintree_node *bintree_new_nil_node(
@ -61,8 +46,7 @@ static struct bintree_node *bintree_new_nil_node(
return node; return node;
} }
static int bintree_insert_internal(bintree_t *t, int bintree_insert(bintree_t *t, struct bintree_node *root, void *value)
struct bintree_node *root, void *value)
{ {
int cmp_res = 0; int cmp_res = 0;
@ -78,31 +62,30 @@ static int bintree_insert_internal(bintree_t *t,
cmp_res = t->cmp(root->value, value, t->ctx); cmp_res = t->cmp(root->value, value, t->ctx);
if (cmp_res > 0) if (cmp_res > 0)
return bintree_insert_internal(t, root->left, value); return bintree_insert(t, root->left, value);
else if (cmp_res < 0) else if (cmp_res < 0)
return bintree_insert_internal(t, root->right, value); return bintree_insert(t, root->right, value);
return -1; return -1;
} }
static int bintree_iterate_internal(struct bintree_node *n, int bintree_iterate(struct bintree_node *n, bintree_iter_cb cb, void *ctx)
bintree_iter_cb cb, void *ctx)
{ {
int rv; int rv;
if (!n || (n->value == NIL_NODE)) if (!n || (n->value == NIL_NODE))
return 0; return 0;
if ((rv = bintree_iterate_internal(n->left, cb, ctx)) != 0) if ((rv = bintree_iterate(n->left, cb, ctx)) != 0)
return rv; return rv;
if ((rv = cb(n->value, ctx)) != 0) if ((rv = cb(n->value, ctx)) != 0)
return rv; return rv;
if ((rv = bintree_iterate_internal(n->right, cb, ctx)) != 0) if ((rv = bintree_iterate(n->right, cb, ctx)) != 0)
return rv; return rv;
return 0; return 0;
} }
static void bintree_free_node(struct bintree_node *n) void bintree_free(struct bintree_node *n)
{ {
if (n->value == NIL_NODE) if (n->value == NIL_NODE)
{ {
@ -112,23 +95,12 @@ static void bintree_free_node(struct bintree_node *n)
n->value = NULL; n->value = NULL;
if (n->left) if (n->left)
bintree_free_node(n->left); bintree_free(n->left);
if (n->right) if (n->right)
bintree_free_node(n->right); bintree_free(n->right);
free(n); free(n);
} }
int bintree_insert(bintree_t *t, void *value)
{
return bintree_insert_internal(t, t->root, value);
}
int bintree_iterate(const bintree_t *t, bintree_iter_cb cb,
void *ctx)
{
return bintree_iterate_internal(t->root, cb, ctx);
}
bintree_t *bintree_new(bintree_cmp_func cmp, void *ctx) bintree_t *bintree_new(bintree_cmp_func cmp, void *ctx)
{ {
bintree_t *t = (bintree_t*)malloc(sizeof(*t)); bintree_t *t = (bintree_t*)malloc(sizeof(*t));
@ -142,9 +114,3 @@ bintree_t *bintree_new(bintree_cmp_func cmp, void *ctx)
return t; return t;
} }
void bintree_free(bintree_t *t)
{
if (t && t->root)
bintree_free_node(t->root);
}

View file

@ -27,18 +27,31 @@
RETRO_BEGIN_DECLS RETRO_BEGIN_DECLS
typedef struct bintree bintree_t;
typedef int (*bintree_cmp_func)(const void *a, const void *b, void *ctx); typedef int (*bintree_cmp_func)(const void *a, const void *b, void *ctx);
typedef int (*bintree_iter_cb) (void *value, void *ctx); typedef int (*bintree_iter_cb) (void *value, void *ctx);
typedef struct bintree_node
{
void *value;
struct bintree_node *parent;
struct bintree_node *left;
struct bintree_node *right;
} bintree_node_t;
typedef struct bintree
{
struct bintree_node *root;
void *ctx;
bintree_cmp_func cmp;
} bintree_t;
bintree_t *bintree_new(bintree_cmp_func cmp, void *ctx); bintree_t *bintree_new(bintree_cmp_func cmp, void *ctx);
int bintree_insert(bintree_t *t, void *value); int bintree_insert(bintree_t *t, struct bintree_node *root, void *value);
int bintree_iterate(const bintree_t *t, bintree_iter_cb cb, void *ctx); int bintree_iterate(struct bintree_node *n, bintree_iter_cb cb, void *ctx);
void bintree_free(bintree_t *t); void bintree_free(struct bintree_node *n);
RETRO_END_DECLS RETRO_END_DECLS

View file

@ -507,7 +507,7 @@ int libretrodb_create_index(libretrodb_t *db,
memcpy(buff_u64, &item_loc, sizeof(uint64_t)); memcpy(buff_u64, &item_loc, sizeof(uint64_t));
/* Value is not unique? */ /* Value is not unique? */
if (bintree_insert(tree, buff) != 0) if (bintree_insert(tree, tree->root, buff) != 0)
{ {
rmsgpack_dom_value_print(field); rmsgpack_dom_value_print(field);
goto clean; goto clean;
@ -527,7 +527,7 @@ int libretrodb_create_index(libretrodb_t *db,
nictx.db = db; nictx.db = db;
nictx.idx = &idx; nictx.idx = &idx;
bintree_iterate(tree, node_iter, &nictx); bintree_iterate(tree->root, node_iter, &nictx);
clean: clean:
rmsgpack_dom_value_free(&item); rmsgpack_dom_value_free(&item);
@ -535,8 +535,8 @@ clean:
free(buff); free(buff);
if (cur.is_valid) if (cur.is_valid)
libretrodb_cursor_close(&cur); libretrodb_cursor_close(&cur);
if (tree) if (tree && tree->root)
bintree_free(tree); bintree_free(tree->root);
free(tree); free(tree);
return 0; return 0;
} }