diff --git a/ase/include/ase/cmn/map.h b/ase/include/ase/cmn/map.h index f22546d2..3d0d45a0 100644 --- a/ase/include/ase/cmn/map.h +++ b/ase/include/ase/cmn/map.h @@ -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} */ @@ -231,6 +231,26 @@ int ase_map_putx ( 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 */ ase_map_pair_t* ase_map_update ( ase_map_t* map /* a map */, diff --git a/ase/lib/cmn/map.c b/ase/lib/cmn/map.c index a22d4553..dc918d9c 100644 --- a/ase/lib/cmn/map.c +++ b/ase/lib/cmn/map.c @@ -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} */ @@ -47,9 +47,7 @@ static int comp_key (map_t* map, const void* kptr2, size_t klen2) { if (klen1 == klen2) return ASE_MEMCMP (kptr1, kptr2, klen1); - - /* when the length are different it indicate that they are - * just different */ + /* it just returns 1 to indicate that they are different. */ return 1; } @@ -127,7 +125,6 @@ static void free_pair (map_t* map, pair_t* pair) static pair_t* change_pair_val ( map_t* map, pair_t* pair, void* vptr, size_t vlen) { - /* use this function with care */ if (VPTR(pair) == vptr) { /* if the old value and the new value are the same, @@ -381,8 +378,7 @@ int ase_map_putx ( pair = next; } - if (map->threshold > 0 && - map->size >= map->threshold) + if (map->threshold > 0 && map->size >= map->threshold) { if (reorganize(map) == 0) /* ignore the error */ { @@ -390,7 +386,6 @@ int ase_map_putx ( } } - ASE_ASSERT (pair == ASE_NULL); #if 0 @@ -417,8 +412,46 @@ int ase_map_putx ( 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; size_t hc;