qse/samples/cmn/rbt01.c

233 lines
5.4 KiB
C
Raw Normal View History

#include <qse/cmn/mem.h>
#include <qse/cmn/str.h>
#include <qse/cmn/rbt.h>
2016-04-29 03:55:42 +00:00
#include <qse/si/sio.h>
#include <stdlib.h>
#define R(f) \
do { \
qse_printf (QSE_T("== %s ==\n"), QSE_T(#f)); \
if (f() == -1) return -1; \
} while (0)
2010-07-14 05:18:30 +00:00
static qse_rbt_walk_t walk (qse_rbt_t* rbt, qse_rbt_pair_t* pair, void* ctx)
{
2010-07-14 05:18:30 +00:00
qse_printf (QSE_T("key = %d, value = %d\n"),
*(int*)QSE_RBT_KPTR(pair), *(int*)QSE_RBT_VPTR(pair));
return QSE_RBT_WALK_FORWARD;
}
2010-07-15 06:54:48 +00:00
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;
int i;
s1 = qse_rbt_open (QSE_MMGR_GETDFL(), 0, 1, 1);
if (s1 == QSE_NULL)
{
2010-07-15 06:54:48 +00:00
qse_printf (QSE_T("cannot open a table\n"));
return -1;
}
qse_rbt_setstyle (s1, qse_getrbtstyle(QSE_RBT_STYLE_INLINE_COPIERS));
for (i = 0; i < 20; i++)
{
2010-07-14 05:18:30 +00:00
int x = i * 20;
qse_printf (QSE_T("inserting at %d\n"), i);
2010-07-14 05:18:30 +00:00
qse_rbt_insert (s1, &i, QSE_SIZEOF(i), &x, QSE_SIZEOF(x));
}
2010-07-14 05:18:30 +00:00
qse_rbt_rwalk (s1, walk, QSE_NULL);
for (i = 0; i < 20; i += 2)
{
qse_printf (QSE_T("deleting %d\n"), i);
2010-07-14 05:18:30 +00:00
qse_rbt_delete (s1, &i, QSE_SIZEOF(i));
}
2010-07-14 05:18:30 +00:00
qse_rbt_rwalk (s1, walk, QSE_NULL);
for (i = 0; i < 20; i++)
{
2010-07-14 05:18:30 +00:00
int x = i * 20;
qse_printf (QSE_T("inserting at %d\n"), i);
2010-07-14 05:18:30 +00:00
qse_rbt_insert (s1, &i, QSE_SIZEOF(i), &x, QSE_SIZEOF(x));
}
2010-07-14 05:18:30 +00:00
qse_rbt_rwalk (s1, walk, QSE_NULL);
qse_rbt_clear (s1);
for (i = 20; i > 0; i--)
{
2010-07-14 05:18:30 +00:00
int x = i * 20;
qse_printf (QSE_T("inserting at %d\n"), i);
2010-07-14 05:18:30 +00:00
qse_rbt_insert (s1, &i, QSE_SIZEOF(i), &x, QSE_SIZEOF(x));
}
2010-07-14 05:18:30 +00:00
qse_rbt_rwalk (s1, walk, QSE_NULL);
for (i = 0; i < 20; i += 3)
{
qse_printf (QSE_T("deleting %d\n"), i);
2010-07-14 05:18:30 +00:00
qse_rbt_delete (s1, &i, QSE_SIZEOF(i));
}
2010-07-14 05:18:30 +00:00
qse_rbt_rwalk (s1, walk, QSE_NULL);
qse_rbt_close (s1);
return 0;
}
2010-07-15 06:54:48 +00:00
static int test2 ()
{
qse_rbt_t* s1;
s1 = qse_rbt_open (QSE_MMGR_GETDFL(), 0, QSE_SIZEOF(qse_char_t), QSE_SIZEOF(qse_char_t));
2010-07-15 06:54:48 +00:00
if (s1 == QSE_NULL)
{
qse_printf (QSE_T("cannot open a table\n"));
return -1;
}
qse_rbt_setstyle (s1, qse_getrbtstyle(QSE_RBT_STYLE_INLINE_COPIERS));
2010-07-15 06:54:48 +00:00
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);
2010-10-31 06:49:18 +00:00
qse_rbt_upsert (s1, QSE_T("thinkpad"), 8, QSE_T("x61 rocks on"), 13);
2010-07-15 06:54:48 +00:00
qse_rbt_ensert (s1, QSE_T("vista"), 5, QSE_T("microsoft"), 9);
2010-10-31 06:49:18 +00:00
qse_rbt_update (s1, QSE_T("world"), 5, QSE_T("baseball classic"), 16);
2010-07-15 06:54:48 +00:00
qse_rbt_rwalk (s1, walk2, QSE_NULL);
qse_rbt_close (s1);
return 0;
}
2010-10-31 06:49:18 +00:00
qse_rbt_walk_t print_map_pair (qse_rbt_t* map, qse_rbt_pair_t* pair, void* arg)
{
qse_printf (QSE_T("%.*s[%d] => %.*s[%d]\n"),
(int)QSE_RBT_KLEN(pair), QSE_RBT_KPTR(pair), (int)QSE_RBT_KLEN(pair),
(int)QSE_RBT_VLEN(pair), QSE_RBT_VPTR(pair), (int)QSE_RBT_VLEN(pair));
return QSE_RBT_WALK_FORWARD;
}
static qse_rbt_pair_t* test5_cbserter (
qse_rbt_t* rbt, qse_rbt_pair_t* pair,
void* kptr, qse_size_t klen, void* ctx)
{
2014-07-08 14:30:42 +00:00
qse_cstr_t* v = (qse_cstr_t*)ctx;
2010-10-31 06:49:18 +00:00
if (pair == QSE_NULL)
{
/* no existing key for the key */
return qse_rbt_allocpair (rbt, kptr, klen, v->ptr, v->len);
}
else
{
/* a pair with the key exists.
* in this sample, i will append the new value to the old value
* separated by a comma */
qse_rbt_pair_t* new_pair;
qse_char_t comma = QSE_T(',');
qse_byte_t* vptr;
/* allocate a new pair, but without filling the actual value.
* note vptr is given QSE_NULL for that purpose */
new_pair = qse_rbt_allocpair (
2011-05-25 09:14:58 +00:00
rbt, kptr, klen, QSE_NULL,
QSE_RBT_VLEN(pair) + 1 + v->len);
2010-10-31 06:49:18 +00:00
if (new_pair == QSE_NULL) return QSE_NULL;
/* fill in the value space */
2011-05-25 09:14:58 +00:00
vptr = QSE_RBT_VPTR(new_pair);
qse_memcpy (vptr, QSE_RBT_VPTR(pair),
QSE_RBT_VLEN(pair) * QSE_SIZEOF(qse_char_t));
vptr += QSE_RBT_VLEN(pair) * QSE_SIZEOF(qse_char_t);
2010-10-31 06:49:18 +00:00
qse_memcpy (vptr, &comma, QSE_SIZEOF(qse_char_t));
vptr += QSE_SIZEOF(qse_char_t);
qse_memcpy (vptr, v->ptr, v->len*QSE_SIZEOF(qse_char_t));
/* this callback requires the old pair to be destroyed */
qse_rbt_freepair (rbt, pair);
/* return the new pair */
return new_pair;
}
}
static int test5 ()
{
qse_rbt_t* s1;
int i;
qse_char_t* keys[] =
{
QSE_T("one"), QSE_T("two"), QSE_T("three")
};
qse_char_t* vals[] =
{
QSE_T("1"), QSE_T("2"), QSE_T("3"), QSE_T("4"), QSE_T("5"),
};
s1 = qse_rbt_open (QSE_MMGR_GETDFL(), 0,
QSE_SIZEOF(qse_char_t), QSE_SIZEOF(qse_char_t));
if (s1 == QSE_NULL)
{
qse_printf (QSE_T("cannot open a hash table\n"));
return -1;
}
qse_rbt_setstyle (s1, qse_getrbtstyle(QSE_RBT_STYLE_INLINE_COPIERS));
2010-10-31 06:49:18 +00:00
for (i = 0; i < QSE_COUNTOF(vals); i++)
{
2014-07-08 14:30:42 +00:00
qse_cstr_t ctx;
2010-10-31 06:49:18 +00:00
qse_printf (QSE_T("setting a key [%s] and a value [%s]: "), keys[i%QSE_COUNTOF(keys)], vals[i]);
ctx.ptr = vals[i];
ctx.len = qse_strlen(vals[i]);
if (qse_rbt_cbsert (s1,
keys[i%QSE_COUNTOF(keys)],
qse_strlen(keys[i%QSE_COUNTOF(keys)]),
test5_cbserter, &ctx) == QSE_NULL)
{
qse_printf (QSE_T("[FAILED]\n"));
}
else
{
qse_printf (QSE_T("[OK]\n"));
}
}
qse_rbt_walk (s1, print_map_pair, QSE_NULL);
qse_rbt_close (s1);
return 0;
}
int main ()
{
2017-09-16 08:54:25 +00:00
qse_open_stdsios ();
R (test1);
2010-07-15 06:54:48 +00:00
R (test2);
2010-10-31 06:49:18 +00:00
R (test5);
2017-09-16 08:54:25 +00:00
qse_close_stdsios ();
return 0;
}