Recovered from cvs revision 2007-07-26 14:33:00

This commit is contained in:
hyung-hwan 2007-07-29 23:42:00 +00:00
parent 21550f9acf
commit 2f04ca531b
7 changed files with 75 additions and 17 deletions

View File

@ -1,5 +1,5 @@
/* /*
* $Id: Awk.cpp,v 1.46 2007/07/15 16:31:59 bacon Exp $ * $Id: Awk.cpp,v 1.47 2007/07/25 07:00:09 bacon Exp $
*/ */
#include <ase/awk/Awk.hpp> #include <ase/awk/Awk.hpp>
@ -503,7 +503,7 @@ namespace ASE
} }
functionMap = ase_awk_map_open ( functionMap = ase_awk_map_open (
this, 512, freeFunctionMapValue, awk); this, 512, 70, freeFunctionMapValue, awk);
if (functionMap == ASE_NULL) if (functionMap == ASE_NULL)
{ {
ase_awk_close (awk); ase_awk_close (awk);

View File

@ -1,5 +1,5 @@
/* /*
* $Id: awk.c,v 1.7 2007/06/17 14:54:10 bacon Exp $ * $Id: awk.c,v 1.8 2007/07/25 07:00:09 bacon Exp $
* *
* {License} * {License}
*/ */
@ -58,7 +58,7 @@ ase_awk_t* ase_awk_open (const ase_awk_prmfns_t* prmfns, void* custom_data)
return ASE_NULL; return ASE_NULL;
} }
awk->kwtab = ase_awk_map_open (awk, 512, free_kw, awk); awk->kwtab = ase_awk_map_open (awk, 512, 70, free_kw, awk);
if (awk->kwtab == ASE_NULL) if (awk->kwtab == ASE_NULL)
{ {
ase_str_close (&awk->token.name); ase_str_close (&awk->token.name);
@ -67,7 +67,7 @@ ase_awk_t* ase_awk_open (const ase_awk_prmfns_t* prmfns, void* custom_data)
} }
/* TODO: initial map size?? */ /* TODO: initial map size?? */
awk->tree.afns = ase_awk_map_open (awk, 512, free_afn, awk); awk->tree.afns = ase_awk_map_open (awk, 512, 70, free_afn, awk);
if (awk->tree.afns == ASE_NULL) if (awk->tree.afns == ASE_NULL)
{ {
ase_awk_map_close (awk->kwtab); ase_awk_map_close (awk->kwtab);

View File

@ -1,15 +1,13 @@
/* /*
* $Id: map.c,v 1.6 2007/05/06 08:52:18 bacon Exp $ * $Id: map.c,v 1.9 2007/07/25 09:53:28 bacon Exp $
* *
* {License} * {License}
*/ */
#include <ase/awk/awk_i.h> #include <ase/awk/awk_i.h>
/* TODO: improve the entire map routines.
support automatic bucket resizing and remaping, etc. */
static ase_size_t hashkey (const ase_char_t* keyptr, ase_size_t keylen); static ase_size_t hashkey (const ase_char_t* keyptr, ase_size_t keylen);
static int rehash (ase_awk_map_t* map);
#define FREE_PAIR(map,pair) \ #define FREE_PAIR(map,pair) \
do { \ do { \
@ -20,7 +18,7 @@ static ase_size_t hashkey (const ase_char_t* keyptr, ase_size_t keylen);
} while (0) } while (0)
ase_awk_map_t* ase_awk_map_open ( ase_awk_map_t* ase_awk_map_open (
void* owner, ase_size_t capa, void* owner, ase_size_t capa, unsigned int factor,
void(*freeval)(void*,void*), ase_awk_t* awk) void(*freeval)(void*,void*), ase_awk_t* awk)
{ {
ase_awk_map_t* map; ase_awk_map_t* map;
@ -46,6 +44,9 @@ ase_awk_map_t* ase_awk_map_open (
map->freeval = freeval; map->freeval = freeval;
while (capa > 0) map->buck[--capa] = ASE_NULL; while (capa > 0) map->buck[--capa] = ASE_NULL;
map->factor = factor;
map->threshold = ((ase_size_t)map->factor) * map->capa / 100;
return map; return map;
} }
@ -93,7 +94,6 @@ ase_awk_pair_t* ase_awk_map_get (
while (pair != ASE_NULL) while (pair != ASE_NULL)
{ {
if (ase_strxncmp ( if (ase_strxncmp (
ASE_AWK_PAIR_KEYPTR(pair), ASE_AWK_PAIR_KEYLEN(pair), ASE_AWK_PAIR_KEYPTR(pair), ASE_AWK_PAIR_KEYLEN(pair),
keyptr, keylen) == 0) return pair; keyptr, keylen) == 0) return pair;
@ -142,6 +142,15 @@ int ase_awk_map_putx (
pair = ASE_AWK_PAIR_LNK(pair); pair = ASE_AWK_PAIR_LNK(pair);
} }
if (map->threshold > 0 &&
map->size >= map->threshold)
{
if (rehash(map) == 0) /* ignore the rehash error */
{
hc = hashkey(keyptr,keylen) % map->capa;
}
}
pair = (ase_awk_pair_t*) ASE_AWK_MALLOC ( pair = (ase_awk_pair_t*) ASE_AWK_MALLOC (
map->awk, ASE_SIZEOF(ase_awk_pair_t)); map->awk, ASE_SIZEOF(ase_awk_pair_t));
if (pair == ASE_NULL) return -1; /* error */ if (pair == ASE_NULL) return -1; /* error */
@ -286,3 +295,48 @@ static ase_size_t hashkey (const ase_char_t* keyptr, ase_size_t keylen)
return n; return n;
} }
static int rehash (ase_awk_map_t* map)
{
ase_size_t i, hc, new_capa;
ase_awk_pair_t** new_buck;
new_capa = (map->capa >= 65536)? (map->capa + 65536): (map->capa << 1);
new_buck = (ase_awk_pair_t**) ASE_AWK_MALLOC (
map->awk, ASE_SIZEOF(ase_awk_pair_t*) * new_capa);
if (new_buck == ASE_NULL)
{
/* once rehash fails, the rehashing is disabled */
map->threshold = 0;
return -1;
}
for (i = 0; i < new_capa; i++) new_buck[i] = ASE_NULL;
for (i = 0; i < map->capa; i++)
{
ase_awk_pair_t* pair = map->buck[i];
while (pair != ASE_NULL)
{
ase_awk_pair_t* next = ASE_AWK_PAIR_LNK(pair);
hc = hashkey(
ASE_AWK_PAIR_KEYPTR(pair),
ASE_AWK_PAIR_KEYLEN(pair)) % new_capa;
ASE_AWK_PAIR_LNK(pair) = new_buck[hc];
new_buck[hc] = pair;
pair = next;
}
}
ASE_AWK_FREE (map->awk, map->buck);
map->buck = new_buck;
map->capa = new_capa;
map->threshold = ((ase_size_t)map->factor) * map->capa / 100;
return 0;
}

View File

@ -1,5 +1,5 @@
/* /*
* $Id: map.h,v 1.6 2007/05/06 08:52:18 bacon Exp $ * $Id: map.h,v 1.7 2007/07/25 07:00:09 bacon Exp $
* *
* {License} * {License}
*/ */
@ -31,6 +31,8 @@ struct ase_awk_map_t
void* owner; void* owner;
ase_size_t size; ase_size_t size;
ase_size_t capa; ase_size_t capa;
unsigned int factor;
ase_size_t threshold;
ase_awk_pair_t** buck; ase_awk_pair_t** buck;
void (*freeval) (void*,void*); void (*freeval) (void*,void*);
ase_awk_t* awk; ase_awk_t* awk;
@ -46,7 +48,7 @@ extern "C" {
#endif #endif
ase_awk_map_t* ase_awk_map_open ( ase_awk_map_t* ase_awk_map_open (
void* owner, ase_size_t capa, void* owner, ase_size_t capa, unsigned int factor,
void(*freeval)(void*,void*), ase_awk_t* awk); void(*freeval)(void*,void*), ase_awk_t* awk);
void ase_awk_map_close (ase_awk_map_t* map); void ase_awk_map_close (ase_awk_map_t* map);

View File

@ -1,5 +1,5 @@
/* /*
* $Id: run.c,v 1.8 2007/05/28 13:53:31 bacon Exp $ * $Id: run.c,v 1.9 2007/07/25 07:00:09 bacon Exp $
* *
* {License} * {License}
*/ */
@ -749,7 +749,7 @@ static int __init_run (
} }
run->named = ase_awk_map_open ( run->named = ase_awk_map_open (
run, DEF_BUF_CAPA, __free_namedval, run->awk); run, 1024, 70, __free_namedval, run->awk);
if (run->named == ASE_NULL) if (run->named == ASE_NULL)
{ {
ase_str_close (&run->format.fmt); ase_str_close (&run->format.fmt);

View File

@ -1,5 +1,5 @@
/* /*
* $Id: val.c,v 1.6 2007/05/10 16:08:37 bacon Exp $ * $Id: val.c,v 1.7 2007/07/25 07:00:09 bacon Exp $
* *
* {License} * {License}
*/ */
@ -242,7 +242,7 @@ ase_awk_val_t* ase_awk_makemapval (ase_awk_run_t* run)
val->type = ASE_AWK_VAL_MAP; val->type = ASE_AWK_VAL_MAP;
val->ref = 0; val->ref = 0;
val->map = ase_awk_map_open (run, 256, free_map_val, run->awk); val->map = ase_awk_map_open (run, 256, 70, free_map_val, run->awk);
if (val->map == ASE_NULL) if (val->map == ASE_NULL)
{ {
ASE_AWK_FREE (run->awk, val); ASE_AWK_FREE (run->awk, val);

View File

@ -15,6 +15,8 @@
* changed test/awk/AseAwk.java to include an option(-w) to utilize * changed test/awk/AseAwk.java to include an option(-w) to utilize
the setWord method. the setWord method.
* enhanced awk/map.c to support automatic rehashing
[0.2.0] [0.2.0]
* fixed bug (nextofile shown as nextfile in source output) * fixed bug (nextofile shown as nextfile in source output)