From ae032cb11e32f08258dd78c23e7624663cd25ef6 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Tue, 1 Dec 2020 21:45:19 +0100 Subject: Added some examples. --- docs/cmap_api.md | 65 +++++++++++++++++++++++++++++--------------------------- docs/cset_api.md | 50 ++++++++++++++++++++++++++++++------------- docs/cvec_api.md | 32 ++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 46 deletions(-) (limited to 'docs') diff --git a/docs/cmap_api.md b/docs/cmap_api.md index 9c90dbdb..1fb5994c 100644 --- a/docs/cmap_api.md +++ b/docs/cmap_api.md @@ -126,42 +126,45 @@ uint32_t c_default_hash16(const void *data, size_t len); uint32_t c_default_hash32(const void* data, size_t len); ``` -Example: +## Example ```c #include -#include #include +#include -using_cmap(ii, int, int); using_cmap_str(); -int main() { - // -- map of ints -- - cmap_ii nums = cmap_ii_init(); - cmap_ii_put(&nums, 8, 64); // similar to insert_or_assign() - cmap_ii_emplace(&nums, 11, 121); - - printf("%d\n", cmap_ii_find(&nums, 8)->second); - cmap_ii_del(&nums); - - // -- map of cstr_t -- - cmap_str strings = cmap_str_init(); - cmap_str_emplace(&strings, "Make", "my"); - cmap_str_emplace(&strings, "Rainy", "day"); - cmap_str_emplace(&strings, "Sunny", "afternoon"); - c_push_items(&strings, cmap_str, { {"Eleven", "XI"}, {"Six", "VI"} }); - - printf("size = %zu\n", cmap_str_size(strings)); - c_foreach (i, cmap_str, strings) - printf("%s: %s\n", i.val->first.str, i.val->second.str); - cmap_str_del(&strings); // frees all strings and map. +int main() +{ + // Create an unordered_map of three strings (that map to strings) + cmap_str u = cmap_inits; + c_push_items(&u, 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", n.val->first.str, n.val->second.str); + } + + // Add two new entries to the unordered map + cmap_str_put(&u, "BLACK", "#000000"); + cmap_str_put(&u, "WHITE", "#FFFFFF"); + + // Output values by key + printf("The HEX of color RED is:[%s]\n", cmap_str_at(&u, "RED")->str); + printf("The HEX of color BLACK is:[%s]\n", cmap_str_at(&u, "BLACK")->str); + + return 0; } -// Output: -64 -size = 5 -Rainy: day -Sunny: afternoon -Six: VI -Make: my -Eleven: XI +``` +Output: +``` +Key:[RED] Value:[#FF0000] +Key:[GREEN] Value:[#00FF00] +Key:[BLUE] Value:[#0000FF] +The HEX of color RED is:[#FF0000] +The HEX of color BLACK is:[#000000] ``` diff --git a/docs/cset_api.md b/docs/cset_api.md index 92d6afe4..f0c62c8a 100644 --- a/docs/cset_api.md +++ b/docs/cset_api.md @@ -104,25 +104,45 @@ uint32_t c_default_hash16(const void *data, size_t len); uint32_t c_default_hash32(const void* data, size_t len); ``` -Example: +## Example ```c +#include #include -#include +#include using_cset_str(); -int main() { - cset_str words = cset_str_init(); - cset_str_emplace(&words, "Hello"); - cset_str_emplace(&words, "Crazy"); - cset_str_emplace(&words, "World"); - cset_str_erase(&words, "Crazy"); - - // iterate the set of cstr_t values: - c_foreach (i, cset_str, words) - printf("%s ", i.val->str); - cset_str_del(&words); +int main () +{ + cset_str first = cset_inits; // empty + cset_str second = cset_inits; c_push_items(&second, cset_str, {"red","green","blue"}); // init list + cset_str third = cset_inits; c_push_items(&third, cset_str, {"orange","pink","yellow"}); // init list + cset_str fourth = cset_inits; + cset_str_emplace(&fourth, "potatoes"); + cset_str_emplace(&fourth, "milk"); + cset_str_emplace(&fourth, "flour"); + + cset_str fifth = cset_inits; + c_foreach (x, cset_str, second) cset_str_emplace(&fifth, x.val->str); + c_foreach (x, cset_str, third) cset_str_emplace(&fifth, x.val->str); + c_foreach (x, cset_str, fourth) cset_str_emplace(&fifth, x.val->str); + + printf("fifth contains:\n-------------\n"); + c_foreach (x, cset_str, fifth) printf("%s\n", x.val->str); + + c_del(cset_str, &first, &second, &third, &fourth, &fifth); + return 0; } -// Output: -Hello World +``` +Output: +``` +red +green +flour +orange +blue +pink +yellow +milk +potatoes ``` diff --git a/docs/cvec_api.md b/docs/cvec_api.md index 996cdb16..1a51470e 100644 --- a/docs/cvec_api.md +++ b/docs/cvec_api.md @@ -107,3 +107,35 @@ cvec_X_iter_t cvec_X_end(const cvec_X* self); void cvec_X_next(cvec_X_iter_t* it); cvec_X_value_t* cvec_X_itval(cvec_X_iter_t it); ``` + +## Example +```c +#include +#include +using_cvec(i, int); + +int main() +{ + // Create a vector containing integers + cvec_i v = cvec_inits; + c_push_items(&v, cvec_i, {7, 5, 16, 8}); + + // Add two more integers to vector + cvec_i_push_back(&v, 25); + cvec_i_push_back(&v, 13); + + // Iterate and print values of vector + c_foreach (n, cvec_i, v) { + printf("%d\n", *n.val); + } +} +``` +Output: +``` +7 +5 +16 +8 +25 +13 +``` \ No newline at end of file -- cgit v1.2.3