6 #define DHASH(d, s) ((d)->level2 ? (((unsigned char) (s)[1]) << 8) | (unsigned char) (s)[0] : (unsigned char) s[0])
9 * initialise a dictionary
11 * notfound: the value returned for missing keys.
12 * dupkeys: if nonzero, store a copy of keys inserted via dict_put().
13 * level2: use two characters for hasing
15 void dict_init(struct dict
*d
, int size
, long notfound
, int dupkeys
, int level2
)
17 memset(d
, 0, sizeof(*d
));
21 d
->notfound
= notfound
;
22 d
->key
= malloc(size
* sizeof(d
->key
[0]));
23 d
->val
= malloc(size
* sizeof(d
->val
[0]));
24 d
->next
= malloc(size
* sizeof(d
->next
[0]));
25 d
->head
= malloc((level2
? 256 * 256 : 256) * sizeof(d
->next
[0]));
27 d
->buf
= malloc(size
* NMLEN
);
30 void dict_done(struct dict
*d
)
39 void dict_put(struct dict
*d
, char *key
, long val
)
45 int len
= strlen(key
) + 1;
46 if (d
->buflen
+ len
>= d
->size
* NMLEN
)
48 memcpy(d
->buf
+ d
->buflen
, key
, len
);
49 key
= d
->buf
+ d
->buflen
;
55 d
->next
[idx
] = d
->head
[DHASH(d
, key
)];
56 d
->head
[DHASH(d
, key
)] = idx
;
59 /* return the index of key in d */
60 int dict_idx(struct dict
*d
, char *key
)
62 int idx
= d
->head
[DHASH(d
, key
)];
64 if (!strcmp(d
->key
[idx
], key
))
71 char *dict_key(struct dict
*d
, int idx
)
76 long dict_val(struct dict
*d
, int idx
)
81 long dict_get(struct dict
*d
, char *key
)
83 int idx
= dict_idx(d
, key
);
84 return idx
>= 0 ? d
->val
[idx
] : d
->notfound
;
87 /* match a prefix of key; in the first call, *idx should be -1 */
88 long dict_prefix(struct dict
*d
, char *key
, int *idx
)
94 *idx
= d
->head
[DHASH(d
, key
)];
98 plen
= strlen(d
->key
[*idx
]);
99 if (!strncmp(d
->key
[*idx
], key
, plen
))
101 *idx
= d
->next
[*idx
];