From 2fbbff81393185eecce9c44c3eb2c62ab1934f72 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Wed, 24 Sep 2008 05:27:24 +0000 Subject: [PATCH] added the sizer function to ase_map_t --- ase/include/ase/cmn/map.h | 13 ++++++++++++- ase/lib/cmn/map.c | 29 +++++++++++++++++++++++++---- ase/lib/cmn/str_dyn.c | 8 ++++++-- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/ase/include/ase/cmn/map.h b/ase/include/ase/cmn/map.h index 4450007b..cef84853 100644 --- a/ase/include/ase/cmn/map.h +++ b/ase/include/ase/cmn/map.h @@ -1,5 +1,5 @@ /* - * $Id: map.h 354 2008-08-31 10:57:24Z baconevi $ + * $Id: map.h 373 2008-09-23 11:27:24Z baconevi $ * * {License} */ @@ -70,6 +70,7 @@ struct ase_map_t ase_map_freeer_t freeer[2]; ase_map_hasher_t hasher; ase_map_comper_t comper; + ase_sizer_t sizer; ase_size_t size; ase_size_t capa; @@ -201,6 +202,16 @@ void ase_map_setcomper ( ase_map_comper_t comper ); +ase_sizer_t ase_map_getsizer ( + ase_map_t* map +); + +/* the sizer function is called with a map object and map->capa + 1 */ +void ase_map_setsizer ( + ase_map_t* map, + ase_sizer_t sizer +); + void* ase_map_getextension ( ase_map_t* map ); diff --git a/ase/lib/cmn/map.c b/ase/lib/cmn/map.c index 4402a368..79dd14f0 100644 --- a/ase/lib/cmn/map.c +++ b/ase/lib/cmn/map.c @@ -1,5 +1,5 @@ /* - * $Id: map.c 356 2008-08-31 11:16:52Z baconevi $ + * $Id: map.c 373 2008-09-23 11:27:24Z baconevi $ * * {License} */ @@ -315,6 +315,16 @@ void ase_map_setcomper (map_t* map, comper_t comper) map->comper = comper; } +ase_sizer_t ase_map_getsizer (map_t* map) +{ + return map->sizer; +} + +void ase_map_setsizer (map_t* map, ase_sizer_t sizer) +{ + map->sizer = sizer; +} + void* ase_map_getextension (map_t* map) { return map + 1; @@ -601,9 +611,20 @@ static int reorganize (map_t* map) size_t i, hc, new_capa; pair_t** new_buck; - /* the bucket is doubled until it grows up to 65536 slots. - * once it has reached it, it grows by 65536 slots */ - new_capa = (map->capa >= 65536)? (map->capa + 65536): (map->capa << 1); + if (map->sizer) + { + new_capa = map->sizer (map, map->capa + 1); + + /* if no change in capacity, return success + * without reorganization */ + if (new_capa == map->capa) return 0; + } + else + { + /* the bucket is doubled until it grows up to 65536 slots. + * once it has reached it, it grows by 65536 slots */ + new_capa = (map->capa >= 65536)? (map->capa + 65536): (map->capa << 1); + } new_buck = (pair_t**) ASE_MMGR_ALLOC ( map->mmgr, SIZEOF(pair_t*) * new_capa); diff --git a/ase/lib/cmn/str_dyn.c b/ase/lib/cmn/str_dyn.c index 80d232a9..a487bb0e 100644 --- a/ase/lib/cmn/str_dyn.c +++ b/ase/lib/cmn/str_dyn.c @@ -1,5 +1,5 @@ /* - * $Id: str_dyn.c 372 2008-09-23 09:51:24Z baconevi $ + * $Id: str_dyn.c 373 2008-09-23 11:27:24Z baconevi $ * * {License} */ @@ -213,6 +213,8 @@ ase_size_t ase_str_ncat (ase_str_t* str, const ase_char_t* s, ase_size_t len) /* let the user determine the new capacity. * pass the minimum capacity required as a hint */ ncapa = str->sizer (str, str->len + len); + /* if no change in capacity, return current length */ + if (ncapa == str->capa) return str->len; } if (ase_str_setcapa (str, ncapa) == (ase_size_t)-1) @@ -223,7 +225,8 @@ ase_size_t ase_str_ncat (ase_str_t* str, const ase_char_t* s, ase_size_t len) if (len > str->capa - str->len) { - /* copy as many characters as the number of cells available */ + /* copy as many characters as the number of cells available. + * if the capacity has been decreased, len is adjusted here */ len = str->capa - str->len; } @@ -233,6 +236,7 @@ ase_size_t ase_str_ncat (ase_str_t* str, const ase_char_t* s, ase_size_t len) str->len += len; str->ptr[str->len] = ASE_T('\0'); } + return str->len; }