]> git.ozlabs.org Git - ccan/blobdiff - ccan/hashtable/_info
Hashtable routines.
[ccan] / ccan / hashtable / _info
diff --git a/ccan/hashtable/_info b/ccan/hashtable/_info
new file mode 100644 (file)
index 0000000..4bc4cc4
--- /dev/null
@@ -0,0 +1,116 @@
+#include <string.h>
+
+/**
+ * hashtable - hash table routines
+ *
+ * A hash table is an efficient structure for looking up keys.  This version
+ * grows with usage and allows efficient deletion.
+ *
+ * Caveat: pointers placed into the hash must be aligned to 64 bits.
+ * ie. from malloc or because the structure they point to is aligned.
+ * An assert will be raised if this isn't true.
+ *
+ * Example:
+ *     #include <ccan/hashtable/hashtable.h>
+ *     #include <ccan/hash/hash.h>
+ *     #include <stdio.h>
+ *     #include <err.h>
+ *     #include <string.h>
+ *     
+ *     struct name_to_digit {
+ *             const char *name;
+ *             unsigned int val;
+ *     };
+ *     
+ *     static struct name_to_digit map[] = { 
+ *             { "zero", 0},
+ *             { "one", 1 },
+ *             { "two", 2 },
+ *             { "three", 3 },
+ *             { "four", 4 },
+ *             { "five", 5 },
+ *             { "six", 6 },
+ *             { "seven", 7 },
+ *             { "eight", 8 },
+ *             { "nine", 9 }
+ *     };
+ *     
+ *     // Wrapper for rehash function pointer.
+ *     static unsigned long rehash(const void *e, void *unused)
+ *     {
+ *             return hash_string(((struct name_to_digit *)e)->name);
+ *     }
+ *     
+ *     // Comparison function.
+ *     static bool streq(const void *e, void *string)
+ *     {
+ *             return strcmp(((struct name_to_digit *)e)->name, string) == 0;
+ *     }
+ *     
+ *     // We let them add their own aliases, eg. --alias=v=5
+ *     static void add_alias(struct hashtable *ht, const char *alias)
+ *     {
+ *             char *eq;
+ *             struct name_to_digit *n;
+ *     
+ *             n = malloc(sizeof(*n));
+ *             n->name = strdup(alias);
+ *     
+ *             eq = strchr(n->name, '=');
+ *             if (!eq || ((n->val = atoi(eq+1)) == 0 && !strcmp(eq+1, "0")))
+ *                     errx(1, "Usage: --alias=<name>=<value>");
+ *             *eq = '\0';
+ *             hashtable_add(ht, hash_string(n->name), n);
+ *     }
+ *     
+ *     int main(int argc, char *argv[])
+ *     {
+ *             struct hashtable *ht;
+ *             unsigned int i;
+ *             unsigned long val;
+ *     
+ *             if (argc < 2)
+ *                     errx(1, "Usage: %s [--alias=<name>=<val>]... <str>...",
+ *                          argv[0]);
+ *     
+ *             // Create and populate hash table.
+ *             ht = hashtable_new(rehash, NULL);
+ *             for (i = 0; i < sizeof(map)/sizeof(map[0]); i++)
+ *                     hashtable_add(ht, hash_string(map[i].name), &map[i]);
+ *     
+ *             // Add any aliases to the hash table.
+ *             for (i = 1; i < argc; i++) {
+ *                     if (!strncmp(argv[i], "--alias=", strlen("--alias=")))
+ *                             add_alias(ht, argv[i] + strlen("--alias="));
+ *                     else
+ *                             break;
+ *             }
+ *     
+ *             // Find the other args in the hash table.
+ *             for (val = 0; i < argc; i++) {
+ *                     struct name_to_digit *n;
+ *                     n = hashtable_find(ht, hash_string(argv[i]),
+ *                                        streq, argv[i]);
+ *                     if (!n)
+ *                             errx(1, "Invalid digit name %s", argv[i]);
+ *                     // Append it to the value we are building up.
+ *                     val *= 10;
+ *                     val += n->val;
+ *             }
+ *             printf("%lu\n", val);
+ *             return 0;
+ *     }
+ *
+ * Licence: GPLv2 (or later)
+ */
+int main(int argc, char *argv[])
+{
+       if (argc != 2)
+               return 1;
+
+       if (strcmp(argv[1], "depends") == 0) {
+               return 0;
+       }
+
+       return 1;
+}