]> git.ozlabs.org Git - ccan/blob - ccan/htable/_info
cpuid: Minor improvements
[ccan] / ccan / htable / _info
1 #include <string.h>
2 #include <stdio.h>
3
4 /**
5  * htable - 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  * Example:
11  *      #include <ccan/htable/htable.h>
12  *      #include <ccan/hash/hash.h>
13  *      #include <stdio.h>
14  *      #include <err.h>
15  *      #include <string.h>
16  *
17  *      struct name_to_digit {
18  *              const char *name;
19  *              unsigned int val;
20  *      };
21  *
22  *      static struct name_to_digit map[] = {
23  *              { "zero", 0},
24  *              { "one", 1 },
25  *              { "two", 2 },
26  *              { "three", 3 },
27  *              { "four", 4 },
28  *              { "five", 5 },
29  *              { "six", 6 },
30  *              { "seven", 7 },
31  *              { "eight", 8 },
32  *              { "nine", 9 }
33  *      };
34  *
35  *      // Wrapper for rehash function pointer.
36  *      static size_t rehash(const void *e, void *unused)
37  *      {
38  *              return hash_string(((struct name_to_digit *)e)->name);
39  *      }
40  *
41  *      // Comparison function.
42  *      static bool streq(const void *e, void *string)
43  *      {
44  *              return strcmp(((struct name_to_digit *)e)->name, string) == 0;
45  *      }
46  *
47  *      // We let them add their own aliases, eg. --alias=v=5
48  *      static void add_alias(struct htable *ht, const char *alias)
49  *      {
50  *              char *eq;
51  *              struct name_to_digit *n;
52  *
53  *              n = malloc(sizeof(*n));
54  *              n->name = strdup(alias);
55  *
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>");
59  *              *eq = '\0';
60  *              htable_add(ht, hash_string(n->name), n);
61  *      }
62  *
63  *      int main(int argc, char *argv[])
64  *      {
65  *              struct htable ht;
66  *              unsigned int i;
67  *              unsigned long val;
68  *
69  *              if (argc < 2)
70  *                      errx(1, "Usage: %s [--alias=<name>=<val>]... <str>...",
71  *                           argv[0]);
72  *
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]);
77  *
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="));
82  *                      else
83  *                              break;
84  *              }
85  *
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]),
90  *                                     streq, argv[i]);
91  *                      if (!n)
92  *                              errx(1, "Invalid digit name %s", argv[i]);
93  *                      // Append it to the value we are building up.
94  *                      val *= 10;
95  *                      val += n->val;
96  *              }
97  *              printf("%lu\n", val);
98  *              return 0;
99  *      }
100  *
101  * License: LGPL (v2.1 or any later version)
102  * Author: Rusty Russell <rusty@rustcorp.com.au>
103  */
104 int main(int argc, char *argv[])
105 {
106         if (argc != 2)
107                 return 1;
108
109         if (strcmp(argv[1], "depends") == 0) {
110                 printf("ccan/compiler\n");
111                 return 0;
112         }
113
114         return 1;
115 }