From e3ff67eaac8dfba081cd72bb3a8fdc448fbd7494 Mon Sep 17 00:00:00 2001 From: Tyge Løvset <60263450+tylo-work@users.noreply.github.com> Date: Thu, 5 Mar 2020 12:34:47 +0100 Subject: Update README.md --- README.md | 55 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 1874a962..6d48adaf 100644 --- a/README.md +++ b/README.md @@ -45,42 +45,49 @@ Simple CMap, int -> int: #include "cmap.h" declare_CMap(ii, int, int); -CMap_ii nums = cmap_initializer; -cmap_ii_put(&nums, 8, 64); -cmap_ii_put(&nums, 11, 121); -printf("%d\n", cmap_ii_get(nums, 8)->value); -cmap_ii_destroy(&nums); +int main() { + CMap_ii nums = cmap_initializer; + cmap_ii_put(&nums, 8, 64); + cmap_ii_put(&nums, 11, 121); + printf("%d\n", cmap_ii_get(nums, 8)->value); + cmap_ii_destroy(&nums); +} ``` Simple CMap with CString keys -> int values ``` +#include "cstring.h" #include "cmap.h" declare_CMap_StringKey(si, int); // Just a shorthand macro for the general declare call. // Keys strings are "magically" managed internally, although CMap is ignorant of CString. -CMap_si nums = cmap_initializer; -cmap_si_put(&nums, "Hello", 64); -cmap_si_put(&nums, "Groovy", 121); -cmap_si_put(&nums, "Groovy", 200); // overwrite previous -// iterate the map: -for (cmap_si_iter_t i = cmap_si_begin(nums); i.item != cmap_si_end(nums).item; i = cmap_si_next(i)) - printf("%s: %d\n", i.item->key.str, i.item->value); - -// or rather use the short form: -cforeach (i, cmap_si, nums) - printf("%s: %d, changed: %s\n", i.item->key.str, i.item->value, i.item->changed ? "yes" : "no"); +int main() { + CMap_si nums = cmap_initializer; + cmap_si_put(&nums, "Hello", 64); + cmap_si_put(&nums, "Groovy", 121); + cmap_si_put(&nums, "Groovy", 200); // overwrite previous + // iterate the map: + for (cmap_si_iter_t i = cmap_si_begin(nums); i.item != cmap_si_end(nums).item; i = cmap_si_next(i)) + printf("%s: %d\n", i.item->key.str, i.item->value); + // or rather use the short form: + cforeach (i, cmap_si, nums) + printf("%s: %d, changed: %s\n", i.item->key.str, i.item->value, i.item->changed ? "yes" : "no"); -cmap_si_destroy(&nums); + cmap_si_destroy(&nums); +} ``` CMap with CString keys -> CString values. Temporary values are created by "make", and moved to the container. ``` +#include "cstring.h" #include "cmap.h" declare_CMap_StringKey(ss, CString, cstring_destroy); -CMap_ss table = cmap_initializer; -cmap_ss_put(&table, "Hello", cstring_make("Goodbye")); -cmap_ss_put(&table, "Groovy", cstring_make("Shaky")); -printf("Groovy: %s\n", cmap_ss_get(table, "Groovy")->value.str); -cmap_ss_erase(&table, "Hello"); -printf("size %d\n", cmap_size(table)); -cmap_ss_destroy(&table); // frees key and value CStrings, and hash table (CVector). +int main() { + CMap_ss table = cmap_initializer; + cmap_ss_put(&table, "Hello", cstring_make("Goodbye")); + cmap_ss_put(&table, "Groovy", cstring_make("Shaky")); + printf("Groovy: %s\n", cmap_ss_get(table, "Groovy")->value.str); + cmap_ss_erase(&table, "Hello"); + printf("size %d\n", cmap_size(table)); + cmap_ss_destroy(&table); // frees key and value CStrings, and hash table (CVector). +} ``` -- cgit v1.2.3