From e52755087766c87f7d5066c959371b8be325a68a Mon Sep 17 00:00:00 2001 From: tylo Date: Wed, 26 Aug 2020 17:55:05 +0200 Subject: Forgot a tiny "detail" in clist.h. Added example words.c --- examples/words.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ stc/clist.h | 2 +- 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 examples/words.c diff --git a/examples/words.c b/examples/words.c new file mode 100644 index 00000000..f6182cbd --- /dev/null +++ b/examples/words.c @@ -0,0 +1,82 @@ + +#include +#include +#include +#include + +declare_cvec_str(); +declare_clist_str(); +declare_cmap_str(si, int); + +typedef const char* input_t; + +int main1() +{ + clist_str lwords = clist_init; + c_push(&lwords, clist_str, c_items( + "this", "sentence", "is", "not", "a", "sentence", + "this", "sentence", "is", "a", "hoax" + )); + c_foreach (w, clist_str, lwords) { + printf("%s\n", w.item->value); + } + + + cvec_str words = cvec_init; + c_push(&words, cvec_str, c_items( + "this", "sentence", "is", "not", "a", "sentence", + "this", "sentence", "is", "a", "hoax" + )); + + cmap_si word_map = cmap_init; + c_foreach (w, cvec_str, words) { + ++cmap_si_insert(&word_map, w.item->str, 0)->value; + } + + c_foreach (pair, cmap_si, word_map) { + printf("%d occurrences of word '%s'\n", + pair.item->value, + pair.item->key.str); + } + + cmap_si_destroy(&word_map); + cvec_str_destroy(&words); + return 0; +} + +#ifdef __cplusplus +#include +#include +#include +#include + +int main2() +{ + std::vector words = { + "this", "sentence", "is", "not", "a", "sentence", + "this", "sentence", "is", "a", "hoax" + }; + + std::unordered_map word_map; + for (const auto &w : words) { + ++word_map[w]; + } + + for (const auto &pair : word_map) { + std::cout << pair.second + << " occurrences of word '" + << pair.first << "'\n"; + } + return 0; +} + +int main() { + main1(); + puts(""); + main2(); +} +#else +int main() { + main1(); +} +#endif \ No newline at end of file diff --git a/stc/clist.h b/stc/clist.h index 99a57686..5bad782a 100644 --- a/stc/clist.h +++ b/stc/clist.h @@ -182,7 +182,7 @@ } \ STC_API void \ clist_##tag##_push_n(clist_##tag *self, const clist_##tag##_input_t in[], size_t size) { \ - for (size_t i=0; i