Rewrote some stuff to work w/ pointer math
This commit is contained in:
parent
f85337e404
commit
8fd6c4b295
1 changed files with 7 additions and 7 deletions
|
@ -13,22 +13,22 @@
|
||||||
|
|
||||||
void * bsearch(const void * key, const void * base, size_t nel, size_t width,
|
void * bsearch(const void * key, const void * base, size_t nel, size_t width,
|
||||||
int (*compar)(const void *, const void*)) {
|
int (*compar)(const void *, const void*)) {
|
||||||
void * left;
|
long int left;
|
||||||
void * right;
|
long int right;
|
||||||
void * mid;
|
long int mid;
|
||||||
|
|
||||||
if(nel == 0 || base == NULL) {
|
if(nel == 0 || base == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
left = base;
|
left = (long int) base;
|
||||||
right = base + (nel * width);
|
right = (long int) (base + (nel * width));
|
||||||
|
|
||||||
while(left <= right) {
|
while(left <= right) {
|
||||||
mid = (left + right) / 2;
|
mid = (left + right) / 2;
|
||||||
int t = compar(key, mid);
|
int t = compar(key, (void *) mid);
|
||||||
if(t == 0) {
|
if(t == 0) {
|
||||||
return mid;
|
return (void *) mid;
|
||||||
}
|
}
|
||||||
else if(t < 0) {
|
else if(t < 0) {
|
||||||
right = mid - width;
|
right = mid - width;
|
||||||
|
|
Loading…
Reference in a new issue