mirror of
https://git.code.sf.net/p/quake/quakeforge
synced 2024-11-27 06:34:11 +00:00
bcc5686606
Fuzzy bsearch is useful for finding an entry in a prefix sum array (value is >= ele[0], < ele[1]), and the reentrant version is good when data needs to be passed to the compare function. Adapted from the code used in pr_resolve.
98 lines
1.8 KiB
C
98 lines
1.8 KiB
C
#include <limits.h>
|
|
#include <string.h>
|
|
|
|
#include "QF/fbsearch.h"
|
|
#include "bsearch.h"
|
|
|
|
void *fbsearch (const void *key, const void *_base, size_t nmemb, size_t size,
|
|
__compar_fn_t cmp)
|
|
{
|
|
// fuzzy bsearh
|
|
const char *base = (const char *) _base;
|
|
unsigned left = 0;
|
|
unsigned right = nmemb - 1;
|
|
unsigned mid;
|
|
const void *p = 0;
|
|
|
|
if (!nmemb) {
|
|
return 0;
|
|
}
|
|
while (left != right) {
|
|
mid = (left + right + 1) / 2;
|
|
p = base + mid * size;
|
|
if (cmp (key, p) < 0) {
|
|
right = mid - 1;
|
|
} else {
|
|
left = mid;
|
|
}
|
|
}
|
|
p = base + left * size;
|
|
if (cmp (key, p) >= 0) {
|
|
return (void *) p;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
void *fbsearch_r (const void *key, const void *_base, size_t nmemb, size_t size,
|
|
__compar_d_fn_t cmp, void *arg)
|
|
{
|
|
// fuzzy bsearh
|
|
const char *base = (const char *) _base;
|
|
unsigned left = 0;
|
|
unsigned right = nmemb - 1;
|
|
unsigned mid;
|
|
const void *p = 0;
|
|
|
|
if (!nmemb) {
|
|
return 0;
|
|
}
|
|
while (left != right) {
|
|
mid = (left + right + 1) / 2;
|
|
p = base + mid * size;
|
|
if (cmp (key, p, arg) < 0) {
|
|
right = mid - 1;
|
|
} else {
|
|
left = mid;
|
|
}
|
|
}
|
|
p = base + left * size;
|
|
if (cmp (key, p, arg) >= 0) {
|
|
return (void *) p;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
void *
|
|
_bsearch(const void *key, const void *_base, size_t nmemb, size_t size,
|
|
__compar_d_fn_t cmp, void *arg)
|
|
{
|
|
// fuzzy bsearh
|
|
const char *base = (const char *) _base;
|
|
unsigned left = 0;
|
|
unsigned right = nmemb;
|
|
unsigned mid;
|
|
const void *p = 0;
|
|
int c;
|
|
|
|
if (!nmemb) {
|
|
return 0;
|
|
}
|
|
while (left != right) {
|
|
mid = (left + right + 1) / 2;
|
|
p = base + mid * size;
|
|
c = cmp (key, p, arg);
|
|
if (c == 0) {
|
|
return (void *) p;
|
|
}
|
|
if (c < 0) {
|
|
right = mid - 1;
|
|
} else {
|
|
left = mid;
|
|
}
|
|
}
|
|
p = base + left * size;
|
|
if (cmp (key, p, arg) == 0) {
|
|
return (void *) p;
|
|
}
|
|
return 0;
|
|
}
|