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)
40 * return hash_string(((struct name_to_digit *)e)->name);
43 * // Comparison function.
44 * static bool nameeq(const void *e, void *string)
46 * return strcmp(((struct name_to_digit *)e)->name, string) == 0;
49 * // We let them add their own aliases, eg. --alias=v=5
50 * static void add_alias(struct htable *ht, const char *alias)
53 * struct name_to_digit *n;
55 * n = malloc(sizeof(*n));
56 * n->name = name = strdup(alias);
58 * eq = strchr(name, '=');
59 * if (!eq || ((n->val = atoi(eq+1)) == 0 && !strcmp(eq+1, "0")))
60 * errx(1, "Usage: --alias=<name>=<value>");
62 * htable_add(ht, hash_string(n->name), n);
65 * int main(int argc, char *argv[])
72 * errx(1, "Usage: %s [--alias=<name>=<val>]... <str>...",
75 * // Create and populate hash table.
76 * htable_init(&ht, rehash, NULL);
77 * for (i = 0; i < (int)(sizeof(map)/sizeof(map[0])); i++)
78 * htable_add(&ht, hash_string(map[i].name), &map[i]);
80 * // Add any aliases to the hash table.
81 * for (i = 1; i < argc; i++) {
82 * if (!strncmp(argv[i], "--alias=", strlen("--alias=")))
83 * add_alias(&ht, argv[i] + strlen("--alias="));
88 * // Find the other args in the hash table.
89 * for (val = 0; i < argc; i++) {
90 * struct name_to_digit *n;
91 * n = htable_get(&ht, hash_string(argv[i]),
94 * errx(1, "Invalid digit name %s", argv[i]);
95 * // Append it to the value we are building up.
99 * printf("%lu\n", val);
103 * License: LGPL (v2.1 or any later version)
104 * Author: Rusty Russell <rusty@rustcorp.com.au>
106 int main(int argc, char *argv[])
111 if (strcmp(argv[1], "depends") == 0) {
112 printf("ccan/compiler\n");
113 printf("ccan/str\n");