fixed a bug in rbt.c

This commit is contained in:
hyung-hwan 2010-07-15 06:54:48 +00:00
parent 0406471ef0
commit a8e9033b86
5 changed files with 254 additions and 42 deletions

140
qse/include/qse/cmn/map.h Normal file
View File

@ -0,0 +1,140 @@
/*
* $Id$
*
Copyright 2006-2009 Chung, Hyung-Hwan.
This file is part of QSE.
QSE is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
QSE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with QSE. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _QSE_CMN_MAP_H_
#define _QSE_CMN_MAP_H_
/*
* it is a convenience header file to switch easily between a red-black tree
* and a hash table. You must define either QSE_MAP_AS_HTB or QSE_MAP_AS_RBT
* before including this file.
*/
#if defined(QSE_MAP_AS_HTB)
# include <qse/cmn/htb.h>
# define qse_map_open(mmgr,ext,capa,factor) qse_htb_open(mmgr,ext,capa,factor)
# define qse_map_close(map) qse_htb_close(map)
# define qse_map_init(map,mmgr,capa,factor) qse_htb_init(map,mmgr,capa,factor)
# define qse_map_fini(map) qse_htb_fini(map)
# define qse_map_getsize(map) qse_htb_getsize(map)
# define qse_map_getcapa(map) qse_htb_getcapa(map)
# define qse_map_getscale(map,id) qse_htb_getscale(map,id)
# define qse_map_setscale(map,id,scale) qse_htb_setscale(map,id,scale)
# define qse_map_getcopier(map,id) qse_htb_getcopier(map,id)
# define qse_map_setcopier(map,id,cb) qse_htb_setcopier(map,id,cb)
# define qse_map_getfreeer(map,id) qse_htb_getfreeer(map,id)
# define qse_map_setfreeer(map,id,cb) qse_htb_setfreeer(map,id,cb)
# define qse_map_getcomper(map,id) qse_htb_getcomper(map,id)
# define qse_map_setcomper(map,id,cb) qse_htb_setcomper(map,id,cb)
# define qse_map_getkeeper(map,id) qse_htb_getkeeper(map,id)
# define qse_map_setkeeper(map,id,cb) qse_htb_setkeeper(map,id,cb)
# define qse_map_gethasher(map) qse_htb_gethasher(map)
# define qse_map_sethasher(map,cb) qse_htb_sethasher(map,cb)
# define qse_map_getsizer(map) qse_htb_getsizer(map)
# define qse_map_setsizer(map,cb) qse_htb_setsizer(map,cb)
# define qse_map_search(map,kptr,klen) qse_htb_search(map,kptr,klen)
# define qse_map_upsert(map,kptr,klen,vptr,vlen) qse_htb_upsert(map,kptr,klen,vptr,vlen)
# define qse_map_ensert(map,kptr,klen,vptr,vlen) qse_htb_ensert(map,kptr,klen,vptr,vlen)
# define qse_map_insert(map,kptr,klen,vptr,vlen) qse_htb_insert(map,kptr,klen,vptr,vlen)
# define qse_map_update(map,kptr,klen,vptr,vlen) qse_htb_update(map,kptr,klen,vptr,vlen)
# define qse_map_delete(map,kptr,klen) qse_htb_delete(map,kptr,klen)
# define qse_map_clear(map) qse_htb_clear(map)
# define qse_map_walk(map,walker,ctx) qse_htb_walk(map,walker,ctx)
# define QSE_MAP_WALK_STOP QSE_HTB_WALK_STOP
# define QSE_MAP_WALK_FORWARD QSE_HTB_WALK_FORWARD
# define qse_map_walk_t qse_htb_walk_t
# define QSE_MAP_KEY QSE_HTB_KEY
# define QSE_MAP_VAL QSE_HTB_VAL
# define qse_map_id_t qse_htb_id_t
# define qse_map_t qse_htb_t
# define qse_map_pair_t qse_htb_pair_t
# define QSE_MAP_COPIER_SIMPLE QSE_HTB_COPIER_SIMPLE
# define QSE_MAP_COPIER_INLINE QSE_HTB_COPIER_INLINE
# define QSE_MAP_SIZE(map) QSE_HTB_SIZE(map)
# define QSE_MAP_KCOPIER(map) QSE_HTB_KCOPIER(map)
# define QSE_MAP_VCOPIER(map) QSE_HTB_VCOPIER(map)
# define QSE_MAP_KFREEER(map) QSE_HTB_KFREEER(map)
# define QSE_MAP_VFREEER(map) QSE_HTB_VFREEER(map)
# define QSE_MAP_COMPER(map) QSE_HTB_COMPER(map)
# define QSE_MAP_KEEPER(map) QSE_HTB_KEEPER(map)
# define QSE_MAP_KSCALE(map) QSE_HTB_KSCALE(map)
# define QSE_MAP_VSCALE(map) QSE_HTB_VSCALE(map)
# define QSE_MAP_KPTR(p) QSE_HTB_KPTR(p)
# define QSE_MAP_KLEN(p) QSE_HTB_KLEN(p)
# define QSE_MAP_VPTR(p) QSE_HTB_VPTR(p)
# define QSE_MAP_VLEN(p) QSE_HTB_VLEN(p)
#elif defined(QSE_MAP_AS_RBT)
# include <qse/cmn/rbt.h>
# define qse_map_open(mmgr,ext,capa,factor) qse_rbt_open(mmgr,ext)
# define qse_map_close(map) qse_rbt_close(map)
# define qse_map_init(map,mmgr,capa,factor) qse_rbt_init(map,mmgr)
# define qse_map_fini(map) qse_rbt_fini(map)
# define qse_map_getsize(map) qse_rbt_getsize(map)
# define qse_map_getcapa(map) qse_rbt_getcapa(map)
# define qse_map_getscale(map,id) qse_rbt_getscale(map,id)
# define qse_map_setscale(map,id,scale) qse_rbt_setscale(map,id,scale)
# define qse_map_getcopier(map,id) qse_rbt_getcopier(map,id)
# define qse_map_setcopier(map,id,cb) qse_rbt_setcopier(map,id,cb)
# define qse_map_getfreeer(map,id) qse_rbt_getfreeer(map,id)
# define qse_map_setfreeer(map,id,cb) qse_rbt_setfreeer(map,id,cb)
# define qse_map_getcomper(map,id) qse_rbt_getcomper(map,id)
# define qse_map_setcomper(map,id,cb) qse_rbt_setcomper(map,id,cb)
# define qse_map_getkeeper(map,id) qse_rbt_getkeeper(map,id)
# define qse_map_setkeeper(map,id,cb) qse_rbt_setkeeper(map,id,cb)
# define qse_map_gethasher(map,id)
# define qse_map_sethasher(map,id,cb)
# define qse_map_getsizer(map,id)
# define qse_map_setsizer(map,id,cb)
# define qse_map_search(map,kptr,klen) qse_rbt_search(map,kptr,klen)
# define qse_map_upsert(map,kptr,klen,vptr,vlen) qse_rbt_upsert(map,kptr,klen,vptr,vlen)
# define qse_map_ensert(map,kptr,klen,vptr,vlen) qse_rbt_ensert(map,kptr,klen,vptr,vlen)
# define qse_map_insert(map,kptr,klen,vptr,vlen) qse_rbt_insert(map,kptr,klen,vptr,vlen)
# define qse_map_update(map,kptr,klen,vptr,vlen) qse_rbt_update(map,kptr,klen,vptr,vlen)
# define qse_map_delete(map,kptr,klen) qse_rbt_delete(map,kptr,klen)
# define qse_map_clear(map) qse_rbt_clear(map)
# define qse_map_walk(map,walker,ctx) qse_rbt_walk(map,walker,ctx)
# define QSE_MAP_WALK_STOP QSE_RBT_WALK_STOP
# define QSE_MAP_WALK_FORWARD QSE_RBT_WALK_FORWARD
# define qse_map_walk_t qse_rbt_walk_t
# define QSE_MAP_KEY QSE_RBT_KEY
# define QSE_MAP_VAL QSE_RBT_VAL
# define qse_map_id_t qse_rbt_id_t
# define qse_map_t qse_rbt_t
# define qse_map_pair_t qse_rbt_pair_t
# define QSE_MAP_COPIER_SIMPLE QSE_RBT_COPIER_SIMPLE
# define QSE_MAP_COPIER_INLINE QSE_RBT_COPIER_INLINE
# define QSE_MAP_SIZE(map) QSE_RBT_SIZE(map)
# define QSE_MAP_KCOPIER(map) QSE_RBT_KCOPIER(map)
# define QSE_MAP_VCOPIER(map) QSE_RBT_VCOPIER(map)
# define QSE_MAP_KFREEER(map) QSE_RBT_KFREEER(map)
# define QSE_MAP_VFREEER(map) QSE_RBT_VFREEER(map)
# define QSE_MAP_COMPER(map) QSE_RBT_COMPER(map)
# define QSE_MAP_KEEPER(map) QSE_RBT_KEEPER(map)
# define QSE_MAP_KSCALE(map) QSE_RBT_KSCALE(map)
# define QSE_MAP_VSCALE(map) QSE_RBT_VSCALE(map)
# define QSE_MAP_KPTR(p) QSE_RBT_KPTR(p)
# define QSE_MAP_KLEN(p) QSE_RBT_KLEN(p)
# define QSE_MAP_VPTR(p) QSE_RBT_VPTR(p)
# define QSE_MAP_VLEN(p) QSE_RBT_VLEN(p)
#else
# error define QSE_MAP_AS_HTB or QSE_MAP_AS_RBT before including this file
#endif
#endif

