Initial revision
This commit is contained in:
28
lang/cem/libcc.ansi/stdlib/bsearch.c
Normal file
28
lang/cem/libcc.ansi/stdlib/bsearch.c
Normal file
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
|
||||
* See the copyright notice in the ACK home directory, in the file "Copyright".
|
||||
*/
|
||||
/* $Header$ */
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
void *
|
||||
bsearch(register const void *key, register const void *base,
|
||||
register size_t nmemb, register size_t size,
|
||||
int (*compar)(const void *, const void *))
|
||||
{
|
||||
register void *mid_point;
|
||||
register int cmp;
|
||||
|
||||
while (nmemb > 0) {
|
||||
mid_point = base + size * (nmemb >> 1);
|
||||
if ((cmp = (*compar)(key, mid_point)) == 0)
|
||||
return(mid_point);
|
||||
if (cmp >= 0) {
|
||||
base = mid_point + size;
|
||||
nmemb = (nmemb - 1) >> 1;
|
||||
} else
|
||||
nmemb >>= 1;
|
||||
}
|
||||
return((void *)NULL);
|
||||
}
|
||||
Reference in New Issue
Block a user