renamed qse_lda_t to qse_arr_t

This commit is contained in:
2016-09-23 14:53:51 +00:00
parent dfdc6ae045
commit 7f24950536
19 changed files with 1777 additions and 1777 deletions

View File

@ -7,6 +7,7 @@ AM_CPPFLAGS = \
bin_PROGRAMS = \
arr01 \
chr01 \
dll \
env01 \
@ -15,7 +16,6 @@ bin_PROGRAMS = \
fmt02 \
htb01 \
ipad01 \
lda \
main01 \
main02 \
mbwc01 \
@ -51,7 +51,7 @@ fmt01_SOURCES = fmt01.c
fmt02_SOURCES = fmt02.c
htb01_SOURCES = htb01.c
ipad01_SOURCES = ipad01.c
lda_SOURCES = lda.c
arr01_SOURCES = arr01.c
main01_SOURCES = main01.c
main02_SOURCES = main02.c
mbwc01_SOURCES = mbwc01.c
@ -75,14 +75,14 @@ if ENABLE_CXX
#rex02_CXXFLAGS = -I/usr/lib/wx/include/gtk2-unicode-release-2.8 -I/usr/include/wx-2.8 -D_FILE_OFFSET_BITS=64 -D_LARGE_FILES -D__WXGTK__ -pthread
#rex02_LDFLAGS = -pthread -Wl,-Bsymbolic-functions -lwx_gtk2ud_richtext-2.8 -lwx_gtk2ud_aui-2.8 -lwx_gtk2ud_xrc-2.8 -lwx_gtk2ud_qa-2.8 -lwx_gtk2ud_html-2.8 -lwx_gtk2ud_adv-2.8 -lwx_gtk2ud_core-2.8 -lwx_baseud_xml-2.8 -lwx_baseud_net-2.8 -lwx_baseud-2.8
bin_PROGRAMS += arr01 arr02 bh01 bh02 hl01 htb02 rbt02 rbt03 sp01 sp02 str02
arr01_SOURCES = arr01.cpp
arr01_LDADD = $(LDADD) -lqsecmnxx
bin_PROGRAMS += arr02 arr03 bh01 bh02 hl01 htb02 rbt02 rbt03 sp01 sp02 str02
arr02_SOURCES = arr02.cpp
arr02_LDADD = $(LDADD) -lqsecmnxx
arr03_SOURCES = arr03.cpp
arr03_LDADD = $(LDADD) -lqsecmnxx
bh01_SOURCES = bh01.cpp
bh01_LDADD = $(LDADD) -lqsecmnxx

View File

