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