Update examples / adding more comments.
This commit is contained in:
1 parent
03eaceaf35
commit
890486d181
4 files changed
+38
-3
No files matched your search
@@ -22,12 +22,14 @@
|
|||||||
*/
|
*/
|
||||||
#include "m-array.h"
|
#include "m-array.h"
|
||||||
|
|
||||||
|
// Let's define an array of 'char *' named 'charp'
|
||||||
ARRAY_DEF (charp, char *)
|
ARRAY_DEF (charp, char *)
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
// Create an array list
|
// Create an array list
|
||||||
array_charp_t al;
|
array_charp_t al;
|
||||||
|
// Initialize it
|
||||||
array_charp_init(al);
|
array_charp_init(al);
|
||||||
|
|
||||||
// add elements to the array list
|
// add elements to the array list
|
||||||
@@ -55,6 +57,7 @@ int main(void)
|
|||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
|
// Clear the array
|
||||||
array_charp_clear(al);
|
array_charp_clear(al);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
+5
-3
@@ -22,11 +22,12 @@
|
|||||||
*/
|
*/
|
||||||
#include "m-array.h"
|
#include "m-array.h"
|
||||||
|
|
||||||
|
// Let's define an array of 'char *' named 'charp'
|
||||||
ARRAY_DEF (charp, char *)
|
ARRAY_DEF (charp, char *)
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
// Create an array list
|
// Create an array list 'al'
|
||||||
M_LET (al, ARRAY_OPLIST(charp)) {
|
M_LET (al, ARRAY_OPLIST(charp)) {
|
||||||
// add elements to the array list
|
// add elements to the array list
|
||||||
array_charp_push_back (al,"C");
|
array_charp_push_back (al,"C");
|
||||||
@@ -38,10 +39,11 @@ int main(void)
|
|||||||
|
|
||||||
// Display contents of al
|
// Display contents of al
|
||||||
printf("Original contents of al: ");
|
printf("Original contents of al: ");
|
||||||
|
// For each element of the array al
|
||||||
for M_EACH(element, al, ARRAY_OPLIST(charp)) {
|
for M_EACH(element, al, ARRAY_OPLIST(charp)) {
|
||||||
printf ("%s ", *element);
|
printf ("%s ", *element);
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
} // After this point al is automatically cleared
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -1,27 +1,34 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "m-array.h"
|
#include "m-array.h"
|
||||||
|
|
||||||
|
// Let's define an array of 'int' named 'int'
|
||||||
ARRAY_DEF(int, int)
|
ARRAY_DEF(int, int)
|
||||||
|
|
||||||
const int n = 100000;
|
const int n = 100000;
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
|
// Declare and initilize the array
|
||||||
array_int_t x;
|
array_int_t x;
|
||||||
array_int_init(x);
|
array_int_init(x);
|
||||||
|
|
||||||
|
// Push some integers in the array
|
||||||
for (int i = 0; i < n; i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
array_int_push_back(x, rand());
|
array_int_push_back(x, rand());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Insert some integers in the array.
|
||||||
for (int i = 0; i < n; i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
array_int_push_at (x, rand() % n, rand());
|
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++) {
|
for (int i = 0; i < n; i++) {
|
||||||
array_int_pop_at (NULL, x, rand() % n);
|
array_int_pop_at (NULL, x, rand() % n);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clear the array
|
||||||
array_int_clear(x);
|
array_int_clear(x);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -4,27 +4,50 @@
|
|||||||
#include "m-dict.h"
|
#include "m-dict.h"
|
||||||
#include "m-string.h"
|
#include "m-string.h"
|
||||||
|
|
||||||
|
// Define a dictionnary of string --> int
|
||||||
|
// named 'str'
|
||||||
DICT_DEF2(str, string_t, STRING_OPLIST, int, M_DEFAULT_OPLIST)
|
DICT_DEF2(str, string_t, STRING_OPLIST, int, M_DEFAULT_OPLIST)
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
|
// Declare the dictionnary 'h'
|
||||||
dict_str_t h;
|
dict_str_t h;
|
||||||
int max = 1;
|
int max = 1;
|
||||||
|
// Declare the string 'key'
|
||||||
string_t key;
|
string_t key;
|
||||||
|
|
||||||
|
// Initialize the dictionnary & the string
|
||||||
dict_str_init(h);
|
dict_str_init(h);
|
||||||
string_init (key);
|
string_init (key);
|
||||||
|
|
||||||
|
// While there is some data in the stream
|
||||||
while (!feof(stdin))
|
while (!feof(stdin))
|
||||||
{
|
{
|
||||||
|
// Read a complete line from the stream and fill in the string 'key'
|
||||||
string_fgets (key, stdin, STRING_READ_PURE_LINE);
|
string_fgets (key, stdin, STRING_READ_PURE_LINE);
|
||||||
|
// If the 'key' exists in the dictionnary
|
||||||
int *v = dict_str_get (h, key);
|
int *v = dict_str_get (h, key);
|
||||||
if (v != NULL) {
|
if (v != NULL) {
|
||||||
|
// Increment the counter in the dictionnary
|
||||||
(*v)++;
|
(*v)++;
|
||||||
max = M_MAX(*v, max);
|
max = M_MAX(*v, max);
|
||||||
} else {
|
} else {
|
||||||
|
// Update the dictionnary with h[key] = 1
|
||||||
dict_str_set_at(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);
|
string_clear(key);
|
||||||
dict_str_clear(h);
|
dict_str_clear(h);
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
Reference in new issue
Block a user