View File

@ -510,6 +510,8 @@ static pair_t* change_pair_val (
if (!IS_NIL(rbt,pair->left)) pair->left->parent = p;
if (!IS_NIL(rbt,pair->right)) pair->right->parent = p;
if (pair == rbt->root) rbt->root = p;
free_pair (rbt, pair);
return p;
}
@ -815,7 +817,30 @@ void qse_rbt_clear (rbt_t* rbt)
while (!IS_NIL(rbt,rbt->root)) delete_pair (rbt, rbt->root);
}
static QSE_INLINE void walk (rbt_t* rbt, walker_t walker, void* ctx, int l, int r)
#if 0
static QSE_INLINE qse_rbt_walk_t walk_recursively (
rbt_t* rbt, walker_t walker, void* ctx, qse_rbt_pair_t* pair)
{
if (!IS_NIL(rbt,pair->left))
{
if (walk_recursively (rbt, walker, ctx, pair->left) == QSE_RBT_WALK_STOP)
return QSE_RBT_WALK_STOP;
}
if (walker (rbt, pair, ctx) == QSE_RBT_WALK_STOP) return QSE_RBT_WALK_STOP;
if (!IS_NIL(rbt,pair->right))
{
if (walk_recursively (rbt, walker, ctx, pair->right) == QSE_RBT_WALK_STOP)
return QSE_RBT_WALK_STOP;
}
return QSE_RBT_WALK_FORWARD;
}
#endif
static QSE_INLINE void walk (
rbt_t* rbt, walker_t walker, void* ctx, int l, int r)
{
pair_t* xcur = rbt->root;
pair_t* prev = rbt->root->parent;
@ -871,7 +896,7 @@ static QSE_INLINE void walk (rbt_t* rbt, walker_t walker, void* ctx, int l, int
}
else
{
/* both the left child and the right child have beem traversed */
/* both the left child and the right child have been traversed */
QSE_ASSERT (prev == xcur->child[r]);
/* just move up to the parent */
prev = xcur;
@ -889,3 +914,4 @@ void qse_rbt_rwalk (rbt_t* rbt, walker_t walker, void* ctx)
{
walk (rbt, walker, ctx, RIGHT, LEFT);
}

View File

@ -1,5 +1,5 @@
/*
* $Id: sed.c 328 2010-07-08 06:58:44Z hyunghwan.chung $
* $Id: sed.c 334 2010-07-14 12:54:48Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
This file is part of QSE.
@ -92,19 +92,19 @@ static qse_sed_t* qse_sed_init (qse_sed_t* sed, qse_mmgr_t* mmgr)
return QSE_NULL;
}
if (qse_htb_init (&sed->tmp.labs, mmgr, 128, 70) == QSE_NULL)
if (qse_map_init (&sed->tmp.labs, mmgr, 128, 70) == 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_htb_setcopier (&sed->tmp.labs, QSE_HTB_KEY, QSE_HTB_COPIER_INLINE);
qse_htb_setscale (&sed->tmp.labs, QSE_HTB_KEY, QSE_SIZEOF(qse_char_t));
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));
if (qse_lda_init (&sed->e.txt.appended, mmgr, 32) == QSE_NULL)
{
qse_htb_fini (&sed->tmp.labs);
qse_map_fini (&sed->tmp.labs);
qse_str_fini (&sed->tmp.lab);
qse_str_fini (&sed->tmp.rex);
return QSE_NULL;
@ -113,7 +113,7 @@ static qse_sed_t* qse_sed_init (qse_sed_t* sed, qse_mmgr_t* mmgr)
if (qse_str_init (&sed->e.txt.read, mmgr, 256) == QSE_NULL)
{
qse_lda_fini (&sed->e.txt.appended);
qse_htb_fini (&sed->tmp.labs);
qse_map_fini (&sed->tmp.labs);
qse_str_fini (&sed->tmp.lab);
qse_str_fini (&sed->tmp.rex);
return QSE_NULL;
@ -123,7 +123,7 @@ static qse_sed_t* qse_sed_init (qse_sed_t* sed, qse_mmgr_t* mmgr)
{
qse_str_fini (&sed->e.txt.read);
qse_lda_fini (&sed->e.txt.appended);
qse_htb_fini (&sed->tmp.labs);
qse_map_fini (&sed->tmp.labs);
qse_str_fini (&sed->tmp.lab);
qse_str_fini (&sed->tmp.rex);
return QSE_NULL;
@ -134,7 +134,7 @@ static qse_sed_t* qse_sed_init (qse_sed_t* sed, qse_mmgr_t* mmgr)
qse_str_fini (&sed->e.txt.held);
qse_str_fini (&sed->e.txt.read);
qse_lda_fini (&sed->e.txt.appended);
qse_htb_fini (&sed->tmp.labs);
qse_map_fini (&sed->tmp.labs);
qse_str_fini (&sed->tmp.lab);
qse_str_fini (&sed->tmp.rex);
return QSE_NULL;
@ -158,7 +158,7 @@ static void qse_sed_fini (qse_sed_t* sed)
qse_str_fini (&sed->e.txt.read);
qse_lda_fini (&sed->e.txt.appended);
qse_htb_fini (&sed->tmp.labs);
qse_map_fini (&sed->tmp.labs);
qse_str_fini (&sed->tmp.lab);
qse_str_fini (&sed->tmp.rex);
}
@ -565,7 +565,7 @@ static int get_label (qse_sed_t* sed, qse_sed_cmd_t* cmd)
}
while (IS_LABCHAR(c));
if (qse_htb_search (
if (qse_map_search (
&sed->tmp.labs,
QSE_STR_PTR(&sed->tmp.lab),
QSE_STR_LEN(&sed->tmp.lab)) != QSE_NULL)
@ -579,7 +579,7 @@ static int get_label (qse_sed_t* sed, qse_sed_cmd_t* cmd)
return -1;
}
if (qse_htb_insert (
if (qse_map_insert (
&sed->tmp.labs,
QSE_STR_PTR(&sed->tmp.lab), QSE_STR_LEN(&sed->tmp.lab),
cmd, 0) == QSE_NULL)
@ -626,7 +626,7 @@ static int get_branch_target (qse_sed_t* sed, qse_sed_cmd_t* cmd)
{
qse_cint_t c;
qse_str_t* t = QSE_NULL;
qse_htb_pair_t* pair;
qse_map_pair_t* pair;
/* skip white spaces */
c = CURSC(sed);
@ -664,7 +664,7 @@ static int get_branch_target (qse_sed_t* sed, qse_sed_cmd_t* cmd)
if (terminate_command (sed) <= -1) goto oops;
pair = qse_htb_search (&sed->tmp.labs, QSE_STR_PTR(t), QSE_STR_LEN(t));
pair = qse_map_search (&sed->tmp.labs, QSE_STR_PTR(t), QSE_STR_LEN(t));
if (pair == QSE_NULL)
{
/* label not resolved yet */
@ -675,7 +675,7 @@ static int get_branch_target (qse_sed_t* sed, qse_sed_cmd_t* cmd)
{
cmd->u.branch.label.ptr = QSE_NULL;
cmd->u.branch.label.len = 0;
cmd->u.branch.target = QSE_HTB_VPTR(pair);
cmd->u.branch.target = QSE_MAP_VPTR(pair);
}
qse_str_close (t);
@ -1247,7 +1247,7 @@ int qse_sed_comp (qse_sed_t* sed, const qse_char_t* sptr, qse_size_t slen)
QSE_ASSERT (sed->cmd.lb == &sed->cmd.fb && sed->cmd.lb->len == 0);
/* clear the label table */
qse_htb_clear (&sed->tmp.labs);
qse_map_clear (&sed->tmp.labs);
/* clear temporary data */
sed->tmp.grp.level = 0;
@ -1750,16 +1750,16 @@ static int write_str_to_file (
const qse_char_t* path, qse_size_t plen)
{
qse_ssize_t n;
qse_htb_pair_t* pair;
qse_map_pair_t* pair;
qse_sed_io_arg_t* ap;
pair = qse_htb_search (&sed->e.out.files, path, plen);
pair = qse_map_search (&sed->e.out.files, path, plen);
if (pair == QSE_NULL)
{
qse_sed_io_arg_t arg;
QSE_MEMSET (&arg, 0, QSE_SIZEOF(arg));
pair = qse_htb_insert (&sed->e.out.files,
pair = qse_map_insert (&sed->e.out.files,
(void*)path, plen, &arg, QSE_SIZEOF(arg));
if (pair == QSE_NULL)
{
@ -1768,7 +1768,7 @@ static int write_str_to_file (
}
}
ap = QSE_HTB_VPTR(pair);
ap = QSE_MAP_VPTR(pair);
if (ap->handle == QSE_NULL)
{
sed->errnum = QSE_SED_ENOERR;
@ -2504,7 +2504,7 @@ static qse_sed_cmd_t* exec_cmd (qse_sed_t* sed, qse_sed_cmd_t* cmd)
return jumpto;
}
static void close_outfile (qse_htb_t* map, void* dptr, qse_size_t dlen)
static void close_outfile (qse_map_t* map, void* dptr, qse_size_t dlen)
{
qse_sed_io_arg_t* arg = dptr;
QSE_ASSERT (dlen == QSE_SIZEOF(*arg));
@ -2550,7 +2550,7 @@ static int init_command_block_for_exec (qse_sed_t* sed, qse_sed_cmd_blk_t* b)
c->u.branch.target == QSE_NULL)
{
/* resolve unresolved branch targets */
qse_htb_pair_t* pair;
qse_map_pair_t* pair;
qse_xstr_t* lab = &c->u.branch.label;
if (lab->ptr == QSE_NULL)
@ -2561,7 +2561,7 @@ static int init_command_block_for_exec (qse_sed_t* sed, qse_sed_cmd_blk_t* b)
else
{
/* resolve the target */
pair = qse_htb_search (
pair = qse_map_search (
&sed->tmp.labs, lab->ptr, lab->len);
if (pair == QSE_NULL)
{
@ -2572,7 +2572,7 @@ static int init_command_block_for_exec (qse_sed_t* sed, qse_sed_cmd_blk_t* b)
return -1;
}
c->u.branch.target = QSE_HTB_VPTR(pair);
c->u.branch.target = QSE_MAP_VPTR(pair);
/* free resolved label name */
QSE_MMGR_FREE (sed->mmgr, lab->ptr);
@ -2640,20 +2640,20 @@ 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_htb_init (&sed->e.out.files, sed->mmgr, 128, 70) == QSE_NULL)
if (qse_map_init (&sed->e.out.files, sed->mmgr, 128, 70) == QSE_NULL)
{
SETERR0 (sed, QSE_SED_ENOMEM, QSE_NULL);
return -1;
}
*(qse_sed_t**)QSE_XTN(&sed->e.out.files) = sed;
qse_htb_setcopier (
&sed->e.out.files, QSE_HTB_KEY, QSE_HTB_COPIER_INLINE);
qse_htb_setscale (
&sed->e.out.files, QSE_HTB_KEY, QSE_SIZEOF(qse_char_t));
qse_htb_setcopier (
&sed->e.out.files, QSE_HTB_VAL, QSE_HTB_COPIER_INLINE);
qse_htb_setfreeer (
&sed->e.out.files, QSE_HTB_VAL, close_outfile);
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);
sed->e.in.fun = inf;
sed->e.in.eof = 0;
@ -2662,7 +2662,7 @@ int qse_sed_exec (qse_sed_t* sed, qse_sed_io_fun_t inf, qse_sed_io_fun_t outf)
sed->e.in.num = 0;
if (qse_str_init (&sed->e.in.line, QSE_MMGR(sed), 256) == QSE_NULL)
{
qse_htb_fini (&sed->e.out.files);
qse_map_fini (&sed->e.out.files);
SETERR0 (sed, QSE_SED_ENOMEM, QSE_NULL);
return -1;
}
@ -2779,13 +2779,13 @@ int qse_sed_exec (qse_sed_t* sed, qse_sed_io_fun_t inf, qse_sed_io_fun_t outf)
}
done:
qse_htb_clear (&sed->e.out.files);
qse_map_clear (&sed->e.out.files);
sed->e.out.fun (sed, QSE_SED_IO_CLOSE, &sed->e.out.arg, QSE_NULL, 0);
done2:
sed->e.in.fun (sed, QSE_SED_IO_CLOSE, &sed->e.in.arg, QSE_NULL, 0);
done3:
qse_str_fini (&sed->e.in.line);
qse_htb_fini (&sed->e.out.files);
qse_map_fini (&sed->e.out.files);
return ret;
}

View File

@ -1,5 +1,5 @@
/*
* $Id: sed.h 328 2010-07-08 06:58:44Z hyunghwan.chung $
* $Id: sed.h 334 2010-07-14 12:54:48Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
This file is part of QSE.
@ -23,9 +23,11 @@
#include <qse/sed/sed.h>
#include <qse/cmn/str.h>
#include <qse/cmn/htb.h>
#include <qse/cmn/lda.h>
#define QSE_MAP_AS_RBT
#include <qse/cmn/map.h>
typedef struct qse_sed_adr_t qse_sed_adr_t;
typedef struct qse_sed_cmd_t qse_sed_cmd_t;
typedef struct qse_sed_cmd_blk_t qse_sed_cmd_blk_t;
@ -188,7 +190,7 @@ struct qse_sed_t
} grp;
/** a table storing labels seen */
qse_htb_t labs;
qse_map_t labs;
} tmp;
/** compiled commands */
@ -218,7 +220,7 @@ struct qse_sed_t
/*****************************************************/
/* the following two fields are very tightly-coupled.
* don't make any partial changes */
qse_htb_t files;
qse_map_t files;
qse_sed_t* files_ext;
/*****************************************************/
} out;

View File

@ -18,6 +18,17 @@ static qse_rbt_walk_t walk (qse_rbt_t* rbt, qse_rbt_pair_t* pair, void* ctx)
return QSE_RBT_WALK_FORWARD;
}
static qse_rbt_walk_t walk2 (qse_rbt_t* rbt, qse_rbt_pair_t* pair, void* ctx)
{
qse_printf (QSE_T("key = %.*s, value = %.*s\n"),
(int)QSE_RBT_KLEN(pair),
QSE_RBT_KPTR(pair),
(int)QSE_RBT_VLEN(pair),
QSE_RBT_VPTR(pair));
return QSE_RBT_WALK_FORWARD;
}
static int test1 ()
{
qse_rbt_t* s1;
@ -26,7 +37,7 @@ static int test1 ()
s1 = qse_rbt_open (QSE_MMGR_GETDFL(), 0);
if (s1 == QSE_NULL)
{
qse_printf (QSE_T("cannot open a string\n"));
qse_printf (QSE_T("cannot open a table\n"));
return -1;
}
@ -88,8 +99,41 @@ qse_printf (QSE_T("deleting %d\n"), i);
return 0;
}
static int test2 ()
{
qse_rbt_t* s1;
int i;
s1 = qse_rbt_open (QSE_MMGR_GETDFL(), 0);
if (s1 == QSE_NULL)
{
qse_printf (QSE_T("cannot open a table\n"));
return -1;
}
qse_rbt_setcopier (s1, QSE_RBT_KEY, QSE_RBT_COPIER_INLINE);
qse_rbt_setcopier (s1, QSE_RBT_VAL, QSE_RBT_COPIER_INLINE);
qse_rbt_setscale (s1, QSE_RBT_KEY, QSE_SIZEOF(qse_char_t));
qse_rbt_setscale (s1, QSE_RBT_VAL, QSE_SIZEOF(qse_char_t));
qse_rbt_insert (s1, QSE_T("hello"), 5, QSE_T("mr. monkey"), 10);
qse_rbt_insert (s1, QSE_T("world"), 5, QSE_T("ms. panda"), 9);
qse_rbt_insert (s1, QSE_T("thinkpad"), 8, QSE_T("x61"), 3);
qse_rbt_rwalk (s1, walk2, QSE_NULL);
qse_printf (QSE_T("-------------------\n"));
qse_rbt_upsert (s1, QSE_T("hello"), 5, QSE_T("dr. gorilla"), 11);
qse_rbt_upsert (s1, QSE_T("thinkpad"), 8, QSE_T("x61 rocks"), 9);
qse_rbt_ensert (s1, QSE_T("vista"), 5, QSE_T("microsoft"), 9);
qse_rbt_rwalk (s1, walk2, QSE_NULL);
qse_rbt_close (s1);
return 0;
}
int main ()
{
R (test1);
R (test2);
return 0;
}