1 /* Licensed under LGPLv2+ - see LICENSE file for details */
2 #include "sparse_bsearch.h"
4 void *_sparse_bsearch(const void *key, const void *base,
5 size_t nmemb, size_t size,
6 int (*cmpfn)(const void *, const void *),
7 bool (*validfn)(const void *))
9 ssize_t start = 0, end = nmemb - 1;
12 while (start <= end) {
13 ssize_t next = (start + end) / 2;
16 while (!validfn(p + next * size)) {
17 /* Try the next one along. */
20 /* Hmm, none of these were valid. */
21 next = (start + end) / 2;
26 result = cmpfn(key, p + next * size);
28 return (void *)(p + next * size);