still modifying ase_lda_t

This commit is contained in:
hyung-hwan 2008-10-09 05:43:56 +00:00
parent b461e7f30d
commit dd2d9190c0
5 changed files with 178 additions and 34 deletions

View File

@ -10,6 +10,11 @@
#include <ase/types.h>
#include <ase/macros.h>
enum ase_lda_opt_t
{
ASE_LDA_NODLEN = (((ase_uint4_t)1) << 16)
};
/****o* ase.cmn.lda/linear dynamic array
* DESCRIPTION
* <ase/cmn/lda.h> provides a linear dynamic array. It grows as more items
@ -38,6 +43,7 @@ typedef struct ase_lda_cell_t ase_lda_cell_t;
#define ASE_LDA_COPIER(lda) ((lda)->copier)
#define ASE_LDA_FREEER(lda) ((lda)->freeer)
#define ASE_LDA_COMPER(lda) ((lda)->comper)
#define ASE_LDA_SIZER(lda) ((lda)->sizer)
/****b* ase.cmn.lda/ase_lda_copier_t
@ -63,7 +69,7 @@ typedef struct ase_lda_cell_t ase_lda_cell_t;
* SYNOPSIS
*/
typedef void* (*ase_lda_copier_t) (
ase_lda_t* lda /* a map */,
ase_lda_t* lda /* a lda */,
void* dptr /* the pointer to data to copy */,
ase_size_t dlen /* the length of data to copy */
);
@ -75,7 +81,7 @@ typedef void* (*ase_lda_copier_t) (
* SYNOPSIS
*/
typedef void (*ase_lda_freeer_t) (
ase_lda_t* lda /* a map */,
ase_lda_t* lda /* a lda */,
void* dptr /* the pointer to data to free */,
ase_size_t dlen /* the length of data to free */
);
@ -104,6 +110,23 @@ typedef int (*ase_lda_comper_t) (
);
/******/
/****t* ase.cmn.lda/ase_lda_sizer_t
* NAME
* ase_lda_sizer_t - define an array size calculator
*
* DESCRIPTION
* The ase_lda_sizer_t type defines an array size claculator that is called
* when the array needs to be resized.
*
* SYNOPSIS
*/
typedef ase_size_t (*ase_lda_sizer_t) (
ase_lda_t* lda, /* a linear dynamic array */
ase_size_t hint /* a sizing hint */
);
/******/
/****s* ase.cmn.lda/ase_lda_t
* NAME
* ase_lda_t - define a linear dynamic array
@ -117,12 +140,16 @@ struct ase_lda_t
ase_lda_copier_t copier; /* data copier */
ase_lda_freeer_t freeer; /* data freeer */
ase_lda_comper_t comper; /* data comparator */
ase_lda_sizer_t sizer; /* size calculator */
ase_byte_t scale; /* scale factor */
ase_uint16_t opt;
ase_size_t size; /* the number of items */
ase_size_t capa; /* capacity */
ase_lda_cell_t** cell;
/*ase_lda_cell_t** cell;*/
void* cell;
};
/******/
@ -151,8 +178,9 @@ extern "C" {
*/
ase_lda_t* ase_lda_open (
ase_mmgr_t* lda,
ase_size_t ext,
ase_size_t capa
ase_size_t ext,
ase_size_t capa,
ase_uint4_t opt
);
/******/
@ -228,6 +256,83 @@ void ase_lda_setmmgr (
);
/******/
int ase_lda_getscale (
ase_lda_t* lda /* a lda */
);
/****f* ase.cmn.lda/ase_lda_setscale
* NAME
* ase_lda_setscale - set the scale factor
*
* DESCRIPTION
* The ase_lda_setscale() function sets the scale factor of the length
* of a key and a value. A scale factor determines the actual length of
* a key and a value in bytes. A lda is created with a scale factor of 1.
* The scale factor should be larger than 0 and less than 256.
*
* NOTES
* It is a bad idea to change the scale factor when a lda is not empty.
*
* SYNOPSIS
*/
void ase_lda_setscale (
ase_lda_t* lda /* a lda */,
int scale /* a scale factor */
);
/******/
ase_lda_copier_t ase_lda_getcopier (
ase_lda_t* lda /* a lda */
);
/****f* ase.cmn.lda/ase_lda_setcopier
* NAME
* ase_lda_setcopier - specify how to clone an element
*
* DESCRIPTION
* A special copier ASE_MAP_COPIER_INLINE is provided. This copier enables
* you to copy the data inline to the internal node. No freeer is invoked
* when the node is freeed.
*
* You may set the copier to ASE_NULL to perform no special operation
* when the data pointer is rememebered.
*
* SYNOPSIS
*/
void ase_lda_setcopier (
ase_lda_t* lda /* a lda */,
ase_lda_copier_t copier /* an element copier */
);
/******/
ase_lda_freeer_t ase_lda_getfreeer (
ase_lda_t* lda /* a lda */
);
/****f* ase.cmn.lda/ase_lda_setfreeer
* NAME
* ase_lda_setfreeer - specify how to destroy an element
*
* DESCRIPTION
* The freeer is called when a node containing the element is destroyed.
*
* SYNOPSIS
*/
void ase_lda_setfreeer (
ase_lda_t* lda /* a lda */,
ase_lda_freeer_t freeer /* an element freeer */
);
/******/
ase_lda_sizer_t ase_lda_getsizer (
ase_lda_t* lda
);
void ase_lda_setsizer (
ase_lda_t* lda,
ase_lda_sizer_t sizer
);
ase_size_t ase_lda_getsize (
ase_lda_t* lda
);

View File

@ -1,5 +1,5 @@
/*
* $Id: map.h 408 2008-10-07 11:30:16Z baconevi $
* $Id: map.h 409 2008-10-08 11:43:56Z baconevi $
*
* {License}
*/
@ -115,17 +115,35 @@ typedef void (*ase_map_keeper_t) (
);
/******/
/*
* bucket resizer
/****t* ase.cmn.map/ase_map_sizer_t
* NAME
* ase_map_sizer_t - define a bucket size calculator
*
* DESCRIPTION
* The ase_map_sizer_T type defines a bucket size claculator that is called
* when a map should resize the bucket. The current bucket size +1 is passed
* as the hint.
*
* SYNOPSIS
*/
typedef ase_size_t (*ase_map_sizer_t) (ase_map_t* data, ase_size_t hint);
typedef ase_size_t (*ase_map_sizer_t) (
ase_map_t* map, /* a map */
ase_size_t hint /* a sizing hint */
);
/******/
/* pair visitor - should return ASE_MAP_WALK_STOP or ASE_MAP_WALK_FORWARD */
/****t* ase.cmn.map/ase_map_walker_t
* NAME
* ase_map_walker_t - define a pair visitor
*
* SYNOPSIS
*/
typedef ase_map_walk_t (*ase_map_walker_t) (
ase_map_t* map /* a map */,
ase_map_pair_t* pair /* the pointer to a key/value pair */,
void* arg /* the pointer to user-defined data */
);
/******/
/****s* ase.cmn.map/ase_map_pair_t
* NAME

View File

@ -12,15 +12,23 @@
#define copier_t ase_lda_copier_t
#define freeer_t ase_lda_freeer_t
#define comper_t ase_lda_comper_t
#define sizer_t ase_lda_sizer_t
#define walker_t ase_lda_walker_t
#define size_t ase_size_t
#define mmgr_t ase_mmgr_t
#define size_t ase_size_t
#define uint4_t ase_uint4_t
#define TOB(lda,len) ((len)*(lda)->scale)
#define DPTR(cell) ASE_LDA_DPTR(cell)
#define DLEN(cell) ASE_LDA_DLEN(cell)
#define INVALID ASE_LDA_INVALID
/* get cell size */
#define CS(lda) ((lda)->opt & 0xFFFF)
/* check if the option is set */
#define OPT(lda,o) ((lda)->opt & (o))
static int comp_data (lda_t* lda,
const void* dptr1, size_t dlen1,
const void* dptr2, size_t dlen2)
@ -44,8 +52,7 @@ static cell_t* alloc_cell (lda_t* lda, void* dptr, size_t dlen)
{
cell_t* n;
if (lda->copier == ASE_NULL ||
lda->copier == ASE_LDA_COPIER_SIMPLE)
if (lda->copier == ASE_LDA_COPIER_SIMPLE)
{
n = ASE_MMGR_ALLOC (lda->mmgr, ASE_SIZEOF(cell_t));
if (n == ASE_NULL) return ASE_NULL;
@ -77,7 +84,7 @@ static cell_t* alloc_cell (lda_t* lda, void* dptr, size_t dlen)
return n;
}
lda_t* ase_lda_open (mmgr_t* mmgr, size_t ext, size_t capa)
lda_t* ase_lda_open (mmgr_t* mmgr, size_t ext, size_t capa, uint4_t opt)
{
lda_t* lda;
@ -94,7 +101,7 @@ lda_t* ase_lda_open (mmgr_t* mmgr, size_t ext, size_t capa)
lda = ASE_MMGR_ALLOC (mmgr, ASE_SIZEOF(lda_t) + ext);
if (lda == ASE_NULL) return ASE_NULL;
return ase_lda_init (lda, mmgr, capa);
return ase_lda_init (lda, mmgr, capa, opt);
}
void ase_lda_close (lda_t* lda)
@ -103,15 +110,17 @@ void ase_lda_close (lda_t* lda)
ASE_MMGR_FREE (lda->mmgr, lda);
}
lda_t* ase_lda_init (lda_t* lda, mmgr_t* mmgr, size_t capa)
lda_t* ase_lda_init (lda_t* lda, mmgr_t* mmgr, size_t capa, uint4_t opt)
{
ASE_MEMSET (lda, 0, ASE_SIZEOF(*lda));
lda->mmgr = mmgr;
lda->size = 0;
lda->capa = 0;
lda->opt = opt;
lda->cell = ASE_NULL;
lda->copier = ASE_LDA_COPIER_SIMPLE;
lda->comper = comp_data;
if (ase_lda_setcapa (lda, capa) == ASE_NULL) return ASE_NULL;
@ -168,6 +177,7 @@ copier_t ase_lda_getcopier (lda_t* lda)
void ase_lda_setcopier (lda_t* lda, copier_t copier)
{
if (copier == ASE_NULL) copier = ASE_LDA_COPIER_SIMPLE;
lda->copier = copier;
}
@ -192,7 +202,15 @@ void ase_lda_setcomper (lda_t* lda, comper_t comper)
lda->comper = comper;
}
sizer_t ase_lda_getsizer (lda_t* lda)
{
return lda->sizer;
}
void ase_lda_setsizer (lda_t* lda, sizer_t sizer)
{
lda->sizer = sizer;
}
size_t ase_lda_getsize (lda_t* lda)
{
@ -218,21 +236,25 @@ lda_t* ase_lda_setcapa (lda_t* lda, size_t capa)
{
if (lda->mmgr->realloc != ASE_NULL)
{
tmp = ASE_MMGR_REALLOC (lda->mmgr,
lda->cell, ASE_SIZEOF(*lda->cell) * capa);
/*tmp = ASE_MMGR_REALLOC (lda->mmgr,
lda->cell, ASE_SIZEOF(*lda->cell) * capa);*/
tmp = ASE_MMGR_REALLOC (lda->mmgr, CS(lda)*capa);
if (tmp == ASE_NULL) return ASE_NULL;
}
else
{
tmp = ASE_MMGR_ALLOC (
lda->mmgr, ASE_SIZEOF(*lda->cell) * capa);
/*tmp = ASE_MMGR_ALLOC (
lda->mmgr, ASE_SIZEOF(*lda->cell) * capa);*/
tmp = ASE_MMGR_ALLOC (lda->mmgr, CS(lda)*capa);
if (tmp == ASE_NULL) return ASE_NULL;
if (lda->cell != ASE_NULL)
{
size_t x;
x = (capa > lda->capa)? lda->capa: capa;
ASE_MEMCPY (tmp, lda->cell,
ASE_SIZEOF(*lda->cell) * x);
/*ASE_MEMCPY (tmp, lda->cell,
ASE_SIZEOF(*lda->cell) * x);*/
ASE_MEMCPY (tmp, lda->cell, CS(lda)*x);
ASE_MMGR_FREE (lda->mmgr, lda->cell);
}
}
@ -396,7 +418,7 @@ size_t ase_lda_adduniq (
lda_t* lda, ase_char_t* str, size_t len)
{
size_t i;
i = ase_lda_find (lda, 0, str, len);
i = ase_lda_search (lda, 0, str, len);
if (i != INVALID) return i; /* found. return the current index */
/* insert a new entry */

View File

@ -1,5 +1,5 @@
/*
* $Id: map.c 408 2008-10-07 11:30:16Z baconevi $
* $Id: map.c 409 2008-10-08 11:43:56Z baconevi $
*
* {License}
*/
@ -75,8 +75,7 @@ static pair_t* alloc_pair (map_t* map,
NEXT(n) = ASE_NULL;
KLEN(n) = klen;
if (kcop == ASE_NULL ||
kcop == ASE_MAP_COPIER_SIMPLE)
if (kcop == ASE_MAP_COPIER_SIMPLE)
{
KPTR(n) = kptr;
}
@ -96,8 +95,7 @@ static pair_t* alloc_pair (map_t* map,
}
VLEN(n) = vlen;
if (vcop == ASE_NULL ||
vcop == ASE_MAP_COPIER_SIMPLE)
if (vcop == ASE_MAP_COPIER_SIMPLE)
{
VPTR(n) = vptr;
}
@ -152,8 +150,7 @@ static pair_t* change_pair_val (
size_t ovlen = VLEN(pair);
/* place the new value according to the copier */
if (vcop == ASE_NULL ||
vcop == ASE_MAP_COPIER_SIMPLE)
if (vcop == ASE_MAP_COPIER_SIMPLE)
{
VPTR(pair) = vptr;
VLEN(pair) = vlen;
@ -258,10 +255,10 @@ map_t* ase_map_init (map_t* map, mmgr_t* mmgr, size_t capa, int factor)
map->hasher = hash_key;
map->comper = comp_key;
map->copier[ASE_MAP_KEY] = ASE_MAP_COPIER_SIMPLE;
map->copier[ASE_MAP_VAL] = ASE_MAP_COPIER_SIMPLE;
/*
map->copier[ASE_MAP_KEY] = ASE_NULL;
map->copier[ASE_MAP_VAL] = ASE_NULL;
map->freeer[ASE_MAP_KEY] = ASE_NULL;
map->freeer[ASE_MAP_VAL] = ASE_NULL;
map->keeper = ASE_NULL;
@ -324,6 +321,7 @@ void ase_map_setcopier (map_t* map, ase_map_id_t id, copier_t copier)
{
ASE_ASSERTX (id == ASE_MAP_KEY || id == ASE_MAP_VAL,
"The ID should be either ASE_MAP_KEY or ASE_MAP_VAL");
if (copier == ASE_NULL) copier = ASE_MAP_COPIER_SIMPLE;
map->copier[id] = copier;
}

View File

@ -41,8 +41,7 @@ static node_t* alloc_node (sll_t* sll, void* dptr, size_t dlen)
{
node_t* n;
if (sll->copier == ASE_NULL ||
sll->copier == ASE_SLL_COPIER_SIMPLE)
if (sll->copier == ASE_SLL_COPIER_SIMPLE)
{
n = ASE_MMGR_ALLOC (sll->mmgr, SIZEOF(node_t));
if (n == ASE_NULL) return ASE_NULL;
@ -111,6 +110,7 @@ sll_t* ase_sll_init (sll_t* sll, mmgr_t* mmgr)
sll->scale = 1;
sll->comper = comp_data;
sll->copier = ASE_SLL_COPIER_SIMPLE;
return sll;
}
@ -157,6 +157,7 @@ copier_t ase_sll_getcopier (sll_t* sll)
void ase_sll_setcopier (sll_t* sll, copier_t copier)
{
if (copier == ASE_NULL) copier = ASE_SLL_COPIER_SIMPLE;
sll->copier = copier;
}