-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbinsearch.c
126 lines (103 loc) · 4.09 KB
/
binsearch.c
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* binsearch.c implements a function for binary searching that is much like
* the standard library qsort
*
* written nml 2002-12-12
*
*/
#include "firstinclude.h"
#include "binsearch.h"
#include "bit.h"
/* FIXME: can be optimised by looping until number of elements between l and r
* is 0 */
void* binsearch(const void* key, const void* base, size_t nel,
size_t width, int (*compar)(const void* one, const void* two)) {
const char* cbase = base; /* to do dodgy pointer arithmetic on */
unsigned int l = 0, /* left end of search */
r = nel - 1, /* right end of search */
m; /* middle of search */
int res; /* result of comparison */
/* indexes must be unsigned int, else risk overflow, but unsigned int gives
* us problems moving r to the left of m below */
/* if nel is 0, then r is (unsigned) -1, so check for it */
if (!nel || !width) {
return (void*) cbase;
}
while (l <= r) {
m = (r + l) >> 1;
if ((res = compar(key, cbase + m * width)) > 0) {
l = m + 1;
} else if (res < 0) {
if (m) {
r = m - 1;
} else {
/* avoid underflow of r (and prevention of crossing) */
return (void*) cbase;
}
} else {
return (void*) (cbase + m * width);
}
}
return (void*) (cbase + l * width);
}
#if 0
/* this version of binsearch contains a possible optimisation based on
* eliminating multiplication from the loop, but it doesn't seem to work, since
* you still need some multiplication (although its only by 0 or 1). The below
* implementation has bugs. */
void* binsearch(const void* key, const void* base, size_t nel,
size_t width, int (*compar)(const void* one, const void* two)) {
const char *l = base, /* left end of search */
*r, /* right end of search */
*m; /* middle of search */
int res, /* result of comparison */
odd; /* whether last division was odd */
unsigned int halfwidth; /* half of width */
/* check for degenerate cases */
if (!nel || !width) {
return (void*) base;
}
halfwidth = BIT_DIV2(width, 1);
/* FIXME: more... initialise r to the end of the array */
for (r = l + (nel - 1) * width; nel; nel = BIT_DIV2(nel - 1, 1)) {
odd = BIT_MOD2(nel, 1);
/* the middle is halfway in between l and r, (l + (r - l) / 2),
* except that if theres an odd number of elements between them we
* have to subtract the half an element that would otherwise screw up
* our calculations (this is the price of not using multiplication) */
m = l + BIT_DIV2(r - l, 1);
if (odd) {
/* correct for incorrect division due to oddness */
m -= halfwidth;
/* compare them */
res = compar(key, m);
if (res > 0) {
/* if the key is greater than the middle, we need to shift the
* left bounds past the middle */
l = m + width;
} else if (res < 0) {
/* if the key is less than the middle, we need to shift the
* right bounds past the middle */
r = m - width;
} else {
return (void*) m;
}
} else {
/* compare them */
res = compar(key, m);
if (res > 0) {
/* if the key is greater than the middle, we need to shift the
* left bounds past the middle */
l = m + width;
nel -= 1;
} else if (res < 0) {
/* if the key is less than the middle, we need to shift the
* right bounds past the middle */
r = m - width;
} else {
return (void*) m;
}
}
}
return (void*) l;
}
#endif