|
|
@ -6,185 +6,186 @@
|
|
|
|
#include "map.h"
|
|
|
|
#include "map.h"
|
|
|
|
|
|
|
|
|
|
|
|
struct map_node_t {
|
|
|
|
struct map_node_t {
|
|
|
|
unsigned hash;
|
|
|
|
unsigned hash;
|
|
|
|
void *value;
|
|
|
|
void* value;
|
|
|
|
map_node_t *next;
|
|
|
|
map_node_t* next;
|
|
|
|
/* char key[]; */
|
|
|
|
/* char key[]; */
|
|
|
|
/* char value[]; */
|
|
|
|
/* char value[]; */
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static unsigned map_hash(const char *str) {
|
|
|
|
static unsigned map_hash(const char* str) {
|
|
|
|
unsigned hash = 5381;
|
|
|
|
unsigned hash = 5381;
|
|
|
|
while (*str) {
|
|
|
|
while (*str) {
|
|
|
|
hash = ((hash << 5) + hash) ^ *str++;
|
|
|
|
hash = ((hash << 5) + hash) ^ *str++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static map_node_t *map_newnode(const char *key, void *value, int vsize) {
|
|
|
|
static map_node_t* map_newnode(const char* key, void* value, int vsize) {
|
|
|
|
map_node_t *node;
|
|
|
|
map_node_t* node;
|
|
|
|
int ksize = strlen(key) + 1;
|
|
|
|
int ksize = strlen(key) + 1;
|
|
|
|
int voffset = ksize + ((sizeof(void*) - ksize) % sizeof(void*));
|
|
|
|
int voffset = ksize + ((sizeof(void*) - ksize) % sizeof(void*));
|
|
|
|
node = malloc(sizeof(*node) + voffset + vsize);
|
|
|
|
node = malloc(sizeof(*node) + voffset + vsize);
|
|
|
|
if (!node) return NULL;
|
|
|
|
if (!node) return NULL;
|
|
|
|
memcpy(node + 1, key, ksize);
|
|
|
|
memcpy(node + 1, key, ksize);
|
|
|
|
node->hash = map_hash(key);
|
|
|
|
node->hash = map_hash(key);
|
|
|
|
node->value = ((char*) (node + 1)) + voffset;
|
|
|
|
node->value = ((char*)(node + 1)) + voffset;
|
|
|
|
memcpy(node->value, value, vsize);
|
|
|
|
memcpy(node->value, value, vsize);
|
|
|
|
return node;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int map_bucketidx(map_base_t *m, unsigned hash) {
|
|
|
|
static int map_bucketidx(map_base_t* m, unsigned hash) {
|
|
|
|
/* If the implementation is changed to allow a non-power-of-2 bucket count,
|
|
|
|
/* If the implementation is changed to allow a non-power-of-2 bucket count,
|
|
|
|
* the line below should be changed to use mod instead of AND */
|
|
|
|
* the line below should be changed to use mod instead of AND */
|
|
|
|
return hash & (m->nbuckets - 1);
|
|
|
|
return hash & (m->nbuckets - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void map_addnode(map_base_t *m, map_node_t *node) {
|
|
|
|
static void map_addnode(map_base_t* m, map_node_t* node) {
|
|
|
|
int n = map_bucketidx(m, node->hash);
|
|
|
|
int n = map_bucketidx(m, node->hash);
|
|
|
|
node->next = m->buckets[n];
|
|
|
|
node->next = m->buckets[n];
|
|
|
|
m->buckets[n] = node;
|
|
|
|
m->buckets[n] = node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int map_resize(map_base_t *m, int nbuckets) {
|
|
|
|
static int map_resize(map_base_t* m, int nbuckets) {
|
|
|
|
map_node_t *nodes, *node, *next;
|
|
|
|
map_node_t* nodes, * node, * next;
|
|
|
|
map_node_t **buckets;
|
|
|
|
map_node_t** buckets;
|
|
|
|
int i;
|
|
|
|
int i;
|
|
|
|
/* Chain all nodes together */
|
|
|
|
/* Chain all nodes together */
|
|
|
|
nodes = NULL;
|
|
|
|
nodes = NULL;
|
|
|
|
i = m->nbuckets;
|
|
|
|
i = m->nbuckets;
|
|
|
|
while (i--) {
|
|
|
|
while (i--) {
|
|
|
|
node = (m->buckets)[i];
|
|
|
|
node = (m->buckets)[i];
|
|
|
|
while (node) {
|
|
|
|
while (node) {
|
|
|
|
next = node->next;
|
|
|
|
next = node->next;
|
|
|
|
node->next = nodes;
|
|
|
|
node->next = nodes;
|
|
|
|
nodes = node;
|
|
|
|
nodes = node;
|
|
|
|
node = next;
|
|
|
|
node = next;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset buckets */
|
|
|
|
|
|
|
|
buckets = realloc(m->buckets, sizeof(*m->buckets) * nbuckets);
|
|
|
|
|
|
|
|
if (buckets != NULL) {
|
|
|
|
|
|
|
|
m->buckets = buckets;
|
|
|
|
|
|
|
|
m->nbuckets = nbuckets;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (m->buckets) {
|
|
|
|
/* Reset buckets */
|
|
|
|
memset(m->buckets, 0, sizeof(*m->buckets) * m->nbuckets);
|
|
|
|
buckets = realloc(m->buckets, sizeof(*m->buckets) * nbuckets);
|
|
|
|
/* Re-add nodes to buckets */
|
|
|
|
if (buckets != NULL) {
|
|
|
|
node = nodes;
|
|
|
|
m->buckets = buckets;
|
|
|
|
while (node) {
|
|
|
|
m->nbuckets = nbuckets;
|
|
|
|
next = node->next;
|
|
|
|
}
|
|
|
|
map_addnode(m, node);
|
|
|
|
if (m->buckets) {
|
|
|
|
node = next;
|
|
|
|
memset(m->buckets, 0, sizeof(*m->buckets) * m->nbuckets);
|
|
|
|
}
|
|
|
|
/* Re-add nodes to buckets */
|
|
|
|
|
|
|
|
node = nodes;
|
|
|
|
|
|
|
|
while (node) {
|
|
|
|
|
|
|
|
next = node->next;
|
|
|
|
|
|
|
|
map_addnode(m, node);
|
|
|
|
|
|
|
|
node = next;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Return error code if realloc() failed */
|
|
|
|
/* Return error code if realloc() failed */
|
|
|
|
return (buckets == NULL) ? -1 : 0;
|
|
|
|
return (buckets == NULL) ? -1 : 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static map_node_t **map_getref(map_base_t *m, const char *key) {
|
|
|
|
static map_node_t** map_getref(map_base_t* m, const char* key) {
|
|
|
|
unsigned hash = map_hash(key);
|
|
|
|
unsigned hash = map_hash(key);
|
|
|
|
map_node_t **next;
|
|
|
|
map_node_t** next;
|
|
|
|
if (m->nbuckets > 0) {
|
|
|
|
if (m->nbuckets > 0) {
|
|
|
|
next = &m->buckets[map_bucketidx(m, hash)];
|
|
|
|
next = &m->buckets[map_bucketidx(m, hash)];
|
|
|
|
while (*next) {
|
|
|
|
while (*next) {
|
|
|
|
if ((*next)->hash == hash && !strcmp((char*) (*next + 1), key)) {
|
|
|
|
if ((*next)->hash == hash && !strcmp((char*)(*next + 1), key)) {
|
|
|
|
return next;
|
|
|
|
return next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
next = &(*next)->next;
|
|
|
|
next = &(*next)->next;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void map_deinit_(map_base_t *m) {
|
|
|
|
void map_deinit_(map_base_t* m) {
|
|
|
|
map_node_t *next, *node;
|
|
|
|
map_node_t* next, * node;
|
|
|
|
int i;
|
|
|
|
int i;
|
|
|
|
i = m->nbuckets;
|
|
|
|
i = m->nbuckets;
|
|
|
|
while (i--) {
|
|
|
|
while (i--) {
|
|
|
|
node = m->buckets[i];
|
|
|
|
node = m->buckets[i];
|
|
|
|
while (node) {
|
|
|
|
while (node) {
|
|
|
|
next = node->next;
|
|
|
|
next = node->next;
|
|
|
|
free(node);
|
|
|
|
free(node);
|
|
|
|
node = next;
|
|
|
|
node = next;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(m->buckets);
|
|
|
|
free(m->buckets);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void *map_get_(map_base_t *m, const char *key) {
|
|
|
|
void* map_get_(map_base_t* m, const char* key) {
|
|
|
|
map_node_t **next = map_getref(m, key);
|
|
|
|
map_node_t** next = map_getref(m, key);
|
|
|
|
return next ? (*next)->value : NULL;
|
|
|
|
return next ? (*next)->value : NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int map_set_(map_base_t *m, const char *key, void *value, int vsize) {
|
|
|
|
int map_set_(map_base_t* m, const char* key, void* value, int vsize) {
|
|
|
|
int n, err;
|
|
|
|
int n, err;
|
|
|
|
map_node_t **next, *node;
|
|
|
|
map_node_t** next, * node;
|
|
|
|
/* Find & replace existing node */
|
|
|
|
/* Find & replace existing node */
|
|
|
|
next = map_getref(m, key);
|
|
|
|
next = map_getref(m, key);
|
|
|
|
if (next) {
|
|
|
|
if (next) {
|
|
|
|
memcpy((*next)->value, value, vsize);
|
|
|
|
memcpy((*next)->value, value, vsize);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add new node */
|
|
|
|
|
|
|
|
node = map_newnode(key, value, vsize);
|
|
|
|
|
|
|
|
if (node == NULL) goto fail;
|
|
|
|
|
|
|
|
if (m->nnodes >= m->nbuckets) {
|
|
|
|
|
|
|
|
n = (m->nbuckets > 0) ? (m->nbuckets << 1) : 1;
|
|
|
|
|
|
|
|
err = map_resize(m, n);
|
|
|
|
|
|
|
|
if (err) goto fail;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
map_addnode(m, node);
|
|
|
|
|
|
|
|
m->nnodes++;
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
fail:
|
|
|
|
/* Add new node */
|
|
|
|
if (node) free(node);
|
|
|
|
node = map_newnode(key, value, vsize);
|
|
|
|
return -1;
|
|
|
|
if (node == NULL) goto fail;
|
|
|
|
|
|
|
|
if (m->nnodes >= m->nbuckets) {
|
|
|
|
|
|
|
|
n = (m->nbuckets > 0) ? (m->nbuckets << 1) : 1;
|
|
|
|
|
|
|
|
err = map_resize(m, n);
|
|
|
|
|
|
|
|
if (err) goto fail;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
map_addnode(m, node);
|
|
|
|
|
|
|
|
m->nnodes++;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
|
|
|
|
if (node) free(node);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void map_remove_(map_base_t *m, const char *key) {
|
|
|
|
void map_remove_(map_base_t* m, const char* key) {
|
|
|
|
map_node_t *node;
|
|
|
|
map_node_t* node;
|
|
|
|
map_node_t **next = map_getref(m, key);
|
|
|
|
map_node_t** next = map_getref(m, key);
|
|
|
|
if (next) {
|
|
|
|
if (next) {
|
|
|
|
node = *next;
|
|
|
|
node = *next;
|
|
|
|
*next = (*next)->next;
|
|
|
|
*next = (*next)->next;
|
|
|
|
free(node);
|
|
|
|
free(node);
|
|
|
|
m->nnodes--;
|
|
|
|
m->nnodes--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
map_iter_t map_iter_(void) {
|
|
|
|
map_iter_t map_iter_(void) {
|
|
|
|
map_iter_t iter;
|
|
|
|
map_iter_t iter;
|
|
|
|
iter.bucketidx = -1;
|
|
|
|
iter.bucketidx = -1;
|
|
|
|
iter.node = NULL;
|
|
|
|
iter.node = NULL;
|
|
|
|
return iter;
|
|
|
|
return iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const char *map_next_(map_base_t *m, map_iter_t *iter) {
|
|
|
|
const char* map_next_(map_base_t* m, map_iter_t* iter) {
|
|
|
|
if (iter->node) {
|
|
|
|
if (iter->node) {
|
|
|
|
iter->node = iter->node->next;
|
|
|
|
iter->node = iter->node->next;
|
|
|
|
if (iter->node == NULL) goto nextBucket;
|
|
|
|
if (iter->node == NULL) goto nextBucket;
|
|
|
|
} else {
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
nextBucket:
|
|
|
|
nextBucket:
|
|
|
|
do {
|
|
|
|
do {
|
|
|
|
if (++iter->bucketidx >= m->nbuckets) {
|
|
|
|
if (++iter->bucketidx >= m->nbuckets) {
|
|
|
|
return NULL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
iter->node = m->buckets[iter->bucketidx];
|
|
|
|
iter->node = m->buckets[iter->bucketidx];
|
|
|
|
} while (iter->node == NULL);
|
|
|
|
} while (iter->node == NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (char*) (iter->node + 1);
|
|
|
|
return (char*)(iter->node + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|