added qse_bsearch(), qse_lsearch(), qse_qsort()
This commit is contained in:
@ -10,6 +10,8 @@ libqsecmn_la_SOURCES = \
|
||||
lda.c oht.c htb.c rbt.c sll.c gdl.c dll.c opt.c \
|
||||
tio.c tio_get.c tio_put.c \
|
||||
fio.c pio.c sio.c \
|
||||
alg_search.c \
|
||||
alg_sort.c \
|
||||
time.c \
|
||||
misc.c \
|
||||
assert.c \
|
||||
|
@ -76,8 +76,8 @@ libqsecmn_la_DEPENDENCIES =
|
||||
am_libqsecmn_la_OBJECTS = mem.lo xma.lo fma.lo chr.lo chr_cnv.lo \
|
||||
rex.lo str_bas.lo str_cnv.lo str_dyn.lo str_utl.lo lda.lo \
|
||||
oht.lo htb.lo rbt.lo sll.lo gdl.lo dll.lo opt.lo tio.lo \
|
||||
tio_get.lo tio_put.lo fio.lo pio.lo sio.lo time.lo misc.lo \
|
||||
assert.lo main.lo stdio.lo
|
||||
tio_get.lo tio_put.lo fio.lo pio.lo sio.lo alg_search.lo \
|
||||
alg_sort.lo time.lo misc.lo assert.lo main.lo stdio.lo
|
||||
libqsecmn_la_OBJECTS = $(am_libqsecmn_la_OBJECTS)
|
||||
libqsecmn_la_LINK = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) \
|
||||
$(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \
|
||||
@ -267,6 +267,8 @@ libqsecmn_la_SOURCES = \
|
||||
lda.c oht.c htb.c rbt.c sll.c gdl.c dll.c opt.c \
|
||||
tio.c tio_get.c tio_put.c \
|
||||
fio.c pio.c sio.c \
|
||||
alg_search.c \
|
||||
alg_sort.c \
|
||||
time.c \
|
||||
misc.c \
|
||||
assert.c \
|
||||
@ -357,6 +359,8 @@ distclean-compile:
|
||||
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Mmgr.Plo@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/StdMmgr.Plo@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/alg_search.Plo@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/alg_sort.Plo@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/assert.Plo@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/chr.Plo@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/chr_cnv.Plo@am__quote@
|
||||
|
55
qse/lib/cmn/alg_search.c
Normal file
55
qse/lib/cmn/alg_search.c
Normal file
@ -0,0 +1,55 @@
|
||||
/*
|
||||
* $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/>.
|
||||
*/
|
||||
|
||||
#include <qse/cmn/alg.h>
|
||||
|
||||
void* qse_bsearch (
|
||||
const void *key, const void *base, qse_size_t nmemb,
|
||||
qse_size_t size, qse_search_comper_t comper, void* ctx)
|
||||
{
|
||||
int n;
|
||||
const void* mid;
|
||||
qse_size_t lim;
|
||||
|
||||
for (lim = nmemb; lim > 0; lim >>= 1)
|
||||
{
|
||||
mid = ((qse_byte_t*)base) + (lim >> 1) * size;
|
||||
|
||||
n = (comper) (key, mid, ctx);
|
||||
if (n == 0) return (void*)mid;
|
||||
if (n > 0) { base = ((const qse_byte_t*)mid) + size; lim--; }
|
||||
}
|
||||
|
||||
return QSE_NULL;
|
||||
}
|
||||
|
||||
void* qse_lsearch (
|
||||
const void* key, const void* base, qse_size_t nmemb,
|
||||
qse_size_t size, qse_search_comper_t comper, void* ctx)
|
||||
{
|
||||
while (nmemb > 0)
|
||||
{
|
||||
if (comper(key, base, ctx) == 0) return (void*)base;
|
||||
base = ((const qse_byte_t*)base) + size;
|
||||
nmemb--;
|
||||
}
|
||||
|
||||
return QSE_NULL;
|
||||
}
|
Reference in New Issue
Block a user