@ -78,20 +78,20 @@ PRE_UNINSTALL = :
POST_UNINSTALL = :
build_triplet = @build@
host_triplet = @host@
bin_PROGRAMS = chr01$(EXEEXT) dll$(EXEEXT) env01$(EXEEXT) fma$(EXEEXT) \
fmt01$(EXEEXT) fmt02$(EXEEXT) htb01$(EXEEXT) ipad01$(EXEEXT) \
lda$(EXEEXT) main01$(EXEEXT) main02$(EXEEXT) mbwc01$(EXEEXT) \
mbwc02$(EXEEXT) oht$(EXEEXT) path01$(EXEEXT) pma$(EXEEXT) \
rex01$(EXEEXT) rbt01$(EXEEXT) sll$(EXEEXT) slmb01$(EXEEXT) \
str01$(EXEEXT) str02$(EXEEXT) time$(EXEEXT) tre01$(EXEEXT) \
uri01$(EXEEXT) xma$(EXEEXT) $(am__EXEEXT_1)
bin_PROGRAMS = arr01$(EXEEXT) chr01$(EXEEXT) dll$(EXEEXT) \
env01$(EXEEXT) fma$(EXEEXT) fmt01$(EXEEXT) fmt02$(EXEEXT) \
htb01$(EXEEXT) ipad01$(EXEEXT) main01$(EXEEXT) main02$(EXEEXT) \
mbwc01$(EXEEXT) mbwc02$(EXEEXT) oht$(EXEEXT) path01$(EXEEXT) \
pma$(EXEEXT) rex01$(EXEEXT) rbt01$(EXEEXT) sll$(EXEEXT) \
slmb01$(EXEEXT) str01$(EXEEXT) str02$(EXEEXT) time$(EXEEXT) \
tre01$(EXEEXT) uri01$(EXEEXT) xma$(EXEEXT) $(am__EXEEXT_1)
@WCHAR_TRUE@@WIN32_TRUE@am__append_1 = $(UNICOWS_LIBS)
#bin_PROGRAMS += rex02
#rex02_SOURCES = rex02.cpp
#rex02_CXXFLAGS = -I/usr/lib/wx/include/gtk2-unicode-release-2.8 -I/usr/include/wx-2.8 -D_FILE_OFFSET_BITS=64 -D_LARGE_FILES -D__WXGTK__ -pthread
#rex02_LDFLAGS = -pthread -Wl,-Bsymbolic-functions -lwx_gtk2ud_richtext-2.8 -lwx_gtk2ud_aui-2.8 -lwx_gtk2ud_xrc-2.8 -lwx_gtk2ud_qa-2.8 -lwx_gtk2ud_html-2.8 -lwx_gtk2ud_adv-2.8 -lwx_gtk2ud_core-2.8 -lwx_baseud_xml-2.8 -lwx_baseud_net-2.8 -lwx_baseud-2.8
@ENABLE_CXX_TRUE@am__append_2 = arr01 arr02 bh01 bh02 hl01 htb02 rbt02 rbt03 sp01 sp02 str02
@ENABLE_CXX_TRUE@am__append_2 = arr02 arr03 bh01 bh02 hl01 htb02 rbt02 rbt03 sp01 sp02 str02
subdir = samples/cmn
DIST_COMMON = $(srcdir)/Makefile.in $(srcdir)/Makefile.am \
$(top_srcdir)/ac/depcomp
@ -110,19 +110,18 @@ mkinstalldirs = $(install_sh) -d
CONFIG_HEADER = $(top_builddir)/include/qse/config.h
CONFIG_CLEAN_FILES =
CONFIG_CLEAN_VPATH_FILES =
@ENABLE_CXX_TRUE@am__EXEEXT_1 = arr01$(EXEEXT) arr02$(EXEEXT) \
@ENABLE_CXX_TRUE@am__EXEEXT_1 = arr02$(EXEEXT) arr03$(EXEEXT) \
@ENABLE_CXX_TRUE@ bh01$(EXEEXT) bh02$(EXEEXT) hl01$(EXEEXT) \
@ENABLE_CXX_TRUE@ htb02$(EXEEXT) rbt02$(EXEEXT) rbt03$(EXEEXT) \
@ENABLE_CXX_TRUE@ sp01$(EXEEXT) sp02$(EXEEXT) str02$(EXEEXT)
am__installdirs = "$(DESTDIR)$(bindir)"
PROGRAMS = $(bin_PROGRAMS)
am__arr01_SOURCES_DIST = arr01.cpp
@ENABLE_CXX_TRUE@am_arr01_OBJECTS = arr01.$(OBJEXT)
am_arr01_OBJECTS = arr01.$(OBJEXT)
arr01_OBJECTS = $(am_arr01_OBJECTS)
arr01_LDADD = $(LDADD)
am__DEPENDENCIES_1 =
@WCHAR_TRUE@@WIN32_TRUE@am__DEPENDENCIES_2 = $(am__DEPENDENCIES_1)
am__DEPENDENCIES_3 = $(am__DEPENDENCIES_2)
@ENABLE_CXX_TRUE@arr01_DEPENDENCIES = $(am__DEPENDENCIES_3)
arr01_DEPENDENCIES = $(am__DEPENDENCIES_2)
AM_V_lt = $(am__v_lt_@AM_V@)
am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@)
am__v_lt_0 = --silent
@ -130,7 +129,12 @@ am__v_lt_1 =
am__arr02_SOURCES_DIST = arr02.cpp
@ENABLE_CXX_TRUE@am_arr02_OBJECTS = arr02.$(OBJEXT)
arr02_OBJECTS = $(am_arr02_OBJECTS)
am__DEPENDENCIES_3 = $(am__DEPENDENCIES_2)
@ENABLE_CXX_TRUE@arr02_DEPENDENCIES = $(am__DEPENDENCIES_3)
am__arr03_SOURCES_DIST = arr03.cpp
@ENABLE_CXX_TRUE@am_arr03_OBJECTS = arr03.$(OBJEXT)
arr03_OBJECTS = $(am_arr03_OBJECTS)
@ENABLE_CXX_TRUE@arr03_DEPENDENCIES = $(am__DEPENDENCIES_3)
am__bh01_SOURCES_DIST = bh01.cpp
@ENABLE_CXX_TRUE@am_bh01_OBJECTS = bh01.$(OBJEXT)
bh01_OBJECTS = $(am_bh01_OBJECTS)
@ -179,10 +183,6 @@ am_ipad01_OBJECTS = ipad01.$(OBJEXT)
ipad01_OBJECTS = $(am_ipad01_OBJECTS)
ipad01_LDADD = $(LDADD)
ipad01_DEPENDENCIES = $(am__DEPENDENCIES_2)
am_lda_OBJECTS = lda.$(OBJEXT)
lda_OBJECTS = $(am_lda_OBJECTS)
lda_LDADD = $(LDADD)
lda_DEPENDENCIES = $(am__DEPENDENCIES_2)
am_main01_OBJECTS = main01.$(OBJEXT)
main01_OBJECTS = $(am_main01_OBJECTS)
main01_LDADD = $(LDADD)
@ -319,11 +319,11 @@ AM_V_CXXLD = $(am__v_CXXLD_@AM_V@)
am__v_CXXLD_ = $(am__v_CXXLD_@AM_DEFAULT_V@)
am__v_CXXLD_0 = @echo " CXXLD " $@;
am__v_CXXLD_1 =
SOURCES = $(arr01_SOURCES) $(arr02_SOURCES) $(bh01_SOURCES) \
$(bh02_SOURCES) $(chr01_SOURCES) $(dll_SOURCES) \
$(env01_SOURCES) $(fma_SOURCES) $(fmt01_SOURCES) \
$(fmt02_SOURCES) $(hl01_SOURCES) $(htb01_SOURCES) \
$(htb02_SOURCES) $(ipad01_SOURCES) $(lda_SOURCES) \
SOURCES = $(arr01_SOURCES) $(arr02_SOURCES) $(arr03_SOURCES) \
$(bh01_SOURCES) $(bh02_SOURCES) $(chr01_SOURCES) \
$(dll_SOURCES) $(env01_SOURCES) $(fma_SOURCES) \
$(fmt01_SOURCES) $(fmt02_SOURCES) $(hl01_SOURCES) \
$(htb01_SOURCES) $(htb02_SOURCES) $(ipad01_SOURCES) \
$(main01_SOURCES) $(main02_SOURCES) $(mbwc01_SOURCES) \
$(mbwc02_SOURCES) $(oht_SOURCES) $(path01_SOURCES) \
$(pma_SOURCES) $(rbt01_SOURCES) $(rbt02_SOURCES) \
@ -331,15 +331,15 @@ SOURCES = $(arr01_SOURCES) $(arr02_SOURCES) $(bh01_SOURCES) \
$(slmb01_SOURCES) $(sp01_SOURCES) $(sp02_SOURCES) \
$(str01_SOURCES) $(str02_SOURCES) $(time_SOURCES) \
$(tre01_SOURCES) uri01.c $(xma_SOURCES)
DIST_SOURCES = $(am__arr01_SOURCES_DIST) $(am__arr02_SOURCES_DIST) \
$(am__bh01_SOURCES_DIST) $(am__bh02_SOURCES_DIST) \
$(chr01_SOURCES) $(dll_SOURCES) $(env01_SOURCES) \
$(fma_SOURCES) $(fmt01_SOURCES) $(fmt02_SOURCES) \
$(am__hl01_SOURCES_DIST) $(htb01_SOURCES) \
$(am__htb02_SOURCES_DIST) $(ipad01_SOURCES) $(lda_SOURCES) \
$(main01_SOURCES) $(main02_SOURCES) $(mbwc01_SOURCES) \
$(mbwc02_SOURCES) $(oht_SOURCES) $(path01_SOURCES) \
$(pma_SOURCES) $(rbt01_SOURCES) $(am__rbt02_SOURCES_DIST) \
DIST_SOURCES = $(arr01_SOURCES) $(am__arr02_SOURCES_DIST) \
$(am__arr03_SOURCES_DIST) $(am__bh01_SOURCES_DIST) \
$(am__bh02_SOURCES_DIST) $(chr01_SOURCES) $(dll_SOURCES) \
$(env01_SOURCES) $(fma_SOURCES) $(fmt01_SOURCES) \
$(fmt02_SOURCES) $(am__hl01_SOURCES_DIST) $(htb01_SOURCES) \
$(am__htb02_SOURCES_DIST) $(ipad01_SOURCES) $(main01_SOURCES) \
$(main02_SOURCES) $(mbwc01_SOURCES) $(mbwc02_SOURCES) \
$(oht_SOURCES) $(path01_SOURCES) $(pma_SOURCES) \
$(rbt01_SOURCES) $(am__rbt02_SOURCES_DIST) \
$(am__rbt03_SOURCES_DIST) $(rex01_SOURCES) $(sll_SOURCES) \
$(slmb01_SOURCES) $(am__sp01_SOURCES_DIST) \
$(am__sp02_SOURCES_DIST) $(str01_SOURCES) \
@ -561,7 +561,7 @@ fmt01_SOURCES = fmt01.c
fmt02_SOURCES = fmt02.c
htb01_SOURCES = htb01.c
ipad01_SOURCES = ipad01.c
lda_SOURCES = lda.c
arr01_SOURCES = arr01.c
main01_SOURCES = main01.c
main02_SOURCES = main02.c
mbwc01_SOURCES = mbwc01.c
@ -577,10 +577,10 @@ str01_SOURCES = str01.c
time_SOURCES = time.c
tre01_SOURCES = tre01.c
xma_SOURCES = xma.c
@ENABLE_CXX_TRUE@arr01_SOURCES = arr01.cpp
@ENABLE_CXX_TRUE@arr01_LDADD = $(LDADD) -lqsecmnxx
@ENABLE_CXX_TRUE@arr02_SOURCES = arr02.cpp
@ENABLE_CXX_TRUE@arr02_LDADD = $(LDADD) -lqsecmnxx
@ENABLE_CXX_TRUE@arr03_SOURCES = arr03.cpp
@ENABLE_CXX_TRUE@arr03_LDADD = $(LDADD) -lqsecmnxx
@ENABLE_CXX_TRUE@bh01_SOURCES = bh01.cpp
@ENABLE_CXX_TRUE@bh01_LDADD = $(LDADD) -lqsecmnxx
@ENABLE_CXX_TRUE@bh02_SOURCES = bh02.cpp
@ -685,12 +685,16 @@ clean-binPROGRAMS:
arr01$(EXEEXT): $(arr01_OBJECTS) $(arr01_DEPENDENCIES) $(EXTRA_arr01_DEPENDENCIES)
@rm -f arr01$(EXEEXT)
$(AM_V_CXXLD)$(CXXLINK) $(arr01_OBJECTS) $(arr01_LDADD) $(LIBS)
$(AM_V_CCLD)$(LINK) $(arr01_OBJECTS) $(arr01_LDADD) $(LIBS)
arr02$(EXEEXT): $(arr02_OBJECTS) $(arr02_DEPENDENCIES) $(EXTRA_arr02_DEPENDENCIES)
@rm -f arr02$(EXEEXT)
$(AM_V_CXXLD)$(CXXLINK) $(arr02_OBJECTS) $(arr02_LDADD) $(LIBS)
arr03$(EXEEXT): $(arr03_OBJECTS) $(arr03_DEPENDENCIES) $(EXTRA_arr03_DEPENDENCIES)
@rm -f arr03$(EXEEXT)
$(AM_V_CXXLD)$(CXXLINK) $(arr03_OBJECTS) $(arr03_LDADD) $(LIBS)
bh01$(EXEEXT): $(bh01_OBJECTS) $(bh01_DEPENDENCIES) $(EXTRA_bh01_DEPENDENCIES)
@rm -f bh01$(EXEEXT)
$(AM_V_CXXLD)$(CXXLINK) $(bh01_OBJECTS) $(bh01_LDADD) $(LIBS)
@ -739,10 +743,6 @@ ipad01$(EXEEXT): $(ipad01_OBJECTS) $(ipad01_DEPENDENCIES) $(EXTRA_ipad01_DEPENDE
@rm -f ipad01$(EXEEXT)
$(AM_V_CCLD)$(LINK) $(ipad01_OBJECTS) $(ipad01_LDADD) $(LIBS)
lda$(EXEEXT): $(lda_OBJECTS) $(lda_DEPENDENCIES) $(EXTRA_lda_DEPENDENCIES)
@rm -f lda$(EXEEXT)
$(AM_V_CCLD)$(LINK) $(lda_OBJECTS) $(lda_LDADD) $(LIBS)
main01$(EXEEXT): $(main01_OBJECTS) $(main01_DEPENDENCIES) $(EXTRA_main01_DEPENDENCIES)
@rm -f main01$(EXEEXT)
$(AM_V_CCLD)$(LINK) $(main01_OBJECTS) $(main01_LDADD) $(LIBS)
@ -835,6 +835,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/arr01.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/arr02.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/arr03.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/bh01.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/bh02.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/chr01.Po@am__quote@
@ -847,7 +848,6 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/htb01.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/htb02.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ipad01.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/lda.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/main01.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/main02.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/mbwc01.Po@am__quote@

462
qse/samples/cmn/arr01.c Normal file
View File

@ -0,0 +1,462 @@
#include <qse/cmn/mem.h>
#include <qse/cmn/str.h>
#include <qse/cmn/arr.h>
#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)
void keeper1 (qse_arr_t* arr, void* dptr, qse_size_t dlen)
{
qse_printf (QSE_T("[%.*s] has been kept\n"), (int)dlen, dptr);
}
qse_arr_walk_t walker1 (qse_arr_t* arr, qse_size_t index, void* arg)
{
qse_printf (QSE_T("%d => [%.*s]\n"),
index, (int)QSE_ARR_DLEN(arr,index), QSE_ARR_DPTR(arr,index));
return QSE_ARR_WALK_FORWARD;
}
qse_arr_walk_t rwalker1 (qse_arr_t* arr, qse_size_t index, void* arg)
{
qse_printf (QSE_T("%d => [%.*s]\n"),
index, (int)QSE_ARR_DLEN(arr,index), QSE_ARR_DPTR(arr,index));
return QSE_ARR_WALK_BACKWARD;
}
qse_arr_walk_t walker3 (qse_arr_t* arr, qse_size_t index, void* arg)
{
qse_printf (QSE_T("%d => [%d]\n"),
index, *(int*)QSE_ARR_DPTR(arr,index));
return QSE_ARR_WALK_FORWARD;
}
static int test1 ()
{
qse_arr_t* s1;
qse_char_t* x[] =
{
QSE_T("this is so good"),
QSE_T("what the fuck"),
QSE_T("do you like it?"),
QSE_T("oopsy!")
};
int i;
s1 = qse_arr_open (QSE_MMGR_GETDFL(), 0, 0);
if (s1 == QSE_NULL)
{
qse_printf (QSE_T("cannot open a string\n"));
return -1;
}
qse_arr_setcopier (s1, QSE_ARR_COPIER_INLINE);
qse_arr_setkeeper (s1, keeper1);
qse_arr_setscale (s1, QSE_SIZEOF(qse_char_t));
for (i = 0; i < QSE_COUNTOF(x); i++)
{
if (qse_arr_insert (s1, 0, x[i], qse_strlen(x[i])) == QSE_ARR_NIL)
{
qse_printf (QSE_T("failed to add at 0 => [%.*s]\n"),
(int)qse_strlen(x[i]), x[i]);
}
else
{
qse_printf (QSE_T("add at 0 => [%.*s]\n"),
(int)qse_strlen(x[i]), x[i]);
}
}
if (qse_arr_update (s1, 0, QSE_ARR_DPTR(s1,0), QSE_ARR_DLEN(s1,0)) == QSE_ARR_NIL)
{
qse_printf (QSE_T("failed to update index 0 with [%.*s]\n"), (int)QSE_ARR_DLEN(s1,0), QSE_ARR_DPTR(s1,0));
}
else
{
qse_printf (QSE_T("updated index 0 with [%.*s]\n"), (int)QSE_ARR_DLEN(s1,0), QSE_ARR_DPTR(s1,0));
}
if (qse_arr_update (s1, 0, QSE_ARR_DPTR(s1,1), QSE_ARR_DLEN(s1,1)) == QSE_ARR_NIL)
{
qse_printf (QSE_T("updated index 0 with [%.*s]\n"), (int)QSE_ARR_DLEN(s1,1), QSE_ARR_DPTR(s1,1));
}
else
{
qse_printf (QSE_T("updated index 0 with [%.*s]\n"), (int)QSE_ARR_DLEN(s1,0), QSE_ARR_DPTR(s1,0));
}
for (i = 0; i < QSE_COUNTOF(x); i++)
{
if (qse_arr_insert (s1, 10, x[i], qse_strlen(x[i])) == QSE_ARR_NIL)
{
qse_printf (QSE_T("failed to add at 10 => [%.*s]\n"),
(int)qse_strlen(x[i]), x[i]);
}
else
{
qse_printf (QSE_T("add at 10 => [%.*s]\n"),
(int)qse_strlen(x[i]), x[i]);
}
}
qse_printf (QSE_T("arr size => %lu\n"), QSE_ARR_SIZE(s1));
qse_arr_walk (s1, walker1, QSE_NULL);
qse_printf (QSE_T("arr size => %lu\n"), QSE_ARR_SIZE(s1));
qse_arr_rwalk (s1, rwalker1, QSE_NULL);
qse_arr_setcapa (s1, 3);
qse_arr_close (s1);
return 0;
}
static int test2 ()
{
qse_arr_t* s1;
qse_arr_slot_t* p;
const qse_char_t* x[] =
{
QSE_T("this is so good"),
QSE_T("what the fuck"),
QSE_T("do you like it?"),
QSE_T("oopsy!"),
QSE_T("hello hello!"),
QSE_T("oopsy!")
};
const qse_char_t* y[] =
{
QSE_T("tipsy!"),
QSE_T("oopsy!")
};
int i, j;
s1 = qse_arr_open (QSE_MMGR_GETDFL(), 0, 0);
if (s1 == QSE_NULL)
{
qse_printf (QSE_T("cannot open a string\n"));
return -1;
}
qse_arr_setcopier (s1, QSE_ARR_COPIER_INLINE);
qse_arr_setscale (s1, QSE_SIZEOF(qse_char_t));
for (j = 0; j < 20; j++)
{
qse_size_t index;
for (i = 0; i < QSE_COUNTOF(x); i++)
{
if (qse_arr_insert (s1, (i + 1) * j - 1, x[i], qse_strlen(x[i])) == QSE_ARR_NIL)
{
qse_printf (QSE_T("failed to add at %u => [%.*s]\n"),
(unsigned int)((i + 1) * j - 1),
(int)qse_strlen(x[i]), x[i]);
}
else
{
qse_printf (QSE_T("add at %u => [%.*s]\n"),
(unsigned int)((i + 1) * j - 1),
(int)qse_strlen(x[i]), x[i]);
}
}
for (i = 0; i < QSE_ARR_SIZE(s1); i++)
{
if (QSE_ARR_SLOT(s1,i))
{
qse_printf (QSE_T("[%d] %d => [%.*s]\n"),
j, i, (int)QSE_ARR_DLEN(s1,i), QSE_ARR_DPTR(s1,i));
}
}
for (i = 0; i < QSE_COUNTOF(y); i++)
{
index = qse_arr_search (s1, 0, y[i], qse_strlen(y[i]));
if (index == QSE_ARR_NIL)
{
qse_printf (QSE_T("failed to find [%s]\n"), y[i]);
}
else
{
qse_printf (QSE_T("found [%.*s] at %lu\n"),
(int)QSE_ARR_DLEN(s1,index), QSE_ARR_DPTR(s1,index), (unsigned long)index);
}
index = qse_arr_rsearch (s1, QSE_ARR_SIZE(s1), y[i], qse_strlen(y[i]));
if (index == QSE_ARR_NIL)
{
qse_printf (QSE_T("failed to find [%s]\n"), y[i]);
}
else
{
qse_printf (QSE_T("found [%.*s] at %lu\n"),
(int)QSE_ARR_DLEN(s1,index), QSE_ARR_DPTR(s1,index), (unsigned long)index);
}
}
qse_arr_clear (s1);
qse_printf (QSE_T("~~~~~~~~\n"));
}
qse_arr_close (s1);
return 0;
}
static int test3 ()
{
qse_arr_t* s1;
const qse_char_t* x[] =
{
QSE_T("this is so good"),
QSE_T("what the fuck"),
QSE_T("do you like it?"),
QSE_T("oopsy!")
};
const qse_char_t* y =
QSE_T("We Accept MasterCard, VISA, JCB, Dinner & eCheck");
int i, j;
s1 = qse_arr_open (QSE_MMGR_GETDFL(), 0, 0);
if (s1 == QSE_NULL)
{
qse_printf (QSE_T("cannot open a string\n"));
return -1;
}
qse_arr_setcopier (s1, QSE_ARR_COPIER_INLINE);
qse_arr_setscale (s1, QSE_SIZEOF(qse_char_t));
for (j = 0; j < 20; j++)
{
for (i = 0; i < QSE_COUNTOF(x); i++)
{
if (qse_arr_insert (s1, (i + 1) * j - 1, x[i], qse_strlen(x[i])) == QSE_ARR_NIL)
{
qse_printf (QSE_T("failed to add at %u => [%.*s]\n"),
(unsigned int)((i + 1) * j - 1),
(int)qse_strlen(x[i]), x[i]);
}
else
{
qse_printf (QSE_T("add at %u => [%.*s]\n"),
(unsigned int)((i + 1) * j - 1),
(int)qse_strlen(x[i]), x[i]);
}
}
for (i = 2; i < 3; i++)
{
if (i < QSE_ARR_SIZE(s1))
{
if (QSE_ARR_SLOT(s1,i))
{
qse_printf (QSE_T("deleted at %d => [%.*s]\n"),
i, (int)QSE_ARR_DLEN(s1,i), QSE_ARR_DPTR(s1,i));
}
qse_arr_delete (s1, i, 1);
}
if (i < QSE_ARR_SIZE(s1))
{
if (qse_arr_update (s1, i, y, qse_strlen(y)) == QSE_ARR_NIL)
{
qse_printf (QSE_T("failed to update at %d => [%.*s]\n"),
i, (int)qse_strlen(y), y);
}
else
{
qse_printf (QSE_T("updated at %d => [%.*s]\n"),
i, (int)QSE_ARR_DLEN(s1,i), QSE_ARR_DPTR(s1,i));
}
}
}
qse_printf (QSE_T("array size => %lu\n"), (unsigned long)QSE_ARR_SIZE(s1));
for (i = 0; i < QSE_ARR_SIZE(s1); i++)
{
if (QSE_ARR_SLOT(s1,i))
{
qse_printf (QSE_T("[%d] %d => [%.*s]\n"),
j, i, (int)QSE_ARR_DLEN(s1,i), QSE_ARR_DPTR(s1,i));
}
}
{
qse_size_t count = qse_arr_uplete (s1, 3, 20);
qse_printf (QSE_T("upleted %lu items from index 3\n"), (unsigned long)count);
}
qse_printf (QSE_T("array size => %lu\n"), (unsigned long)QSE_ARR_SIZE(s1));
for (i = 0; i < QSE_ARR_SIZE(s1); i++)
{
if (QSE_ARR_SLOT(s1,i))
{
qse_printf (QSE_T("[%d] %d => [%.*s]\n"),
j, i, (int)QSE_ARR_DLEN(s1,i), QSE_ARR_DPTR(s1,i));
}
}
qse_arr_clear (s1);
qse_printf (QSE_T("~~~~~~~~\n"));
}
qse_arr_close (s1);
return 0;
}
qse_size_t sizer1 (qse_arr_t* arr, qse_size_t hint)
{
return 2;
}
static int test4 ()
{
int i;
qse_arr_t* s1;
const qse_char_t* x[] =
{
QSE_T("this is so good"),
QSE_T("what the fuck"),
QSE_T("do you like it?"),
QSE_T("oopsy!")
};
s1 = qse_arr_open (QSE_MMGR_GETDFL(), 0, 3);
if (s1 == QSE_NULL)
{
qse_printf (QSE_T("cannot open an array\n"));
return -1;
}
qse_arr_setcopier (s1, QSE_ARR_COPIER_INLINE);
qse_arr_setsizer (s1, sizer1);
qse_arr_setscale (s1, QSE_SIZEOF(qse_char_t));
for (i = 0; i < QSE_COUNTOF(x); i++)
{
if (qse_arr_insert (s1, 0, x[i], qse_strlen(x[i])) == QSE_ARR_NIL)
{
qse_printf (QSE_T("failed to add at 0 => [%.*s]\n"),
(int)qse_strlen(x[i]), x[i]);
}
else
{
qse_printf (QSE_T("add at 0 => [%.*s]\n"),
(int)qse_strlen(x[i]), x[i]);
}
}
qse_printf (QSE_T("arr size => %lu\n"), QSE_ARR_SIZE(s1));
qse_arr_walk (s1, walker1, QSE_NULL);
qse_printf (QSE_T("arr size => %lu\n"), QSE_ARR_SIZE(s1));
qse_arr_rwalk (s1, rwalker1, QSE_NULL);
qse_arr_close (s1);
return 0;
}
qse_arr_comper_t default_comparator;
static int inverse_comparator (qse_arr_t* arr,
const void* dptr1, size_t dlen1,
const void* dptr2, size_t dlen2)
{
return -default_comparator (arr, dptr1, dlen1, dptr2, dlen2);
}
static int test5 ()
{
qse_arr_t* s1;
int i, j;
s1 = qse_arr_open (QSE_MMGR_GETDFL(), 0, 3);
if (s1 == QSE_NULL)
{
qse_printf (QSE_T("cannot open an array\n"));
return -1;
}
qse_arr_setcopier (s1, QSE_ARR_COPIER_INLINE);
qse_arr_setscale (s1, QSE_SIZEOF(i));
/* inverse the comparator to implement min-heap */
default_comparator = qse_arr_getcomper (s1);
qse_arr_setcomper (s1, inverse_comparator);
for (i = 0; i < 25; i++)
{
j = rand () % 100;
qse_arr_pushheap (s1, &j, 1);
}
qse_printf (QSE_T("arr size => %lu\n"), QSE_ARR_SIZE(s1));
qse_arr_walk (s1, walker3, QSE_NULL);
while (QSE_ARR_SIZE(s1) > 10 )
{
qse_printf (QSE_T("top => %d\n"), *(int*)QSE_ARR_DPTR(s1,0));
qse_arr_popheap (s1);
}
for (i = 0; i < 25; i++)
{
j = rand () % 100;
qse_arr_pushheap (s1, &j, 1);
}
qse_printf (QSE_T("arr size => %lu\n"), QSE_ARR_SIZE(s1));
qse_arr_walk (s1, walker3, QSE_NULL);
while (QSE_ARR_SIZE(s1))
{
qse_printf (QSE_T("top => %d\n"), *(int*)QSE_ARR_DPTR(s1,0));
qse_arr_popheap (s1);
}
qse_arr_setcomper (s1, default_comparator);
for (i = 0; i < 25; i++)
{
j = rand () % 100;
qse_arr_pushheap (s1, &j, 1);
}
qse_printf (QSE_T("arr size => %lu\n"), QSE_ARR_SIZE(s1));
qse_arr_walk (s1, walker3, QSE_NULL);
while (QSE_ARR_SIZE(s1))
{
qse_printf (QSE_T("top => %d\n"), *(int*)QSE_ARR_DPTR(s1,0));
qse_arr_popheap (s1);
}
qse_arr_close (s1);
return 0;
}
int main ()
{
qse_openstdsios ();
R (test1);
R (test2);
R (test3);
R (test4);
R (test5);
qse_closestdsios ();
return 0;
}

View File

@ -1,148 +0,0 @@
#include <stdio.h>
#include <string.h>
#include <qse/cmn/Array.hpp>
#include <qse/cmn/String.hpp>
/*
typedef QSE::Array<int,int> IntArray;
struct IntClass
{
IntClass (int x = 0): x (x) {}
int x;
};
struct IntClassComparator
{
int operator() (const IntClass& v, int y) const
{
IntTable::DefaultComparator comp;
return comp (v.x, y);
}
};
*/
#if 1
class PosStr: public QSE::MbString
{
public:
PosStr (const char* str = ""): QSE::MbString(str), pos((qse_size_t)-1) {};
PosStr (const PosStr& ps): QSE::MbString (ps), pos(ps.pos)
{
}
~PosStr()
{
}
qse_size_t pos;
};
#else
class PosStr
{
public:
PosStr (const char* str = "")
{
strcpy (buf, str);
}
const char* getBuffer() const { return this->buf; }
const char* data() const { return this->buf; }
char buf[512];
};
#endif
struct cstr_comparator
{
int operator() (const char* v1, const QSE::MbString& v2) const
{
return strcmp (v1, v2.getBuffer());
}
};
typedef QSE::Array<PosStr> StrArray;
int main ()
{
//StrArray h (QSE_NULL, 100);
StrArray h (QSE_NULL, 15);
char buf[20];
for (int i = 0; i < 20; i++)
{
sprintf (buf, "hello %d", i);
PosStr x (buf);
h.insert (0, x);
}
#if 0
printf ("%s\n", h.get(3).getBuffer());
h.remove (3, 5);
printf ("%s\n", h.get(3).getBuffer());
h.remove (3, 5);
printf ("%s\n", h.get(3).getBuffer());
#endif
printf ("--------------------\n");
for (qse_size_t i = 0; i < h.getSize(); i++)
{
printf ("[%s] at [%lu]\n", h[i].getBuffer(), (unsigned long int)h.getIndex(h[i]));
}
printf ("--------------------\n");
StrArray h2 (h);
StrArray h3;
h3 = h2;
h.clear (true);
printf ("--------------------\n");
printf ("%d\n", (int)h.getSize());
printf ("--------------------\n");
for (qse_size_t i = 0; i < h2.getSize(); i++)
{
printf ("[%s] at [%lu]\n", h2[i].getBuffer(), (unsigned long int)h2.getIndex(h2[i]));
}
printf ("--------------------\n");
h3.insert (21, "this is a large string");
printf ("%d %d\n", (int)h2.getSize(), (int)h3.getSize());
printf ("--------------------\n");
h3.insert (21, "mystery!");
for (qse_size_t i = 0; i < h3.getSize(); i++)
{
printf ("[%s] at [%lu]\n", h3[i].getBuffer(), (unsigned long int)h3.getIndex(h3[i]));
}
printf ("--------------------\n");
h3.setCapacity (6);
h3.insert (6, "good?");
h3.rotate (StrArray::ROTATE_RIGHT, h3.getSize() / 2);
printf ("[%s] [%s]\n", h3.getValueAt(5).getBuffer(), h3.getValueAt(6).getBuffer());
printf ("%d\n", (int)h3.getSize());
printf ("--------------------\n");
h3.insert (1, "bad?");
for (qse_size_t i = 0; i < h3.getSize(); i++)
{
printf ("[%s] at [%lu]\n", h3[i].getBuffer(), (unsigned long int)h3.getIndex(h3[i]));
}
printf ("--------------------\n");
QSE::Array<int> a;
a.insert (0, 10);
a.insert (0, 20);
a.insert (0, 30);
const int& t = a[2u];
printf ("%lu\n", (unsigned long int)a.getIndex(t));
return 0;
}

View File

@ -4,105 +4,145 @@
#include <qse/cmn/Array.hpp>
#include <qse/cmn/String.hpp>
class Julia
/*
typedef QSE::Array<int,int> IntArray;
struct IntClass
{
public:
Julia (int q = 0): x(QSE_NULL)
{
this->x = new int (q);
printf ("constructor %d\n", q);
}
Julia (const Julia& q): x(QSE_NULL)
{
this->x = new int (*q.x);
printf ("copy constructor %d\n", *q.x);
}
#if defined(QSE_CPP_ENABLE_CPP11_MOVE)
Julia (Julia&& q)
{
printf ("move constructor %d\n", *q.x);
this->x = q.x;
q.x = QSE_NULL;
}
#endif
~Julia()
{
if (this->x)
{
printf ("deleting %p %d\n", this, *x);
delete this->x;
}
}
Julia& operator= (const Julia& q)
{
if (this != &q)
{
if (this->x) { delete this->x; this->x = QSE_NULL; }
this->x = new int (*q.x);
printf ("operator= %d\n", *q.x);
}
return *this;
}
#if defined(QSE_CPP_ENABLE_CPP11_MOVE)
Julia& operator= (Julia&& q)
{
if (this != &q)
{
if (this->x) { delete this->x; this->x = QSE_NULL; }
printf ("move operator= %d\n", *q.x);
this->x = q.x;
q.x = QSE_NULL;
}
return *this;
}
#endif
int* x;
IntClass (int x = 0): x (x) {}
int x;
};
typedef QSE::Array<Julia> JuliaArray;
struct IntClassComparator
{
int operator() (const IntClass& v, int y) const
{
IntTable::DefaultComparator comp;
return comp (v.x, y);
}
};
*/
#if 1
class PosStr: public QSE::MbString
{
public:
PosStr (const char* str = ""): QSE::MbString(str), pos((qse_size_t)-1) {};
PosStr (const PosStr& ps): QSE::MbString (ps), pos(ps.pos)
{
}
~PosStr()
{
}
qse_size_t pos;
};
#else
class PosStr
{
public:
PosStr (const char* str = "")
{
strcpy (buf, str);
}
const char* getBuffer() const { return this->buf; }
const char* data() const { return this->buf; }
char buf[512];
};
#endif
struct cstr_comparator
{
int operator() (const char* v1, const QSE::MbString& v2) const
{
return strcmp (v1, v2.getBuffer());
}
};
typedef QSE::Array<PosStr> StrArray;
int main ()
{
JuliaArray a0;
//StrArray h (QSE_NULL, 100);
StrArray h (QSE_NULL, 15);
char buf[20];
for (int i = 0; i < 256; i++)
for (int i = 0; i < 20; i++)
{
a0.insert (0, Julia(i));
sprintf (buf, "hello %d", i);
PosStr x (buf);
h.insert (0, x);
}
a0.setCapacity (1024);
a0.insert (512, Julia (512));
a0.remove (2, 5);
a0.update (5, Julia(9999));
#if defined(QSE_CPP_ENABLE_CPP11_MOVE)
JuliaArray a1 ((JuliaArray&&)a0);
#else
JuliaArray a1 (a0);
#if 0
printf ("%s\n", h.get(3).getBuffer());
h.remove (3, 5);
printf ("%s\n", h.get(3).getBuffer());
h.remove (3, 5);
printf ("%s\n", h.get(3).getBuffer());
#endif
printf ("OK: %d %d\n", (int)a0.getSize(), (int)a1.getSize());
printf ("--------------------\n");
for (qse_size_t i = 0; i < h.getSize(); i++)
{
printf ("[%s] at [%lu]\n", h[i].getBuffer(), (unsigned long int)h.getIndex(h[i]));
}
printf ("--------------------\n");
for (qse_size_t i = 0; i < a1.getSize(); i++)
StrArray h2 (h);
StrArray h3;
h3 = h2;
h.clear (true);
printf ("--------------------\n");
printf ("%d\n", (int)h.getSize());
printf ("--------------------\n");
for (qse_size_t i = 0; i < h2.getSize(); i++)
{
printf ("ITEM: %d => %d\n", (int)i, *a1[i].x);
printf ("[%s] at [%lu]\n", h2[i].getBuffer(), (unsigned long int)h2.getIndex(h2[i]));
}
printf ("----------------\n");
a1.rotate (JuliaArray::ROTATE_LEFT, 2);
for (qse_size_t i = 0; i < a1.getSize(); i++)
printf ("--------------------\n");
h3.insert (21, "this is a large string");
printf ("%d %d\n", (int)h2.getSize(), (int)h3.getSize());
printf ("--------------------\n");
h3.insert (21, "mystery!");
for (qse_size_t i = 0; i < h3.getSize(); i++)
{
printf ("ITEM: %d => %d\n", (int)i, *a1[i].x);
printf ("[%s] at [%lu]\n", h3[i].getBuffer(), (unsigned long int)h3.getIndex(h3[i]));
}
printf ("--------------------\n");
h3.setCapacity (6);
h3.insert (6, "good?");
h3.rotate (StrArray::ROTATE_RIGHT, h3.getSize() / 2);
printf ("[%s] [%s]\n", h3.getValueAt(5).getBuffer(), h3.getValueAt(6).getBuffer());
printf ("%d\n", (int)h3.getSize());
printf ("--------------------\n");
h3.insert (1, "bad?");
for (qse_size_t i = 0; i < h3.getSize(); i++)
{
printf ("[%s] at [%lu]\n", h3[i].getBuffer(), (unsigned long int)h3.getIndex(h3[i]));
}
printf ("--------------------\n");
QSE::Array<int> a;
a.insert (0, 10);
a.insert (0, 20);
a.insert (0, 30);
const int& t = a[2u];
printf ("%lu\n", (unsigned long int)a.getIndex(t));
return 0;
}

108
qse/samples/cmn/arr03.cpp Normal file
View File

@ -0,0 +1,108 @@
#include <stdio.h>
#include <string.h>
#include <qse/cmn/Array.hpp>
#include <qse/cmn/String.hpp>
class Julia
{
public:
Julia (int q = 0): x(QSE_NULL)
{
this->x = new int (q);
printf ("constructor %d\n", q);
}
Julia (const Julia& q): x(QSE_NULL)
{
this->x = new int (*q.x);
printf ("copy constructor %d\n", *q.x);
}
#if defined(QSE_CPP_ENABLE_CPP11_MOVE)
Julia (Julia&& q)
{
printf ("move constructor %d\n", *q.x);
this->x = q.x;
q.x = QSE_NULL;
}
#endif
~Julia()
{
if (this->x)
{
printf ("deleting %p %d\n", this, *x);
delete this->x;
}
}
Julia& operator= (const Julia& q)
{
if (this != &q)
{
if (this->x) { delete this->x; this->x = QSE_NULL; }
this->x = new int (*q.x);
printf ("operator= %d\n", *q.x);
}
return *this;
}
#if defined(QSE_CPP_ENABLE_CPP11_MOVE)
Julia& operator= (Julia&& q)
{
if (this != &q)
{
if (this->x) { delete this->x; this->x = QSE_NULL; }
printf ("move operator= %d\n", *q.x);
this->x = q.x;
q.x = QSE_NULL;
}
return *this;
}
#endif
int* x;
};
typedef QSE::Array<Julia> JuliaArray;
int main ()
{
JuliaArray a0;
for (int i = 0; i < 256; i++)
{
a0.insert (0, Julia(i));
}
a0.setCapacity (1024);
a0.insert (512, Julia (512));
a0.remove (2, 5);
a0.update (5, Julia(9999));
#if defined(QSE_CPP_ENABLE_CPP11_MOVE)
JuliaArray a1 ((JuliaArray&&)a0);
#else
JuliaArray a1 (a0);
#endif
printf ("OK: %d %d\n", (int)a0.getSize(), (int)a1.getSize());
for (qse_size_t i = 0; i < a1.getSize(); i++)
{
printf ("ITEM: %d => %d\n", (int)i, *a1[i].x);
}
printf ("----------------\n");
a1.rotate (JuliaArray::ROTATE_LEFT, 2);
for (qse_size_t i = 0; i < a1.getSize(); i++)
{
printf ("ITEM: %d => %d\n", (int)i, *a1[i].x);
}
return 0;
}

View File

@ -1,462 +0,0 @@
#include <qse/cmn/mem.h>
#include <qse/cmn/str.h>
#include <qse/cmn/lda.h>
#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)
void keeper1 (qse_lda_t* lda, void* dptr, qse_size_t dlen)
{
qse_printf (QSE_T("[%.*s] has been kept\n"), (int)dlen, dptr);
}
qse_lda_walk_t walker1 (qse_lda_t* lda, qse_size_t index, void* arg)
{
qse_printf (QSE_T("%d => [%.*s]\n"),
index, (int)QSE_LDA_DLEN(lda,index), QSE_LDA_DPTR(lda,index));
return QSE_LDA_WALK_FORWARD;
}
qse_lda_walk_t rwalker1 (qse_lda_t* lda, qse_size_t index, void* arg)
{
qse_printf (QSE_T("%d => [%.*s]\n"),
index, (int)QSE_LDA_DLEN(lda,index), QSE_LDA_DPTR(lda,index));
return QSE_LDA_WALK_BACKWARD;
}
qse_lda_walk_t walker3 (qse_lda_t* lda, qse_size_t index, void* arg)
{
qse_printf (QSE_T("%d => [%d]\n"),
index, *(int*)QSE_LDA_DPTR(lda,index));
return QSE_LDA_WALK_FORWARD;
}
static int test1 ()
{
qse_lda_t* s1;
qse_char_t* x[] =
{
QSE_T("this is so good"),
QSE_T("what the fuck"),
QSE_T("do you like it?"),
QSE_T("oopsy!")
};
int i;
s1 = qse_lda_open (QSE_MMGR_GETDFL(), 0, 0);
if (s1 == QSE_NULL)
{
qse_printf (QSE_T("cannot open a string\n"));
return -1;
}
qse_lda_setcopier (s1, QSE_LDA_COPIER_INLINE);
qse_lda_setkeeper (s1, keeper1);
qse_lda_setscale (s1, QSE_SIZEOF(qse_char_t));
for (i = 0; i < QSE_COUNTOF(x); i++)
{
if (qse_lda_insert (s1, 0, x[i], qse_strlen(x[i])) == QSE_LDA_NIL)
{
qse_printf (QSE_T("failed to add at 0 => [%.*s]\n"),
(int)qse_strlen(x[i]), x[i]);
}
else
{
qse_printf (QSE_T("add at 0 => [%.*s]\n"),
(int)qse_strlen(x[i]), x[i]);
}
}
if (qse_lda_update (s1, 0, QSE_LDA_DPTR(s1,0), QSE_LDA_DLEN(s1,0)) == QSE_LDA_NIL)
{
qse_printf (QSE_T("failed to update index 0 with [%.*s]\n"), (int)QSE_LDA_DLEN(s1,0), QSE_LDA_DPTR(s1,0));
}
else
{
qse_printf (QSE_T("updated index 0 with [%.*s]\n"), (int)QSE_LDA_DLEN(s1,0), QSE_LDA_DPTR(s1,0));
}
if (qse_lda_update (s1, 0, QSE_LDA_DPTR(s1,1), QSE_LDA_DLEN(s1,1)) == QSE_LDA_NIL)
{
qse_printf (QSE_T("updated index 0 with [%.*s]\n"), (int)QSE_LDA_DLEN(s1,1), QSE_LDA_DPTR(s1,1));
}
else
{
qse_printf (QSE_T("updated index 0 with [%.*s]\n"), (int)QSE_LDA_DLEN(s1,0), QSE_LDA_DPTR(s1,0));
}
for (i = 0; i < QSE_COUNTOF(x); i++)
{
if (qse_lda_insert (s1, 10, x[i], qse_strlen(x[i])) == QSE_LDA_NIL)
{
qse_printf (QSE_T("failed to add at 10 => [%.*s]\n"),
(int)qse_strlen(x[i]), x[i]);
}
else
{
qse_printf (QSE_T("add at 10 => [%.*s]\n"),
(int)qse_strlen(x[i]), x[i]);
}
}
qse_printf (QSE_T("lda size => %lu\n"), QSE_LDA_SIZE(s1));
qse_lda_walk (s1, walker1, QSE_NULL);
qse_printf (QSE_T("lda size => %lu\n"), QSE_LDA_SIZE(s1));
qse_lda_rwalk (s1, rwalker1, QSE_NULL);
qse_lda_setcapa (s1, 3);
qse_lda_close (s1);
return 0;
}
static int test2 ()
{
qse_lda_t* s1;
qse_lda_slot_t* p;
const qse_char_t* x[] =
{
QSE_T("this is so good"),
QSE_T("what the fuck"),
QSE_T("do you like it?"),
QSE_T("oopsy!"),
QSE_T("hello hello!"),
QSE_T("oopsy!")
};
const qse_char_t* y[] =
{
QSE_T("tipsy!"),
QSE_T("oopsy!")
};
int i, j;
s1 = qse_lda_open (QSE_MMGR_GETDFL(), 0, 0);
if (s1 == QSE_NULL)
{
qse_printf (QSE_T("cannot open a string\n"));
return -1;
}
qse_lda_setcopier (s1, QSE_LDA_COPIER_INLINE);
qse_lda_setscale (s1, QSE_SIZEOF(qse_char_t));
for (j = 0; j < 20; j++)
{
qse_size_t index;
for (i = 0; i < QSE_COUNTOF(x); i++)
{
if (qse_lda_insert (s1, (i + 1) * j - 1, x[i], qse_strlen(x[i])) == QSE_LDA_NIL)
{
qse_printf (QSE_T("failed to add at %u => [%.*s]\n"),
(unsigned int)((i + 1) * j - 1),
(int)qse_strlen(x[i]), x[i]);
}
else
{
qse_printf (QSE_T("add at %u => [%.*s]\n"),
(unsigned int)((i + 1) * j - 1),
(int)qse_strlen(x[i]), x[i]);
}
}
for (i = 0; i < QSE_LDA_SIZE(s1); i++)
{
if (QSE_LDA_SLOT(s1,i))
{
qse_printf (QSE_T("[%d] %d => [%.*s]\n"),
j, i, (int)QSE_LDA_DLEN(s1,i), QSE_LDA_DPTR(s1,i));
}
}
for (i = 0; i < QSE_COUNTOF(y); i++)
{
index = qse_lda_search (s1, 0, y[i], qse_strlen(y[i]));
if (index == QSE_LDA_NIL)
{
qse_printf (QSE_T("failed to find [%s]\n"), y[i]);
}
else
{
qse_printf (QSE_T("found [%.*s] at %lu\n"),
(int)QSE_LDA_DLEN(s1,index), QSE_LDA_DPTR(s1,index), (unsigned long)index);
}
index = qse_lda_rsearch (s1, QSE_LDA_SIZE(s1), y[i], qse_strlen(y[i]));
if (index == QSE_LDA_NIL)
{
qse_printf (QSE_T("failed to find [%s]\n"), y[i]);
}
else
{
qse_printf (QSE_T("found [%.*s] at %lu\n"),
(int)QSE_LDA_DLEN(s1,index), QSE_LDA_DPTR(s1,index), (unsigned long)index);
}
}
qse_lda_clear (s1);
qse_printf (QSE_T("~~~~~~~~\n"));
}
qse_lda_close (s1);
return 0;
}
static int test3 ()
{
qse_lda_t* s1;
const qse_char_t* x[] =
{
QSE_T("this is so good"),
QSE_T("what the fuck"),
QSE_T("do you like it?"),
QSE_T("oopsy!")
};
const qse_char_t* y =
QSE_T("We Accept MasterCard, VISA, JCB, Dinner & eCheck");
int i, j;
s1 = qse_lda_open (QSE_MMGR_GETDFL(), 0, 0);
if (s1 == QSE_NULL)
{
qse_printf (QSE_T("cannot open a string\n"));
return -1;
}
qse_lda_setcopier (s1, QSE_LDA_COPIER_INLINE);
qse_lda_setscale (s1, QSE_SIZEOF(qse_char_t));
for (j = 0; j < 20; j++)
{
for (i = 0; i < QSE_COUNTOF(x); i++)
{
if (qse_lda_insert (s1, (i + 1) * j - 1, x[i], qse_strlen(x[i])) == QSE_LDA_NIL)
{
qse_printf (QSE_T("failed to add at %u => [%.*s]\n"),
(unsigned int)((i + 1) * j - 1),
(int)qse_strlen(x[i]), x[i]);
}
else
{
qse_printf (QSE_T("add at %u => [%.*s]\n"),
(unsigned int)((i + 1) * j - 1),
(int)qse_strlen(x[i]), x[i]);
}
}
for (i = 2; i < 3; i++)
{
if (i < QSE_LDA_SIZE(s1))
{
if (QSE_LDA_SLOT(s1,i))
{
qse_printf (QSE_T("deleted at %d => [%.*s]\n"),
i, (int)QSE_LDA_DLEN(s1,i), QSE_LDA_DPTR(s1,i));
}
qse_lda_delete (s1, i, 1);
}
if (i < QSE_LDA_SIZE(s1))
{
if (qse_lda_update (s1, i, y, qse_strlen(y)) == QSE_LDA_NIL)
{
qse_printf (QSE_T("failed to update at %d => [%.*s]\n"),
i, (int)qse_strlen(y), y);
}
else
{
qse_printf (QSE_T("updated at %d => [%.*s]\n"),
i, (int)QSE_LDA_DLEN(s1,i), QSE_LDA_DPTR(s1,i));
}
}
}
qse_printf (QSE_T("array size => %lu\n"), (unsigned long)QSE_LDA_SIZE(s1));
for (i = 0; i < QSE_LDA_SIZE(s1); i++)
{
if (QSE_LDA_SLOT(s1,i))
{
qse_printf (QSE_T("[%d] %d => [%.*s]\n"),
j, i, (int)QSE_LDA_DLEN(s1,i), QSE_LDA_DPTR(s1,i));
}
}
{
qse_size_t count = qse_lda_uplete (s1, 3, 20);
qse_printf (QSE_T("upleted %lu items from index 3\n"), (unsigned long)count);
}
qse_printf (QSE_T("array size => %lu\n"), (unsigned long)QSE_LDA_SIZE(s1));
for (i = 0; i < QSE_LDA_SIZE(s1); i++)
{
if (QSE_LDA_SLOT(s1,i))
{
qse_printf (QSE_T("[%d] %d => [%.*s]\n"),
j, i, (int)QSE_LDA_DLEN(s1,i), QSE_LDA_DPTR(s1,i));
}
}
qse_lda_clear (s1);
qse_printf (QSE_T("~~~~~~~~\n"));
}
qse_lda_close (s1);
return 0;
}
qse_size_t sizer1 (qse_lda_t* lda, qse_size_t hint)
{
return 2;
}
static int test4 ()
{
int i;
qse_lda_t* s1;
const qse_char_t* x[] =
{
QSE_T("this is so good"),
QSE_T("what the fuck"),
QSE_T("do you like it?"),
QSE_T("oopsy!")
};
s1 = qse_lda_open (QSE_MMGR_GETDFL(), 0, 3);
if (s1 == QSE_NULL)
{
qse_printf (QSE_T("cannot open an array\n"));
return -1;
}
qse_lda_setcopier (s1, QSE_LDA_COPIER_INLINE);
qse_lda_setsizer (s1, sizer1);
qse_lda_setscale (s1, QSE_SIZEOF(qse_char_t));
for (i = 0; i < QSE_COUNTOF(x); i++)
{
if (qse_lda_insert (s1, 0, x[i], qse_strlen(x[i])) == QSE_LDA_NIL)
{
qse_printf (QSE_T("failed to add at 0 => [%.*s]\n"),
(int)qse_strlen(x[i]), x[i]);
}
else
{
qse_printf (QSE_T("add at 0 => [%.*s]\n"),
(int)qse_strlen(x[i]), x[i]);
}
}
qse_printf (QSE_T("lda size => %lu\n"), QSE_LDA_SIZE(s1));
qse_lda_walk (s1, walker1, QSE_NULL);
qse_printf (QSE_T("lda size => %lu\n"), QSE_LDA_SIZE(s1));
qse_lda_rwalk (s1, rwalker1, QSE_NULL);
qse_lda_close (s1);
return 0;
}
qse_lda_comper_t default_comparator;
static int inverse_comparator (qse_lda_t* lda,
const void* dptr1, size_t dlen1,
const void* dptr2, size_t dlen2)
{
return -default_comparator (lda, dptr1, dlen1, dptr2, dlen2);
}
static int test5 ()
{
qse_lda_t* s1;
int i, j;
s1 = qse_lda_open (QSE_MMGR_GETDFL(), 0, 3);
if (s1 == QSE_NULL)
{
qse_printf (QSE_T("cannot open an array\n"));
return -1;
}
qse_lda_setcopier (s1, QSE_LDA_COPIER_INLINE);
qse_lda_setscale (s1, QSE_SIZEOF(i));
/* inverse the comparator to implement min-heap */
default_comparator = qse_lda_getcomper (s1);
qse_lda_setcomper (s1, inverse_comparator);
for (i = 0; i < 25; i++)
{
j = rand () % 100;
qse_lda_pushheap (s1, &j, 1);
}
qse_printf (QSE_T("lda size => %lu\n"), QSE_LDA_SIZE(s1));
qse_lda_walk (s1, walker3, QSE_NULL);
while (QSE_LDA_SIZE(s1) > 10 )
{
qse_printf (QSE_T("top => %d\n"), *(int*)QSE_LDA_DPTR(s1,0));
qse_lda_popheap (s1);
}
for (i = 0; i < 25; i++)
{
j = rand () % 100;
qse_lda_pushheap (s1, &j, 1);
}
qse_printf (QSE_T("lda size => %lu\n"), QSE_LDA_SIZE(s1));
qse_lda_walk (s1, walker3, QSE_NULL);
while (QSE_LDA_SIZE(s1))
{
qse_printf (QSE_T("top => %d\n"), *(int*)QSE_LDA_DPTR(s1,0));
qse_lda_popheap (s1);
}
qse_lda_setcomper (s1, default_comparator);
for (i = 0; i < 25; i++)
{
j = rand () % 100;
qse_lda_pushheap (s1, &j, 1);
}
qse_printf (QSE_T("lda size => %lu\n"), QSE_LDA_SIZE(s1));
qse_lda_walk (s1, walker3, QSE_NULL);
while (QSE_LDA_SIZE(s1))
{
qse_printf (QSE_T("top => %d\n"), *(int*)QSE_LDA_DPTR(s1,0));
qse_lda_popheap (s1);
}
qse_lda_close (s1);
return 0;
}
int main ()
{
qse_openstdsios ();
R (test1);
R (test2);
R (test3);
R (test4);
R (test5);
qse_closestdsios ();
return 0;
}