blob: 78b0af2de86ab672d70d5033c704afefe5dc31e6 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
#include <stdio.h>
int cmp_int(const void *a, const void *b)
{
return *(int*)a - *(int*)b;
}
int cmp_int_desc(const void * a, const void * b)
{
// return cmp_int(b, a);
return *(int*)b - *(int*)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);
}
|