2006-03-02 15:10:59 +00:00
|
|
|
/*
|
2006-08-03 05:05:48 +00:00
|
|
|
* $Id: map.c,v 1.21 2006-08-03 05:05:47 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
|
|
|
|
2006-08-03 05:05:48 +00:00
|
|
|
static xp_size_t __hash (const xp_char_t* key, xp_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-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-08-03 05:05:48 +00:00
|
|
|
xp_awk_pair_t* xp_awk_map_get (
|
|
|
|
xp_awk_map_t* map, const xp_char_t* key, xp_size_t key_len)
|
2006-03-02 15:10:59 +00:00
|
|
|
{
|
|
|
|
xp_awk_pair_t* pair;
|
|
|
|
xp_size_t hc;
|
|
|
|
|
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-03-07 15:55:14 +00:00
|
|
|
while (pair != XP_NULL)
|
|
|
|
{
|
2006-08-03 05:05:48 +00:00
|
|
|
|
|
|
|
if (xp_strxncmp (
|
|
|
|
pair->key, pair->key_len,
|
|
|
|
key, key_len) == 0) return pair;
|
|
|
|
|
2006-03-02 15:10:59 +00:00
|
|
|
pair = pair->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return XP_NULL;
|
|
|
|
}
|
|
|
|
|
2006-08-03 05:05:48 +00:00
|
|
|
xp_awk_pair_t* xp_awk_map_put (
|
|
|
|
xp_awk_map_t* map, xp_char_t* key, xp_size_t key_len, 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;
|
|
|
|
|
2006-08-03 05:05:48 +00:00
|
|
|
n = xp_awk_map_putx (map, key, key_len, val, &px);
|
2006-04-18 14:49:42 +00:00
|
|
|
if (n < 0) return XP_NULL;
|
|
|
|
return px;
|
|
|
|
}
|
|
|
|
|
2006-04-19 03:42:08 +00:00
|
|
|
int xp_awk_map_putx (
|
2006-08-03 05:05:48 +00:00
|
|
|
xp_awk_map_t* map, xp_char_t* key, xp_size_t key_len,
|
|
|
|
void* val, xp_awk_pair_t** px)
|
2006-04-18 14:49:42 +00:00
|
|
|
{
|
|
|
|
xp_awk_pair_t* pair;
|
|
|
|
xp_size_t hc;
|
|
|
|
|
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];
|
|
|
|
|
|
|
|
while (pair != XP_NULL)
|
|
|
|
{
|
2006-08-03 05:05:48 +00:00
|
|
|
if (xp_strxncmp (pair->key, pair->key_len, key, key_len) == 0)
|
2006-04-18 14:49:42 +00:00
|
|
|
{
|
|
|
|
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-08-03 05:05:48 +00:00
|
|
|
/*pair->key = key;*/
|
|
|
|
|
|
|
|
/* duplicate the key if it is new */
|
|
|
|
pair->key = xp_strxdup (key, key_len);
|
2006-04-19 03:42:08 +00:00
|
|
|
if (pair->key == XP_NULL)
|
|
|
|
{
|
|
|
|
xp_free (pair);
|
|
|
|
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++;
|
|
|
|
|
|
|
|
if (px != XP_NULL) *px = pair;
|
|
|
|
return 1; /* new key added */
|
2006-03-02 15:10:59 +00:00
|
|
|
}
|
|
|
|
|
2006-08-03 05:05:48 +00:00
|
|
|
xp_awk_pair_t* xp_awk_map_set (
|
|
|
|
xp_awk_map_t* map, xp_char_t* key, xp_size_t key_len, void* val)
|
2006-03-02 15:10:59 +00:00
|
|
|
{
|
|
|
|
xp_awk_pair_t* pair;
|
|
|
|
xp_size_t hc;
|
|
|
|
|
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-03-07 15:55:14 +00:00
|
|
|
while (pair != XP_NULL)
|
|
|
|
{
|
2006-08-03 05:05:48 +00:00
|
|
|
if (xp_strxncmp (pair->key, pair->key_len, key, key_len) == 0)
|
2006-03-07 15:55:14 +00:00
|
|
|
{
|
|
|
|
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-08-03 05:05:48 +00:00
|
|
|
xp_awk_map_t* map, const xp_char_t* key, xp_size_t key_len, void** val)
|
2006-03-06 04:04:47 +00:00
|
|
|
{
|
|
|
|
xp_awk_pair_t* pair;
|
|
|
|
|
2006-08-03 05:05:48 +00:00
|
|
|
pair = xp_awk_map_get (map, key, key_len);
|
2006-03-06 04:04:47 +00:00
|
|
|
if (pair == XP_NULL) return XP_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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2006-08-03 05:05:48 +00:00
|
|
|
int xp_awk_map_remove (xp_awk_map_t* map, xp_char_t* key, xp_size_t key_len)
|
2006-03-02 15:10:59 +00:00
|
|
|
{
|
|
|
|
xp_awk_pair_t* pair, * prev;
|
|
|
|
xp_size_t hc;
|
|
|
|
|
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];
|
|
|
|
prev = XP_NULL;
|
|
|
|
|
2006-03-07 15:55:14 +00:00
|
|
|
while (pair != XP_NULL)
|
|
|
|
{
|
2006-08-03 05:05:48 +00:00
|
|
|
if (xp_strxncmp (pair->key, pair->key_len, key, key_len) == 0)
|
2006-03-07 15:55:14 +00:00
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
2006-08-03 05:05:48 +00:00
|
|
|
static xp_size_t __hash (const xp_char_t* key, xp_size_t key_len)
|
2006-03-02 15:10:59 +00:00
|
|
|
{
|
|
|
|
xp_size_t n = 0, i;
|
2006-08-03 05:05:48 +00:00
|
|
|
const xp_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-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;
|
|
|
|
}
|