summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/mixed/inits.c
blob: 53a49f1ff961595e2f30376796357ae487f81ad1 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#define i_implement
#include <stc/cstr.h>

#define i_key int
#define i_val_str
#define i_tag id   // Map of int => cstr
#include <stc/cmap.h>

#define i_key_str
#define i_val int
#define i_tag cnt  // Map of cstr => int
#include <stc/cmap.h>

typedef struct {int x, y;} ipair_t;
inline static int ipair_cmp(const ipair_t* a, const ipair_t* b) {
    int c = c_default_cmp(&a->x, &b->x);
    return c ? c : c_default_cmp(&a->y, &b->y);
}


#define i_key ipair_t
#define i_cmp ipair_cmp
#define i_tag ip
#include <stc/cvec.h>

#define i_key ipair_t
#define i_cmp ipair_cmp
#define i_tag ip
#include <stc/clist.h>

#define i_key float
#define i_tag f
#include <stc/cpque.h>

int main(void)
{
    // CVEC FLOAT / PRIORITY QUEUE

    cpque_f floats = {0};
    const float nums[] = {4.0f, 2.0f, 5.0f, 3.0f, 1.0f};

    // PRIORITY QUEUE
    c_forrange (i, c_arraylen(nums))
        cpque_f_push(&floats, nums[i]);

    puts("\npop and show high priorites first:");
    while (! cpque_f_empty(&floats)) {
        printf("%.1f ", (double)*cpque_f_top(&floats));
        cpque_f_pop(&floats);
    }
    puts("\n");
    cpque_f_drop(&floats);

    // CMAP ID

    int year = 2020;
    cmap_id idnames = {0};
    cmap_id_emplace(&idnames, 100, "Hello");
    cmap_id_insert(&idnames, 110, cstr_lit("World"));
    cmap_id_insert(&idnames, 120, cstr_from_fmt("Howdy, -%d-", year));

    c_foreach (i, cmap_id, idnames)
        printf("%d: %s\n", i.ref->first, cstr_str(&i.ref->second));
    puts("");
    cmap_id_drop(&idnames);

    // CMAP CNT

    cmap_cnt countries = c_init(cmap_cnt, {
        {"Norway", 100},
        {"Denmark", 50},
        {"Iceland", 10},
        {"Belgium", 10},
        {"Italy", 10},
        {"Germany", 10},
        {"Spain", 10},
        {"France", 10},
    });
    cmap_cnt_emplace(&countries, "Greenland", 0).ref->second += 20;
    cmap_cnt_emplace(&countries, "Sweden", 0).ref->second += 20;
    cmap_cnt_emplace(&countries, "Norway", 0).ref->second += 20;
    cmap_cnt_emplace(&countries, "Finland", 0).ref->second += 20;

    c_forpair (country, health, cmap_cnt, countries)
        printf("%s: %d\n", cstr_str(_.country), *_.health);
    puts("");
    cmap_cnt_drop(&countries);

    // CVEC PAIR

    cvec_ip pairs1 = c_init(cvec_ip, {{5, 6}, {3, 4}, {1, 2}, {7, 8}});
    cvec_ip_sort(&pairs1);

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

    // CLIST PAIR

    clist_ip pairs2 = c_init(clist_ip, {{5, 6}, {3, 4}, {1, 2}, {7, 8}});
    clist_ip_sort(&pairs2);

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