2 * Hic Est Leo -- Universal Dictionaries
4 * (c) 2014--2015 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_FIND
22 #define HASH_WANT_LOOKUP
23 #define HASH_AUTO_POOL 4096
24 #define HASH_ZERO_FILL
25 #define HASH_TABLE_DYNAMIC
26 #define HASH_GIVE_ALLOC
27 #define HASH_TABLE_VARS struct mempool *kv_pool;
28 static void *kv_map_alloc(struct kv_map_table *table, uns size);
29 #include <ucw/hashtable.h>
31 static void *kv_map_alloc(struct kv_map_table *table, uns size)
33 return mp_alloc(table->kv_pool, size);
36 void dict_init(struct dict *dict, const char * const *init)
38 GARY_INIT(dict->names, 1);
39 dict->pool = mp_new(4096);
40 dict->hash = mp_alloc_zero(dict->pool, sizeof(struct kv_map_table));
41 dict->hash->kv_pool = dict->pool;
42 kv_map_init(dict->hash);
46 for (uns i=1; init[i]; i++)
48 u32 id = dict_encode(dict, init[i]);
54 u32 dict_encode(struct dict *d, const char *key)
56 struct kv_map *k = kv_map_lookup(d->hash, (char *) key);
60 k->id = GARY_SIZE(d->names);
61 k->name = mp_strdup(d->pool, key);
62 *GARY_PUSH(d->names) = k->name;
66 u32 dict_encode_if_exists(struct dict *d, const char *key)
68 struct kv_map *k = kv_map_find(d->hash, (char *) key);