1 /* Licensed under LGPLv2+ - see LICENSE file for details */
2 #include <sys/types.h> //for ssize_t definition
3 #include "sparse_bsearch.h"
5 void *_sparse_bsearch(const void *key, const void *base,
6 size_t nmemb, size_t size,
7 int (*cmpfn)(const void *, const void *),
8 bool (*validfn)(const void *))
10 ssize_t start = 0, end = nmemb - 1;
13 while (start <= end) {
14 ssize_t next = (start + end) / 2;
17 while (!validfn(p + next * size)) {
18 /* Try the next one along. */
21 /* Hmm, none of these were valid. */
22 next = (start + end) / 2;
27 result = cmpfn(key, p + next * size);
29 return (void *)(p + next * size);