summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/new_map.c
blob: a4d5289c41837bec00311be8d290c3fad9ba6d25 (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
#include <stc/cstr.h>
#include <stc/forward.h>

forward_cmap(cmap_pnt, struct Point, int);

struct MyStruct {
    cmap_pnt pntmap;
    cstr name;
} typedef MyStruct;

// int => int map
#define i_key int
#define i_val int
#include <stc/cmap.h>

// Point => int map
struct Point { int x, y; } typedef Point;

int point_cmp(const Point* a, const Point* b) {
    int c = a->x - b->x;
    return c ? c : a->y - b->y;
}

// Point => int map
#define i_key Point
#define i_val int
#define i_cmp point_cmp
#define i_hash c_default_hash
#define i_opt c_is_forward
#define i_tag pnt
#include <stc/cmap.h>

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

// string set
#define i_key_str
#include <stc/cset.h>


int main()
{
    c_auto (cmap_int, map)
    c_auto (cmap_pnt, pmap)
    c_auto (cmap_str, smap)
    c_auto (cset_str, sset)
    {
        cmap_int_insert(&map, 123, 321);
        cmap_int_insert(&map, 456, 654);
        cmap_int_insert(&map, 789, 987);

        pmap = c_make(cmap_pnt, {{{42, 14}, 1}, {{32, 94}, 2}, {{62, 81}, 3}});

        c_foreach (i, cmap_pnt, pmap)
            printf(" (%d, %d: %d)", i.ref->first.x, i.ref->first.y, i.ref->second);
        puts("");

        smap = c_make(cmap_str, {
            {"Hello, friend", "long time no see"},
            {"So long", "see you around"},
        });

        sset = c_make(cset_str, {
            "Hello, friend",
            "Nice to see you again",
            "So long",
        });

        c_foreach (i, cset_str, sset)
            printf(" %s\n", cstr_str(i.ref));
    }
}