diff options
Diffstat (limited to 'docs/cmap_api.md')
| -rw-r--r-- | docs/cmap_api.md | 201 |
1 files changed, 92 insertions, 109 deletions
diff --git a/docs/cmap_api.md b/docs/cmap_api.md index bf3dddcc..d2a94ee8 100644 --- a/docs/cmap_api.md +++ b/docs/cmap_api.md @@ -36,9 +36,7 @@ See the c++ class [std::unordered_map](https://en.cppreference.com/w/cpp/contain #define i_valto // convertion func i_val* => i_valraw #define i_tag // alternative typename: cmap_{i_tag}. i_tag defaults to i_val -#define i_hash_functor // advanced, see examples/functor.c for similar usage. -#define i_eq_functor // advanced, see examples/functor.c for similar usage. -#define i_ssize // internal; default int32_t. If defined, table expand 2x (else 1.5x) +#define i_ssize // internal; default int32_t. If defined, table expand 2x (else 1.5x) #include <stc/cmap.h> ``` `X` should be replaced by the value of `i_tag` in all of the following documentation. @@ -104,12 +102,12 @@ bool c_memcmp_eq(const i_keyraw* a, const i_keyraw* b); // ! | Type name | Type definition | Used to represent... | |:-------------------|:------------------------------------------------|:------------------------------| | `cmap_X` | `struct { ... }` | The cmap type | -| `cmap_X_rawkey` | `i_keyraw` | The raw key type | -| `cmap_X_rawmapped` | `i_valraw` | The raw mapped type | -| `cmap_X_raw` | `struct { i_keyraw first; i_valraw second; }` | i_keyraw + i_valraw type | | `cmap_X_key` | `i_key` | The key type | | `cmap_X_mapped` | `i_val` | The mapped type | | `cmap_X_value` | `struct { const i_key first; i_val second; }` | The value: key is immutable | +| `cmap_X_keyraw` | `i_keyraw` | The raw key type | +| `cmap_X_rmapped` | `i_valraw` | The raw mapped type | +| `cmap_X_raw` | `struct { i_keyraw first; i_valraw second; }` | i_keyraw + i_valraw type | | `cmap_X_result` | `struct { cmap_X_value *ref; bool inserted; }` | Result of insert/emplace | | `cmap_X_iter` | `struct { cmap_X_value *ref; ... }` | Iterator type | @@ -125,27 +123,26 @@ bool c_memcmp_eq(const i_keyraw* a, const i_keyraw* b); // ! int main() { // Create an unordered_map of three strings (that map to strings) - c_auto (cmap_str, u) - { - u = c_make(cmap_str, { - {"RED", "#FF0000"}, - {"GREEN", "#00FF00"}, - {"BLUE", "#0000FF"} - }); - - // Iterate and print keys and values of unordered map - c_foreach (n, cmap_str, u) { - printf("Key:[%s] Value:[%s]\n", cstr_str(&n.ref->first), cstr_str(&n.ref->second)); - } - - // Add two new entries to the unordered map - cmap_str_emplace(&u, "BLACK", "#000000"); - cmap_str_emplace(&u, "WHITE", "#FFFFFF"); - - // Output values by key - printf("The HEX of color RED is:[%s]\n", cstr_str(cmap_str_at(&u, "RED"))); - printf("The HEX of color BLACK is:[%s]\n", cstr_str(cmap_str_at(&u, "BLACK"))); + cmap_str umap = c_make(cmap_str, { + {"RED", "#FF0000"}, + {"GREEN", "#00FF00"}, + {"BLUE", "#0000FF"} + }); + + // Iterate and print keys and values of unordered map + c_foreach (n, cmap_str, umap) { + printf("Key:[%s] Value:[%s]\n", cstr_str(&n.ref->first), cstr_str(&n.ref->second)); } + + // Add two new entries to the unordered map + cmap_str_emplace(&umap, "BLACK", "#000000"); + cmap_str_emplace(&umap, "WHITE", "#FFFFFF"); + + // Output values by key + printf("The HEX of color RED is:[%s]\n", cstr_str(cmap_str_at(&umap, "RED"))); + printf("The HEX of color BLACK is:[%s]\n", cstr_str(cmap_str_at(&umap, "BLACK"))); + + cmap_str_drop(&umap); } ``` Output: @@ -161,33 +158,33 @@ The HEX of color BLACK is:[#000000] This example uses a cmap with cstr as mapped value. ```c #include <stc/cstr.h> - +#define i_type IDMap #define i_key int #define i_val_str -#define i_tag id #include <stc/cmap.h> int main() { uint32_t col = 0xcc7744ff; - c_auto (cmap_id, idnames) - { - c_forlist (i, cmap_id_raw, { {100, "Red"}, {110, "Blue"} }) - cmap_id_emplace(&idnames, c_PAIR(i.ref)); - - // replace existing mapped value: - cmap_id_emplace_or_assign(&idnames, 110, "White"); - - // insert a new constructed mapped string into map: - cmap_id_insert_or_assign(&idnames, 120, cstr_from_fmt("#%08x", col)); - - // emplace/insert does nothing if key already exist: - cmap_id_emplace(&idnames, 100, "Green"); - - c_foreach (i, cmap_id, idnames) - printf("%d: %s\n", i.ref->first, cstr_str(&i.ref->second)); - } + IDMap idnames = {0}; + + c_forlist (i, IDMap_raw, { {100, "Red"}, {110, "Blue"} }) + IDMap_emplace(&idnames, i.ref->first, i.ref->second); + + // replace existing mapped value: + IDMap_emplace_or_assign(&idnames, 110, "White"); + + // insert a new constructed mapped string into map: + IDMap_insert_or_assign(&idnames, 120, cstr_from_fmt("#%08x", col)); + + // emplace/insert does nothing if key already exist: + IDMap_emplace(&idnames, 100, "Green"); + + c_foreach (i, IDMap, idnames) + printf("%d: %s\n", i.ref->first, cstr_str(&i.ref->second)); + + IDMap_drop(&idnames); } ``` Output: @@ -212,16 +209,17 @@ typedef struct { int x, y, z; } Vec3i; int main() { // Define map with defered destruct - c_with (cmap_vi vecs = cmap_vi_init(), cmap_vi_drop(&vecs)) - { - cmap_vi_insert(&vecs, (Vec3i){100, 0, 0}, 1); - cmap_vi_insert(&vecs, (Vec3i){ 0, 100, 0}, 2); - cmap_vi_insert(&vecs, (Vec3i){ 0, 0, 100}, 3); - cmap_vi_insert(&vecs, (Vec3i){100, 100, 100}, 4); - - c_forpair (v3, num, cmap_vi, vecs) - printf("{ %3d, %3d, %3d }: %d\n", _.v3->x, _.v3->y, _.v3->z, *_.num); - } + cmap_vi vecs = {0}; + + cmap_vi_insert(&vecs, (Vec3i){100, 0, 0}, 1); + cmap_vi_insert(&vecs, (Vec3i){ 0, 100, 0}, 2); + cmap_vi_insert(&vecs, (Vec3i){ 0, 0, 100}, 3); + cmap_vi_insert(&vecs, (Vec3i){100, 100, 100}, 4); + + c_forpair (v3, num, cmap_vi, vecs) + printf("{ %3d, %3d, %3d }: %d\n", _.v3->x, _.v3->y, _.v3->z, *_.num); + + cmap_vi_drop(&vecs); } ``` Output: @@ -245,16 +243,17 @@ typedef struct { int x, y, z; } Vec3i; int main() { - c_auto (cmap_iv, vecs) // shorthand for c_with with _init(), _drop(). - { - cmap_iv_insert(&vecs, 1, (Vec3i){100, 0, 0}); - cmap_iv_insert(&vecs, 2, (Vec3i){ 0, 100, 0}); - cmap_iv_insert(&vecs, 3, (Vec3i){ 0, 0, 100}); - cmap_iv_insert(&vecs, 4, (Vec3i){100, 100, 100}); - - c_forpair (num, v3, cmap_iv, vecs) - printf("%d: { %3d, %3d, %3d }\n", *_.num, _.v3->x, _.v3->y, _.v3->z); - } + cmap_iv vecs = {0} + + cmap_iv_insert(&vecs, 1, (Vec3i){100, 0, 0}); + cmap_iv_insert(&vecs, 2, (Vec3i){ 0, 100, 0}); + cmap_iv_insert(&vecs, 3, (Vec3i){ 0, 0, 100}); + cmap_iv_insert(&vecs, 4, (Vec3i){100, 100, 100}); + + c_forpair (num, v3, cmap_iv, vecs) + printf("%d: { %3d, %3d, %3d }\n", *_.num, _.v3->x, _.v3->y, _.v3->z); + + cmap_iv_drop(&vecs); } ``` Output: @@ -300,35 +299,27 @@ static inline void Viking_drop(Viking* vk) { #define i_type Vikings #define i_keyclass Viking #define i_val int -/* - i_keyclass implies these defines, unless they are already defined: - #define i_cmp Viking_cmp - #define i_hash Viking_hash - #define i_keyclone Viking_clone - #define i_keydrop Viking_drop -*/ #include <stc/cmap.h> int main() { // Use a HashMap to store the vikings' health points. - c_auto (Vikings, vikings) // uses Vikings_init(), Vikings_drop() - { - Vikings_insert(&vikings, (Viking){cstr_lit("Einar"), cstr_lit("Norway")}, 25); - Vikings_insert(&vikings, (Viking){cstr_lit("Olaf"), cstr_lit("Denmark")}, 24); - Vikings_insert(&vikings, (Viking){cstr_lit("Harald"), cstr_lit("Iceland")}, 12); - Vikings_insert(&vikings, (Viking){cstr_lit("Einar"), cstr_lit("Denmark")}, 21); - - c_auto (Viking, lookup) { - lookup = (Viking){cstr_lit("Einar"), cstr_lit("Norway")}; - printf("Lookup: Einar of Norway has %d hp\n\n", *Vikings_at(&vikings, lookup)); - } - - // Print the status of the vikings. - c_forpair (vik, hp, Vikings, vikings) { - printf("%s of %s has %d hp\n", cstr_str(&_.vik->name), cstr_str(&_.vik->country), *_.hp); - } + Vikings vikings = {0}; + + Vikings_insert(&vikings, (Viking){cstr_lit("Einar"), cstr_lit("Norway")}, 25); + Vikings_insert(&vikings, (Viking){cstr_lit("Olaf"), cstr_lit("Denmark")}, 24); + Vikings_insert(&vikings, (Viking){cstr_lit("Harald"), cstr_lit("Iceland")}, 12); + Vikings_insert(&vikings, (Viking){cstr_lit("Einar"), cstr_lit("Denmark")}, 21); + + Viking lookup = (Viking){cstr_lit("Einar"), cstr_lit("Norway")}; + printf("Lookup: Einar of Norway has %d hp\n\n", *Vikings_at(&vikings, lookup)); + Viking_drop(&lookup); + + // Print the status of the vikings. + c_forpair (vik, hp, Vikings, vikings) { + printf("%s of %s has %d hp\n", cstr_str(&_.vik->name), cstr_str(&_.vik->country), *_.hp); } + Vikings_drop(&vikings); } ``` Output: @@ -384,30 +375,22 @@ static inline RViking Viking_toraw(const Viking* vp) { #define i_hash(rp) (cstrhash(rp->name) ^ cstrhash(rp->country)) #define i_val int #include <stc/cmap.h> -/* - i_keyclass implies these defines, unless they are already defined: - #define i_cmp RViking_cmp - //#define i_hash RViking_hash // already defined above. - //#define i_keyclone Viking_clone // not used because c_no_clone - #define i_keyto Viking_toraw // because i_keyraw type is defined - #define i_keydrop Viking_drop -*/ int main() { - c_auto (Vikings, vikings) - { - 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); - - Vikings_value *v = Vikings_get_mut(&vikings, (RViking){"Einar", "Norway"}); - if (v) v->second += 3; // add 3 hp points to Einar - - c_forpair (vk, hp, Vikings, vikings) { - printf("%s of %s has %d hp\n", cstr_str(&_.vk->name), cstr_str(&_.vk->country), *_.hp); - } + Vikings vikings = {0}; + + 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); + + Vikings_value *v = Vikings_get_mut(&vikings, (RViking){"Einar", "Norway"}); + if (v) v->second += 3; // add 3 hp points to Einar + + c_forpair (vk, hp, Vikings, vikings) { + printf("%s of %s has %d hp\n", cstr_str(&_.vk->name), cstr_str(&_.vk->country), *_.hp); } + Vikings_drop(&vikings); } ``` |
