This commit is contained in:
hyung-hwan 2008-08-27 04:31:24 +00:00
parent 0590924941
commit b361aee101
3 changed files with 46 additions and 29 deletions

View File

@ -1,5 +1,5 @@
/* /*
* $Id: map.h 345 2008-08-26 08:50:12Z baconevi $ * $Id: map.h 346 2008-08-26 10:31:24Z baconevi $
* *
* {License} * {License}
*/ */
@ -231,7 +231,8 @@ int ase_map_putx (
ase_map_pair_t** px ase_map_pair_t** px
); );
ase_map_pair_t* ase_map_set ( /* update the value of a existing pair with a matching key */
ase_map_pair_t* ase_map_update (
ase_map_t* map /* a map */, ase_map_t* map /* a map */,
void* kptr, void* kptr,
ase_size_t klen, ase_size_t klen,
@ -239,8 +240,8 @@ ase_map_pair_t* ase_map_set (
ase_size_t vlen ase_size_t vlen
); );
/* remove a pair with a matching key */ /* delete a pair with a matching key */
int ase_map_remove ( int ase_map_delete (
ase_map_t* map /* a map */, ase_map_t* map /* a map */,
const void* kptr, const void* kptr,
ase_size_t klen ase_size_t klen

View File

@ -214,8 +214,8 @@ ase_sll_node_t* ase_sll_pushtail (
); );
void ase_sll_delete ( void ase_sll_delete (
ase_sll_t* sll, ase_sll_t* sll /* a singly linked list */,
ase_sll_node_t* pos ase_sll_node_t* pos /* a node to delete */
); );
void ase_sll_pophead ( void ase_sll_pophead (

View File

@ -1,5 +1,5 @@
/* /*
* $Id: map.c 345 2008-08-26 08:50:12Z baconevi $ * $Id: map.c 346 2008-08-26 10:31:24Z baconevi $
* *
* {License} * {License}
*/ */
@ -19,6 +19,7 @@
#define KLEN(p) ASE_MAP_KLEN(p) #define KLEN(p) ASE_MAP_KLEN(p)
#define VPTR(p) ASE_MAP_VPTR(p) #define VPTR(p) ASE_MAP_VPTR(p)
#define VLEN(p) ASE_MAP_VLEN(p) #define VLEN(p) ASE_MAP_VLEN(p)
#define NEXT(p) ASE_MAP_NEXT(p)
#define size_t ase_size_t #define size_t ase_size_t
#define byte_t ase_byte_t #define byte_t ase_byte_t
@ -67,7 +68,7 @@ static pair_t* alloc_pair (map_t* map,
n = ASE_MMGR_ALLOC (map->mmgr, as); n = ASE_MMGR_ALLOC (map->mmgr, as);
if (n == ASE_NULL) return ASE_NULL; if (n == ASE_NULL) return ASE_NULL;
ASE_MAP_NEXT(n) = ASE_NULL; NEXT(n) = ASE_NULL;
KLEN(n) = klen; KLEN(n) = klen;
if (kcop == ASE_NULL) if (kcop == ASE_NULL)
@ -234,7 +235,7 @@ void ase_map_clear (map_t* map)
while (pair != ASE_NULL) while (pair != ASE_NULL)
{ {
next = ASE_MAP_NEXT(pair); next = NEXT(pair);
free_pair (map, pair); free_pair (map, pair);
map->size--; map->size--;
pair = next; pair = next;
@ -328,7 +329,7 @@ pair_t* ase_map_get (
return pair; return pair;
} }
pair = ASE_MAP_NEXT(pair); pair = NEXT(pair);
} }
return ASE_NULL; return ASE_NULL;
@ -349,15 +350,16 @@ int ase_map_putx (
map_t* map, void* kptr, size_t klen, map_t* map, void* kptr, size_t klen,
void* vptr, size_t vlen, pair_t** px) void* vptr, size_t vlen, pair_t** px)
{ {
pair_t* pair, * p, * prev = ASE_NULL, * next; pair_t* pair, * p, * prev, * next;
size_t hc; size_t hc;
hc = map->hasher(map,kptr,klen) % map->capa; hc = map->hasher(map,kptr,klen) % map->capa;
pair = map->buck[hc]; pair = map->buck[hc];
prev = ASE_NULL;
while (pair != ASE_NULL) while (pair != ASE_NULL)
{ {
next = ASE_MAP_NEXT(pair); next = NEXT(pair);
if (map->comper (map, KPTR(pair), KLEN(pair), kptr, klen) == 0) if (map->comper (map, KPTR(pair), KLEN(pair), kptr, klen) == 0)
{ {
@ -367,8 +369,8 @@ int ase_map_putx (
{ {
/* the pair has been reallocated. relink it */ /* the pair has been reallocated. relink it */
if (prev == ASE_NULL) map->buck[hc] = p; if (prev == ASE_NULL) map->buck[hc] = p;
else ASE_MAP_NEXT(prev) = p; else NEXT(prev) = p;
ASE_MAP_NEXT(p) = next; NEXT(p) = next;
} }
if (px != ASE_NULL) *px = p; if (px != ASE_NULL) *px = p;
@ -407,7 +409,7 @@ int ase_map_putx (
pair = alloc_pair (map, kptr, klen, vptr, vlen); pair = alloc_pair (map, kptr, klen, vptr, vlen);
if (pair == ASE_NULL) return -1; /* error */ if (pair == ASE_NULL) return -1; /* error */
ASE_MAP_NEXT(pair) = map->buck[hc]; NEXT(pair) = map->buck[hc];
map->buck[hc] = pair; map->buck[hc] = pair;
map->size++; map->size++;
@ -415,30 +417,44 @@ int ase_map_putx (
return 1; /* new key added */ return 1; /* new key added */
} }
pair_t* ase_map_set (map_t* map, pair_t* ase_map_update (map_t* map,
void* kptr, size_t klen, void* vptr, size_t vlen) void* kptr, size_t klen, void* vptr, size_t vlen)
{ {
pair_t* pair; pair_t* pair, * p, * prev, * next;
size_t hc; size_t hc;
hc = map->hasher(map,kptr,klen) % map->capa; hc = map->hasher(map,kptr,klen) % map->capa;
pair = map->buck[hc]; pair = map->buck[hc];
prev = ASE_NULL;
while (pair != ASE_NULL) while (pair != ASE_NULL)
{ {
next = NEXT(pair);
if (map->comper (map, KPTR(pair), KLEN(pair), kptr, klen) == 0) if (map->comper (map, KPTR(pair), KLEN(pair), kptr, klen) == 0)
{ {
/*TODO: this is wrong... change code .... same way as putx... */ p = change_pair_val (map, pair, vptr, vlen);
return change_pair_val (map, pair, vptr, vlen);
if (p == ASE_NULL) return ASE_NULL; /* change error */
if (p != pair)
{
/* the pair has been reallocated. relink it */
if (prev == ASE_NULL) map->buck[hc] = p;
else NEXT(prev) = p;
NEXT(p) = next;
} }
pair = ASE_MAP_NEXT(pair);
return 0; /* value changed for the existing key */
}
prev = pair;
pair = next;
} }
return ASE_NULL; return ASE_NULL;
} }
int ase_map_remove ( int ase_map_delete (map_t* map, const void* kptr, size_t klen)
map_t* map, const void* kptr, size_t klen)
{ {
pair_t* pair, * prev; pair_t* pair, * prev;
size_t hc; size_t hc;
@ -452,8 +468,8 @@ int ase_map_remove (
if (map->comper (map, KPTR(pair), KLEN(pair), kptr, klen) == 0) if (map->comper (map, KPTR(pair), KLEN(pair), kptr, klen) == 0)
{ {
if (prev == ASE_NULL) if (prev == ASE_NULL)
map->buck[hc] = ASE_MAP_NEXT(pair); map->buck[hc] = NEXT(pair);
else ASE_MAP_NEXT(prev) = ASE_MAP_NEXT(pair); else NEXT(prev) = NEXT(pair);
free_pair (map, pair); free_pair (map, pair);
map->size--; map->size--;
@ -462,7 +478,7 @@ int ase_map_remove (
} }
prev = pair; prev = pair;
pair = ASE_MAP_NEXT(pair); pair = NEXT(pair);
} }
return -1; return -1;
@ -479,7 +495,7 @@ void ase_map_walk (map_t* map, walker_t walker, void* arg)
while (pair != ASE_NULL) while (pair != ASE_NULL)
{ {
next = ASE_MAP_NEXT(pair); next = NEXT(pair);
if (walker(map, pair, arg) == ASE_MAP_WALK_STOP) return; if (walker(map, pair, arg) == ASE_MAP_WALK_STOP) return;
pair = next; pair = next;
} }
@ -510,7 +526,7 @@ pair_t* ase_map_getnextpair (
size_t i; size_t i;
pair_t* next; pair_t* next;
next = ASE_MAP_NEXT(pair); next = NEXT(pair);
if (next != ASE_NULL) if (next != ASE_NULL)
{ {
/* no change in bucket number */ /* no change in bucket number */
@ -556,13 +572,13 @@ static int reorganize (map_t* map)
while (pair != ASE_NULL) while (pair != ASE_NULL)
{ {
pair_t* next = ASE_MAP_NEXT(pair); pair_t* next = NEXT(pair);
hc = map->hasher (map, hc = map->hasher (map,
KPTR(pair), KPTR(pair),
KLEN(pair)) % new_capa; KLEN(pair)) % new_capa;
ASE_MAP_NEXT(pair) = new_buck[hc]; NEXT(pair) = new_buck[hc];
new_buck[hc] = pair; new_buck[hc] = pair;
pair = next; pair = next;