2 * Hic Est Leo -- Universal Dictionaries
4 * (c) 2014 Martin Mares <mj@ucw.cz>
8 #include <ucw/mempool.h>
18 #define HASH_NODE struct kv_map
19 #define HASH_PREFIX(x) kv_map_##x
20 #define HASH_KEY_STRING name
21 #define HASH_WANT_LOOKUP
22 #define HASH_AUTO_POOL 4096
23 #define HASH_ZERO_FILL
24 #define HASH_TABLE_DYNAMIC
25 #define HASH_GIVE_ALLOC
26 #define HASH_TABLE_VARS struct mempool *kv_pool;
27 static void *kv_map_alloc(struct kv_map_table *table, uns size);
28 #include <ucw/hashtable.h>
30 static void *kv_map_alloc(struct kv_map_table *table, uns size)
32 return mp_alloc(table->kv_pool, size);
35 void dict_init(struct dict *dict, const char * const *init)
37 GARY_INIT(dict->names, 1);
38 dict->pool = mp_new(4096);
39 dict->hash = mp_alloc_zero(dict->pool, sizeof(struct kv_map_table));
40 dict->hash->kv_pool = dict->pool;
41 kv_map_init(dict->hash);
45 for (uns i=1; init[i]; i++)
47 u32 id = dict_encode(dict, init[i]);
53 u32 dict_encode(struct dict *d, const char *key)
55 struct kv_map *k = kv_map_lookup(d->hash, (char *) key);
59 k->id = GARY_SIZE(d->names);
60 k->name = mp_strdup(d->pool, key);
61 *GARY_PUSH(d->names) = k->name;