enhanced qse_htb_t and qse_rbt_t

- defined builtin mancbs'
- changed qse_rbt_t to point to mancbs
- touched up a few other functions
This commit is contained in:
2010-10-30 07:54:36 +00:00
parent 2265531c97
commit d58631e70b
14 changed files with 673 additions and 551 deletions

View File

@ -1,5 +1,5 @@
/*
* $Id: Awk.cpp 363 2010-10-27 12:54:37Z hyunghwan.chung $
* $Id: Awk.cpp 365 2010-10-29 13:54:36Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
This file is part of QSE.
@ -1152,18 +1152,12 @@ int Awk::open ()
QSE_HTB_FREEER_DEFAULT,
free_function_map_value
},
QSE_HTB_HASHER_DEFAULT,
QSE_HTB_COMPER_DEFAULT,
QSE_HTB_KEEPER_DEFAULT,
QSE_HTB_SIZER_DEFAULT
QSE_HTB_SIZER_DEFAULT,
QSE_HTB_HASHER_DEFAULT
};
qse_htb_setmancbs (functionMap, &mancbs);
#if 0
qse_htb_setscale (functionMap, QSE_HTB_KEY, QSE_SIZEOF(qse_char_t));
qse_htb_setcopier (functionMap, QSE_HTB_KEY, QSE_HTB_COPIER_INLINE);
qse_htb_setfreeer (functionMap, QSE_HTB_VAL, free_function_map_value);
#endif
return 0;
}

View File

@ -1,5 +1,5 @@
/*
* $Id: awk.c 363 2010-10-27 12:54:37Z hyunghwan.chung $
* $Id: awk.c 365 2010-10-29 13:54:36Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
This file is part of QSE.
@ -77,38 +77,6 @@ qse_awk_t* qse_awk_open (qse_mmgr_t* mmgr, qse_size_t xtn, qse_awk_prm_t* prm)
{
qse_awk_t* awk;
static qse_htb_mancbs_t mancbs1 =
{
{
QSE_HTB_COPIER_INLINE,
QSE_HTB_COPIER_INLINE
},
{
QSE_HTB_FREEER_DEFAULT,
QSE_HTB_FREEER_DEFAULT
},
QSE_HTB_HASHER_DEFAULT,
QSE_HTB_COMPER_DEFAULT,
QSE_HTB_KEEPER_DEFAULT,
QSE_HTB_SIZER_DEFAULT
};
static qse_htb_mancbs_t mancbs2 =
{
{
QSE_HTB_COPIER_INLINE,
QSE_HTB_COPIER_DEFAULT
},
{
QSE_HTB_FREEER_DEFAULT,
QSE_HTB_FREEER_DEFAULT
},
QSE_HTB_HASHER_DEFAULT,
QSE_HTB_COMPER_DEFAULT,
QSE_HTB_KEEPER_DEFAULT,
QSE_HTB_SIZER_DEFAULT
};
static qse_htb_mancbs_t treefuncbs =
{
{
@ -119,10 +87,10 @@ qse_awk_t* qse_awk_open (qse_mmgr_t* mmgr, qse_size_t xtn, qse_awk_prm_t* prm)
QSE_HTB_FREEER_DEFAULT,
free_fun
},
QSE_HTB_HASHER_DEFAULT,
QSE_HTB_COMPER_DEFAULT,
QSE_HTB_KEEPER_DEFAULT,
QSE_HTB_SIZER_DEFAULT
QSE_HTB_SIZER_DEFAULT,
QSE_HTB_HASHER_DEFAULT
};
static qse_htb_mancbs_t fncusercbs =
@ -135,10 +103,10 @@ qse_awk_t* qse_awk_open (qse_mmgr_t* mmgr, qse_size_t xtn, qse_awk_prm_t* prm)
QSE_HTB_FREEER_DEFAULT,
free_fnc
},
QSE_HTB_HASHER_DEFAULT,
QSE_HTB_COMPER_DEFAULT,
QSE_HTB_KEEPER_DEFAULT,
QSE_HTB_SIZER_DEFAULT
QSE_HTB_SIZER_DEFAULT,
QSE_HTB_HASHER_DEFAULT
};
if (mmgr == QSE_NULL)
@ -184,7 +152,9 @@ qse_awk_t* qse_awk_open (qse_mmgr_t* mmgr, qse_size_t xtn, qse_awk_prm_t* prm)
);
if (awk->wtab == QSE_NULL) goto oops;
*(qse_awk_t**)QSE_XTN(awk->wtab) = awk;
qse_htb_setmancbs (awk->wtab, &mancbs1);
qse_htb_setmancbs (awk->wtab,
qse_htb_mancbs(QSE_HTB_MANCBS_INLINE_COPIERS)
);
awk->rwtab = qse_htb_open (
mmgr, QSE_SIZEOF(awk),
@ -192,14 +162,18 @@ qse_awk_t* qse_awk_open (qse_mmgr_t* mmgr, qse_size_t xtn, qse_awk_prm_t* prm)
);
if (awk->rwtab == QSE_NULL) goto oops;
*(qse_awk_t**)QSE_XTN(awk->rwtab) = awk;
qse_htb_setmancbs (awk->rwtab, &mancbs1);
qse_htb_setmancbs (awk->rwtab,
qse_htb_mancbs(QSE_HTB_MANCBS_INLINE_COPIERS)
);
awk->sio.names = qse_htb_open (
mmgr, QSE_SIZEOF(awk), 128, 70, QSE_SIZEOF(qse_char_t), 1
);
if (awk->sio.names == QSE_NULL) goto oops;
*(qse_awk_t**)QSE_XTN(awk->sio.names) = awk;
qse_htb_setmancbs (awk->sio.names, &mancbs2);
qse_htb_setmancbs (awk->sio.names,
qse_htb_mancbs(QSE_HTB_MANCBS_INLINE_KEY_COPIER)
);
awk->sio.inp = &awk->sio.arg;
/* TODO: initial map size?? */
@ -215,14 +189,18 @@ qse_awk_t* qse_awk_open (qse_mmgr_t* mmgr, qse_size_t xtn, qse_awk_prm_t* prm)
);
if (awk->parse.funs == QSE_NULL) goto oops;
*(qse_awk_t**)QSE_XTN(awk->parse.funs) = awk;
qse_htb_setmancbs (awk->parse.funs, &mancbs2);
qse_htb_setmancbs (awk->parse.funs,
qse_htb_mancbs(QSE_HTB_MANCBS_INLINE_KEY_COPIER)
);
awk->parse.named = qse_htb_open (
mmgr, QSE_SIZEOF(awk), 256, 70, QSE_SIZEOF(qse_char_t), 1
);
if (awk->parse.named == QSE_NULL) goto oops;
*(qse_awk_t**)QSE_XTN(awk->parse.named) = awk;
qse_htb_setmancbs (awk->parse.named, &mancbs2);
qse_htb_setmancbs (awk->parse.named,
qse_htb_mancbs(QSE_HTB_MANCBS_INLINE_KEY_COPIER)
);
awk->parse.gbls = qse_lda_open (mmgr, QSE_SIZEOF(awk), 128);
awk->parse.lcls = qse_lda_open (mmgr, QSE_SIZEOF(awk), 64);

