2006-03-02 15:10:59 +00:00
|
|
|
/*
|
2006-10-26 09:31:28 +00:00
|
|
|
* $Id: map.c,v 1.29 2006-10-26 09:27:15 bacon Exp $
|
2006-03-02 15:10:59 +00:00
|
|
|
*/
|
|
|
|
|
2006-10-24 04:10:12 +00:00
|
|
|
#include <ase/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-24 04:10:12 +00:00
|
|
|
static ase_size_t __hash (const ase_char_t* key, ase_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-24 04:10:12 +00:00
|
|
|
ASE_AWK_FREE ((map)->awk, (ase_char_t*)(pair)->key); \
|
|
|
|
if ((map)->freeval != ASE_NULL) \
|
2006-04-21 17:24:31 +00:00
|
|
|
(map)->freeval ((map)->owner, (pair)->val); \
|
2006-10-24 04:10:12 +00:00
|
|
|
ASE_AWK_FREE ((map)->awk, pair); \
|
2006-03-02 15:10:59 +00:00
|
|
|
} while (0)
|
|
|
|
|
2006-10-24 04:10:12 +00:00
|
|
|
ase_awk_map_t* ase_awk_map_open (
|
|
|
|
ase_awk_map_t* map, void* owner, ase_size_t capa,
|
|
|
|
void(*freeval)(void*,void*), ase_awk_t* awk)
|
2006-03-02 15:10:59 +00:00
|
|
|
{
|
2006-10-24 04:10:12 +00:00
|
|
|
if (map == ASE_NULL)
|
2006-03-07 15:55:14 +00:00
|
|
|
{
|
2006-10-24 04:10:12 +00:00
|
|
|
map = (ase_awk_map_t*) ASE_AWK_MALLOC (
|
|
|
|
awk, ase_sizeof(ase_awk_map_t));
|
|
|
|
if (map == ASE_NULL) return ASE_NULL;
|
|
|
|
map->__dynamic = ase_true;
|
2006-03-02 15:10:59 +00:00
|
|
|
}
|
2006-10-24 04:10:12 +00:00
|
|
|
else map->__dynamic = ase_false;
|
2006-03-02 15:10:59 +00:00
|
|
|
|
2006-09-01 03:44:51 +00:00
|
|
|
map->awk = awk;
|
2006-10-24 04:10:12 +00:00
|
|
|
map->buck = (ase_awk_pair_t**)
|
|
|
|
ASE_AWK_MALLOC (awk, ase_sizeof(ase_awk_pair_t*) * capa);
|
|
|
|
if (map->buck == ASE_NULL)
|
2006-03-07 15:55:14 +00:00
|
|
|
{
|
2006-10-24 04:10:12 +00:00
|
|
|
if (map->__dynamic) ASE_AWK_FREE (awk, map);
|
|
|
|
return ASE_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-24 04:10:12 +00:00
|
|
|
while (capa > 0) map->buck[--capa] = ASE_NULL;
|
2006-03-02 15:10:59 +00:00
|
|
|
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
2006-10-24 04:10:12 +00:00
|
|
|
void ase_awk_map_close (ase_awk_map_t* map)
|
2006-03-02 15:10:59 +00:00
|
|
|
{
|
2006-10-24 04:10:12 +00:00
|
|
|
ase_awk_map_clear (map);
|
|
|
|
ASE_AWK_FREE (map->awk, map->buck);
|
|
|
|
if (map->__dynamic) ASE_AWK_FREE (map->awk, map);
|
2006-03-02 15:10:59 +00:00
|
|
|
}
|
|
|
|
|
2006-10-24 04:10:12 +00:00
|
|
|
void ase_awk_map_clear (ase_awk_map_t* map)
|
2006-03-02 15:10:59 +00:00
|
|
|
{
|
2006-10-24 04:10:12 +00:00
|
|
|
ase_size_t i;
|
|
|
|
ase_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-24 04:10:12 +00:00
|
|
|
while (pair != ASE_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-24 04:10:12 +00:00
|
|
|
map->buck[i] = ASE_NULL;
|
2006-03-02 15:10:59 +00:00
|
|
|
}
|
|
|
|
|
2006-10-26 09:31:28 +00:00
|
|
|
ASE_AWK_ASSERT (map->awk, map->size == 0);
|
2006-03-02 15:10:59 +00:00
|
|
|
}
|
|
|
|
|
2006-10-24 04:10:12 +00:00
|
|
|
ase_awk_pair_t* ase_awk_map_get (
|
|
|
|
ase_awk_map_t* map, const ase_char_t* key, ase_size_t key_len)
|
2006-03-02 15:10:59 +00:00
|
|
|
{
|
2006-10-24 04:10:12 +00:00
|
|
|
ase_awk_pair_t* pair;
|
|
|
|
ase_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-24 04:10:12 +00:00
|
|
|
while (pair != ASE_NULL)
|
2006-03-07 15:55:14 +00:00
|
|
|
{
|
2006-08-03 05:05:48 +00:00
|
|
|
|
2006-10-24 04:10:12 +00:00
|
|
|
if (ase_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-24 04:10:12 +00:00
|
|
|
return ASE_NULL;
|
2006-03-02 15:10:59 +00:00
|
|
|
}
|
|
|
|
|
2006-10-24 04:10:12 +00:00
|
|
|
ase_awk_pair_t* ase_awk_map_put (
|
|
|
|
ase_awk_map_t* map, ase_char_t* key, ase_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-24 04:10:12 +00:00
|
|
|
ase_awk_pair_t* px;
|
2006-04-18 14:49:42 +00:00
|
|
|
|
2006-10-24 04:10:12 +00:00
|
|
|
n = ase_awk_map_putx (map, key, key_len, val, &px);
|
|
|
|
if (n < 0) return ASE_NULL;
|
2006-04-18 14:49:42 +00:00
|
|
|
return px;
|
|
|
|
}
|
|
|
|
|
2006-10-24 04:10:12 +00:00
|
|
|
int ase_awk_map_putx (
|
|
|
|
ase_awk_map_t* map, ase_char_t* key, ase_size_t key_len,
|
|
|
|
void* val, ase_awk_pair_t** px)
|
2006-04-18 14:49:42 +00:00
|
|
|
{
|
2006-10-24 04:10:12 +00:00
|
|
|
ase_awk_pair_t* pair;
|
|
|
|
ase_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-24 04:10:12 +00:00
|
|
|
while (pair != ASE_NULL)
|
2006-04-18 14:49:42 +00:00
|
|
|
{
|
2006-10-24 04:10:12 +00:00
|
|
|
if (ase_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-24 04:10:12 +00:00
|
|
|
if (px != ASE_NULL)
|
|
|
|
*px = ase_awk_map_setpair (map, pair, val);
|
2006-04-18 14:49:42 +00:00
|
|
|
else
|
2006-10-24 04:10:12 +00:00
|
|
|
ase_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-24 04:10:12 +00:00
|
|
|
pair = (ase_awk_pair_t*) ASE_AWK_MALLOC (
|
|
|
|
map->awk, ase_sizeof(ase_awk_pair_t));
|
|
|
|
if (pair == ASE_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-24 04:10:12 +00:00
|
|
|
pair->key = ase_awk_strxdup (map->awk, key, key_len);
|
|
|
|
if (pair->key == ASE_NULL)
|
2006-04-19 03:42:08 +00:00
|
|
|
{
|
2006-10-24 04:10:12 +00:00
|
|
|
ASE_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-24 04:10:12 +00:00
|
|
|
if (px != ASE_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-24 04:10:12 +00:00
|
|
|
ase_awk_pair_t* ase_awk_map_set (
|
|
|
|
ase_awk_map_t* map, ase_char_t* key, ase_size_t key_len, void* val)
|
2006-03-02 15:10:59 +00:00
|
|
|
{
|
2006-10-24 04:10:12 +00:00
|
|
|
ase_awk_pair_t* pair;
|
|
|
|
ase_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-24 04:10:12 +00:00
|
|
|
while (pair != ASE_NULL)
|
2006-03-07 15:55:14 +00:00
|
|
|
{
|
2006-10-24 04:10:12 +00:00
|
|
|
if (ase_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-24 04:10:12 +00:00
|
|
|
return ase_awk_map_setpair (map, pair, val);
|
2006-03-02 15:10:59 +00:00
|
|
|
}
|
|
|
|
pair = pair->next;
|
|
|
|
}
|
|
|
|
|
2006-10-24 04:10:12 +00:00
|
|
|
return ASE_NULL;
|
2006-03-02 15:10:59 +00:00
|
|
|
}
|
|
|
|
|
2006-10-24 04:10:12 +00:00
|
|
|
ase_awk_pair_t* ase_awk_map_getpair (
|
|
|
|
ase_awk_map_t* map, const ase_char_t* key, ase_size_t key_len, void** val)
|
2006-03-06 04:04:47 +00:00
|
|
|
{
|
2006-10-24 04:10:12 +00:00
|
|
|
ase_awk_pair_t* pair;
|
2006-03-06 04:04:47 +00:00
|
|
|
|
2006-10-24 04:10:12 +00:00
|
|
|
pair = ase_awk_map_get (map, key, key_len);
|
|
|
|
if (pair == ASE_NULL) return ASE_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-24 04:10:12 +00:00
|
|
|
ase_awk_pair_t* ase_awk_map_setpair (
|
|
|
|
ase_awk_map_t* map, ase_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-24 04:10:12 +00:00
|
|
|
if (map->freeval != ASE_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-24 04:10:12 +00:00
|
|
|
int ase_awk_map_remove (ase_awk_map_t* map, ase_char_t* key, ase_size_t key_len)
|
2006-03-02 15:10:59 +00:00
|
|
|
{
|
2006-10-24 04:10:12 +00:00
|
|
|
ase_awk_pair_t* pair, * prev;
|
|
|
|
ase_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-24 04:10:12 +00:00
|
|
|
prev = ASE_NULL;
|
2006-03-02 15:10:59 +00:00
|
|
|
|
2006-10-24 04:10:12 +00:00
|
|
|
while (pair != ASE_NULL)
|
2006-03-07 15:55:14 +00:00
|
|
|
{
|
2006-10-24 04:10:12 +00:00
|
|
|
if (ase_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-24 04:10:12 +00:00
|
|
|
if (prev == ASE_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-24 04:10:12 +00:00
|
|
|
int ase_awk_map_walk (ase_awk_map_t* map,
|
|
|
|
int (*walker) (ase_awk_pair_t*,void*), void* arg)
|
2006-03-02 15:10:59 +00:00
|
|
|
{
|
2006-10-24 04:10:12 +00:00
|
|
|
ase_size_t i;
|
|
|
|
ase_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-24 04:10:12 +00:00
|
|
|
while (pair != ASE_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-24 04:10:12 +00:00
|
|
|
static ase_size_t __hash (const ase_char_t* key, ase_size_t key_len)
|
2006-03-02 15:10:59 +00:00
|
|
|
{
|
2006-10-24 04:10:12 +00:00
|
|
|
ase_size_t n = 0, i;
|
|
|
|
const ase_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-24 04:10:12 +00:00
|
|
|
ase_byte_t* bp = (ase_byte_t*)key;
|
|
|
|
for (i = 0; i < ase_sizeof(*key); i++) n = n * 31 + *bp++;
|
2006-03-02 15:10:59 +00:00
|
|
|
key++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|