Fix the example
This commit is contained in:
1 parent
a3ece09e66
commit
738123d4a7
1 file changed
+11
-10
+11
-10
@@ -4,44 +4,45 @@
|
|||||||
#include "m-dict.h"
|
#include "m-dict.h"
|
||||||
#include "m-string.h"
|
#include "m-string.h"
|
||||||
|
|
||||||
// Define a dictionnary of string --> int
|
// Define a dictionary of string --> int
|
||||||
// named 'dict_str'
|
// named 'dict_str'
|
||||||
DICT_DEF2(dict_str, string_t, STRING_OPLIST, int, M_BASIC_OPLIST)
|
DICT_DEF2(dict_str, string_t, STRING_OPLIST, int, M_BASIC_OPLIST)
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
// Declare the dictionnary 'h'
|
// Declare the dictionary 'h'
|
||||||
dict_str_t h;
|
dict_str_t h;
|
||||||
int max = 1;
|
int max = 1;
|
||||||
// Declare the string 'key'
|
// Declare the string 'key'
|
||||||
string_t key;
|
string_t key;
|
||||||
|
|
||||||
// Initialize the dictionnary & the string
|
// Initialize the dictionary & 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 there is some data in the stream
|
||||||
while (!feof(stdin))
|
while (true)
|
||||||
{
|
{
|
||||||
// Read a complete line from the stream and fill in the string 'key'
|
// 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
|
if (feof(stdin)) break;
|
||||||
|
// If the 'key' exists in the dictionary
|
||||||
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
|
// Increment the counter in the dictionary
|
||||||
(*v)++;
|
(*v)++;
|
||||||
max = M_MAX(*v, max);
|
max = M_MAX(*v, max);
|
||||||
} else {
|
} else {
|
||||||
// Update the dictionnary with h[key] = 1
|
// Update the dictionary with h[key] = 1
|
||||||
dict_str_set_at(h, key, 1);
|
dict_str_set_at(h, key, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
printf ("Max occurence = %d\n", max);
|
printf ("Max occurrence = %d\n", max);
|
||||||
|
|
||||||
// Iterate on the dictionnary
|
// Iterate on the dictionary
|
||||||
dict_str_it_t it;
|
dict_str_it_t it;
|
||||||
for(dict_str_it(it, h); !dict_str_end_p(it); dict_str_next(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
|
// Get a constant reference on the pair stored in the dictionary
|
||||||
const struct dict_str_pair_s *pair = dict_str_cref(it);
|
const struct dict_str_pair_s *pair = dict_str_cref(it);
|
||||||
printf ("Key=%s Value=%d\n",
|
printf ("Key=%s Value=%d\n",
|
||||||
string_get_cstr(pair->key), pair->value);
|
string_get_cstr(pair->key), pair->value);
|
||||||
|
|||||||
Reference in new issue
Block a user