qse/ase/awk/map.c

264 lines
4.7 KiB
C
Raw Normal View History

2006-03-02 15:10:59 +00:00
/*
2006-05-06 12:52:36 +00:00
* $Id: map.c,v 1.19 2006-05-06 12:52:36 bacon Exp $
2006-03-02 15:10:59 +00:00
*/
2006-03-31 16:35:37 +00:00
#include <xp/awk/awk_i.h>
2006-03-02 15:10:59 +00:00
2006-04-16 04:31:38 +00:00
#ifndef XP_AWK_STAND_ALONE
2006-03-02 15:10:59 +00:00
#include <xp/bas/memory.h>
#include <xp/bas/string.h>
#include <xp/bas/assert.h>
#endif
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
static xp_size_t __hash (const xp_char_t* key);
#define FREE_PAIR(map,pair) \
2006-05-03 16:07:24 +00:00
do { \
2006-03-06 04:04:47 +00:00
xp_free ((xp_char_t*)(pair)->key); \
2006-03-05 17:07:33 +00:00
if ((map)->freeval != XP_NULL) \
2006-04-21 17:24:31 +00:00
(map)->freeval ((map)->owner, (pair)->val); \
2006-03-02 15:10:59 +00:00
xp_free (pair); \
} while (0)
2006-04-24 07:46:35 +00:00
xp_awk_map_t* xp_awk_map_open (xp_awk_map_t* map,
void* owner, xp_size_t capa, void(*freeval)(void*,void*))
2006-03-02 15:10:59 +00:00
{
2006-03-07 15:55:14 +00:00
if (map == XP_NULL)
{
2006-03-02 15:36:30 +00:00
map = (xp_awk_map_t*) xp_malloc (xp_sizeof(xp_awk_map_t));
2006-03-02 15:10:59 +00:00
if (map == XP_NULL) return XP_NULL;
map->__dynamic = xp_true;
}
else map->__dynamic = xp_false;
2006-03-02 15:36:30 +00:00
map->buck = (xp_awk_pair_t**)
xp_malloc (xp_sizeof(xp_awk_pair_t*) * capa);
2006-03-07 15:55:14 +00:00
if (map->buck == XP_NULL)
{
2006-03-02 15:10:59 +00:00
if (map->__dynamic) xp_free (map);
return XP_NULL;
}
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-03-02 15:10:59 +00:00
while (capa > 0) map->buck[--capa] = XP_NULL;
return map;
}
void xp_awk_map_close (xp_awk_map_t* map)
{
xp_awk_map_clear (map);
xp_free (map->buck);
if (map->__dynamic) xp_free (map);
}
void xp_awk_map_clear (xp_awk_map_t* map)
{
xp_size_t i;
xp_awk_pair_t* pair, * next;
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-03-07 15:55:14 +00:00
while (pair != XP_NULL)
{
2006-03-02 15:10:59 +00:00
next = pair->next;
FREE_PAIR (map, pair);
map->size--;
pair = next;
}
map->buck[i] = XP_NULL;
}
xp_assert (map->size == 0);
}
2006-03-06 04:04:47 +00:00
xp_awk_pair_t* xp_awk_map_get (xp_awk_map_t* map, xp_char_t* key)
2006-03-02 15:10:59 +00:00
{
xp_awk_pair_t* pair;
xp_size_t hc;
hc = __hash(key) % map->capa;
pair = map->buck[hc];
2006-03-07 15:55:14 +00:00
while (pair != XP_NULL)
{
2006-03-02 15:10:59 +00:00
if (xp_strcmp(pair->key,key) == 0) return pair;
pair = pair->next;
}
return XP_NULL;
}
2006-03-07 15:55:14 +00:00
xp_awk_pair_t* xp_awk_map_put (xp_awk_map_t* map, xp_char_t* key, void* val)
2006-03-02 15:10:59 +00:00
{
2006-04-18 14:49:42 +00:00
int n;
xp_awk_pair_t* px;
n = xp_awk_map_putx (map, key, val, &px);
if (n < 0) return XP_NULL;
return px;
}
2006-04-19 03:42:08 +00:00
int xp_awk_map_putx (
xp_awk_map_t* map, xp_char_t* key, void* val, xp_awk_pair_t** px)
2006-04-18 14:49:42 +00:00
{
xp_awk_pair_t* pair;
xp_size_t hc;
hc = __hash(key) % map->capa;
pair = map->buck[hc];
while (pair != XP_NULL)
{
if (xp_strcmp(pair->key,key) == 0)
{
if (px != XP_NULL)
*px = xp_awk_map_setpair (map, pair, val);
else
xp_awk_map_setpair (map, pair, val);
return 0; /* value changed for the existing key */
}
pair = pair->next;
}
pair = (xp_awk_pair_t*) xp_malloc (xp_sizeof(xp_awk_pair_t));
if (pair == XP_NULL) return -1; /* error */
2006-04-19 03:42:08 +00:00
/*pair->key = key;*/
pair->key = xp_strdup (key); /* duplicate the key if it is new */
if (pair->key == XP_NULL)
{
xp_free (pair);
return -1; /* error */
}
2006-04-18 14:49:42 +00:00
pair->val = val;
pair->next = map->buck[hc];
map->buck[hc] = pair;
map->size++;
if (px != XP_NULL) *px = pair;
return 1; /* new key added */
2006-03-02 15:10:59 +00:00
}
2006-03-07 15:55:14 +00:00
xp_awk_pair_t* xp_awk_map_set (xp_awk_map_t* map, xp_char_t* key, void* val)
2006-03-02 15:10:59 +00:00
{
xp_awk_pair_t* pair;
xp_size_t hc;
hc = __hash(key) % map->capa;
pair = map->buck[hc];
2006-03-07 15:55:14 +00:00
while (pair != XP_NULL)
{
if (xp_strcmp(pair->key,key) == 0)
{
return xp_awk_map_setpair (map, pair, val);
2006-03-02 15:10:59 +00:00
}
pair = pair->next;
}
return XP_NULL;
}
2006-03-06 04:04:47 +00:00
xp_awk_pair_t* xp_awk_map_getpair (
2006-03-07 16:09:18 +00:00
xp_awk_map_t* map, xp_char_t* key, void** val)
2006-03-06 04:04:47 +00:00
{
xp_awk_pair_t* pair;
pair = xp_awk_map_get (map, key);
if (pair == XP_NULL) return XP_NULL;
2006-03-07 15:55:14 +00:00
*val = pair->val;
2006-03-06 04:04:47 +00:00
return pair;
}
xp_awk_pair_t* xp_awk_map_setpair (
2006-03-07 15:55:14 +00:00
xp_awk_map_t* map, xp_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)
{
if (map->freeval != XP_NULL)
{
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;
}
int xp_awk_map_remove (xp_awk_map_t* map, xp_char_t* key)
2006-03-02 15:10:59 +00:00
{
xp_awk_pair_t* pair, * prev;
xp_size_t hc;
hc = __hash(key) % map->capa;
pair = map->buck[hc];
prev = XP_NULL;
2006-03-07 15:55:14 +00:00
while (pair != XP_NULL)
{
if (xp_strcmp(pair->key,key) == 0)
{
2006-03-02 15:10:59 +00:00
if (prev == XP_NULL)
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-04-30 17:12:51 +00:00
int xp_awk_map_walk (xp_awk_map_t* map,
int (*walker) (xp_awk_pair_t*,void*), void* arg)
2006-03-02 15:10:59 +00:00
{
xp_size_t i;
xp_awk_pair_t* pair, * next;
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-03-07 15:55:14 +00:00
while (pair != XP_NULL)
{
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;
}
static xp_size_t __hash (const xp_char_t* key)
{
xp_size_t n = 0, i;
2006-05-06 12:52:36 +00:00
while (*key != XP_T('\0'))
2006-03-07 15:55:14 +00:00
{
2006-03-02 15:10:59 +00:00
xp_byte_t* bp = (xp_byte_t*)key;
for (i = 0; i < xp_sizeof(*key); i++) n = n * 31 + *bp++;
key++;
}
return n;
}