View File

@ -1,5 +1,5 @@
/*
* $Id: run.c 363 2010-10-27 12:54:37Z hyunghwan.chung $
* $Id: run.c 365 2010-10-29 13:54:36Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
This file is part of QSE.
@ -797,10 +797,10 @@ static int init_rtx (qse_awk_rtx_t* rtx, qse_awk_t* awk, qse_awk_rio_t* rio)
QSE_HTB_FREEER_DEFAULT,
free_namedval
},
QSE_HTB_HASHER_DEFAULT,
QSE_HTB_COMPER_DEFAULT,
same_namedval,
QSE_HTB_SIZER_DEFAULT
QSE_HTB_SIZER_DEFAULT,
QSE_HTB_HASHER_DEFAULT
};
/* zero out the runtime context excluding the extension */

View File

@ -1,5 +1,5 @@
/*
* $Id: val.c 363 2010-10-27 12:54:37Z hyunghwan.chung $
* $Id: val.c 365 2010-10-29 13:54:36Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
This file is part of QSE.
@ -403,10 +403,10 @@ qse_awk_val_t* qse_awk_rtx_makemapval (qse_awk_rtx_t* rtx)
QSE_HTB_FREEER_DEFAULT,
free_mapval
},
QSE_HTB_HASHER_DEFAULT,
QSE_HTB_COMPER_DEFAULT,
same_mapval,
QSE_HTB_SIZER_DEFAULT
QSE_HTB_SIZER_DEFAULT,
QSE_HTB_HASHER_DEFAULT
};
/* CHECK */

View File

