diff options
| -rw-r--r-- | README.md | 55 |
1 files changed, 31 insertions, 24 deletions
@@ -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). +} ``` |
