qse/ase/awk/map.c

290 lines
5.6 KiB
C
Raw Normal View History

2006-03-02 15:10:59 +00:00
/*
2007-04-24 14:42:24 +00:00
* $Id: map.c,v 1.39 2007-04-24 14:42:24 bacon Exp $
2007-02-03 10:47:41 +00:00
*
* {License}
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
2007-04-24 14:42:24 +00:00
static ase_size_t hashkey (const ase_char_t* keyptr, ase_size_t keylen);
2006-03-02 15:10:59 +00:00
#define FREE_PAIR(map,pair) \
2006-05-03 16:07:24 +00:00
do { \
2007-04-24 14:42:24 +00:00
ASE_AWK_FREE ((map)->awk, (ase_char_t*)PAIR_KEYPTR(pair)); \
2006-10-24 04:10:12 +00:00
if ((map)->freeval != ASE_NULL) \
2007-04-24 14:42:24 +00:00
(map)->freeval ((map)->owner, PAIR_VAL(pair)); \
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
{
2007-03-06 14:58:00 +00:00
ASE_ASSERTX (capa > 0, "the initial capacity should be greater than 0");
2006-12-30 08:54:43 +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 (
2006-11-29 02:54:17 +00:00
awk, ASE_SIZEOF(ase_awk_map_t));
2006-10-24 04:10:12 +00:00
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**)
2006-11-29 02:54:17 +00:00
ASE_AWK_MALLOC (awk, ASE_SIZEOF(ase_awk_pair_t*) * capa);
2006-10-24 04:10:12 +00:00
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
{
2007-04-24 14:42:24 +00:00
next = PAIR_LNK(pair);
2006-03-02 15:10:59 +00:00
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-12-11 08:44:52 +00:00
ase_size_t ase_awk_map_getsize (ase_awk_map_t* map)
{
return map->size;
}
2006-10-24 04:10:12 +00:00
ase_awk_pair_t* ase_awk_map_get (
2007-04-24 14:42:24 +00:00
ase_awk_map_t* map, const ase_char_t* keyptr, ase_size_t keylen)
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
2007-04-24 14:42:24 +00:00
hc = hashkey(keyptr,keylen) % 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
2007-02-23 08:17:51 +00:00
if (ase_strxncmp (
2007-04-24 14:42:24 +00:00
PAIR_KEYPTR(pair), PAIR_KEYLEN(pair),
keyptr, keylen) == 0) return pair;
2006-08-03 05:05:48 +00:00
2007-04-24 14:42:24 +00:00
pair = PAIR_LNK(pair);
2006-03-02 15:10:59 +00:00
}
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 (
2007-04-24 14:42:24 +00:00
ase_awk_map_t* map, ase_char_t* keyptr, ase_size_t keylen, 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
2007-04-24 14:42:24 +00:00
n = ase_awk_map_putx (map, keyptr, keylen, val, &px);
2006-10-24 04:10:12 +00:00
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 (
2007-04-24 14:42:24 +00:00
ase_awk_map_t* map, ase_char_t* keyptr, ase_size_t keylen,
2006-10-24 04:10:12 +00:00
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
2007-04-24 14:42:24 +00:00
hc = hashkey(keyptr,keylen) % 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
{
2007-02-23 08:17:51 +00:00
if (ase_strxncmp (
2007-04-24 14:42:24 +00:00
PAIR_KEYPTR(pair), PAIR_KEYLEN(pair),
keyptr, keylen) == 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 */
}
2007-04-24 14:42:24 +00:00
pair = PAIR_LNK(pair);
2006-04-18 14:49:42 +00:00
}
2006-10-24 04:10:12 +00:00
pair = (ase_awk_pair_t*) ASE_AWK_MALLOC (
2006-11-29 02:54:17 +00:00
map->awk, ASE_SIZEOF(ase_awk_pair_t));
2006-10-24 04:10:12 +00:00
if (pair == ASE_NULL) return -1; /* error */
2006-04-18 14:49:42 +00:00
2006-08-03 05:05:48 +00:00
/* duplicate the key if it is new */
2007-04-24 14:42:24 +00:00
PAIR_KEYPTR(pair) = ase_strxdup (
keyptr, keylen, &map->awk->prmfns.mmgr);
if (PAIR_KEYPTR(pair) == 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
2007-04-24 14:42:24 +00:00
PAIR_KEYLEN(pair) = keylen;
PAIR_VAL(pair) = val;
PAIR_LNK(pair) = map->buck[hc];
2006-04-18 14:49:42 +00:00
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 (
2007-04-24 14:42:24 +00:00
ase_awk_map_t* map, ase_char_t* keyptr, ase_size_t keylen, 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
2007-04-24 14:42:24 +00:00
hc = hashkey(keyptr,keylen) % 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
{
2007-02-23 08:17:51 +00:00
if (ase_strxncmp (
2007-04-24 14:42:24 +00:00
PAIR_KEYPTR(pair), PAIR_KEYLEN(pair),
keyptr, keylen) == 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
}
2007-04-24 14:42:24 +00:00
pair = PAIR_LNK(pair);
2006-03-02 15:10:59 +00:00
}
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 (
2007-04-24 14:42:24 +00:00
ase_awk_map_t* map, const ase_char_t* keyptr, ase_size_t keylen,
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
2007-04-24 14:42:24 +00:00
pair = ase_awk_map_get (map, keyptr, keylen);
2006-10-24 04:10:12 +00:00
if (pair == ASE_NULL) return ASE_NULL;
2007-04-24 14:42:24 +00:00
*val = PAIR_VAL(pair);
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 */
2007-04-24 14:42:24 +00:00
if (PAIR_VAL(pair) != val)
2006-03-07 15:55:14 +00:00
{
2006-10-24 04:10:12 +00:00
if (map->freeval != ASE_NULL)
2006-03-07 15:55:14 +00:00
{
2007-04-24 14:42:24 +00:00
map->freeval (map->owner, PAIR_VAL(pair));
2006-03-06 04:04:47 +00:00
}
2007-04-24 14:42:24 +00:00
PAIR_VAL(pair) = val;
2006-03-06 04:04:47 +00:00
}
return pair;
}
2007-04-24 14:42:24 +00:00
int ase_awk_map_remove (
ase_awk_map_t* map, ase_char_t* keyptr, ase_size_t keylen)
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
2007-04-24 14:42:24 +00:00
hc = hashkey(keyptr,keylen) % 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
{
2007-02-23 08:17:51 +00:00
if (ase_strxncmp (
2007-04-24 14:42:24 +00:00
PAIR_KEYPTR(pair), PAIR_KEYLEN(pair),
keyptr, keylen) == 0)
2006-03-07 15:55:14 +00:00
{
2006-10-24 04:10:12 +00:00
if (prev == ASE_NULL)
2007-04-24 14:42:24 +00:00
map->buck[hc] = PAIR_LNK(pair);
else prev->next = PAIR_LNK(pair);
2006-03-02 15:10:59 +00:00
FREE_PAIR (map, pair);
map->size--;
return 0;
}
prev = pair;
2007-04-24 14:42:24 +00:00
pair = PAIR_LNK(pair);
2006-03-02 15:10:59 +00:00
}
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
{
2007-04-24 14:42:24 +00:00
next = PAIR_LNK(pair);
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;
}
2007-04-24 14:42:24 +00:00
static ase_size_t hashkey (const ase_char_t* keyptr, ase_size_t keylen)
2006-03-02 15:10:59 +00:00
{
2006-10-24 04:10:12 +00:00
ase_size_t n = 0, i;
2007-04-24 14:42:24 +00:00
const ase_char_t* end = keyptr + keylen;
2006-03-02 15:10:59 +00:00
2007-04-24 14:42:24 +00:00
while (keyptr < end)
2006-03-07 15:55:14 +00:00
{
2007-04-24 14:42:24 +00:00
ase_byte_t* bp = (ase_byte_t*)keyptr;
for (i = 0; i < ASE_SIZEOF(*keyptr); i++) n = n * 31 + *bp++;
keyptr++;
2006-03-02 15:10:59 +00:00
}
return n;
}