6 * htable - hash table routines
8 * A hash table is an efficient structure for looking up keys. This version
9 * grows with usage and allows efficient deletion.
12 * #include <ccan/htable/htable.h>
13 * #include <ccan/hash/hash.h>
18 * struct name_to_digit {
23 * static struct name_to_digit map[] = {
36 * // Wrapper for rehash function pointer.
37 * static size_t rehash(const void *e, void *unused)
39 * return hash_string(((struct name_to_digit *)e)->name);
42 * // Comparison function.
43 * static bool streq(const void *e, void *string)
45 * return strcmp(((struct name_to_digit *)e)->name, string) == 0;
48 * // We let them add their own aliases, eg. --alias=v=5
49 * static void add_alias(struct htable *ht, const char *alias)
52 * struct name_to_digit *n;
54 * n = malloc(sizeof(*n));
55 * n->name = strdup(alias);
57 * eq = strchr(n->name, '=');
58 * if (!eq || ((n->val = atoi(eq+1)) == 0 && !strcmp(eq+1, "0")))
59 * errx(1, "Usage: --alias=<name>=<value>");
61 * htable_add(ht, hash_string(n->name), n);
64 * int main(int argc, char *argv[])
71 * errx(1, "Usage: %s [--alias=<name>=<val>]... <str>...",
74 * // Create and populate hash table.
75 * htable_init(&ht, rehash, NULL);
76 * for (i = 0; i < sizeof(map)/sizeof(map[0]); i++)
77 * htable_add(&ht, hash_string(map[i].name), &map[i]);
79 * // Add any aliases to the hash table.
80 * for (i = 1; i < argc; i++) {
81 * if (!strncmp(argv[i], "--alias=", strlen("--alias=")))
82 * add_alias(&ht, argv[i] + strlen("--alias="));
87 * // Find the other args in the hash table.
88 * for (val = 0; i < argc; i++) {
89 * struct name_to_digit *n;
90 * n = htable_get(&ht, hash_string(argv[i]),
93 * errx(1, "Invalid digit name %s", argv[i]);
94 * // Append it to the value we are building up.
98 * printf("%lu\n", val);
102 * License: LGPL (v2.1 or any later version)
103 * Author: Rusty Russell <rusty@rustcorp.com.au>
105 int main(int argc, char *argv[])
110 if (strcmp(argv[1], "depends") == 0) {
111 printf("ccan/compiler\n");