From 43bc5ffb7997b8b2e884ac37167cd7e40d11dec6 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Sat, 25 Sep 2010 03:03:38 +0000 Subject: [PATCH] added qse_bsearch(), qse_lsearch(), qse_qsort() --- qse/include/qse/cmn/Makefile.am | 26 ++++++++-- qse/include/qse/cmn/alg.h | 88 +++++++++++++++++++++++++++++++++ qse/lib/cmn/Makefile.am | 2 + qse/lib/cmn/Makefile.in | 8 ++- qse/lib/cmn/alg_search.c | 55 +++++++++++++++++++++ 5 files changed, 174 insertions(+), 5 deletions(-) create mode 100644 qse/include/qse/cmn/alg.h create mode 100644 qse/lib/cmn/alg_search.c diff --git a/qse/include/qse/cmn/Makefile.am b/qse/include/qse/cmn/Makefile.am index e82e9166..ef564dbe 100644 --- a/qse/include/qse/cmn/Makefile.am +++ b/qse/include/qse/cmn/Makefile.am @@ -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 diff --git a/qse/include/qse/cmn/alg.h b/qse/include/qse/cmn/alg.h new file mode 100644 index 00000000..7513c666 --- /dev/null +++ b/qse/include/qse/cmn/alg.h @@ -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 . + */ + +#ifndef _QSE_ALG_H_ +#define _QSE_ALG_H_ + +/** @file + * This file provides functions for commonly used algorithms. + */ + +#include +#include + +/** + * 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 diff --git a/qse/lib/cmn/Makefile.am b/qse/lib/cmn/Makefile.am index 1155d3ea..ee0d8963 100644 --- a/qse/lib/cmn/Makefile.am +++ b/qse/lib/cmn/Makefile.am @@ -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 \ diff --git a/qse/lib/cmn/Makefile.in b/qse/lib/cmn/Makefile.in index 473a0c8c..02e6f2dd 100644 --- a/qse/lib/cmn/Makefile.in +++ b/qse/lib/cmn/Makefile.in @@ -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@ diff --git a/qse/lib/cmn/alg_search.c b/qse/lib/cmn/alg_search.c new file mode 100644 index 00000000..f4f4e375 --- /dev/null +++ b/qse/lib/cmn/alg_search.c @@ -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 . + */ + +#include + +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; +}