From 890486d1818e18b57640a4a6fa4ee1a71cea7614 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Sun, 5 Mar 2017 09:27:22 +0100 Subject: [PATCH] Update examples / adding more comments. --- example/ex01.c | 3 +++ example/ex01b.c | 8 +++++--- example/ex02.c | 15 +++++++++++---- example/ex03.c | 23 +++++++++++++++++++++++ 4 files changed, 42 insertions(+), 7 deletions(-) diff --git a/example/ex01.c b/example/ex01.c index 4e0d6110..41ec7ea1 100644 --- a/example/ex01.c +++ b/example/ex01.c @@ -22,12 +22,14 @@ */ #include "m-array.h" +// Let's define an array of 'char *' named 'charp' ARRAY_DEF (charp, char *) int main(void) { // Create an array list array_charp_t al; + // Initialize it array_charp_init(al); // add elements to the array list @@ -55,6 +57,7 @@ int main(void) } printf("\n"); + // Clear the array array_charp_clear(al); return 0; } diff --git a/example/ex01b.c b/example/ex01b.c index 7e332afa..9adcbeaa 100644 --- a/example/ex01b.c +++ b/example/ex01b.c @@ -22,11 +22,12 @@ */ #include "m-array.h" +// Let's define an array of 'char *' named 'charp' ARRAY_DEF (charp, char *) int main(void) { - // Create an array list + // Create an array list 'al' M_LET (al, ARRAY_OPLIST(charp)) { // add elements to the array list array_charp_push_back (al,"C"); @@ -38,10 +39,11 @@ int main(void) // Display contents of al printf("Original contents of al: "); + // For each element of the array al for M_EACH(element, al, ARRAY_OPLIST(charp)) { printf ("%s ", *element); - } + } printf("\n"); - } + } // After this point al is automatically cleared return 0; } diff --git a/example/ex02.c b/example/ex02.c index 074181a6..0a28799c 100644 --- a/example/ex02.c +++ b/example/ex02.c @@ -1,27 +1,34 @@ #include #include "m-array.h" +// Let's define an array of 'int' named 'int' ARRAY_DEF(int, int) const int n = 100000; int main(void) { + // Declare and initilize the array array_int_t x; array_int_init(x); - + + // Push some integers in the array for (int i = 0; i < n; i++) { array_int_push_back(x, rand()); } - + + // Insert some integers in the array. for (int i = 0; i < n; i++) { array_int_push_at (x, rand() % n, rand()); } - + + // Pop some integers from integer into NULL, + // i.e. erase them. for (int i = 0; i < n; i++) { array_int_pop_at (NULL, x, rand() % n); } - + + // Clear the array array_int_clear(x); return 0; } diff --git a/example/ex03.c b/example/ex03.c index ce51a0f6..72836509 100644 --- a/example/ex03.c +++ b/example/ex03.c @@ -4,27 +4,50 @@ #include "m-dict.h" #include "m-string.h" +// Define a dictionnary of string --> int +// named 'str' DICT_DEF2(str, string_t, STRING_OPLIST, int, M_DEFAULT_OPLIST) int main(void) { + // Declare the dictionnary 'h' dict_str_t h; int max = 1; + // Declare the string 'key' string_t key; + // Initialize the dictionnary & the string dict_str_init(h); string_init (key); + + // While there is some data in the stream while (!feof(stdin)) { + // Read a complete line from the stream and fill in the string 'key' string_fgets (key, stdin, STRING_READ_PURE_LINE); + // If the 'key' exists in the dictionnary int *v = dict_str_get (h, key); if (v != NULL) { + // Increment the counter in the dictionnary (*v)++; max = M_MAX(*v, max); } else { + // Update the dictionnary with h[key] = 1 dict_str_set_at(h, key, 1); } } + printf ("Max occurence = %d\n", max); + + // Iterate on the dictionnary + dict_it_str_t it; + for(dict_str_it(it, h); !dict_str_end_p(it); dict_str_next(it)) { + // Get a constant reference on the pair stored in the dictionnary + const dict_pair_str_t *pair = dict_str_cref(it); + printf ("Key=%s Value=%d\n", + string_get_cstr((*pair)->key), (*pair)->value); + } + + // Clear variables string_clear(key); dict_str_clear(h); return 0;