Files
mlib/example/ex-dict02.c
T
Patrick Pelissier 8914d336a0 dict_ref & dict_cref returns a pointer to struct pair_s instead of pair_t (which
is an array of struct pair_s).
This will break API compatibility. However:
it makes the user code cleaner as a level of indirection is removed.
it uniformizes with the OA dict version
2017-10-18 23:25:39 +02:00

47 lines
1.7 KiB
C

#include "m-dict.h"
// Let's define a dictionary of 'unsigned int' --> 'char
DICT_DEF2(dict_m32, unsigned int, M_DEFAULT_OPLIST, char, M_DEFAULT_OPLIST)
// Let's create a synonym for its oplist.
#define M32_OPLIST DICT_OPLIST(dict_m32, M_DEFAULT_OPLIST, M_DEFAULT_OPLIST)
#if 1 // Non macro version.
int main(void) {
dict_m32_t h; // Declare h as a dictionary
dict_m32_init(h); // h is initialized.
dict_m32_set_at (h, 5, 10); // h[5] = 10
char *k = dict_m32_get(h, 10); // No 10 in h, so k == NULL
int is_missing = (k != NULL); // true
assert (is_missing);
dict_m32_remove(h, 5); // h is now empty
dict_m32_it_t it; // iterate over all dictionnary
for (dict_m32_it (it, h) ; !dict_m32_end_p (it); dict_m32_next(it)) {
struct dict_m32_pair_s *item = dict_m32_ref(it);
item->value = 1; // Set its value to 1
}
dict_m32_clear(h); // h is cleared
return 0;
}
#else // Use of M_LET & M_FOR macros (otherwise equivalent)
int main(void) {
M_LET(h, M32_OPLIST) { // h is init
dict_m32_set_at (h, 5, 10); // h[5] = 10
char *k = dict_m32_get(h, 10); // k == NULL
int is_missing = (k != NULL); // true
assert (is_missing);
dict_m32_remove(h, 5); // h is now empty
for M_EACH(item, h, M32_OPLIST) { // traverse each item
item->value = 1; // Set its value to 1
}
} // h is cleared
return 0;
}
#endif