|
|
1.1 root 1: /*
2: * C general utilities library.
3: * bsearch()
4: * ANSI 4.10.5.1.
5: * Binary search.
6: */
7:
8: #include <stdlib.h>
9:
10: __VOID__ *
11: bsearch(key, base, nmemb, size, compar)
12: const __VOID__ *key, *base;
13: register size_t nmemb;
14: size_t size;
15: int (*compar)();
16: {
17: register size_t i;
18: register int n;
19: register __VOID__ *p;
20:
21: while (nmemb > 0) {
22: i = nmemb / 2;
23: p = (__VOID__ *)(((char *)base) + i * size);
24: if ((n = (*compar)(key, p)) == 0)
25: return p;
26: else if (n < 0)
27: nmemb = i;
28: else {
29: nmemb -= i + 1;
30: base = (__VOID__ *)(((char *)p) + size);
31: }
32: }
33: return NULL;
34: }
35:
36: /* end of bsearch.c */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.