summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/new_smap.c
blob: 0870ee3d1ddb1184c82b9ee295d05c9eb8e21669 (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
76
77
#include <stc/cstr.h>
#include <stc/forward.h>

forward_csmap(PMap, struct Point, int);

// Use forward declared PMap in struct
struct MyStruct {
    PMap pntmap;
    cstr name;
} typedef MyStruct;

// int => int map
#define i_key int
#define i_val int
#include <stc/csmap.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;
}

#define i_type PMap
#define i_key Point
#define i_val int
#define i_cmp point_cmp
#define i_opt c_is_forward
#include <stc/csmap.h>

// cstr => cstr map
#define i_type SMap
#define i_key_str
#define i_val_str
#include <stc/csmap.h>

// cstr set
#define i_type SSet
#define i_key_str
#include <stc/csset.h>


int main()
{
    c_auto (csmap_int, imap) {
        csmap_int_insert(&imap, 123, 321);
    }

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

        c_forpair (p, i, PMap, pmap)
            printf(" (%d,%d: %d)", _.p->x, _.p->y, *_.i);
        puts("");
    }

    c_auto (SMap, smap) {
        smap = c_make(SMap, {
            {"Hello, friend", "this is the mapped value"},
            {"The brown fox", "jumped"},
            {"This is the time", "for all good things"},
        });

        c_forpair (i, j, SMap, smap)
            printf(" (%s: %s)\n", cstr_str(_.i), cstr_str(_.j));
    }

    c_auto (SSet, sset) {
        SSet_emplace(&sset, "Hello, friend");
        SSet_emplace(&sset, "Goodbye, foe");
        printf("Found? %s\n", SSet_contains(&sset, "Hello, friend") ? "true" : "false");
    }
}