2007-03-14 20:24:57 +00:00
|
|
|
/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
|
|
|
|
#include <stdlib.h>
|
2006-11-08 11:47:44 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
void *
|
2015-09-15 10:35:49 +00:00
|
|
|
__cdecl
|
2006-11-08 11:47:44 +00:00
|
|
|
bsearch(const void *key, const void *base0, size_t nelem,
|
2009-08-06 15:27:23 +00:00
|
|
|
size_t size, int (__cdecl *cmp)(const void *ck, const void *ce))
|
2006-11-08 11:47:44 +00:00
|
|
|
{
|
2007-03-14 20:24:57 +00:00
|
|
|
char *base = (char *)base0;
|
2011-09-15 17:11:53 +00:00
|
|
|
size_t lim;
|
|
|
|
int cmpval;
|
2007-03-14 20:24:57 +00:00
|
|
|
void *p;
|
2006-11-08 11:47:44 +00:00
|
|
|
|
|
|
|
for (lim = nelem; lim != 0; lim >>= 1)
|
|
|
|
{
|
|
|
|
p = base + (lim >> 1) * size;
|
|
|
|
cmpval = (*cmp)(key, p);
|
|
|
|
if (cmpval == 0)
|
2007-03-14 20:24:57 +00:00
|
|
|
return p;
|
2006-11-08 11:47:44 +00:00
|
|
|
if (cmpval > 0)
|
|
|
|
{ /* key > p: move right */
|
2007-03-14 20:24:57 +00:00
|
|
|
base = (char *)p + size;
|
2006-11-08 11:47:44 +00:00
|
|
|
lim--;
|
|
|
|
} /* else move left */
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|