From 6749cc21a2045d307c239d82891cb860687dfd2a Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Tue, 14 Dec 2021 19:50:10 +0100 Subject: Added and renamed some examples. --- examples/hashmap.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 examples/hashmap.c (limited to 'examples/hashmap.c') diff --git a/examples/hashmap.c b/examples/hashmap.c new file mode 100644 index 00000000..c3e9afce --- /dev/null +++ b/examples/hashmap.c @@ -0,0 +1,48 @@ +// https://doc.rust-lang.org/rust-by-example/std/hash.html + +#define i_key_str +#define i_val_str +#include +#include + +const char* call(const char* number) { + if (!strcmp(number, "798-1364")) + return "We're sorry, the call cannot be completed as dialed." + " Please hang up and try again."; + else if (!strcmp(number, "645-7689")) + return "Hello, this is Mr. Awesome's Pizza. My name is Fred." + " What can I get for you today?"; + else + return "Hi! Who is this again?"; +} + +int main(void) { + c_auto (cmap_str, contacts) + { + cmap_str_emplace(&contacts, "Daniel", "798-1364"); + cmap_str_emplace(&contacts, "Ashley", "645-7689"); + cmap_str_emplace(&contacts, "Katie", "435-8291"); + cmap_str_emplace(&contacts, "Robert", "956-1745"); + + const cmap_str_value* v; + if ((v = cmap_str_get(&contacts, "Daniel"))) + printf("Calling Daniel: %s\n", call(v->second.str)); + else + printf("Don't have Daniel's number."); + + cmap_str_emplace(&contacts, "Daniel", "164-6743"); + + if ((v = cmap_str_get(&contacts, "Ashley"))) + printf("Calling Ashley: %s\n", call(v->second.str)); + else + printf("Don't have Ashley's number."); + + cmap_str_erase(&contacts, "Ashley"); + + puts(""); + c_forpair (contact, number, cmap_str, contacts) { + printf("Calling %s: %s\n", _.contact.str, call(_.number.str)); + } + puts(""); + } +} -- cgit v1.2.3