diff options
author | Ben Longbons <b.r.longbons@gmail.com> | 2011-09-10 16:12:07 -0700 |
---|---|---|
committer | Ben Longbons <b.r.longbons@gmail.com> | 2011-09-10 16:12:07 -0700 |
commit | f841b6fdcc802e73d52da0e67ee192c0c2c1c7e1 (patch) | |
tree | d9b013ab252968ec1e90e721f7b2ab819af0acb0 /src/common/db.h | |
parent | 5939e1bec75f2550d3ce109b9cd9a5d22c0626c2 (diff) | |
parent | 723fb5d3431b847526c433a13aa74485cfb564a3 (diff) | |
download | tmwa-f841b6fdcc802e73d52da0e67ee192c0c2c1c7e1.tar.gz tmwa-f841b6fdcc802e73d52da0e67ee192c0c2c1c7e1.tar.bz2 tmwa-f841b6fdcc802e73d52da0e67ee192c0c2c1c7e1.tar.xz tmwa-f841b6fdcc802e73d52da0e67ee192c0c2c1c7e1.zip |
Merge commit '723fb5d3431b847526c433a13aa74485cfb564a3'
Diffstat (limited to 'src/common/db.h')
-rw-r--r-- | src/common/db.h | 102 |
1 files changed, 70 insertions, 32 deletions
diff --git a/src/common/db.h b/src/common/db.h index 8de32d5..7152854 100644 --- a/src/common/db.h +++ b/src/common/db.h @@ -1,49 +1,87 @@ -#ifndef _DB_H_ -#define _DB_H_ +// WARNING: there is a system header by this name +#ifndef DB_H +#define DB_H +# include "sanity.h" -#include <stdarg.h> +# include <stdarg.h> -#define HASH_SIZE (256+27) +/// Number of tree roots +// Somewhat arbitrary - larger wastes more space but is faster for large trees +// num % HASH_SIZE minimize collisions even for similar num +# define HASH_SIZE (256+27) -#define RED 0 -#define BLACK 1 +typedef enum dbn_color +{ + RED, + BLACK +} dbn_color; + +typedef intptr_t numdb_key_t; +typedef union db_key_t +{ + char *ms __attribute__((deprecated)); + const char* s; + numdb_key_t i; +} db_key_t; +typedef void* db_val_t; +typedef uint32_t hash_t; +typedef void (*db_func_t)(db_key_t, db_val_t, va_list); +/// DataBase Node struct dbn { struct dbn *parent, *left, *right; - int color; - void *key; - void *data; + dbn_color color; + db_key_t key; + db_val_t data; }; +typedef enum dbt_type +{ + DB_NUMBER, + DB_STRING, +} dbt_type; + +/// DataBase Table struct dbt { - int (*cmp) (struct dbt *, void *, void *); - unsigned int (*hash) (struct dbt *, void *); - // which 1 - key, 2 - data, 3 - both - void (*release) (struct dbn *, int which); - int maxlen; + dbt_type type; + /// Note, before replacement, key/values to be replaced + // TODO refactor to decrease/eliminate the uses of this? + void (*release) (db_key_t, db_val_t) __attribute__((deprecated)); + /// Maximum length of a string key - TODO refactor to ensure all strings are NUL-terminated + size_t maxlen __attribute__((deprecated)); + /// The root trees struct dbn *ht[HASH_SIZE]; }; -#define strdb_search(t,k) db_search((t),(void*)(k)) -#define strdb_insert(t,k,d) db_insert((t),(void*)(k),(void*)(d)) -#define strdb_erase(t,k) db_erase ((t),(void*)(k)) -#define strdb_foreach db_foreach -#define strdb_final db_final -#define numdb_search(t,k) db_search((t),(void*)(k)) -#define numdb_insert(t,k,d) db_insert((t),(void*)(k),(void*)(d)) -#define numdb_erase(t,k) db_erase ((t),(void*)(k)) -#define numdb_foreach db_foreach -#define numdb_final db_final - -struct dbt *strdb_init (int maxlen); +# define strdb_search(t,k) db_search((t),(db_key_t)(k)) +# define strdb_insert(t,k,d) db_insert((t),(db_key_t)(k),(db_val_t)(d)) +# define strdb_erase(t,k) db_erase ((t),(db_key_t)(k)) +# define strdb_foreach db_foreach +# define strdb_final db_final +# define numdb_search(t,k) db_search((t),(db_key_t)(k)) +# define numdb_insert(t,k,d) db_insert((t),(db_key_t)(k),(db_val_t)(d)) +# define numdb_erase(t,k) db_erase ((t),(db_key_t)(k)) +# define numdb_foreach db_foreach +# define numdb_final db_final + +/// Create a map from char* to void*, with strings possibly not null-terminated +struct dbt *strdb_init (size_t maxlen); +/// Create a map from int to void* struct dbt *numdb_init (void); -void *db_search (struct dbt *table, void *key); -void *db_search2 (struct dbt *table, const char *key); // [MouseJstr] -struct dbn *db_insert (struct dbt *table, void *key, void *data); -void *db_erase (struct dbt *table, void *key); -void db_foreach (struct dbt *, int (*)(void *, void *, va_list), ...); -void db_final (struct dbt *, int (*)(void *, void *, va_list), ...); +/// Return the value corresponding to the key, or NULL if not found +db_val_t db_search (struct dbt *table, db_key_t key); +/// Add or replace table[key] = data +// if it was already there, call release +struct dbn *db_insert (struct dbt *table, db_key_t key, db_val_t data); +/// Remove a key from the table, returning the data +db_val_t db_erase (struct dbt *table, db_key_t key); + +/// Execute a function for every element, in unspecified order +void db_foreach (struct dbt *, db_func_t, ...); +// opposite of init? Calls release for every element and frees memory +// This probably isn't really needed: we don't have to free memory while exiting +void db_final (struct dbt *, db_func_t, ...) __attribute__((deprecated)); #endif |