added to ase_map_t a keeper function that is called when a value is replace by itself. deprecated the generic ase_sizer_t and defined context-specific sizer types such as ase_str_sizer_t and ase_map_sizer_t

This commit is contained in:
hyung-hwan 2008-09-24 08:47:23 +00:00
parent ac92d8581a
commit 21c331febf
5 changed files with 59 additions and 33 deletions

View File

@ -1,5 +1,5 @@
/*
* $Id: map.h 373 2008-09-23 11:27:24Z baconevi $
* $Id: map.h 375 2008-09-23 14:47:23Z baconevi $
*
* {License}
*/
@ -43,6 +43,21 @@ typedef int (*ase_map_comper_t) (
ase_size_t klen2 /* the length of a key in bytes */
);
/*
* value keeper
* it is called when a value can be kept without explicit free and copy
*/
typedef void (*ase_map_keeper_t) (
ase_map_t* map,
void* dptr,
ase_size_t dlen
);
/*
* bucket resizer
*/
typedef ase_size_t (*ase_map_sizer_t) (ase_map_t* data, ase_size_t hint);
/* pair visitor - should return ASE_MAP_WALK_STOP or ASE_MAP_WALK_FORWARD */
typedef int (*ase_map_walker_t) (
ase_map_t* map /* a map */,
@ -68,9 +83,10 @@ struct ase_map_t
ase_map_copier_t copier[2];
ase_map_freeer_t freeer[2];
ase_map_hasher_t hasher;
ase_map_comper_t comper;
ase_sizer_t sizer;
ase_map_hasher_t hasher; /* key hasher */
ase_map_comper_t comper; /* key comparator */
ase_map_keeper_t keeper; /* value keeper */
ase_map_sizer_t sizer; /* bucket resizer */
ase_size_t size;
ase_size_t capa;
@ -78,8 +94,6 @@ struct ase_map_t
ase_uint_t factor;
ase_size_t threshold;
ase_map_pair_t** bucket;
void (*sameval) (void* owner, void* vptr, ase_size_t vlen);
};
enum ase_map_id_t
@ -202,14 +216,23 @@ void ase_map_setcomper (
ase_map_comper_t comper
);
ase_sizer_t ase_map_getsizer (
ase_map_keeper_t ase_map_getkeeper (
ase_map_t* map
);
/* the sizer function is called with a map object and map->capa + 1 */
void ase_map_setkeeper (
ase_map_t* map,
ase_map_keeper_t keeper
);
ase_map_sizer_t ase_map_getsizer (
ase_map_t* map
);
/* the sizer function is passed a map object and map->capa + 1 */
void ase_map_setsizer (
ase_map_t* map,
ase_sizer_t sizer
ase_map_sizer_t sizer
);
void* ase_map_getextension (

View File

@ -1,5 +1,5 @@
/*
* $Id: str.h 372 2008-09-23 09:51:24Z baconevi $
* $Id: str.h 375 2008-09-23 14:47:23Z baconevi $
*
* {License}
*/
@ -16,11 +16,12 @@
#define ASE_STR_CHAR(x,idx) ((x)->ptr[idx])
typedef struct ase_str_t ase_str_t;
typedef ase_size_t (*ase_str_sizer_t) (ase_str_t* data, ase_size_t hint);
struct ase_str_t
{
ase_mmgr_t* mmgr;
ase_sizer_t sizer;
ase_str_sizer_t sizer;
ase_char_t* ptr;
ase_size_t len;
@ -225,7 +226,7 @@ int ase_str_yield (
*
* RETURNS: a sizer function set or ASE_NULL if no sizer is set.
*/
ase_sizer_t ase_str_getsizer (
ase_str_sizer_t ase_str_getsizer (
ase_str_t* str /* a dynamic string */
);
@ -241,8 +242,8 @@ ase_sizer_t ase_str_getsizer (
* than the hint passed.
*/
void ase_str_setsizer (
ase_str_t* str /* a dynamic string */,
ase_sizer_t sizer /* a sizer function */
ase_str_t* str /* a dynamic string */,
ase_str_sizer_t sizer /* a sizer function */
);
/*

View File

@ -1,5 +1,5 @@
/*
* $Id: types.h 372 2008-09-23 09:51:24Z baconevi $
* $Id: types.h 375 2008-09-23 14:47:23Z baconevi $
*
* {License}
*/
@ -359,14 +359,4 @@ struct ase_ccls_t
void* data;
};
/*
* NAME: determine the size of data
*
* DESCRIPTION:
* The ase_sizer_t is a generic type used by many other modules usually to
* get the new size for resizing data structure.
*/
typedef ase_size_t (*ase_sizer_t) (void* data, ase_size_t hint);
#endif

View File

@ -1,5 +1,5 @@
/*
* $Id: map.c 374 2008-09-23 11:37:38Z baconevi $
* $Id: map.c 375 2008-09-23 14:47:23Z baconevi $
*
* {License}
*/
@ -13,6 +13,8 @@
#define freeer_t ase_map_freeer_t
#define hasher_t ase_map_hasher_t
#define comper_t ase_map_comper_t
#define keeper_t ase_map_keeper_t
#define sizer_t ase_map_sizer_t
#define walker_t ase_map_walker_t
#define KPTR(p) ASE_MAP_KPTR(p)
@ -132,9 +134,9 @@ static pair_t* change_pair_val (
/* if the old value and the new value are the same,
* it just calls the handler for this condition.
* No value replacement occurs. */
if (map->sameval != ASE_NULL)
if (map->keeper != ASE_NULL)
{
map->sameval (map, vptr, vlen);
map->keeper (map, vptr, vlen);
}
}
else
@ -319,12 +321,22 @@ void ase_map_setcomper (map_t* map, comper_t comper)
map->comper = comper;
}
ase_sizer_t ase_map_getsizer (map_t* map)
keeper_t ase_map_getkeeper (map_t* map)
{
return map->keeper;
}
void ase_map_setkeeper (map_t* map, keeper_t keeper)
{
map->keeper = keeper;
}
sizer_t ase_map_getsizer (map_t* map)
{
return map->sizer;
}
void ase_map_setsizer (map_t* map, ase_sizer_t sizer)
void ase_map_setsizer (map_t* map, sizer_t sizer)
{
map->sizer = sizer;
}

View File

@ -1,5 +1,5 @@
/*
* $Id: str_dyn.c 373 2008-09-23 11:27:24Z baconevi $
* $Id: str_dyn.c 375 2008-09-23 14:47:23Z baconevi $
*
* {License}
*/
@ -82,12 +82,12 @@ int ase_str_yield (ase_str_t* str, ase_xstr_t* buf, int new_capa)
return 0;
}
ase_sizer_t ase_str_getsizer (ase_str_t* str)
ase_str_sizer_t ase_str_getsizer (ase_str_t* str)
{
return str->sizer;
}
void ase_str_setsizer (ase_str_t* str, ase_sizer_t sizer)
void ase_str_setsizer (ase_str_t* str, ase_str_sizer_t sizer)
{
str->sizer = sizer;
}