From e755863dc764c11e2ee28daf05df6f53bf9be0f4 Mon Sep 17 00:00:00 2001 From: piaip Date: Mon, 31 Mar 2008 12:46:22 +0000 Subject: - (internal) enable building utmpserver git-svn-id: http://opensvn.csie.org/pttbbs/trunk/pttbbs@4053 63ad8ddf-47c3-0310-b6dd-a9e9d9715204 --- common/sys/sort.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'common/sys') diff --git a/common/sys/sort.c b/common/sys/sort.c index edecc0a8..d649b9e4 100644 --- a/common/sys/sort.c +++ b/common/sys/sort.c @@ -1,3 +1,4 @@ +#include int cmp_int(const void *a, const void *b) { @@ -8,3 +9,45 @@ int cmp_int_desc(const void * a, const void * b) { return cmp_int(b, a); } + +int * +intbsearch(int key, const int *base0, int nmemb) +{ + /* 改自 /usr/src/lib/libc/stdlib/bsearch.c , + 專給搜 int array 用的, 不透過 compar function 故較快些 */ + const char *base = (const char *)base0; + size_t lim; + int *p; + + for (lim = nmemb; lim != 0; lim >>= 1) { + p = (int *)(base + (lim >> 1) * 4); + if( key == *p ) + return p; + if( key > *p ){/* key > p: move right */ + base = (char *)p + 4; + lim--; + } /* else move left */ + } + return (NULL); +} + +unsigned int * +uintbsearch(const unsigned int key, const unsigned int *base0, const int nmemb) +{ + /* 改自 /usr/src/lib/libc/stdlib/bsearch.c , + 專給搜 int array 用的, 不透過 compar function 故較快些 */ + const char *base = (const char *)base0; + size_t lim; + unsigned int *p; + + for (lim = nmemb; lim != 0; lim >>= 1) { + p = (unsigned int *)(base + (lim >> 1) * 4); + if( key == *p ) + return p; + if( key > *p ){/* key > p: move right */ + base = (char *)p + 4; + lim--; + } /* else move left */ + } + return (NULL); +} -- cgit v1.2.3