1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
#include "config.h"
#include <string.h>
#include <stdio.h>
/**
* sparse_bsearch - search a sorted array with some invalid entries
*
* bsearch() is great for searching an array, but if you are deleting from
* the array, you then need to memmove() to keep it dense.
*
* Sometimes there is a useful invalid value which can be used to mark deleted
* entries, but such a "gappy" array breaks bsearch. This function allows
* "invalid" entries in your array, which are ignored for the search.
*
* Example:
* #include <ccan/sparse_bsearch/sparse_bsearch.h>
*
* static bool val_valid(const unsigned int *val)
* {
* return *val != 0;
* }
* static int val_cmp(const unsigned int *a, const unsigned int *b)
* {
* return (int)((*a) - (*b));
* }
* static unsigned int values[] = { 1, 7, 11, 1235, 99999 };
*
* // Return true if this value is in set, and remove it.
* static bool remove_from_values(unsigned int val)
* {
* unsigned int *p;
* // We use 5 here, but ccan/array_size.h is better!
* p = sparse_bsearch(&val, values, 5, val_cmp, val_valid);
* if (!p)
* return false;
* *p = 0;
* return true;
* }
*
* int main(int argc, char *argv[])
* {
* int i, val;
* for (i = 1; i < argc; i++) {
* val = atoi(argv[i]);
* if (remove_from_values(val))
* printf("%i removed.\n", val);
* else
* printf("%i wasn't there.\n", val);
* }
* return 0;
* }
*
* License: LGPL (v2.1 or any later version)
* Author: Rusty Russell <rusty@rustcorp.com.au>
*/
int main(int argc, char *argv[])
{
/* Expect exactly one argument */
if (argc != 2)
return 1;
if (strcmp(argv[1], "depends") == 0) {
printf("ccan/typesafe_cb\n"
"ccan/check_type\n");
return 0;
}
return 1;
}
|