mirror of
https://github.com/libretro/RetroArch
synced 2025-02-03 17:54:04 +00:00
(libretro-db) Cleanup bintree.c
This commit is contained in:
parent
ff17917a90
commit
c8535dce1f
@ -1,24 +1,32 @@
|
|||||||
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stdio.h>
|
|
||||||
#include <retro_inline.h>
|
#include <retro_inline.h>
|
||||||
|
|
||||||
#include "bintree.h"
|
#include "bintree.h"
|
||||||
|
|
||||||
static void *NIL_NODE = &NIL_NODE;
|
struct bintree_node
|
||||||
|
|
||||||
static struct bintree_node *new_nil_node(struct bintree_node *parent);
|
|
||||||
|
|
||||||
void bintree_new(struct bintree *t, bintree_cmp_func cmp, void *ctx)
|
|
||||||
{
|
{
|
||||||
t->root = new_nil_node(NULL);
|
void *value;
|
||||||
t->cmp = cmp;
|
struct bintree_node *parent;
|
||||||
t->ctx = ctx;
|
struct bintree_node *left;
|
||||||
}
|
struct bintree_node *right;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct bintree
|
||||||
|
{
|
||||||
|
struct bintree_node *root;
|
||||||
|
bintree_cmp_func cmp;
|
||||||
|
void *ctx;
|
||||||
|
} bintree_t;
|
||||||
|
|
||||||
|
static void *NIL_NODE = &NIL_NODE;
|
||||||
|
|
||||||
static struct bintree_node *new_nil_node(struct bintree_node *parent)
|
static struct bintree_node *new_nil_node(struct bintree_node *parent)
|
||||||
{
|
{
|
||||||
struct bintree_node *node = (struct bintree_node *)calloc(1, sizeof(struct bintree_node));
|
struct bintree_node *node = (struct bintree_node *)
|
||||||
|
calloc(1, sizeof(struct bintree_node));
|
||||||
|
|
||||||
if (!node)
|
if (!node)
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -34,7 +42,7 @@ static INLINE int is_nil(const struct bintree_node *node)
|
|||||||
return (node == NULL) || (node->value == NIL_NODE);
|
return (node == NULL) || (node->value == NIL_NODE);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int insert(struct bintree *t, struct bintree_node *root, void *value)
|
static int insert(bintree_t *t, struct bintree_node *root, void *value)
|
||||||
{
|
{
|
||||||
int cmp_res = 0;
|
int cmp_res = 0;
|
||||||
|
|
||||||
@ -71,10 +79,6 @@ static int insert(struct bintree *t, struct bintree_node *root, void *value)
|
|||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int bintree_insert(struct bintree *t, void *value)
|
|
||||||
{
|
|
||||||
return insert(t, t->root, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int _bintree_iterate(struct bintree_node *n,
|
static int _bintree_iterate(struct bintree_node *n,
|
||||||
bintree_iter_cb cb, void *ctx)
|
bintree_iter_cb cb, void *ctx)
|
||||||
@ -94,12 +98,6 @@ static int _bintree_iterate(struct bintree_node *n,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int bintree_iterate(const struct bintree *t, bintree_iter_cb cb,
|
|
||||||
void *ctx)
|
|
||||||
{
|
|
||||||
return _bintree_iterate(t->root, cb, ctx);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void bintree_free_node(struct bintree_node *n)
|
static void bintree_free_node(struct bintree_node *n)
|
||||||
{
|
{
|
||||||
if (!n)
|
if (!n)
|
||||||
@ -117,7 +115,32 @@ static void bintree_free_node(struct bintree_node *n)
|
|||||||
free(n);
|
free(n);
|
||||||
}
|
}
|
||||||
|
|
||||||
void bintree_free(struct bintree *t)
|
int bintree_insert(bintree_t *t, void *value)
|
||||||
|
{
|
||||||
|
return insert(t, t->root, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
int bintree_iterate(const bintree_t *t, bintree_iter_cb cb,
|
||||||
|
void *ctx)
|
||||||
|
{
|
||||||
|
return _bintree_iterate(t->root, cb, ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
bintree_t *bintree_new(bintree_cmp_func cmp, void *ctx)
|
||||||
|
{
|
||||||
|
bintree_t *t = (bintree_t*)calloc(1, sizeof(*t));
|
||||||
|
|
||||||
|
if (!t)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
t->root = new_nil_node(NULL);
|
||||||
|
t->cmp = cmp;
|
||||||
|
t->ctx = ctx;
|
||||||
|
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
void bintree_free(bintree_t *t)
|
||||||
{
|
{
|
||||||
bintree_free_node(t->root);
|
bintree_free_node(t->root);
|
||||||
}
|
}
|
||||||
|
@ -1,45 +1,25 @@
|
|||||||
#ifndef __RARCHDB_BINTREE_H__
|
#ifndef __RARCHDB_BINTREE_H__
|
||||||
#define __RARCHDB_BINTREE_H__
|
#define __RARCHDB_BINTREE_H__
|
||||||
|
|
||||||
typedef int (* bintree_cmp_func)(
|
#ifdef __cplusplus
|
||||||
const void * a,
|
extern "C" {
|
||||||
const void * b,
|
#endif
|
||||||
void * ctx
|
|
||||||
);
|
|
||||||
|
|
||||||
typedef int (* bintree_iter_cb)(
|
typedef struct bintree bintree_t;
|
||||||
void * value,
|
|
||||||
void * ctx
|
|
||||||
);
|
|
||||||
|
|
||||||
|
typedef int (*bintree_cmp_func)(const void *a, const void *b, void *ctx);
|
||||||
|
typedef int (*bintree_iter_cb) (void *value, void *ctx);
|
||||||
|
|
||||||
struct bintree_node {
|
bintree_t *bintree_new(bintree_cmp_func cmp, void *ctx);
|
||||||
void * value;
|
|
||||||
struct bintree_node * parent;
|
|
||||||
struct bintree_node * left;
|
|
||||||
struct bintree_node * right;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct bintree {
|
int bintree_insert(bintree_t *t, void *value);
|
||||||
struct bintree_node * root;
|
|
||||||
bintree_cmp_func cmp;
|
|
||||||
void * ctx;
|
|
||||||
};
|
|
||||||
|
|
||||||
void bintree_new(
|
int bintree_iterate(const bintree_t *t, bintree_iter_cb cb, void *ctx);
|
||||||
struct bintree * t,
|
|
||||||
bintree_cmp_func cmp,
|
void bintree_free(bintree_t *t);
|
||||||
void * ctx
|
|
||||||
);
|
#ifdef __cplusplus
|
||||||
int bintree_insert(
|
}
|
||||||
struct bintree * t,
|
#endif
|
||||||
void * value
|
|
||||||
);
|
|
||||||
int bintree_iterate(
|
|
||||||
const struct bintree * t,
|
|
||||||
bintree_iter_cb cb,
|
|
||||||
void * ctx
|
|
||||||
);
|
|
||||||
void bintree_free(struct bintree * t);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -80,8 +80,8 @@ int libretrodb_create(int fd, libretrodb_value_provider value_provider,
|
|||||||
int rv;
|
int rv;
|
||||||
off_t root;
|
off_t root;
|
||||||
libretrodb_metadata_t md;
|
libretrodb_metadata_t md;
|
||||||
|
struct rmsgpack_dom_value item;
|
||||||
uint64_t item_count = 0;
|
uint64_t item_count = 0;
|
||||||
struct rmsgpack_dom_value item = {0};
|
|
||||||
libretrodb_header_t header = {{0}};
|
libretrodb_header_t header = {{0}};
|
||||||
|
|
||||||
memcpy(header.magic_number, MAGIC_NUMBER, sizeof(MAGIC_NUMBER)-1);
|
memcpy(header.magic_number, MAGIC_NUMBER, sizeof(MAGIC_NUMBER)-1);
|
||||||
@ -409,15 +409,19 @@ int libretrodb_create_index(libretrodb_t *db,
|
|||||||
libretrodb_index_t idx;
|
libretrodb_index_t idx;
|
||||||
struct rmsgpack_dom_value item;
|
struct rmsgpack_dom_value item;
|
||||||
struct rmsgpack_dom_value * field;
|
struct rmsgpack_dom_value * field;
|
||||||
struct bintree tree;
|
|
||||||
libretrodb_cursor_t cur;
|
|
||||||
uint64_t idx_header_offset;
|
uint64_t idx_header_offset;
|
||||||
|
libretrodb_cursor_t cur = {0};
|
||||||
void * buff = NULL;
|
void * buff = NULL;
|
||||||
uint64_t * buff_u64 = NULL;
|
uint64_t * buff_u64 = NULL;
|
||||||
uint8_t field_size = 0;
|
uint8_t field_size = 0;
|
||||||
uint64_t item_loc = libretrodb_tell(db);
|
uint64_t item_loc = libretrodb_tell(db);
|
||||||
|
bintree_t *tree = bintree_new(node_compare, &field_size);
|
||||||
|
|
||||||
bintree_new(&tree, node_compare, &field_size);
|
if (!tree)
|
||||||
|
{
|
||||||
|
rv = -1;
|
||||||
|
goto clean;
|
||||||
|
}
|
||||||
|
|
||||||
if (libretrodb_cursor_open(db, &cur, NULL) != 0)
|
if (libretrodb_cursor_open(db, &cur, NULL) != 0)
|
||||||
{
|
{
|
||||||
@ -485,7 +489,7 @@ int libretrodb_create_index(libretrodb_t *db,
|
|||||||
|
|
||||||
memcpy(buff_u64, &item_loc, sizeof(uint64_t));
|
memcpy(buff_u64, &item_loc, sizeof(uint64_t));
|
||||||
|
|
||||||
if (bintree_insert(&tree, buff) != 0)
|
if (bintree_insert(tree, buff) != 0)
|
||||||
{
|
{
|
||||||
printf("Value is not unique: ");
|
printf("Value is not unique: ");
|
||||||
rmsgpack_dom_value_print(field);
|
rmsgpack_dom_value_print(field);
|
||||||
@ -511,8 +515,8 @@ 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, node_iter, &nictx);
|
||||||
bintree_free(&tree);
|
bintree_free(tree);
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rmsgpack_dom_value_free(&item);
|
rmsgpack_dom_value_free(&item);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user