Add a new example of the new M_LET feature.

This commit is contained in:
Patrick Pelissier committed 2021-07-01 22:12:15 +02:00
1 parent 3d3eb6f1d2
commit 08668aa02b
1 file changed
+40
+40
View File
@@ -0,0 +1,40 @@
#include <stdio.h>
#include "m-string.h"
#include "m-dict.h"
#include "m-array.h"
/* Definition of an associative map string_t --> size_t */
DICT_DEF2(dict_str, string_t, size_t)
#define M_OPL_dict_str_t() DICT_OPLIST(dict_str, STRING_OPLIST, M_DEFAULT_OPLIST)
/* Definition of an array of string_t */
ARRAY_DEF(vector_str, string_t)
#define M_OPL_vector_str_t() ARRAY_OPLIST(vector_str, STRING_OPLIST)
int main(void)
{
// Construct an array of string, performing a convertion of the C const char *
// into a proper string_t at real time and push then into a dynamic array
// that is declared, initialized and cleared.
M_LET( (words, ("This"), ("is"), ("a"), ("useless"), ("sentence"), ("."),
("It"), ("is"), ("used"), ("a"), ("bit"), ("to"), ("count"), ("words"), (".") ),
vector_str_t) {
// Print the arrays.
printf("The words are: ");
vector_str_out_str(stdout, words);
printf("\n");
// Count the words.
M_LET(map, dict_str_t) {
// Count the words:
for M_EACH(w, words, vector_str_t) {
(*dict_str_get_at(map, *w)) ++;
}
// Print the count:
for M_EACH(p, map, dict_str_t) {
printf ("%zu occurences of %s\n", p->value, string_get_cstr(p->key));
}
}
}
}