@ -1,5 +1,5 @@
/*
* $Id: htb.c 364 2010-10-28 13:09:53Z hyunghwan.chung $
* $Id: htb.c 365 2010-10-29 13:54:36Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
This file is part of QSE.
@ -72,7 +72,9 @@ QSE_INLINE pair_t* qse_htb_allocpair (
else if (kcop == QSE_HTB_COPIER_INLINE)
{
KPTR(n) = n + 1;
QSE_MEMCPY (KPTR(n), kptr, KTOB(htb,klen));
/* if kptr is QSE_NULL, the inline copier does not fill
* the actual key area */
if (kptr) QSE_MEMCPY (KPTR(n), kptr, KTOB(htb,klen));
}
else
{
@ -94,7 +96,9 @@ QSE_INLINE pair_t* qse_htb_allocpair (
VPTR(n) = n + 1;
if (kcop == QSE_HTB_COPIER_INLINE)
VPTR(n) = (byte_t*)VPTR(n) + KTOB(htb,klen);
QSE_MEMCPY (VPTR(n), vptr, VTOB(htb,vlen));
/* if vptr is QSE_NULL, the inline copier does not fill
* the actual value area */
if (vptr) QSE_MEMCPY (VPTR(n), vptr, VTOB(htb,vlen));
}
else
{
@ -181,24 +185,76 @@ static QSE_INLINE pair_t* change_pair_val (
return pair;
}
static qse_htb_mancbs_t mancbs =
static qse_htb_mancbs_t mancbs[] =
{
{
QSE_HTB_COPIER_DEFAULT,
QSE_HTB_COPIER_DEFAULT
{
QSE_HTB_COPIER_DEFAULT,
QSE_HTB_COPIER_DEFAULT
},
{
QSE_HTB_FREEER_DEFAULT,
QSE_HTB_FREEER_DEFAULT
},
QSE_HTB_COMPER_DEFAULT,
QSE_HTB_KEEPER_DEFAULT,
QSE_HTB_SIZER_DEFAULT,
QSE_HTB_HASHER_DEFAULT
},
{
QSE_HTB_FREEER_DEFAULT,
QSE_HTB_FREEER_DEFAULT
{
QSE_HTB_COPIER_INLINE,
QSE_HTB_COPIER_INLINE
},
{
QSE_HTB_FREEER_DEFAULT,
QSE_HTB_FREEER_DEFAULT
},
QSE_HTB_COMPER_DEFAULT,
QSE_HTB_KEEPER_DEFAULT,
QSE_HTB_SIZER_DEFAULT,
QSE_HTB_HASHER_DEFAULT
},
QSE_HTB_HASHER_DEFAULT,
QSE_HTB_COMPER_DEFAULT,
QSE_HTB_KEEPER_DEFAULT,
QSE_HTB_SIZER_DEFAULT
{
{
QSE_HTB_COPIER_INLINE,
QSE_HTB_COPIER_DEFAULT
},
{
QSE_HTB_FREEER_DEFAULT,
QSE_HTB_FREEER_DEFAULT
},
QSE_HTB_COMPER_DEFAULT,
QSE_HTB_KEEPER_DEFAULT,
QSE_HTB_SIZER_DEFAULT,
QSE_HTB_HASHER_DEFAULT
},
{
{
QSE_HTB_COPIER_DEFAULT,
QSE_HTB_COPIER_INLINE
},
{
QSE_HTB_FREEER_DEFAULT,
QSE_HTB_FREEER_DEFAULT
},
QSE_HTB_COMPER_DEFAULT,
QSE_HTB_KEEPER_DEFAULT,
QSE_HTB_SIZER_DEFAULT,
QSE_HTB_HASHER_DEFAULT
}
};
const qse_htb_mancbs_t* qse_htb_mancbs (qse_htb_mancbs_kind_t kind)
{
return &mancbs[kind];
};
htb_t* qse_htb_open (
mmgr_t* mmgr, size_t ext, size_t capa, int factor, int kscale, int vscale)
mmgr_t* mmgr, size_t xtnsize, size_t capa, int factor, int kscale, int vscale)
{
htb_t* htb;
@ -212,7 +268,7 @@ htb_t* qse_htb_open (
if (mmgr == QSE_NULL) return QSE_NULL;
}
htb = (htb_t*) QSE_MMGR_ALLOC (mmgr, QSE_SIZEOF(htb_t) + ext);
htb = (htb_t*) QSE_MMGR_ALLOC (mmgr, QSE_SIZEOF(htb_t) + xtnsize);
if (htb == QSE_NULL) return QSE_NULL;
if (qse_htb_init (htb, mmgr, capa, factor, kscale, vscale) == QSE_NULL)
@ -256,10 +312,6 @@ htb_t* qse_htb_init (
QSE_MEMSET (htb->bucket, 0, capa*SIZEOF(pair_t*));
htb->factor = factor;
#if 0
htb->scale[QSE_HTB_KEY] = 1;
htb->scale[QSE_HTB_VAL] = 1;
#endif
htb->scale[QSE_HTB_KEY] = (kscale < 1)? 1: kscale;
htb->scale[QSE_HTB_VAL] = (vscale < 1)? 1: vscale;
@ -268,22 +320,7 @@ htb_t* qse_htb_init (
htb->threshold = htb->capa * htb->factor / 100;
if (htb->capa > 0 && htb->threshold <= 0) htb->threshold = 1;
htb->mancbs = &mancbs;
#if 0
htb->hasher = hash_key;
htb->comper = comp_key;
htb->copier[QSE_HTB_KEY] = QSE_HTB_COPIER_SIMPLE;
htb->copier[QSE_HTB_VAL] = QSE_HTB_COPIER_SIMPLE;
/*
htb->freeer[QSE_HTB_KEY] = QSE_NULL;
htb->freeer[QSE_HTB_VAL] = QSE_NULL;
htb->keeper = QSE_NULL;
htb->sizer = QSE_NULL;
*/
#endif
htb->mancbs = &mancbs[0];
return htb;
}
@ -293,29 +330,6 @@ void qse_htb_fini (htb_t* htb)
QSE_MMGR_FREE (htb->mmgr, htb->bucket);
}
#if 0
int qse_htb_getscale (htb_t* htb, qse_htb_id_t id)
{
QSE_ASSERTX (id == QSE_HTB_KEY || id == QSE_HTB_VAL,
"The ID should be either QSE_HTB_KEY or QSE_HTB_VAL");
return htb->scale[id];
}
void qse_htb_setscale (htb_t* htb, qse_htb_id_t id, int scale)
{
QSE_ASSERTX (id == QSE_HTB_KEY || id == QSE_HTB_VAL,
"The ID should be either QSE_HTB_KEY or QSE_HTB_VAL");
QSE_ASSERTX (scale > 0 && scale <= QSE_TYPE_MAX(qse_byte_t),
"The scale should be larger than 0 and less than or equal to the maximum value that the qse_byte_t type can hold");
if (scale <= 0) scale = 1;
if (scale > QSE_TYPE_MAX(qse_byte_t)) scale = QSE_TYPE_MAX(qse_byte_t);
htb->scale[id] = scale;
}
#endif
const qse_htb_mancbs_t* qse_htb_getmancbs (htb_t* htb)
{
return htb->mancbs;

View File

@ -58,37 +58,13 @@ QSE_IMPLEMENT_COMMON_FUNCTIONS (rbt)
#define rotate_left(rbt,pivot) rotate(rbt,pivot,1);
#define rotate_right(rbt,pivot) rotate(rbt,pivot,0);
static QSE_INLINE int comp_key (
qse_rbt_t* rbt,
const void* kptr1, size_t klen1,
const void* kptr2, size_t klen2)
{
size_t min;
int n, nn;
if (klen1 < klen2)
{
min = klen1;
nn = -1;
}
else
{
min = klen2;
nn = (klen1 == klen2)? 0: 1;
}
n = QSE_MEMCMP (kptr1, kptr2, KTOB(rbt,min));
if (n == 0) n = nn;
return n;
}
static pair_t* alloc_pair (rbt_t* rbt,
void* kptr, size_t klen, void* vptr, size_t vlen)
QSE_INLINE pair_t* qse_rbt_allocpair (
rbt_t* rbt, void* kptr, size_t klen, void* vptr, size_t vlen)
{
pair_t* n;
copier_t kcop = rbt->copier[QSE_RBT_KEY];
copier_t vcop = rbt->copier[QSE_RBT_VAL];
copier_t kcop = rbt->mancbs->copier[QSE_RBT_KEY];
copier_t vcop = rbt->mancbs->copier[QSE_RBT_VAL];
size_t as = SIZEOF(pair_t);
if (kcop == QSE_RBT_COPIER_INLINE) as += KTOB(rbt,klen);
@ -110,7 +86,7 @@ static pair_t* alloc_pair (rbt_t* rbt,
else if (kcop == QSE_RBT_COPIER_INLINE)
{
KPTR(n) = n + 1;
QSE_MEMCPY (KPTR(n), kptr, KTOB(rbt,klen));
if (kptr) QSE_MEMCPY (KPTR(n), kptr, KTOB(rbt,klen));
}
else
{
@ -132,15 +108,15 @@ static pair_t* alloc_pair (rbt_t* rbt,
VPTR(n) = n + 1;
if (kcop == QSE_RBT_COPIER_INLINE)
VPTR(n) = (byte_t*)VPTR(n) + KTOB(rbt,klen);
QSE_MEMCPY (VPTR(n), vptr, VTOB(rbt,vlen));
if (vptr) QSE_MEMCPY (VPTR(n), vptr, VTOB(rbt,vlen));
}
else
{
VPTR(n) = vcop (rbt, vptr, vlen);
if (VPTR(n) != QSE_NULL)
{
if (rbt->freeer[QSE_RBT_KEY] != QSE_NULL)
rbt->freeer[QSE_RBT_KEY] (rbt, KPTR(n), KLEN(n));
if (rbt->mancbs->freeer[QSE_RBT_KEY] != QSE_NULL)
rbt->mancbs->freeer[QSE_RBT_KEY] (rbt, KPTR(n), KLEN(n));
QSE_MMGR_FREE (rbt->mmgr, n);
return QSE_NULL;
}
@ -149,16 +125,76 @@ static pair_t* alloc_pair (rbt_t* rbt,
return n;
}
static void free_pair (rbt_t* rbt, pair_t* pair)
QSE_INLINE void qse_rbt_freepair (rbt_t* rbt, pair_t* pair)
{
if (rbt->freeer[QSE_RBT_KEY] != QSE_NULL)
rbt->freeer[QSE_RBT_KEY] (rbt, KPTR(pair), KLEN(pair));
if (rbt->freeer[QSE_RBT_VAL] != QSE_NULL)
rbt->freeer[QSE_RBT_VAL] (rbt, VPTR(pair), VLEN(pair));
if (rbt->mancbs->freeer[QSE_RBT_KEY] != QSE_NULL)
rbt->mancbs->freeer[QSE_RBT_KEY] (rbt, KPTR(pair), KLEN(pair));
if (rbt->mancbs->freeer[QSE_RBT_VAL] != QSE_NULL)
rbt->mancbs->freeer[QSE_RBT_VAL] (rbt, VPTR(pair), VLEN(pair));
QSE_MMGR_FREE (rbt->mmgr, pair);
}
rbt_t* qse_rbt_open (mmgr_t* mmgr, size_t ext)
static qse_rbt_mancbs_t mancbs[] =
{
{
{
QSE_RBT_COPIER_DEFAULT,
QSE_RBT_COPIER_DEFAULT
},
{
QSE_RBT_FREEER_DEFAULT,
QSE_RBT_FREEER_DEFAULT
},
QSE_RBT_COMPER_DEFAULT,
QSE_RBT_KEEPER_DEFAULT
},
{
{
QSE_RBT_COPIER_INLINE,
QSE_RBT_COPIER_INLINE
},
{
QSE_RBT_FREEER_DEFAULT,
QSE_RBT_FREEER_DEFAULT
},
QSE_RBT_COMPER_DEFAULT,
QSE_RBT_KEEPER_DEFAULT
},
{
{
QSE_RBT_COPIER_INLINE,
QSE_RBT_COPIER_DEFAULT
},
{
QSE_RBT_FREEER_DEFAULT,
QSE_RBT_FREEER_DEFAULT
},
QSE_RBT_COMPER_DEFAULT,
QSE_RBT_KEEPER_DEFAULT
},
{
{
QSE_RBT_COPIER_DEFAULT,
QSE_RBT_COPIER_INLINE
},
{
QSE_RBT_FREEER_DEFAULT,
QSE_RBT_FREEER_DEFAULT
},
QSE_RBT_COMPER_DEFAULT,
QSE_RBT_KEEPER_DEFAULT
}
};
const qse_rbt_mancbs_t* qse_rbt_mancbs (qse_rbt_mancbs_kind_t kind)
{
return &mancbs[kind];
};
rbt_t* qse_rbt_open (mmgr_t* mmgr, size_t xtnsize, int kscale, int vscale)
{
rbt_t* rbt;
@ -172,10 +208,10 @@ rbt_t* qse_rbt_open (mmgr_t* mmgr, size_t ext)
if (mmgr == QSE_NULL) return QSE_NULL;
}
rbt = (rbt_t*) QSE_MMGR_ALLOC (mmgr, QSE_SIZEOF(rbt_t) + ext);
rbt = (rbt_t*) QSE_MMGR_ALLOC (mmgr, QSE_SIZEOF(rbt_t) + xtnsize);
if (rbt == QSE_NULL) return QSE_NULL;
if (qse_rbt_init (rbt, mmgr) == QSE_NULL)
if (qse_rbt_init (rbt, mmgr, kscale, vscale) == QSE_NULL)
{
QSE_MMGR_FREE (mmgr, rbt);
return QSE_NULL;
@ -190,7 +226,7 @@ void qse_rbt_close (rbt_t* rbt)
QSE_MMGR_FREE (rbt->mmgr, rbt);
}
rbt_t* qse_rbt_init (rbt_t* rbt, mmgr_t* mmgr)
rbt_t* qse_rbt_init (rbt_t* rbt, mmgr_t* mmgr, int kscale, int vscale)
{
if (mmgr == QSE_NULL) mmgr = QSE_MMGR_GETDFL();
@ -198,19 +234,11 @@ rbt_t* qse_rbt_init (rbt_t* rbt, mmgr_t* mmgr)
QSE_MEMSET (rbt, 0, SIZEOF(*rbt));
rbt->mmgr = mmgr;
rbt->scale[QSE_RBT_KEY] = 1;
rbt->scale[QSE_RBT_VAL] = 1;
rbt->scale[QSE_RBT_KEY] = (kscale < 1)? 1: kscale;
rbt->scale[QSE_RBT_VAL] = (vscale < 1)? 1: vscale;
rbt->size = 0;
rbt->comper = comp_key;
rbt->copier[QSE_RBT_KEY] = QSE_RBT_COPIER_SIMPLE;
rbt->copier[QSE_RBT_VAL] = QSE_RBT_COPIER_SIMPLE;
/*
rbt->freeer[QSE_RBT_KEY] = QSE_NULL;
rbt->freeer[QSE_RBT_VAL] = QSE_NULL;
rbt->keeper = QSE_NULL;
*/
rbt->mancbs = &mancbs[0];
/* self-initializing nil */
QSE_MEMSET(&rbt->nil, 0, QSE_SIZEOF(rbt->nil));
@ -229,75 +257,15 @@ void qse_rbt_fini (rbt_t* rbt)
qse_rbt_clear (rbt);
}
int qse_rbt_getscale (rbt_t* rbt, id_t id)
const qse_rbt_mancbs_t* qse_rbt_getmancbs (rbt_t* rbt)
{
QSE_ASSERTX (id == QSE_RBT_KEY || id == QSE_RBT_VAL,
"The ID should be either QSE_RBT_KEY or QSE_RBT_VAL");
return rbt->scale[id];
return rbt->mancbs;
}
void qse_rbt_setscale (rbt_t* rbt, id_t id, int scale)
void qse_rbt_setmancbs (rbt_t* rbt, const qse_rbt_mancbs_t* mancbs)
{
QSE_ASSERTX (id == QSE_RBT_KEY || id == QSE_RBT_VAL,
"The ID should be either QSE_RBT_KEY or QSE_RBT_VAL");
QSE_ASSERTX (scale > 0 && scale <= QSE_TYPE_MAX(qse_byte_t),
"The scale should be larger than 0 and less than or equal to the maximum value that the qse_byte_t type can hold");
if (scale <= 0) scale = 1;
if (scale > QSE_TYPE_MAX(qse_byte_t)) scale = QSE_TYPE_MAX(qse_byte_t);
rbt->scale[id] = scale;
}
copier_t qse_rbt_getcopier (rbt_t* rbt, id_t id)
{
QSE_ASSERTX (id == QSE_RBT_KEY || id == QSE_RBT_VAL,
"The ID should be either QSE_RBT_KEY or QSE_RBT_VAL");
return rbt->copier[id];
}
void qse_rbt_setcopier (rbt_t* rbt, id_t id, copier_t copier)
{
QSE_ASSERTX (id == QSE_RBT_KEY || id == QSE_RBT_VAL,
"The ID should be either QSE_RBT_KEY or QSE_RBT_VAL");
if (copier == QSE_NULL) copier = QSE_RBT_COPIER_SIMPLE;
rbt->copier[id] = copier;
}
freeer_t qse_rbt_getfreeer (rbt_t* rbt, id_t id)
{
QSE_ASSERTX (id == QSE_RBT_KEY || id == QSE_RBT_VAL,
"The ID should be either QSE_RBT_KEY or QSE_RBT_VAL");
return rbt->freeer[id];
}
void qse_rbt_setfreeer (rbt_t* rbt, id_t id, freeer_t freeer)
{
QSE_ASSERTX (id == QSE_RBT_KEY || id == QSE_RBT_VAL,
"The ID should be either QSE_RBT_KEY or QSE_RBT_VAL");
rbt->freeer[id] = freeer;
}
comper_t qse_rbt_getcomper (rbt_t* rbt)
{
return rbt->comper;
}
void qse_rbt_setcomper (rbt_t* rbt, comper_t comper)
{
if (comper == QSE_NULL) comper = comp_key;
rbt->comper = comper;
}
keeper_t qse_rbt_getkeeper (rbt_t* rbt)
{
return rbt->keeper;
}
void qse_rbt_setkeeper (rbt_t* rbt, keeper_t keeper)
{
rbt->keeper = keeper;
QSE_ASSERT (mancbs != QSE_NULL);
rbt->mancbs = mancbs;
}
size_t qse_rbt_getsize (rbt_t* rbt)
@ -311,7 +279,7 @@ pair_t* qse_rbt_search (rbt_t* rbt, const void* kptr, size_t klen)
while (!IS_NIL(rbt,pair))
{
int n = rbt->comper (rbt, kptr, klen, pair->kptr, pair->klen);
int n = rbt->mancbs->comper (rbt, kptr, klen, pair->kptr, pair->klen);
if (n == 0) return pair;
if (n > 0) pair = pair->right;
@ -461,14 +429,14 @@ 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 (rbt->keeper != QSE_NULL)
if (rbt->mancbs->keeper != QSE_NULL)
{
rbt->keeper (rbt, vptr, vlen);
rbt->mancbs->keeper (rbt, vptr, vlen);
}
}
else
{
copier_t vcop = rbt->copier[QSE_RBT_VAL];
copier_t vcop = rbt->mancbs->copier[QSE_RBT_VAL];
void* ovptr = VPTR(pair);
size_t ovlen = VLEN(pair);
@ -487,7 +455,7 @@ static pair_t* change_pair_val (
else
{
/* need to reconstruct the pair */
pair_t* p = alloc_pair (rbt,
pair_t* p = qse_rbt_allocpair (rbt,
KPTR(pair), KLEN(pair),
vptr, vlen);
if (p == QSE_NULL) return QSE_NULL;
@ -514,7 +482,7 @@ static pair_t* change_pair_val (
if (pair == rbt->root) rbt->root = p;
free_pair (rbt, pair);
qse_rbt_freepair (rbt, pair);
return p;
}
}
@ -527,9 +495,9 @@ static pair_t* change_pair_val (
}
/* free up the old value */
if (rbt->freeer[QSE_RBT_VAL] != QSE_NULL)
if (rbt->mancbs->freeer[QSE_RBT_VAL] != QSE_NULL)
{
rbt->freeer[QSE_RBT_VAL] (rbt, ovptr, ovlen);
rbt->mancbs->freeer[QSE_RBT_VAL] (rbt, ovptr, ovlen);
}
}
@ -545,7 +513,7 @@ static pair_t* insert (
while (!IS_NIL(rbt,xcur))
{
int n = rbt->comper (rbt, kptr, klen, xcur->kptr, xcur->klen);
int n = rbt->mancbs->comper (rbt, kptr, klen, xcur->kptr, xcur->klen);
if (n == 0)
{
switch (opt)
@ -572,7 +540,7 @@ static pair_t* insert (
if (opt == UPDATE) return QSE_NULL;
xnew = alloc_pair (rbt, kptr, klen, vptr, vlen);
xnew = qse_rbt_allocpair (rbt, kptr, klen, vptr, vlen);
if (xnew == QSE_NULL) return QSE_NULL;
if (xpar == QSE_NULL)
@ -584,7 +552,7 @@ static pair_t* insert (
else
{
/* perform normal binary insert */
int n = rbt->comper (rbt, kptr, klen, xpar->kptr, xpar->klen);
int n = rbt->mancbs->comper (rbt, kptr, klen, xpar->kptr, xpar->klen);
if (n > 0)
{
QSE_ASSERT (xpar->right == &rbt->nil);
@ -755,7 +723,7 @@ static void delete_pair (rbt_t* rbt, pair_t* pair)
if (y->color == QSE_RBT_BLACK && !IS_NIL(rbt,x))
adjust_for_delete (rbt, x, par);
free_pair (rbt, y);
qse_rbt_freepair (rbt, y);
}
else
{
@ -796,7 +764,7 @@ static void delete_pair (rbt_t* rbt, pair_t* pair)
if (y->right->parent == pair) y->right->parent = y;
#endif
free_pair (rbt, pair);
qse_rbt_freepair (rbt, pair);
}
rbt->size--;
@ -917,3 +885,27 @@ void qse_rbt_rwalk (rbt_t* rbt, walker_t walker, void* ctx)
walk (rbt, walker, ctx, RIGHT, LEFT);
}
int qse_rbt_dflcomp (
qse_rbt_t* rbt,
const void* kptr1, size_t klen1,
const void* kptr2, size_t klen2)
{
size_t min;
int n, nn;
if (klen1 < klen2)
{
min = klen1;
nn = -1;
}
else
{
min = klen2;
nn = (klen1 == klen2)? 0: 1;
}
n = QSE_MEMCMP (kptr1, kptr2, KTOB(rbt,min));
if (n == 0) n = nn;
return n;
}

View File

@ -1,5 +1,5 @@
/*
* $Id: sed.c 360 2010-10-21 13:29:12Z hyunghwan.chung $
* $Id: sed.c 365 2010-10-29 13:54:36Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
This file is part of QSE.
@ -94,15 +94,17 @@ static qse_sed_t* qse_sed_init (qse_sed_t* sed, qse_mmgr_t* mmgr)
return QSE_NULL;
}
if (qse_map_init (&sed->tmp.labs, mmgr, 128, 70) == QSE_NULL)
if (qse_map_init (&sed->tmp.labs, mmgr,
128, 70, QSE_SIZEOF(qse_char_t), 1) == QSE_NULL)
{
qse_str_fini (&sed->tmp.lab);
qse_str_fini (&sed->tmp.rex);
SETERR0 (sed, QSE_SED_ENOMEM, QSE_NULL);
return QSE_NULL;
}
qse_map_setcopier (&sed->tmp.labs, QSE_MAP_KEY, QSE_MAP_COPIER_INLINE);
qse_map_setscale (&sed->tmp.labs, QSE_MAP_KEY, QSE_SIZEOF(qse_char_t));
qse_map_setmancbs (&sed->tmp.labs,
qse_map_mancbs(QSE_MAP_MANCBS_INLINE_KEY_COPIER)
);
if (qse_lda_init (&sed->e.txt.appended, mmgr, 32) == QSE_NULL)
{
@ -2627,6 +2629,25 @@ int qse_sed_exec (qse_sed_t* sed, qse_sed_io_fun_t inf, qse_sed_io_fun_t outf)
qse_ssize_t n;
int ret = 0;
static qse_map_mancbs_t mancbs =
{
{
QSE_MAP_COPIER_INLINE,
QSE_MAP_COPIER_INLINE
},
{
QSE_MAP_FREEER_DEFAULT,
close_outfile
},
QSE_MAP_COMPER_DEFAULT,
QSE_MAP_KEEPER_DEFAULT
#ifdef QSE_MAP_AS_HTB
,
QSE_MAP_SIZER_DEFAULT,
QSE_MAP_HASHER_DEFAULT
#endif
};
sed->e.subst_done = 0;
qse_lda_clear (&sed->e.txt.appended);
qse_str_clear (&sed->e.txt.read);
@ -2641,20 +2662,14 @@ int qse_sed_exec (qse_sed_t* sed, qse_sed_io_fun_t inf, qse_sed_io_fun_t outf)
sed->e.out.fun = outf;
sed->e.out.eof = 0;
sed->e.out.len = 0;
if (qse_map_init (&sed->e.out.files, sed->mmgr, 128, 70) == QSE_NULL)
if (qse_map_init (&sed->e.out.files, sed->mmgr,
128, 70, QSE_SIZEOF(qse_char_t), 1) == QSE_NULL)
{
SETERR0 (sed, QSE_SED_ENOMEM, QSE_NULL);
return -1;
}
*(qse_sed_t**)QSE_XTN(&sed->e.out.files) = sed;
qse_map_setcopier (
&sed->e.out.files, QSE_MAP_KEY, QSE_MAP_COPIER_INLINE);
qse_map_setscale (
&sed->e.out.files, QSE_MAP_KEY, QSE_SIZEOF(qse_char_t));
qse_map_setcopier (
&sed->e.out.files, QSE_MAP_VAL, QSE_MAP_COPIER_INLINE);
qse_map_setfreeer (
&sed->e.out.files, QSE_MAP_VAL, close_outfile);
qse_map_setmancbs (&sed->e.out.files, &mancbs);
sed->e.in.fun = inf;
sed->e.in.eof = 0;