Update examples / adding more comments.

This commit is contained in:
Patrick Pelissier committed 2017-03-05 09:27:22 +01:00
1 parent 03eaceaf35
commit 890486d181
4 files changed
+42 -7

No files matched your search

+3
View File
@@ -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;
}
+5 -3
View File
@@ -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;
}
+11 -4
View File
@@ -1,27 +1,34 @@
#include <stdlib.h>
#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;
}
+23
View File
@@ -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;