qse/ase/awk/map.c

279 lines
5.3 KiB
C
Raw Normal View History

2006-03-02 15:10:59 +00:00
/*
2006-10-22 11:34:53 +00:00
* $Id: map.c,v 1.27 2006-10-22 11:34:53 bacon Exp $
2006-03-02 15:10:59 +00:00
*/
2006-10-22 11:34:53 +00:00
#include <sse/awk/awk_i.h>
2006-03-02 15:10:59 +00:00
2006-04-14 10:56:42 +00:00
/* TODO: improve the entire map routines.
support automatic bucket resizing and remaping, etc. */
2006-03-02 15:10:59 +00:00
2006-10-22 11:34:53 +00:00
static sse_size_t __hash (const sse_char_t* key, sse_size_t key_len);
2006-03-02 15:10:59 +00:00
#define FREE_PAIR(map,pair) \
2006-05-03 16:07:24 +00:00
do { \
2006-10-22 11:34:53 +00:00
SSE_AWK_FREE ((map)->awk, (sse_char_t*)(pair)->key); \
if ((map)->freeval != SSE_NULL) \
2006-04-21 17:24:31 +00:00
(map)->freeval ((map)->owner, (pair)->val); \
2006-10-22 11:34:53 +00:00
SSE_AWK_FREE ((map)->awk, pair); \
2006-03-02 15:10:59 +00:00
} while (0)
2006-10-22 11:34:53 +00:00
sse_awk_map_t* sse_awk_map_open (
sse_awk_map_t* map, void* owner, sse_size_t capa,
void(*freeval)(void*,void*), sse_awk_t* awk)
2006-03-02 15:10:59 +00:00
{
2006-10-22 11:34:53 +00:00
if (map == SSE_NULL)
2006-03-07 15:55:14 +00:00
{
2006-10-22 11:34:53 +00:00
map = (sse_awk_map_t*) SSE_AWK_MALLOC (
awk, sse_sizeof(sse_awk_map_t));
if (map == SSE_NULL) return SSE_NULL;
map->__dynamic = sse_true;
2006-03-02 15:10:59 +00:00
}
2006-10-22 11:34:53 +00:00
else map->__dynamic = sse_false;
2006-03-02 15:10:59 +00:00
2006-09-01 03:44:51 +00:00
map->awk = awk;
2006-10-22 11:34:53 +00:00
map->buck = (sse_awk_pair_t**)
SSE_AWK_MALLOC (awk, sse_sizeof(sse_awk_pair_t*) * capa);
if (map->buck == SSE_NULL)
2006-03-07 15:55:14 +00:00
{
2006-10-22 11:34:53 +00:00
if (map->__dynamic) SSE_AWK_FREE (awk, map);
return SSE_NULL;
2006-03-02 15:10:59 +00:00
}
2006-04-21 17:24:31 +00:00
map->owner = owner;
2006-03-02 15:10:59 +00:00
map->capa = capa;
map->size = 0;
2006-03-05 17:07:33 +00:00
map->freeval = freeval;
2006-10-22 11:34:53 +00:00
while (capa > 0) map->buck[--capa] = SSE_NULL;
2006-03-02 15:10:59 +00:00
return map;
}
2006-10-22 11:34:53 +00:00
void sse_awk_map_close (sse_awk_map_t* map)
2006-03-02 15:10:59 +00:00
{
2006-10-22 11:34:53 +00:00
sse_awk_map_clear (map);
SSE_AWK_FREE (map->awk, map->buck);
if (map->__dynamic) SSE_AWK_FREE (map->awk, map);
2006-03-02 15:10:59 +00:00
}
2006-10-22 11:34:53 +00:00
void sse_awk_map_clear (sse_awk_map_t* map)
2006-03-02 15:10:59 +00:00
{
2006-10-22 11:34:53 +00:00
sse_size_t i;
sse_awk_pair_t* pair, * next;
2006-03-02 15:10:59 +00:00
2006-03-07 15:55:14 +00:00
for (i = 0; i < map->capa; i++)
{
2006-03-02 15:10:59 +00:00
pair = map->buck[i];
2006-10-22 11:34:53 +00:00
while (pair != SSE_NULL)
2006-03-07 15:55:14 +00:00
{
2006-03-02 15:10:59 +00:00
next = pair->next;
FREE_PAIR (map, pair);
map->size--;
pair = next;
}
2006-10-22 11:34:53 +00:00
map->buck[i] = SSE_NULL;
2006-03-02 15:10:59 +00:00
}
2006-10-22 11:34:53 +00:00
sse_awk_assert (map->awk, map->size == 0);
2006-03-02 15:10:59 +00:00
}
2006-10-22 11:34:53 +00:00
sse_awk_pair_t* sse_awk_map_get (
sse_awk_map_t* map, const sse_char_t* key, sse_size_t key_len)
2006-03-02 15:10:59 +00:00
{
2006-10-22 11:34:53 +00:00
sse_awk_pair_t* pair;
sse_size_t hc;
2006-03-02 15:10:59 +00:00
2006-08-03 05:05:48 +00:00
hc = __hash(key,key_len) % map->capa;
2006-03-02 15:10:59 +00:00
pair = map->buck[hc];
2006-10-22 11:34:53 +00:00
while (pair != SSE_NULL)
2006-03-07 15:55:14 +00:00
{
2006-08-03 05:05:48 +00:00
2006-10-22 11:34:53 +00:00
if (sse_awk_strxncmp (
2006-08-03 05:05:48 +00:00
pair->key, pair->key_len,
key, key_len) == 0) return pair;
2006-03-02 15:10:59 +00:00
pair = pair->next;
}
2006-10-22 11:34:53 +00:00
return SSE_NULL;
2006-03-02 15:10:59 +00:00
}
2006-10-22 11:34:53 +00:00
sse_awk_pair_t* sse_awk_map_put (
sse_awk_map_t* map, sse_char_t* key, sse_size_t key_len, void* val)
2006-03-02 15:10:59 +00:00
{
2006-04-18 14:49:42 +00:00
int n;
2006-10-22 11:34:53 +00:00
sse_awk_pair_t* px;
2006-04-18 14:49:42 +00:00
2006-10-22 11:34:53 +00:00
n = sse_awk_map_putx (map, key, key_len, val, &px);
if (n < 0) return SSE_NULL;
2006-04-18 14:49:42 +00:00
return px;
}
2006-10-22 11:34:53 +00:00
int sse_awk_map_putx (
sse_awk_map_t* map, sse_char_t* key, sse_size_t key_len,
void* val, sse_awk_pair_t** px)
2006-04-18 14:49:42 +00:00
{
2006-10-22 11:34:53 +00:00
sse_awk_pair_t* pair;
sse_size_t hc;
2006-04-18 14:49:42 +00:00
2006-08-03 05:05:48 +00:00
hc = __hash(key,key_len) % map->capa;
2006-04-18 14:49:42 +00:00
pair = map->buck[hc];
2006-10-22 11:34:53 +00:00
while (pair != SSE_NULL)
2006-04-18 14:49:42 +00:00
{
2006-10-22 11:34:53 +00:00
if (sse_awk_strxncmp (
2006-09-01 06:23:58 +00:00
pair->key, pair->key_len, key, key_len) == 0)
2006-04-18 14:49:42 +00:00
{
2006-10-22 11:34:53 +00:00
if (px != SSE_NULL)
*px = sse_awk_map_setpair (map, pair, val);
2006-04-18 14:49:42 +00:00
else
2006-10-22 11:34:53 +00:00
sse_awk_map_setpair (map, pair, val);
2006-04-18 14:49:42 +00:00
return 0; /* value changed for the existing key */
}
pair = pair->next;
}
2006-10-22 11:34:53 +00:00
pair = (sse_awk_pair_t*) SSE_AWK_MALLOC (
map->awk, sse_sizeof(sse_awk_pair_t));
if (pair == SSE_NULL) return -1; /* error */
2006-04-18 14:49:42 +00:00
2006-08-03 05:05:48 +00:00
/*pair->key = key;*/
/* duplicate the key if it is new */
2006-10-22 11:34:53 +00:00
pair->key = sse_awk_strxdup (map->awk, key, key_len);
if (pair->key == SSE_NULL)
2006-04-19 03:42:08 +00:00
{
2006-10-22 11:34:53 +00:00
SSE_AWK_FREE (map->awk, pair);
2006-04-19 03:42:08 +00:00
return -1; /* error */
}
2006-08-03 05:05:48 +00:00
pair->key_len = key_len;
2006-04-18 14:49:42 +00:00
pair->val = val;
pair->next = map->buck[hc];
map->buck[hc] = pair;
map->size++;
2006-10-22 11:34:53 +00:00
if (px != SSE_NULL) *px = pair;
2006-04-18 14:49:42 +00:00
return 1; /* new key added */
2006-03-02 15:10:59 +00:00
}
2006-10-22 11:34:53 +00:00
sse_awk_pair_t* sse_awk_map_set (
sse_awk_map_t* map, sse_char_t* key, sse_size_t key_len, void* val)
2006-03-02 15:10:59 +00:00
{
2006-10-22 11:34:53 +00:00
sse_awk_pair_t* pair;
sse_size_t hc;
2006-03-02 15:10:59 +00:00
2006-08-03 05:05:48 +00:00
hc = __hash(key,key_len) % map->capa;
2006-03-02 15:10:59 +00:00
pair = map->buck[hc];
2006-10-22 11:34:53 +00:00
while (pair != SSE_NULL)
2006-03-07 15:55:14 +00:00
{
2006-10-22 11:34:53 +00:00
if (sse_awk_strxncmp (
2006-09-01 06:23:58 +00:00
pair->key, pair->key_len, key, key_len) == 0)
2006-03-07 15:55:14 +00:00
{
2006-10-22 11:34:53 +00:00
return sse_awk_map_setpair (map, pair, val);
2006-03-02 15:10:59 +00:00
}
pair = pair->next;
}
2006-10-22 11:34:53 +00:00
return SSE_NULL;
2006-03-02 15:10:59 +00:00
}
2006-10-22 11:34:53 +00:00
sse_awk_pair_t* sse_awk_map_getpair (
sse_awk_map_t* map, const sse_char_t* key, sse_size_t key_len, void** val)
2006-03-06 04:04:47 +00:00
{
2006-10-22 11:34:53 +00:00
sse_awk_pair_t* pair;
2006-03-06 04:04:47 +00:00
2006-10-22 11:34:53 +00:00
pair = sse_awk_map_get (map, key, key_len);
if (pair == SSE_NULL) return SSE_NULL;
2006-03-07 15:55:14 +00:00
*val = pair->val;
2006-07-10 14:28:46 +00:00
2006-03-06 04:04:47 +00:00
return pair;
}
2006-10-22 11:34:53 +00:00
sse_awk_pair_t* sse_awk_map_setpair (
sse_awk_map_t* map, sse_awk_pair_t* pair, void* val)
2006-03-06 04:04:47 +00:00
{
/* use this function with care */
2006-03-07 15:55:14 +00:00
if (pair->val != val)
{
2006-10-22 11:34:53 +00:00
if (map->freeval != SSE_NULL)
2006-03-07 15:55:14 +00:00
{
2006-04-21 17:24:31 +00:00
map->freeval (map->owner, pair->val);
2006-03-06 04:04:47 +00:00
}
2006-03-07 15:55:14 +00:00
pair->val = val;
2006-03-06 04:04:47 +00:00
}
return pair;
}
2006-10-22 11:34:53 +00:00
int sse_awk_map_remove (sse_awk_map_t* map, sse_char_t* key, sse_size_t key_len)
2006-03-02 15:10:59 +00:00
{
2006-10-22 11:34:53 +00:00
sse_awk_pair_t* pair, * prev;
sse_size_t hc;
2006-03-02 15:10:59 +00:00
2006-08-03 05:05:48 +00:00
hc = __hash(key,key_len) % map->capa;
2006-03-02 15:10:59 +00:00
pair = map->buck[hc];
2006-10-22 11:34:53 +00:00
prev = SSE_NULL;
2006-03-02 15:10:59 +00:00
2006-10-22 11:34:53 +00:00
while (pair != SSE_NULL)
2006-03-07 15:55:14 +00:00
{
2006-10-22 11:34:53 +00:00
if (sse_awk_strxncmp (
2006-09-01 06:23:58 +00:00
pair->key, pair->key_len, key, key_len) == 0)
2006-03-07 15:55:14 +00:00
{
2006-10-22 11:34:53 +00:00
if (prev == SSE_NULL)
2006-03-02 15:10:59 +00:00
map->buck[hc] = pair->next;
else prev->next = pair->next;
FREE_PAIR (map, pair);
map->size--;
return 0;
}
prev = pair;
pair = pair->next;
}
return -1;
}
2006-10-22 11:34:53 +00:00
int sse_awk_map_walk (sse_awk_map_t* map,
int (*walker) (sse_awk_pair_t*,void*), void* arg)
2006-03-02 15:10:59 +00:00
{
2006-10-22 11:34:53 +00:00
sse_size_t i;
sse_awk_pair_t* pair, * next;
2006-03-02 15:10:59 +00:00
2006-03-07 15:55:14 +00:00
for (i = 0; i < map->capa; i++)
{
2006-03-02 15:10:59 +00:00
pair = map->buck[i];
2006-10-22 11:34:53 +00:00
while (pair != SSE_NULL)
2006-03-07 15:55:14 +00:00
{
2006-03-02 15:10:59 +00:00
next = pair->next;
2006-04-30 17:12:51 +00:00
if (walker(pair,arg) == -1) return -1;
2006-03-02 15:10:59 +00:00
pair = next;
}
}
return 0;
}
2006-10-22 11:34:53 +00:00
static sse_size_t __hash (const sse_char_t* key, sse_size_t key_len)
2006-03-02 15:10:59 +00:00
{
2006-10-22 11:34:53 +00:00
sse_size_t n = 0, i;
const sse_char_t* end = key + key_len;
2006-03-02 15:10:59 +00:00
2006-08-03 05:05:48 +00:00
while (key < end)
2006-03-07 15:55:14 +00:00
{
2006-10-22 11:34:53 +00:00
sse_byte_t* bp = (sse_byte_t*)key;
for (i = 0; i < sse_sizeof(*key); i++) n = n * 31 + *bp++;
2006-03-02 15:10:59 +00:00
key++;
}
return n;
}