]> mj.ucw.cz Git - leo.git/blob - dict.c
Style: Avoid compiler warning
[leo.git] / dict.c
1 /*
2  *      Hic Est Leo -- Universal Dictionaries
3  *
4  *      (c) 2014 Martin Mares <mj@ucw.cz>
5  */
6
7 #include <ucw/lib.h>
8 #include <ucw/mempool.h>
9
10 #include "leo.h"
11 #include "dict.h"
12
13 struct kv_map {
14   u32 id;
15   char *name;
16 };
17
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>
29
30 static void *kv_map_alloc(struct kv_map_table *table, uns size)
31 {
32   return mp_alloc(table->kv_pool, size);
33 }
34
35 void dict_init(struct dict *dict, const char * const *init)
36 {
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);
42
43   if (init)
44     {
45       for (uns i=1; init[i]; i++)
46         {
47           u32 id = dict_encode(dict, init[i]);
48           ASSERT(id == i);
49         }
50     }
51 }
52
53 u32 dict_encode(struct dict *d, const char *key)
54 {
55   struct kv_map *k = kv_map_lookup(d->hash, (char *) key);
56   if (k->id)
57     return k->id;
58
59   k->id = GARY_SIZE(d->names);
60   k->name = mp_strdup(d->pool, key);
61   *GARY_PUSH(d->names) = k->name;
62   return k->id;
63 }