diff options
| author | Tyge Løvset <[email protected]> | 2022-08-07 14:49:45 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2022-08-07 14:49:45 +0200 |
| commit | 621cf0d0cf508cbdd7b15a9b8416c5938f2b27a0 (patch) | |
| tree | c494be25e2d05f8d1896563d6916a1a8c1d935e6 /examples | |
| parent | c87898773d1af364a9847610401a9959f6019fe7 (diff) | |
| download | STC-modified-621cf0d0cf508cbdd7b15a9b8416c5938f2b27a0.tar.gz STC-modified-621cf0d0cf508cbdd7b15a9b8416c5938f2b27a0.zip | |
Refined template.h when specifying i_key_bind (and i_val_bind): i_keyclone becomes defined `c_derived_keyclone` when i_keyfrom is defined, i.e. you don't have to define both, but you can.
Improved cmap docs.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/vikings.c | 37 |
1 files changed, 15 insertions, 22 deletions
diff --git a/examples/vikings.c b/examples/vikings.c index 667c4426..b5b3417b 100644 --- a/examples/vikings.c +++ b/examples/vikings.c @@ -25,10 +25,7 @@ static inline int RViking_cmp(const RViking* rx, const RViking* ry) { 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 Viking Viking_clone(Viking vk) { // note: parameter is by value - vk.name = cstr_clone(vk.name), vk.country = cstr_clone(vk.country); - return vk; -} + static inline RViking Viking_toraw(const Viking* vp) { return c_make(RViking){cstr_str(&vp->name), cstr_str(&vp->country)}; } @@ -37,36 +34,32 @@ static inline RViking Viking_toraw(const Viking* vp) { #define i_type Vikings #define i_key_bind Viking // key type #define i_keyraw RViking // lookup type -#define i_keyfrom Viking_from // convert from lookup type (enables emplace) +#define i_keyfrom Viking_from #define i_hash(rp) c_strhash(rp->name) ^ c_strhash(rp->country) #define i_val int // mapped type -// i_key_bind auto-binds these functions (unless they are defined by i_...): + +// i_key_bind makes up these defines, unless they are already defined: // i_cmp => RViking_cmp -// i_hash => RViking_hash -// i_keyclone => Viking_clone -// i_keyto => Viking_toraw // because i_keyraw is defined +// //i_hash => RViking_hash // already defined. +// i_keyclone => c_derived_keyclone // because i_keyfrom is defined +// i_keyto => Viking_toraw // because i_keyraw is defined // i_keydrop => Viking_drop + #include <stc/cmap.h> int main() { c_auto (Vikings, vikings) { - c_forarray (Vikings_raw, v, { - {{"Einar", "Norway"}, 20}, - {{"Olaf", "Denmark"}, 24}, - {{"Harald", "Iceland"}, 12}, - }) Vikings_emplace(&vikings, c_pair(v)); - - RViking bjorn = {"Bjorn", "Sweden"}; - Vikings_emplace_or_assign(&vikings, bjorn, 10); + Vikings_emplace(&vikings, (RViking){"Einar", "Norway"}, 20); + Vikings_emplace(&vikings, (RViking){"Olaf", "Denmark"}, 24); + Vikings_emplace(&vikings, (RViking){"Harald", "Iceland"}, 12); + Vikings_emplace(&vikings, (RViking){"Björn", "Sweden"}, 10); - RViking einar = {"Einar", "Norway"}; - Vikings_value* v = Vikings_get_mut(&vikings, einar); + Vikings_value* v = Vikings_get_mut(&vikings, (RViking){"Einar", "Norway"}); v->second += 3; // add 3 hp points to Einar - Vikings_emplace(&vikings, einar, 0).ref->second += 5; // add 5 more to Einar - c_forpair (vik, hp, Vikings, vikings) { - printf("%s of %s has %d hp\n", cstr_str(&_.vik->name), cstr_str(&_.vik->country), *_.hp); + c_forpair (vk, hp, Vikings, vikings) { + printf("%s of %s has %d hp\n", cstr_str(&_.vk->name), cstr_str(&_.vk->country), *_.hp); } } } |
