I found that Windows CE doesn't include bsearch function.
Here's a replacement for it with an (in my opinion) improved
interface. It's from a real program, so criticism and bug
reports are welcomed.
/* Compares A and B, given auxiliary data AUX, and returns a
strcmp()-type result. */
typedef int algo_compare_func (const void *a, const void *b, void *aux);
/* Searches ARRAY, which contains COUNT of SIZE bytes each, using
a binary search. Returns any element that equals VALUE, if
one exists, or a null pointer otherwise. ARRAY must ordered
according to COMPARE. AUX is passed to COMPARE as auxiliary
data. */
void *
binary_search (const void *array, size_t count, size_t size,
void *value,
algo_compare_func *compare, void *aux)
{
assert (array != NULL);
assert (count <= INT_MAX);
assert (compare != NULL);
if (count != 0)
{
const unsigned char *first = array;
int low = 0;
int high = count - 1;
while (low <= high)
{
int middle = (low + high) / 2;
const unsigned char *element = first + middle * size;
int cmp = compare (value, element, aux);
if (cmp > 0)
low = middle + 1;
else if (cmp < 0)
high = middle - 1;
else
return (void *) element;
}
}
return NULL;
}