improving and fixing ase_lda_t

This commit is contained in:
hyung-hwan 2008-10-06 08:52:15 +00:00
parent 602be420ee
commit 76580ef4eb
2 changed files with 221 additions and 114 deletions

View File

@ -20,7 +20,23 @@
*/ */
typedef struct ase_lda_t ase_lda_t; typedef struct ase_lda_t ase_lda_t;
typedef struct ase_lda_slot_t ase_lda_slot_t; typedef struct ase_lda_cell_t ase_lda_cell_t;
#define ASE_LDA_COPIER_INLINE ase_lda_copyinline
#define ASE_LDA_INVALID ((ase_size_t)-1)
#define ASE_LDA_SIZE(lda) ((lda)->size)
#define ASE_LDA_CAPA(lda) ((lda)->capa)
#define ASE_LDA_CELL(lda,index) ((lda)->cell[index])
#define ASE_LDA_DPTR(cell) ((cell)->dptr)
#define ASE_LDA_DLEN(cell) ((cell)->dlen)
#define ASE_LDA_MMGR(lda) ((lda)->mmgr)
#define ASE_LDA_COPIER(lda) ((lda)->copier)
#define ASE_LDA_FREEER(lda) ((lda)->freeer)
#define ASE_LDA_COMPER(lda) ((lda)->comper)
/****b* ase.cmn.lda/ase_lda_copier_t /****b* ase.cmn.lda/ase_lda_copier_t
@ -70,7 +86,7 @@ typedef void (*ase_lda_freeer_t) (
* *
* DESCRIPTION * DESCRIPTION
* The ase_lda_comper_t type defines a key comparator that is called when * The ase_lda_comper_t type defines a key comparator that is called when
* the list needs to compare data. A singly linked list is created with a * the list needs to compare data. A linear dynamic array is created with a
* default comparator that performs bitwise comparison. * default comparator that performs bitwise comparison.
* *
* The comparator should return 0 if the data are the same and a non-zero * The comparator should return 0 if the data are the same and a non-zero
@ -79,7 +95,7 @@ typedef void (*ase_lda_freeer_t) (
* SYNOPSIS * SYNOPSIS
*/ */
typedef int (*ase_lda_comper_t) ( typedef int (*ase_lda_comper_t) (
ase_lda_t* lda /* a singly linked list */, ase_lda_t* lda /* a linear dynamic array */,
const void* dptr1 /* a data pointer */, const void* dptr1 /* a data pointer */,
ase_size_t dlen1 /* a data length */, ase_size_t dlen1 /* a data length */,
const void* dptr2 /* a data pointer */, const void* dptr2 /* a data pointer */,
@ -105,17 +121,17 @@ struct ase_lda_t
ase_size_t size; /* the number of items */ ase_size_t size; /* the number of items */
ase_size_t capa; /* capacity */ ase_size_t capa; /* capacity */
ase_lda_slot_t** slot; ase_lda_cell_t** cell;
}; };
/******/ /******/
/****s* /****s*
* NAME * NAME
* ase_lda_slot_t - define a linear dynamic array slot * ase_lda_cell_t - define a linear dynamic array cell
* *
* SYNOPSIS * SYNOPSIS
*/ */
struct ase_lda_slot_t struct ase_lda_cell_t
{ {
void* dptr; void* dptr;
ase_size_t dlen; ase_size_t dlen;
@ -174,14 +190,60 @@ void ase_lda_fini (
); );
/******/ /******/
ase_size_t ase_lda_getsize (ase_lda_t* lda); /****f* ase.cmn.lda/ase_lda_getextension
ase_size_t ase_lda_getcapa (ase_lda_t* lda); * NAME
ase_lda_t* ase_lda_setcapa (ase_lda_t* lda, ase_size_t capa); * ase_lda_getextension - get the pointer to the extension
*
* DESCRIPTION
* The ase_lda_getextension() function returns the pointer to the extension.
*
* SYNOPSIS
*/
void* ase_lda_getextension (
ase_lda_t* lda /* a linear dynamic array */
);
/******/
/****f* ase.cmn.lda/ase_lda_getmmgr
* NAME
* ase_lda_getmmgr - get the memory manager
*
* SYNOPSIS
*/
ase_mmgr_t* ase_lda_getmmgr (
ase_lda_t* lda /* a linear dynamic array */
);
/******/
/****f* ase.cmn.lda/ase_lda_setmmgr
* NAME
* ase_lda_setmmgr - set the memory manager
*
* SYNOPSIS
*/
void ase_lda_setmmgr (
ase_lda_t* lda /* a linear dynamic array */,
ase_mmgr_t* mmgr /* a memory manager */
);
/******/
ase_size_t ase_lda_getsize (
ase_lda_t* lda
);
ase_size_t ase_lda_getcapa (
ase_lda_t* lda
);
ase_lda_t* ase_lda_setcapa (
ase_lda_t* lda,
ase_size_t capa
);
ase_size_t ase_lda_insert ( ase_size_t ase_lda_insert (
ase_lda_t* lda, ase_lda_t* lda,
ase_size_t index, ase_size_t index,
ase_char_t* dptr, void* dptr,
ase_size_t dlen ase_size_t dlen
); );
@ -221,6 +283,13 @@ ase_size_t ase_lda_rrfindx (
void ase_lda_clear (ase_lda_t* lda); void ase_lda_clear (ase_lda_t* lda);
void* ase_lda_copyinline (
ase_lda_t* lda /* a linear dynamic array */,
void* data /* pointer to data to copy */ ,
ase_size_t len /* length of data in bytes */
);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -7,11 +7,19 @@
#include <ase/cmn/lda.h> #include <ase/cmn/lda.h>
#include "mem.h" #include "mem.h"
#define DPTR(cell) ASE_LDA_DPTR(cell)
#define DLEN(cell) ASE_LDA_DLEN(cell)
#define TOB(lda,len) ((len)*(lda)->scale) #define TOB(lda,len) ((len)*(lda)->scale)
#define INVALID ASE_LDA_INVALID
ase_lda_t* ase_lda_open (ase_mmgr_t* mmgr, ase_size_t ext, ase_size_t capa) #define cell_t ase_lda_cell_t
#define lda_t ase_lda_t
#define size_t ase_size_t
#define mmgr_t ase_mmgr_t
lda_t* ase_lda_open (mmgr_t* mmgr, size_t ext, size_t capa)
{ {
ase_lda_t* lda; lda_t* lda;
if (mmgr == ASE_NULL) if (mmgr == ASE_NULL)
{ {
@ -23,54 +31,69 @@ ase_lda_t* ase_lda_open (ase_mmgr_t* mmgr, ase_size_t ext, ase_size_t capa)
if (mmgr == ASE_NULL) return ASE_NULL; if (mmgr == ASE_NULL) return ASE_NULL;
} }
lda = ASE_MMGR_ALLOC (mmgr, ASE_SIZEOF(ase_lda_t) + ext); lda = ASE_MMGR_ALLOC (mmgr, ASE_SIZEOF(lda_t) + ext);
if (lda == ASE_NULL) return ASE_NULL; if (lda == ASE_NULL) return ASE_NULL;
return ase_lda_init (lda, mmgr, capa); return ase_lda_init (lda, mmgr, capa);
} }
void ase_lda_close (ase_lda_t* lda) void ase_lda_close (lda_t* lda)
{ {
ase_lda_fini (lda); ase_lda_fini (lda);
ASE_MMGR_FREE (lda->mmgr, lda); ASE_MMGR_FREE (lda->mmgr, lda);
} }
ase_lda_t* ase_lda_init (ase_lda_t* lda, ase_mmgr_t* mmgr, ase_size_t capa) lda_t* ase_lda_init (lda_t* lda, mmgr_t* mmgr, size_t capa)
{ {
ASE_MEMSET (lda, 0, ASE_SIZEOF(*lda)); ASE_MEMSET (lda, 0, ASE_SIZEOF(*lda));
lda->mmgr = mmgr; lda->mmgr = mmgr;
lda->size = 0; lda->size = 0;
lda->capa = 0; lda->capa = 0;
lda->slot = ASE_NULL; lda->cell = ASE_NULL;
if (ase_lda_setcapa (lda, capa) == ASE_NULL) return ASE_NULL; if (ase_lda_setcapa (lda, capa) == ASE_NULL) return ASE_NULL;
return lda; return lda;
} }
void ase_lda_fini (ase_lda_t* lda) void ase_lda_fini (lda_t* lda)
{ {
ase_lda_clear (lda); ase_lda_clear (lda);
if (lda->slot != ASE_NULL) if (lda->cell != ASE_NULL)
{ {
ASE_MMGR_FREE (lda->mmgr, lda->slot); ASE_MMGR_FREE (lda->mmgr, lda->cell);
lda->slot = ASE_NULL; lda->cell = ASE_NULL;
lda->capa = 0; lda->capa = 0;
} }
} }
ase_size_t ase_lda_getsize (ase_lda_t* lda) void* ase_lda_getextension (lda_t* lda)
{
return lda + 1;
}
mmgr_t* ase_lda_getmmgr (lda_t* lda)
{
return lda->mmgr;
}
void ase_lda_setmmgr (lda_t* lda, mmgr_t* mmgr)
{
lda->mmgr = mmgr;
}
size_t ase_lda_getsize (lda_t* lda)
{ {
return lda->size; return lda->size;
} }
ase_size_t ase_lda_getcapa (ase_lda_t* lda) size_t ase_lda_getcapa (lda_t* lda)
{ {
return lda->capa; return lda->capa;
} }
ase_lda_t* ase_lda_setcapa (ase_lda_t* lda, ase_size_t capa) lda_t* ase_lda_setcapa (lda_t* lda, size_t capa)
{ {
void* tmp; void* tmp;
@ -85,51 +108,54 @@ ase_lda_t* ase_lda_setcapa (ase_lda_t* lda, ase_size_t capa)
if (lda->mmgr->realloc != ASE_NULL) if (lda->mmgr->realloc != ASE_NULL)
{ {
tmp = ASE_MMGR_REALLOC (lda->mmgr, tmp = ASE_MMGR_REALLOC (lda->mmgr,
lda->slot, ASE_SIZEOF(*lda->slot) * capa); lda->cell, ASE_SIZEOF(*lda->cell) * capa);
if (tmp == ASE_NULL) return ASE_NULL; if (tmp == ASE_NULL) return ASE_NULL;
} }
else else
{ {
tmp = ASE_MMGR_ALLOC ( tmp = ASE_MMGR_ALLOC (
lda->mmgr, ASE_SIZEOF(*lda->slot) * capa); lda->mmgr, ASE_SIZEOF(*lda->cell) * capa);
if (tmp == ASE_NULL) return ASE_NULL; if (tmp == ASE_NULL) return ASE_NULL;
if (lda->slot != ASE_NULL) if (lda->cell != ASE_NULL)
{ {
ase_size_t x; size_t x;
x = (capa > lda->capa)? lda->capa: capa; x = (capa > lda->capa)? lda->capa: capa;
ASE_MEMCPY ( ASE_MEMCPY (tmp, lda->cell,
tmp, lda->slot, ASE_SIZEOF(*lda->cell) * x);
ASE_SIZEOF(*lda->slot) * x); ASE_MMGR_FREE (lda->mmgr, lda->cell);
ASE_MMGR_FREE (lda->mmgr, lda->slot);
} }
} }
} }
else else
{ {
if (lda->slot != ASE_NULL) ASE_MMGR_FREE (lda->mmgr, lda->slot); if (lda->cell != ASE_NULL)
{
ase_lda_clear (lda);
ASE_MMGR_FREE (lda->mmgr, lda->cell);
}
tmp = ASE_NULL; tmp = ASE_NULL;
} }
lda->slot = tmp; lda->cell = tmp;
lda->capa = capa; lda->capa = capa;
return lda; return lda;
} }
static ase_lda_slot_t* alloc_slot (ase_lda_t* lda, void* dptr, ase_size_t dlen) static cell_t* alloc_cell (lda_t* lda, void* dptr, size_t dlen)
{ {
ase_lda_slot_t* n; cell_t* n;
if (lda->copier == ASE_NULL) if (lda->copier == ASE_NULL)
{ {
n = ASE_MMGR_ALLOC (lda->mmgr, ASE_SIZEOF(ase_lda_slot_t)); n = ASE_MMGR_ALLOC (lda->mmgr, ASE_SIZEOF(cell_t));
if (n == ASE_NULL) return ASE_NULL; if (n == ASE_NULL) return ASE_NULL;
DPTR(n) = dptr; DPTR(n) = dptr;
} }
else if (lda->copier == ASE_LDA_COPIER_INLINE) else if (lda->copier == ASE_LDA_COPIER_INLINE)
{ {
n = ASE_MMGR_ALLOC (lda->mmgr, n = ASE_MMGR_ALLOC (lda->mmgr,
ASE_SIZEOF(ase_lda_slot_t) + TOB(lda,dlen)); ASE_SIZEOF(cell_t) + TOB(lda,dlen));
if (n == ASE_NULL) return ASE_NULL; if (n == ASE_NULL) return ASE_NULL;
ASE_MEMCPY (n + 1, dptr, TOB(lda,dlen)); ASE_MEMCPY (n + 1, dptr, TOB(lda,dlen));
@ -137,7 +163,7 @@ static ase_lda_slot_t* alloc_slot (ase_lda_t* lda, void* dptr, ase_size_t dlen)
} }
else else
{ {
n = ASE_MMGR_ALLOC (lda->mmgr, ASE_SIZEOF(slot_t)); n = ASE_MMGR_ALLOC (lda->mmgr, ASE_SIZEOF(cell_t));
if (n == ASE_NULL) return ASE_NULL; if (n == ASE_NULL) return ASE_NULL;
DPTR(n) = lda->copier (lda, dptr, dlen); DPTR(n) = lda->copier (lda, dptr, dlen);
if (DPTR(n) == ASE_NULL) if (DPTR(n) == ASE_NULL)
@ -152,18 +178,17 @@ static ase_lda_slot_t* alloc_slot (ase_lda_t* lda, void* dptr, ase_size_t dlen)
return n; return n;
} }
ase_lda_slot_t* ase_lda_insert ( size_t ase_lda_insert (lda_t* lda, size_t index, void* dptr, size_t dlen)
ase_lda_t* lda, ase_size_t index, void* dptr, ase_size_t dlen)
{ {
ase_size_t i; size_t i;
ase_lda_slot_t* slot; cell_t* cell;
slot = alloc_slot (lda, dptr, dlen); cell = alloc_cell (lda, dptr, dlen);
if (slot == ASE_NULL) return ASE_NULL; if (cell == ASE_NULL) return INVALID;
if (index >= lda->capa) if (index >= lda->capa)
{ {
ase_size_t capa; size_t capa;
if (lda->capa <= 0) capa = (index + 1); if (lda->capa <= 0) capa = (index + 1);
else else
@ -174,14 +199,14 @@ ase_lda_slot_t* ase_lda_insert (
if (ase_lda_setcapa(lda,capa) == ASE_NULL) if (ase_lda_setcapa(lda,capa) == ASE_NULL)
{ {
if (lda->freeer) if (lda->freeer)
lda->freeer (lda, slot->dptr, slot->dlen); lda->freeer (lda, DPTR(cell), DLEN(cell));
return ASE_NULL; return INVALID;
} }
} }
for (i = lda->size; i > index; i--) for (i = lda->size; i > index; i--)
lda->slot[i] = lda->slot[i-1]; lda->cell[i] = lda->cell[i-1];
lda->slot[index] = slot; lda->cell[index] = cell;
if (index > lda->size) lda->size = index + 1; if (index > lda->size) lda->size = index + 1;
else lda->size++; else lda->size++;
@ -189,10 +214,9 @@ ase_lda_slot_t* ase_lda_insert (
return index; return index;
} }
ase_size_t ase_lda_delete ( size_t ase_lda_delete (lda_t* lda, size_t index, size_t count)
ase_lda_t* lda, ase_size_t index, ase_size_t count)
{ {
ase_size_t i, j, k; size_t i, j, k;
if (index >= lda->size) return 0; if (index >= lda->size) return 0;
if (count > lda->size - index) count = lda->size - index; if (count > lda->size - index) count = lda->size - index;
@ -203,18 +227,20 @@ ase_size_t ase_lda_delete (
while (i < k) while (i < k)
{ {
ASE_AWK_FREE (lda->awk, lda->slot[i].name.ptr); cell_t* c = lda->cell[i];
if (lda->freeer != ASE_NULL)
lda->freeer (lda, DPTR(c), DLEN(c));
ASE_MMGR_FREE (lda->mmgr, c);
if (j >= lda->size) if (j >= lda->size)
{ {
lda->slot[i].name.ptr = ASE_NULL; lda->cell[i] = ASE_NULL;
lda->slot[i].name.len = 0;
i++; i++;
} }
else else
{ {
lda->slot[i].name.ptr = lda->slot[j].name.ptr; lda->cell[i] = lda->cell[j];
lda->slot[i].name.len = lda->slot[j].name.len;
i++; j++; i++; j++;
} }
} }
@ -223,152 +249,164 @@ ase_size_t ase_lda_delete (
return count; return count;
} }
void ase_lda_clear (ase_lda_t* lda) void ase_lda_clear (lda_t* lda)
{ {
ase_size_t i; size_t i;
for (i = 0; i < lda->size; i++) for (i = 0; i < lda->size; i++)
{ {
ASE_MMGR_FREE (lda->mmgr, lda->slot[i].name.ptr); cell_t* c = lda->cell[i];
lda->slot[i].name.ptr = ASE_NULL; if (lda->freeer)
lda->slot[i].name.len = 0; lda->freeer (lda, DPTR(c), DLEN(c));
ASE_MMGR_FREE (lda->mmgr, c);
lda->cell[i] = ASE_NULL;
} }
lda->size = 0; lda->size = 0;
} }
ase_size_t ase_lda_add ( size_t ase_lda_add (
ase_lda_t* lda, ase_char_t* str, ase_size_t len) lda_t* lda, ase_char_t* str, size_t len)
{ {
return ase_lda_insert (lda, lda->size, str, len); return ase_lda_insert (lda, lda->size, str, len);
} }
ase_size_t ase_lda_adduniq ( size_t ase_lda_adduniq (
ase_lda_t* lda, ase_char_t* str, ase_size_t len) lda_t* lda, ase_char_t* str, size_t len)
{ {
ase_size_t i; size_t i;
i = ase_lda_find (lda, 0, str, len); i = ase_lda_find (lda, 0, str, len);
if (i != (ase_size_t)-1) return i; /* found. return the current index */ if (i != INVALID) return i; /* found. return the current index */
/* insert a new entry */ /* insert a new entry */
return ase_lda_insert (lda, lda->size, str, len); return ase_lda_insert (lda, lda->size, str, len);
} }
ase_size_t ase_lda_find ( #if 0
ase_lda_t* lda, ase_size_t index, size_t ase_lda_find (
const ase_char_t* str, ase_size_t len) lda_t* lda, size_t index,
const ase_char_t* str, size_t len)
{ {
ase_size_t i; size_t i;
for (i = index; i < lda->size; i++) for (i = index; i < lda->size; i++)
{ {
if (ase_strxncmp ( if (ase_strxncmp (
lda->slot[i].name.ptr, lda->slot[i].name.len, lda->cell[i].name.ptr, lda->cell[i].name.len,
str, len) == 0) return i; str, len) == 0) return i;
} }
return (ase_size_t)-1; return INVALID;
} }
ase_size_t ase_lda_rfind ( size_t ase_lda_rfind (
ase_lda_t* lda, ase_size_t index, lda_t* lda, size_t index,
const ase_char_t* str, ase_size_t len) const ase_char_t* str, size_t len)
{ {
ase_size_t i; size_t i;
if (index >= lda->size) return (ase_size_t)-1; if (index >= lda->size) return INVALID;
for (i = index + 1; i-- > 0; ) for (i = index + 1; i-- > 0; )
{ {
if (ase_strxncmp ( if (ase_strxncmp (
lda->slot[i].name.ptr, lda->slot[i].name.len, lda->cell[i].name.ptr, lda->cell[i].name.len,
str, len) == 0) return i; str, len) == 0) return i;
} }
return (ase_size_t)-1; return INVALID;
} }
ase_size_t ase_lda_rrfind ( size_t ase_lda_rrfind (
ase_lda_t* lda, ase_size_t index, lda_t* lda, size_t index,
const ase_char_t* str, ase_size_t len) const ase_char_t* str, size_t len)
{ {
ase_size_t i; size_t i;
if (index >= lda->size) return (ase_size_t)-1; if (index >= lda->size) return INVALID;
for (i = lda->size - index; i-- > 0; ) for (i = lda->size - index; i-- > 0; )
{ {
if (ase_strxncmp ( if (ase_strxncmp (
lda->slot[i].name.ptr, lda->slot[i].name.len, lda->cell[i].name.ptr, lda->cell[i].name.len,
str, len) == 0) return i; str, len) == 0) return i;
} }
return (ase_size_t)-1; return INVALID;
} }
ase_size_t ase_lda_findx ( size_t ase_lda_findx (
ase_lda_t* lda, ase_size_t index, lda_t* lda, size_t index,
const ase_char_t* str, ase_size_t len, const ase_char_t* str, size_t len,
void(*transform)(ase_size_t, ase_cstr_t*,void*), void* arg) void(*transform)(size_t, ase_cstr_t*,void*), void* arg)
{ {
ase_size_t i; size_t i;
for (i = index; i < lda->size; i++) for (i = index; i < lda->size; i++)
{ {
ase_cstr_t x; ase_cstr_t x;
x.ptr = lda->slot[i].name.ptr; x.ptr = lda->cell[i].name.ptr;
x.len = lda->slot[i].name.len; x.len = lda->cell[i].name.len;
transform (i, &x, arg); transform (i, &x, arg);
if (ase_strxncmp (x.ptr, x.len, str, len) == 0) return i; if (ase_strxncmp (x.ptr, x.len, str, len) == 0) return i;
} }
return (ase_size_t)-1; return INVALID;
} }
ase_size_t ase_lda_rfindx ( size_t ase_lda_rfindx (
ase_lda_t* lda, ase_size_t index, lda_t* lda, size_t index,
const ase_char_t* str, ase_size_t len, const ase_char_t* str, size_t len,
void(*transform)(ase_size_t, ase_cstr_t*,void*), void* arg) void(*transform)(size_t, ase_cstr_t*,void*), void* arg)
{ {
ase_size_t i; size_t i;
if (index >= lda->size) return (ase_size_t)-1; if (index >= lda->size) return INVALID;
for (i = index + 1; i-- > 0; ) for (i = index + 1; i-- > 0; )
{ {
ase_cstr_t x; ase_cstr_t x;
x.ptr = lda->slot[i].name.ptr; x.ptr = lda->cell[i].name.ptr;
x.len = lda->slot[i].name.len; x.len = lda->cell[i].name.len;
transform (i, &x, arg); transform (i, &x, arg);
if (ase_strxncmp (x.ptr, x.len, str, len) == 0) return i; if (ase_strxncmp (x.ptr, x.len, str, len) == 0) return i;
} }
return (ase_size_t)-1; return INVALID;
} }
ase_size_t ase_lda_rrfindx ( size_t ase_lda_rrfindx (
ase_lda_t* lda, ase_size_t index, lda_t* lda, size_t index,
const ase_char_t* str, ase_size_t len, const ase_char_t* str, size_t len,
void(*transform)(ase_size_t, ase_cstr_t*,void*), void* arg) void(*transform)(size_t, ase_cstr_t*,void*), void* arg)
{ {
ase_size_t i; size_t i;
if (index >= lda->size) return (ase_size_t)-1; if (index >= lda->size) return INVALID;
for (i = lda->size - index; i-- > 0; ) for (i = lda->size - index; i-- > 0; )
{ {
ase_cstr_t x; ase_cstr_t x;
x.ptr = lda->slot[i].name.ptr; x.ptr = lda->cell[i].name.ptr;
x.len = lda->slot[i].name.len; x.len = lda->cell[i].name.len;
transform (i, &x, arg); transform (i, &x, arg);
if (ase_strxncmp (x.ptr, x.len, str, len) == 0) return i; if (ase_strxncmp (x.ptr, x.len, str, len) == 0) return i;
} }
return (ase_size_t)-1; return INVALID;
} }
void* ase_lda_copyinline (lda_t* lda, void* dptr, size_t dlen)
{
/* this is a dummy copier */
return ASE_NULL;
}
#endif