mirror of
https://github.com/DarkPlacesEngine/gmqcc.git
synced 2025-02-21 02:40:56 +00:00
No copy, just store pointer for hash table
This commit is contained in:
parent
58c87ac328
commit
6e047ec65b
2 changed files with 14 additions and 25 deletions
6
gmqcc.h
6
gmqcc.h
|
@ -280,8 +280,8 @@ typedef struct hash_table_t {
|
|||
* ht foo = util_htnew(1024);
|
||||
* int data = 100;
|
||||
* char *test = "hello world\n";
|
||||
* util_htset(foo, "foo", (void*)&data, sizeof(int));
|
||||
* util_gtset(foo, "bar", (void*)test, strlen(test));
|
||||
* util_htset(foo, "foo", (void*)&data);
|
||||
* util_gtset(foo, "bar", (void*)test);
|
||||
*
|
||||
* printf("foo: %d, bar %s",
|
||||
* *((int *)util_htget(foo, "foo")),
|
||||
|
@ -291,7 +291,7 @@ typedef struct hash_table_t {
|
|||
* util_htdel(foo);
|
||||
*/
|
||||
hash_table_t *util_htnew(size_t size);
|
||||
void util_htset(hash_table_t *ht, const char *key, void *value, size_t size);
|
||||
void util_htset(hash_table_t *ht, const char *key, void *value);
|
||||
void *util_htget(hash_table_t *ht, const char *key);
|
||||
void util_htdel(hash_table_t *ht);
|
||||
/*===================================================================*/
|
||||
|
|
33
util.c
33
util.c
|
@ -519,10 +519,9 @@ void _util_vec_grow(void **a, size_t i, size_t s) {
|
|||
* EXPOSED INTERFACE comment below
|
||||
*/
|
||||
typedef struct hash_node_t {
|
||||
char *key; /* the key for this node in table */
|
||||
void *value; /* allocated memory storing data */
|
||||
size_t size; /* size of data */
|
||||
struct hash_node_t *next; /* next node (linked list) */
|
||||
char *key; /* the key for this node in table */
|
||||
void *value; /* pointer to the data as void* */
|
||||
struct hash_node_t *next; /* next node (linked list) */
|
||||
} hash_node_t;
|
||||
|
||||
|
||||
|
@ -542,7 +541,7 @@ size_t _util_hthash(hash_table_t *ht, const char *key) {
|
|||
return val % ht->size;
|
||||
}
|
||||
|
||||
hash_node_t *_util_htnewpair(const char *key, void *value, size_t size) {
|
||||
hash_node_t *_util_htnewpair(const char *key, void *value) {
|
||||
hash_node_t *node;
|
||||
if (!(node = mem_a(sizeof(hash_node_t))))
|
||||
return NULL;
|
||||
|
@ -552,15 +551,8 @@ hash_node_t *_util_htnewpair(const char *key, void *value, size_t size) {
|
|||
return NULL;
|
||||
}
|
||||
|
||||
if (!(node->value = mem_a(size))) {
|
||||
mem_d(node->key);
|
||||
mem_d(node);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memcpy(node->value, value, size);
|
||||
node->size = size;
|
||||
node->next = NULL;
|
||||
node->value = value;
|
||||
node->next = NULL;
|
||||
|
||||
return node;
|
||||
}
|
||||
|
@ -591,7 +583,7 @@ hash_table_t *util_htnew(size_t size) {
|
|||
return hashtable;
|
||||
}
|
||||
|
||||
void util_htset(hash_table_t *ht, const char *key, void *value, size_t size) {
|
||||
void util_htset(hash_table_t *ht, const char *key, void *value) {
|
||||
size_t bin = 0;
|
||||
hash_node_t *newnode = NULL;
|
||||
hash_node_t *next = NULL;
|
||||
|
@ -605,13 +597,10 @@ void util_htset(hash_table_t *ht, const char *key, void *value, size_t size) {
|
|||
|
||||
/* already in table, do a replace */
|
||||
if (next && next->key && !strcmp(key, next->key)) {
|
||||
mem_d(next->value);
|
||||
next->value = mem_a(size);
|
||||
next->size = size;
|
||||
memcpy(next->value, value, size);
|
||||
next->value = value;
|
||||
} else {
|
||||
/* not found, grow a pair man :P */
|
||||
newnode = _util_htnewpair(key, value, size);
|
||||
newnode = _util_htnewpair(key, value);
|
||||
if (next == ht->table[bin]) {
|
||||
newnode->next = next;
|
||||
ht->table[bin] = newnode;
|
||||
|
@ -648,8 +637,8 @@ void util_htdel(hash_table_t *ht) {
|
|||
|
||||
/* free in list */
|
||||
while (n) {
|
||||
if (n->key) mem_d(n->key);
|
||||
if (n->value) mem_d(n->value);
|
||||
if (n->key)
|
||||
mem_d(n->key);
|
||||
n = n->next;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue