diff --git a/libretro-db/bintree.c b/libretro-db/bintree.c index 225797e1cf..313c7fe541 100644 --- a/libretro-db/bintree.c +++ b/libretro-db/bintree.c @@ -27,21 +27,6 @@ #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 struct bintree_node *bintree_new_nil_node( @@ -61,8 +46,7 @@ static struct bintree_node *bintree_new_nil_node( return node; } -static int bintree_insert_internal(bintree_t *t, - struct bintree_node *root, void *value) +int bintree_insert(bintree_t *t, struct bintree_node *root, void *value) { 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); if (cmp_res > 0) - return bintree_insert_internal(t, root->left, value); + return bintree_insert(t, root->left, value); else if (cmp_res < 0) - return bintree_insert_internal(t, root->right, value); + return bintree_insert(t, root->right, value); return -1; } -static int bintree_iterate_internal(struct bintree_node *n, - bintree_iter_cb cb, void *ctx) +int bintree_iterate(struct bintree_node *n, bintree_iter_cb cb, void *ctx) { int rv; if (!n || (n->value == NIL_NODE)) return 0; - if ((rv = bintree_iterate_internal(n->left, cb, ctx)) != 0) + if ((rv = bintree_iterate(n->left, cb, ctx)) != 0) return rv; if ((rv = cb(n->value, ctx)) != 0) return rv; - if ((rv = bintree_iterate_internal(n->right, cb, ctx)) != 0) + if ((rv = bintree_iterate(n->right, cb, ctx)) != 0) return rv; return 0; } -static void bintree_free_node(struct bintree_node *n) +void bintree_free(struct bintree_node *n) { if (n->value == NIL_NODE) { @@ -112,23 +95,12 @@ static void bintree_free_node(struct bintree_node *n) n->value = NULL; if (n->left) - bintree_free_node(n->left); + bintree_free(n->left); if (n->right) - bintree_free_node(n->right); + bintree_free(n->right); 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 *t = (bintree_t*)malloc(sizeof(*t)); @@ -142,9 +114,3 @@ bintree_t *bintree_new(bintree_cmp_func cmp, void *ctx) return t; } - -void bintree_free(bintree_t *t) -{ - if (t && t->root) - bintree_free_node(t->root); -} diff --git a/libretro-db/bintree.h b/libretro-db/bintree.h index 586714ac47..84d71c846e 100644 --- a/libretro-db/bintree.h +++ b/libretro-db/bintree.h @@ -27,18 +27,31 @@ RETRO_BEGIN_DECLS -typedef struct bintree bintree_t; - typedef int (*bintree_cmp_func)(const void *a, const void *b, 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); -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 diff --git a/libretro-db/libretrodb.c b/libretro-db/libretrodb.c index e0a8332ba6..379973512a 100644 --- a/libretro-db/libretrodb.c +++ b/libretro-db/libretrodb.c @@ -507,7 +507,7 @@ int libretrodb_create_index(libretrodb_t *db, memcpy(buff_u64, &item_loc, sizeof(uint64_t)); /* Value is not unique? */ - if (bintree_insert(tree, buff) != 0) + if (bintree_insert(tree, tree->root, buff) != 0) { rmsgpack_dom_value_print(field); goto clean; @@ -527,7 +527,7 @@ int libretrodb_create_index(libretrodb_t *db, nictx.db = db; nictx.idx = &idx; - bintree_iterate(tree, node_iter, &nictx); + bintree_iterate(tree->root, node_iter, &nictx); clean: rmsgpack_dom_value_free(&item); @@ -535,8 +535,8 @@ clean: free(buff); if (cur.is_valid) libretrodb_cursor_close(&cur); - if (tree) - bintree_free(tree); + if (tree && tree->root) + bintree_free(tree->root); free(tree); return 0; }