summaryrefslogtreecommitdiffhomepage
path: root/examples/mmap.c
blob: 4754656f41bb8557e62254d8d7e2f103b1b20092 (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// This implements the multimap c++ example found at:
// https://en.cppreference.com/w/cpp/container/multimap/insert

// Multimap entries
#define i_implement
#include <stc/cstr.h>
#define i_val_str
#include <stc/clist.h>

// Map of int => clist_str.
#define i_type Multimap
#define i_key int
#define i_val_bind clist_str // uses clist_str as i_val and binds clist_str_clone, clist_str_drop
#define i_cmp -c_default_cmp // like std::greater<int>
#include <stc/csmap.h>

void print(const char* lbl, const Multimap mmap)
{
    printf("%s ", lbl);
    c_foreach (e, Multimap, mmap) {
        c_foreach (s, clist_str, e.ref->second)
            printf("{%d,%s} ", e.ref->first, cstr_str(s.ref));
    }
    puts("");
}

void insert(Multimap* mmap, int key, const char* str)
{
    clist_str *list = &Multimap_insert(mmap, key, clist_str_init()).ref->second;
    clist_str_emplace_back(list, str);
}

int main()
{
    c_auto (Multimap, mmap)
    {
        // list-initialize
        struct { int first; const char* second; } vals[] =
            {{2, "foo"}, {2, "bar"}, {3, "baz"}, {1, "abc"}, {5, "def"}};
        c_forrange (i, c_arraylen(vals)) insert(&mmap, c_pair(&vals[i]));
        print("#1", mmap);

        // insert using value_type
        insert(&mmap, 5, "pqr");
        print("#2", mmap);

        // insert using make_pair
        insert(&mmap, 6, "uvw");
        print("#3", mmap);

        insert(&mmap, 7, "xyz");
        print("#4", mmap);

        // insert using initialization_list
        insert(&mmap, 5, "one");
        insert(&mmap, 5, "two");
        print("#5", mmap);

        // FOLLOWING NOT IN ORIGINAL EXAMPLE:
        
        // erase all entries with key 5
        Multimap_erase(&mmap, 5);
        print("+6", mmap);

        // find and erase first entry containing "bar"
        clist_str_iter pos;
        c_foreach (e, Multimap, mmap) {
            if ((pos = clist_str_find(&e.ref->second, "bar")).ref != clist_str_end(&e.ref->second).ref) {
                clist_str_erase_at(&e.ref->second, pos);
                break;
            }
        }
        print("+7", mmap);
    }
}