summaryrefslogtreecommitdiffhomepage
path: root/examples/inits.c
blob: 5f8563c733dbe3178574583ce4ab3e8ba73d18d6 (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
#include <stdio.h>
#include <stc/cstr.h>
#include <stc/cmap.h>
#include <stc/cvec.h>
#include <stc/clist.h>

declare_cmap(id, int, cstr_t, cstr_destroy); // Map of int -> cstr_t
declare_cmap_str(cnt, int);

typedef struct {int x, y;} ipair_t;
declare_cvec(ip, ipair_t, c_defaultDestroy, c_memCompare);
declare_clist(ip, ipair_t, c_defaultDestroy, c_memCompare);


int main(void) {
    int year = 2020;
    cmap_id idnames = cmap_init;
    c_push(&idnames, cmap_id, c_items(
        {100, cstr_make("Hello")},
        {110, cstr_make("World")},
        {120, cstr_from("Howdy, -%d-", year)},
    ));

    c_foreach (i, cmap_id, idnames)
        printf("%d: %s\n", i.item->key, i.item->value.str);
    cmap_id_destroy(&idnames);

    // ------------------

    cmap_cnt countries = cmap_init;

    cmap_cnt_insert(&countries, "Greenland", 0)->value += 20;
    c_push(&countries, cmap_cnt, c_items(
        {"Norway", 100},
        {"Denmark", 50},
        {"Iceland", 10},
    ));

    cmap_cnt_insert(&countries, "Sweden", 0)->value += 20;
    cmap_cnt_insert(&countries, "Norway", 0)->value += 20;
    cmap_cnt_insert(&countries, "Finland", 0)->value += 20;

    c_foreach (i, cmap_cnt, countries)
        printf("%s: %d\n", i.item->key.str, i.item->value);
    cmap_cnt_destroy(&countries);

    // ------------------

    cvec_ip pairs1 = cvec_init; 
    c_push(&pairs1, cvec_ip, c_items(
        {1, 2},
        {3, 4},
        {5, 6},
        {7, 8},
    ));

    c_foreach (i, cvec_ip, pairs1)
        printf("(%d %d) ", i.item->x, i.item->y);
    puts("");
    cvec_ip_destroy(&pairs1);

    // ------------------

    clist_ip pairs2 = clist_init;
    c_push(&pairs2, clist_ip, c_items(
        {1, 2},
        {3, 4},
        {5, 6},
        {7, 8},
    ));

    c_foreach (i, clist_ip, pairs2)
        printf("(%d %d) ", i.item->value.x, i.item->value.y);
    puts("");
    clist_ip_destroy(&pairs2);
}