(libretro-db) Cleanup bintree.c

This commit is contained in:
twinaphex 2015-09-17 09:25:06 +02:00
parent ff17917a90
commit c8535dce1f
3 changed files with 77 additions and 70 deletions

View File

@ -1,24 +1,32 @@
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <retro_inline.h>
#include "bintree.h"
static void *NIL_NODE = &NIL_NODE;
static struct bintree_node *new_nil_node(struct bintree_node *parent);
void bintree_new(struct bintree *t, bintree_cmp_func cmp, void *ctx)
struct bintree_node
{
t->root = new_nil_node(NULL);
t->cmp = cmp;
t->ctx = ctx;
}
void *value;
struct bintree_node *parent;
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)
{
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)
return NULL;
@ -34,7 +42,7 @@ static INLINE int is_nil(const struct bintree_node *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;
@ -71,10 +79,6 @@ static int insert(struct bintree *t, struct bintree_node *root, void *value)
return -EINVAL;
}
int bintree_insert(struct bintree *t, void *value)
{
return insert(t, t->root, value);
}
static int _bintree_iterate(struct bintree_node *n,
bintree_iter_cb cb, void *ctx)
@ -94,12 +98,6 @@ static int _bintree_iterate(struct bintree_node *n,
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)
{
if (!n)
@ -117,7 +115,32 @@ static void bintree_free_node(struct bintree_node *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);
}

View File

@ -1,45 +1,25 @@
#ifndef __RARCHDB_BINTREE_H__
#define __RARCHDB_BINTREE_H__
typedef int (* bintree_cmp_func)(
const void * a,
const void * b,
void * ctx
);
#ifdef __cplusplus
extern "C" {
#endif
typedef int (* bintree_iter_cb)(
void * value,
void * ctx
);
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);
struct bintree_node {
void * value;
struct bintree_node * parent;
struct bintree_node * left;
struct bintree_node * right;
};
bintree_t *bintree_new(bintree_cmp_func cmp, void *ctx);
struct bintree {
struct bintree_node * root;
bintree_cmp_func cmp;
void * ctx;
};
int bintree_insert(bintree_t *t, void *value);
void bintree_new(
struct bintree * t,
bintree_cmp_func cmp,
void * ctx
);
int bintree_insert(
struct bintree * t,
void * value
);
int bintree_iterate(
const struct bintree * t,
bintree_iter_cb cb,
void * ctx
);
void bintree_free(struct bintree * t);
int bintree_iterate(const bintree_t *t, bintree_iter_cb cb, void *ctx);
void bintree_free(bintree_t *t);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -80,8 +80,8 @@ int libretrodb_create(int fd, libretrodb_value_provider value_provider,
int rv;
off_t root;
libretrodb_metadata_t md;
uint64_t item_count = 0;
struct rmsgpack_dom_value item = {0};
struct rmsgpack_dom_value item;
uint64_t item_count = 0;
libretrodb_header_t header = {{0}};
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;
struct rmsgpack_dom_value item;
struct rmsgpack_dom_value * field;
struct bintree tree;
libretrodb_cursor_t cur;
uint64_t idx_header_offset;
void * buff = NULL;
uint64_t * buff_u64 = NULL;
uint8_t field_size = 0;
uint64_t item_loc = libretrodb_tell(db);
libretrodb_cursor_t cur = {0};
void * buff = NULL;
uint64_t * buff_u64 = NULL;
uint8_t field_size = 0;
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)
{
@ -485,7 +489,7 @@ int libretrodb_create_index(libretrodb_t *db,
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: ");
rmsgpack_dom_value_print(field);
@ -511,8 +515,8 @@ int libretrodb_create_index(libretrodb_t *db,
nictx.db = db;
nictx.idx = &idx;
bintree_iterate(&tree, node_iter, &nictx);
bintree_free(&tree);
bintree_iterate(tree, node_iter, &nictx);
bintree_free(tree);
clean:
rmsgpack_dom_value_free(&item);