diff options
Diffstat (limited to 'examples/vikings.c')
| -rw-r--r-- | examples/vikings.c | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/examples/vikings.c b/examples/vikings.c new file mode 100644 index 00000000..87977538 --- /dev/null +++ b/examples/vikings.c @@ -0,0 +1,69 @@ +#include <stdio.h> +#include <stc/cstr.h> + +typedef struct Viking { + cstr name; + cstr country; +} Viking; + +void Viking_drop(Viking* vk) { + cstr_drop(&vk->name); + cstr_drop(&vk->country); +} + +// Define Viking raw struct with hash, equals, and convertion functions between Viking and RViking structs: + +typedef struct RViking { + const char* name; + const char* country; +} RViking; + +uint64_t RViking_hash(const RViking* raw, size_t ignore) { + uint64_t hash = c_strhash(raw->name) ^ (c_strhash(raw->country) >> 15); + return hash; +} +static inline bool RViking_equalto(const RViking* rx, const RViking* ry) { + return strcmp(rx->name, ry->name) == 0 && strcmp(rx->country, ry->country) == 0; +} + +static inline Viking Viking_from(RViking raw) { // note: parameter is by value + return c_make(Viking){cstr_from(raw.name), cstr_from(raw.country)}; +} +static inline RViking Viking_toraw(const Viking* vk) { + return c_make(RViking){vk->name.str, vk->country.str}; +} + +// With this in place, we define the Viking => int hash map type: +#define i_type Vikings +#define i_key_bind Viking +#define i_val int +#define i_keyraw RViking +// i_key_bind auto-maps these functions: +// #define i_hash Viking_hash +// #define i_equ Viking_equalto +// #define i_keyfrom Viking_from // uses _from because i_keyraw is defined +// #define i_keyto Viking_toraw +// #define i_keydrop Viking_drop +#include <stc/cmap.h> + +int main() +{ + c_auto (Vikings, vikings) { + c_apply_pair(Vikings, emplace, &vikings, { + {{"Einar", "Norway"}, 20}, + {{"Olaf", "Denmark"}, 24}, + {{"Harald", "Iceland"}, 12}, + }); + RViking bjorn = {"Bjorn", "Sweden"}; + Vikings_emplace_or_assign(&vikings, bjorn, 10); + + RViking einar = {"Einar", "Norway"}; + Vikings_value *e = Vikings_find(&vikings, einar).ref; + e->second += 3; // add 3 hp points to Einar + Vikings_emplace(&vikings, einar, 0).ref->second += 5; // add 5 more to Einar + + c_forpair (viking, hp, Vikings, vikings) { + printf("%s of %s has %d hp\n", _.viking.name.str, _.viking.country.str, _.hp); + } + } +}
\ No newline at end of file |
