summaryrefslogtreecommitdiffhomepage
path: root/examples/advanced.c
blob: 60276b098b5db2c77540829b406358e96a1fe96c (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
#include <stdio.h>
#include <stc/cstr.h>

typedef struct Viking {
    cstr name;
    cstr country;
} Viking;

void viking_del(Viking* vk) {
    cstr_del(&vk->name);
    cstr_del(&vk->country);
}

// Define Viking raw struct with hash, equals, and convertion functions between Viking and VikingRaw structs:

typedef struct VikingRaw {
    const char* name;
    const char* country;
} VikingRaw;

uint64_t vikingraw_hash(const VikingRaw* raw, size_t ignore) {
    uint64_t hash = c_strhash(raw->name) ^ (c_strhash(raw->country) >> 15);
    return hash;
}
static inline bool vikingraw_equalto(const VikingRaw* rx, const VikingRaw* ry) {
    return strcmp(rx->name, ry->name) == 0 && strcmp(rx->country, ry->country) == 0;
}

static inline Viking viking_fromRaw(VikingRaw raw) { // note: parameter is by value
    return c_make(Viking){cstr_from(raw.name), cstr_from(raw.country)};
}
static inline VikingRaw viking_toRaw(const Viking* vk) {
    return c_make(VikingRaw){vk->name.str, vk->country.str};
}

// With this in place, we define the Viking => int hash map type:
#define i_tag     vk
#define i_key     Viking
#define i_val     int
#define i_equ     vikingraw_equalto
#define i_hash    vikingraw_hash
#define i_keyraw  VikingRaw
#define i_keyfrom viking_fromRaw
#define i_keyto   viking_toRaw
#define i_keydel  viking_del
#include <stc/cmap.h>

int main()
{
    c_auto (cmap_vk, vikings) {
        c_apply_pair(cmap_vk, emplace, &vikings, {
            {{"Einar", "Norway"}, 20},
            {{"Olaf", "Denmark"}, 24},
            {{"Harald", "Iceland"}, 12},
        });
        VikingRaw bjorn = {"Bjorn", "Sweden"};
        cmap_vk_emplace_or_assign(&vikings, bjorn, 10);

        VikingRaw einar = {"Einar", "Norway"};
        cmap_vk_value *e = cmap_vk_find(&vikings, einar).ref;
        e->second += 3; // add 3 hp points to Einar
        cmap_vk_emplace(&vikings, einar, 0).ref->second += 5; // add 5 more to Einar

        c_foreach (k, cmap_vk, vikings) {
            printf("%s of %s has %d hp\n", k.ref->first.name.str, k.ref->first.country.str, k.ref->second);
        }
    }
}