]> git.ozlabs.org Git - ccan/blob - ccan/hashtable/_info
0974c1558e6f8770dbae931675fd8e9577fc638e
[ccan] / ccan / hashtable / _info
1 #include <string.h>
2 #include <stdio.h>
3
4 /**
5  * hashtable - hash table routines
6  *
7  * A hash table is an efficient structure for looking up keys.  This version
8  * grows with usage and allows efficient deletion.
9  *
10  * Caveat: pointers placed into the hash must be aligned to 64 bits.
11  * ie. from malloc or because the structure they point to is aligned.
12  * An assert will be raised if this isn't true.
13  *
14  * Example:
15  *      #include <ccan/hashtable/hashtable.h>
16  *      #include <ccan/hash/hash.h>
17  *      #include <stdio.h>
18  *      #include <err.h>
19  *      #include <string.h>
20  *      
21  *      struct name_to_digit {
22  *              const char *name;
23  *              unsigned int val;
24  *      };
25  *      
26  *      static struct name_to_digit map[] = { 
27  *              { "zero", 0},
28  *              { "one", 1 },
29  *              { "two", 2 },
30  *              { "three", 3 },
31  *              { "four", 4 },
32  *              { "five", 5 },
33  *              { "six", 6 },
34  *              { "seven", 7 },
35  *              { "eight", 8 },
36  *              { "nine", 9 }
37  *      };
38  *      
39  *      // Wrapper for rehash function pointer.
40  *      static unsigned long rehash(const void *e, void *unused)
41  *      {
42  *              return hash_string(((struct name_to_digit *)e)->name);
43  *      }
44  *      
45  *      // Comparison function.
46  *      static bool streq(const void *e, void *string)
47  *      {
48  *              return strcmp(((struct name_to_digit *)e)->name, string) == 0;
49  *      }
50  *      
51  *      // We let them add their own aliases, eg. --alias=v=5
52  *      static void add_alias(struct hashtable *ht, const char *alias)
53  *      {
54  *              char *eq;
55  *              struct name_to_digit *n;
56  *      
57  *              n = malloc(sizeof(*n));
58  *              n->name = strdup(alias);
59  *      
60  *              eq = strchr(n->name, '=');
61  *              if (!eq || ((n->val = atoi(eq+1)) == 0 && !strcmp(eq+1, "0")))
62  *                      errx(1, "Usage: --alias=<name>=<value>");
63  *              *eq = '\0';
64  *              hashtable_add(ht, hash_string(n->name), n);
65  *      }
66  *      
67  *      int main(int argc, char *argv[])
68  *      {
69  *              struct hashtable *ht;
70  *              unsigned int i;
71  *              unsigned long val;
72  *      
73  *              if (argc < 2)
74  *                      errx(1, "Usage: %s [--alias=<name>=<val>]... <str>...",
75  *                           argv[0]);
76  *      
77  *              // Create and populate hash table.
78  *              ht = hashtable_new(rehash, NULL);
79  *              for (i = 0; i < sizeof(map)/sizeof(map[0]); i++)
80  *                      hashtable_add(ht, hash_string(map[i].name), &map[i]);
81  *      
82  *              // Add any aliases to the hash table.
83  *              for (i = 1; i < argc; i++) {
84  *                      if (!strncmp(argv[i], "--alias=", strlen("--alias=")))
85  *                              add_alias(ht, argv[i] + strlen("--alias="));
86  *                      else
87  *                              break;
88  *              }
89  *      
90  *              // Find the other args in the hash table.
91  *              for (val = 0; i < argc; i++) {
92  *                      struct name_to_digit *n;
93  *                      n = hashtable_find(ht, hash_string(argv[i]),
94  *                                         streq, argv[i]);
95  *                      if (!n)
96  *                              errx(1, "Invalid digit name %s", argv[i]);
97  *                      // Append it to the value we are building up.
98  *                      val *= 10;
99  *                      val += n->val;
100  *              }
101  *              printf("%lu\n", val);
102  *              return 0;
103  *      }
104  *
105  * License: GPLv2 (or later)
106  * Author: Rusty Russell <rusty@rustcorp.com.au>
107  */
108 int main(int argc, char *argv[])
109 {
110         if (argc != 2)
111                 return 1;
112
113         if (strcmp(argv[1], "depends") == 0) {
114                 printf("ccan/typesafe_cb\n");
115                 return 0;
116         }
117
118         return 1;
119 }