gen.sh: invoke mkfn instead of mktrfn
[neatmkfn.git] / tab.c
blobe6ec05a9050386dd4a7150a6fd76e5b80efc6013
1 #include <stdlib.h>
2 #include <string.h>
3 #include "tab.h"
5 struct tab {
6 char **keys;
7 void **vals;
8 int n;
9 int *next;
10 int head[256];
13 struct tab *tab_alloc(int sz)
15 struct tab *tab = malloc(sizeof(*tab));
16 int i;
17 memset(tab, 0, sizeof(*tab));
18 tab->keys = malloc(sz * sizeof(tab->keys[0]));
19 tab->vals = malloc(sz * sizeof(tab->vals[0]));
20 tab->next = malloc(sz * sizeof(tab->next[0]));
21 for (i = 0; i < 256; i++)
22 tab->head[i] = -1;
23 return tab;
26 void tab_free(struct tab *tab)
28 free(tab->keys);
29 free(tab->vals);
30 free(tab->next);
31 free(tab);
34 void tab_put(struct tab *tab, char *k, void *v)
36 tab->keys[tab->n] = k;
37 tab->vals[tab->n] = v;
38 tab->next[tab->n] = tab->head[(unsigned char) k[0]];
39 tab->head[(unsigned char) k[0]] = tab->n;
40 tab->n++;
43 void *tab_get(struct tab *tab, char *k)
45 int i = tab->head[(unsigned char) k[0]];
46 while (i >= 0) {
47 if (k[1] == tab->keys[i][1] && !strcmp(k, tab->keys[i]))
48 return tab->vals[i];
49 i = tab->next[i];
51 return NULL;