summaryrefslogtreecommitdiffhomepage
path: root/examples/mapmap.c
blob: 44cf7da520b1f53ab38ef3ab37b6c7e062542931 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

#include <stdio.h>
#include <stc/cstr.h>

// unordered_map<string, unordered_map<string, string>>:

#define i_key_str
#define i_val_str
#include <stc/cmap.h>

#define i_key_str
#define i_val cmap_str
#define i_valdel cmap_str_del
#define i_tag cfg
#include <stc/cmap.h>

int main(void)
{
    c_auto (cmap_cfg, cfg)
    {
        cmap_cfg_insert(&cfg, cstr_from("user"), cmap_str_init());
        cmap_cfg_insert(&cfg, cstr_from("group"), cmap_str_init());
        cmap_cfg_insert(&cfg, cstr_from("admin"), cmap_str_init());

        cmap_str_emplace(cmap_cfg_at(&cfg, "user"), "name", "Joe");
        cmap_str_emplace(cmap_cfg_at(&cfg, "user"), "groups", "proj1,proj3");
        cmap_str_emplace(cmap_cfg_at(&cfg, "group"), "proj1", "Energy");
        cmap_str_emplace(cmap_cfg_at(&cfg, "group"), "proj2", "Windy");
        cmap_str_emplace(cmap_cfg_at(&cfg, "group"), "proj3", "Oil");
        cmap_str_emplace(cmap_cfg_at(&cfg, "admin"), "employees", "2302");

        cmap_str_emplace_or_assign(cmap_cfg_at(&cfg, "group"), "proj2", "Wind"); // Update

        c_foreach (i, cmap_cfg, cfg)
            c_foreach (j, cmap_str, i.ref->second)
                printf("%s: %s - %s (%u)\n", i.ref->first.str, j.ref->first.str, j.ref->second.str, 
                                             i.ref->second.bucket_count);
    }
}