5 * htable - hash table routines
7 * A hash table is an efficient structure for looking up keys. This version
8 * grows with usage and allows efficient deletion.
11 * #include <ccan/htable/htable.h>
12 * #include <ccan/hash/hash.h>
17 * struct name_to_digit {
22 * static struct name_to_digit map[] = {
35 * // Wrapper for rehash function pointer.
36 * static size_t rehash(const void *e, void *unused)
38 * return hash_string(((struct name_to_digit *)e)->name);
41 * // Comparison function.
42 * static bool streq(const void *e, void *string)
44 * return strcmp(((struct name_to_digit *)e)->name, string) == 0;
47 * // We let them add their own aliases, eg. --alias=v=5
48 * static void add_alias(struct htable *ht, const char *alias)
51 * struct name_to_digit *n;
53 * n = malloc(sizeof(*n));
54 * n->name = strdup(alias);
56 * eq = strchr(n->name, '=');
57 * if (!eq || ((n->val = atoi(eq+1)) == 0 && !strcmp(eq+1, "0")))
58 * errx(1, "Usage: --alias=<name>=<value>");
60 * htable_add(ht, hash_string(n->name), n);
63 * int main(int argc, char *argv[])
70 * errx(1, "Usage: %s [--alias=<name>=<val>]... <str>...",
73 * // Create and populate hash table.
74 * htable_init(&ht, rehash, NULL);
75 * for (i = 0; i < sizeof(map)/sizeof(map[0]); i++)
76 * htable_add(&ht, hash_string(map[i].name), &map[i]);
78 * // Add any aliases to the hash table.
79 * for (i = 1; i < argc; i++) {
80 * if (!strncmp(argv[i], "--alias=", strlen("--alias=")))
81 * add_alias(&ht, argv[i] + strlen("--alias="));
86 * // Find the other args in the hash table.
87 * for (val = 0; i < argc; i++) {
88 * struct name_to_digit *n;
89 * n = htable_get(&ht, hash_string(argv[i]),
92 * errx(1, "Invalid digit name %s", argv[i]);
93 * // Append it to the value we are building up.
97 * printf("%lu\n", val);
101 * License: LGPL (v2.1 or any later version)
102 * Author: Rusty Russell <rusty@rustcorp.com.au>
104 int main(int argc, char *argv[])
109 if (strcmp(argv[1], "depends") == 0) {
110 printf("ccan/compiler\n");