This commit is contained in:
hyung-hwan 2008-08-27 05:04:16 +00:00
parent b361aee101
commit 7f353d7c8a
2 changed files with 64 additions and 11 deletions

View File

@ -1,5 +1,5 @@
/* /*
* $Id: map.h 346 2008-08-26 10:31:24Z baconevi $ * $Id: map.h 347 2008-08-26 11:04:16Z baconevi $
* *
* {License} * {License}
*/ */
@ -231,6 +231,26 @@ int ase_map_putx (
ase_map_pair_t** px ase_map_pair_t** px
); );
/*
* NAME: insert a new pair with a key and a value
*
* DESCRIPTION:
* The ase_map_insert() function inserts a new pair with the key and the value
* given. If there exists a pair with the key given, the function returns
* ASE_NULL without channging the value.
*
* RETURNS:
* a pointer to the pair successfully created on success.
* ASE_NULL on failure.
*/
ase_map_pair_t* ase_map_insert (
ase_map_t* map /* a map */,
void* kptr,
ase_size_t klen,
void* vptr,
ase_size_t vlen
);
/* update the value of a existing pair with a matching key */ /* update the value of a existing pair with a matching key */
ase_map_pair_t* ase_map_update ( ase_map_pair_t* ase_map_update (
ase_map_t* map /* a map */, ase_map_t* map /* a map */,

View File

@ -1,5 +1,5 @@
/* /*
* $Id: map.c 346 2008-08-26 10:31:24Z baconevi $ * $Id: map.c 347 2008-08-26 11:04:16Z baconevi $
* *
* {License} * {License}
*/ */
@ -47,9 +47,7 @@ static int comp_key (map_t* map,
const void* kptr2, size_t klen2) const void* kptr2, size_t klen2)
{ {
if (klen1 == klen2) return ASE_MEMCMP (kptr1, kptr2, klen1); if (klen1 == klen2) return ASE_MEMCMP (kptr1, kptr2, klen1);
/* it just returns 1 to indicate that they are different. */
/* when the length are different it indicate that they are
* just different */
return 1; return 1;
} }
@ -127,7 +125,6 @@ static void free_pair (map_t* map, pair_t* pair)
static pair_t* change_pair_val ( static pair_t* change_pair_val (
map_t* map, pair_t* pair, void* vptr, size_t vlen) map_t* map, pair_t* pair, void* vptr, size_t vlen)
{ {
/* use this function with care */
if (VPTR(pair) == vptr) if (VPTR(pair) == vptr)
{ {
/* if the old value and the new value are the same, /* if the old value and the new value are the same,
@ -381,8 +378,7 @@ int ase_map_putx (
pair = next; pair = next;
} }
if (map->threshold > 0 && if (map->threshold > 0 && map->size >= map->threshold)
map->size >= map->threshold)
{ {
if (reorganize(map) == 0) /* ignore the error */ if (reorganize(map) == 0) /* ignore the error */
{ {
@ -390,7 +386,6 @@ int ase_map_putx (
} }
} }
ASE_ASSERT (pair == ASE_NULL); ASE_ASSERT (pair == ASE_NULL);
#if 0 #if 0
@ -417,8 +412,46 @@ int ase_map_putx (
return 1; /* new key added */ return 1; /* new key added */
} }
pair_t* ase_map_update (map_t* map,
void* kptr, size_t klen, void* vptr, size_t vlen) pair_t* ase_map_insert (map_t* map, void* kptr, size_t klen, void* vptr, size_t vlen)
{
pair_t* pair;
size_t hc;
hc = map->hasher(map,kptr,klen) % map->capa;
pair = map->buck[hc];
while (pair != ASE_NULL)
{
if (map->comper (map, KPTR(pair), KLEN(pair), kptr, klen) == 0)
{
return ASE_NULL;
}
pair = NEXT(pair);
}
if (map->threshold > 0 && map->size >= map->threshold)
{
if (reorganize(map) == 0) /* ignore the error */
{
hc = map->hasher(map,kptr,klen) % map->capa;
}
}
ASE_ASSERT (pair == ASE_NULL);
pair = alloc_pair (map, kptr, klen, vptr, vlen);
if (pair == ASE_NULL) return ASE_NULL;
NEXT(pair) = map->buck[hc];
map->buck[hc] = pair;
map->size++;
return pair;
}
pair_t* ase_map_update (map_t* map, void* kptr, size_t klen, void* vptr, size_t vlen)
{ {
pair_t* pair, * p, * prev, * next; pair_t* pair, * p, * prev, * next;
size_t hc; size_t hc;