6 * sparse_bsearch - search a sorted array with some invalid entries
8 * bsearch() is great for searching an array, but if you are deleting from
9 * the array, you then need to memmove() to keep it dense.
11 * Sometimes there is a useful invalid value which can be used to mark deleted
12 * entries, but such a "gappy" array breaks bsearch. This function allows
13 * "invalid" entries in your array, which are ignored for the search.
16 * #include <ccan/sparse_bsearch/sparse_bsearch.h>
18 * static bool val_valid(const unsigned int *val)
22 * static int val_cmp(const unsigned int *a, const unsigned int *b)
24 * return (int)((*a) - (*b));
26 * static unsigned int values[] = { 1, 7, 11, 1235, 99999 };
28 * // Return true if this value is in set, and remove it.
29 * static bool remove_from_values(unsigned int val)
32 * // We use 5 here, but ccan/array_size.h is better!
33 * p = sparse_bsearch(&val, values, 5, val_cmp, val_valid);
40 * int main(int argc, char *argv[])
43 * for (i = 1; i < argc; i++) {
44 * val = atoi(argv[i]);
45 * if (remove_from_values(val))
46 * printf("%i removed.\n", val);
48 * printf("%i wasn't there.\n", val);
53 * License: LGPL (v2.1 or any later version)
54 * Author: Rusty Russell <rusty@rustcorp.com.au>
56 int main(int argc, char *argv[])
58 /* Expect exactly one argument */
62 if (strcmp(argv[1], "depends") == 0) {
63 printf("ccan/typesafe_cb\n"