added qse_bsearch(), qse_lsearch(), qse_qsort()

This commit is contained in:
hyung-hwan 2010-09-25 03:03:38 +00:00
parent 70bf0fceaf
commit 43bc5ffb79
5 changed files with 174 additions and 5 deletions

View File

@ -1,9 +1,29 @@
pkgincludedir = $(includedir)/qse/cmn
pkginclude_HEADERS = \
mem.h xma.h fma.h chr.h str.h lda.h oht.h htb.h rbt.h \
rex.h sll.h gdl.h dll.h opt.h tio.h \
fio.h pio.h sio.h time.h misc.h main.h stdio.h
alg.h \
chr.h \
dll.h \
fio.h \
fma.h \
gdl.h \
htb.h \
lda.h \
main.h \
mem.h \
misc.h \
oht.h \
opt.h \
pio.h \
rbt.h \
rex.h \
sio.h \
sll.h \
stdio.h \
str.h \
time.h \
tio.h \
xma.h
if ENABLE_CXX
pkginclude_HEADERS += Mmgr.hpp StdMmgr.hpp Mmged.hpp

88
qse/include/qse/cmn/alg.h Normal file
View File

@ -0,0 +1,88 @@
/*
* $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_ALG_H_
#define _QSE_ALG_H_
/** @file
* This file provides functions for commonly used algorithms.
*/
#include <qse/types.h>
#include <qse/macros.h>
/**
* The qse_search_comper_t type defines a search callback function.
* The callback function is called by search functions for each comparison
* performed. It should return 0 if @a ptr1 and @a ptr2 are
* euqal, a positive integer if @a ptr1 is greater than @a ptr2, a negative
* if @a ptr2 is greater than @a ptr1. Both @a ptr1 and @a ptr2 are
* pointers to any two items in the array. @a ctx which is a pointer to
* user-defined data passed to a search function is passed to the callback
* with no modification.
*/
typedef int (*qse_search_comper_t) (
const void* ptr1,
const void* ptr2,
void* ctx
);
/**
* The qse_sort_comper_t type defines a sort callback function.
*/
typedef qse_search_comper_t qse_sort_comper_t;
/**
* The qse_bsearch() function performs binary search over a sorted array.
*/
void* qse_bsearch (
const void* key,
const void* base,
qse_size_t nmemb,
qse_size_t size,
qse_search_comper_t comper,
void* ctx
);
/**
* The qse_lsearch() function performs linear search over an array.
*/
void* qse_bsearch (
const void* key,
const void* base,
qse_size_t nmemb,
qse_size_t size,
qse_search_comper_t comper,
void* ctx
);
/**
* The qse_qsort() function performs quick-sorting over an array.
*/
void qse_qsort (
void* base,
qse_size_t nmemb,
qse_size_t size,
qse_sort_comper_t comper,
void* ctx
);
#endif

View File

@ -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 \

View File

@ -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
View 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;
}