summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/mmap.c
diff options
context:
space:
mode:
Diffstat (limited to 'misc/examples/mmap.c')
-rw-r--r--misc/examples/mmap.c58
1 files changed, 25 insertions, 33 deletions
diff --git a/misc/examples/mmap.c b/misc/examples/mmap.c
index 3934cf26..0394a2df 100644
--- a/misc/examples/mmap.c
+++ b/misc/examples/mmap.c
@@ -4,14 +4,12 @@
// Multimap entries
#include <stc/cstr.h>
#define i_val_str
-//#define i_valdrop(x) (printf("drop %s\n", cstr_str(x)), cstr_drop(x))
-#define i_extern // define _clist_mergesort() once
#include <stc/clist.h>
// Map of int => clist_str.
#define i_type Multimap
#define i_key int
-#define i_valclass clist_str // uses clist_str as i_val and binds clist_str_clone, clist_str_drop
+#define i_valclass clist_str // set i_val = clist_str, bind clist_str_clone and clist_str_drop
#define i_cmp -c_default_cmp // like std::greater<int>
#include <stc/csmap.h>
@@ -33,40 +31,34 @@ void insert(Multimap* mmap, int key, const char* str)
int main()
{
- c_auto (Multimap, mmap)
- {
- typedef struct {int a; const char* b;} pair;
+ Multimap mmap = {0};
- // list-initialize
- c_forlist (i, pair, {{2, "foo"}, {2, "bar"}, {3, "baz"}, {1, "abc"}, {5, "def"}})
- insert(&mmap, i.ref->a, i.ref->b);
- print("#1", mmap);
+ // list-initialize
+ typedef struct {int a; const char* b;} pair;
+ c_forlist (i, pair, {{2, "foo"}, {2, "bar"}, {3, "baz"}, {1, "abc"}, {5, "def"}})
+ insert(&mmap, i.ref->a, i.ref->b);
+ print("#1", mmap);
- // insert using value_type
- insert(&mmap, 5, "pqr");
- print("#2", mmap);
+ // insert using value_type
+ insert(&mmap, 5, "pqr");
+ print("#2", mmap);
- // insert using make_pair
- insert(&mmap, 6, "uvw");
- print("#3", mmap);
+ // insert using make_pair
+ insert(&mmap, 6, "uvw");
+ print("#3", mmap);
- insert(&mmap, 7, "xyz");
- print("#4", mmap);
+ insert(&mmap, 7, "xyz");
+ print("#4", mmap);
- // insert using initialization_list
- c_forlist (i, pair, {{5, "one"}, {5, "two"}})
- insert(&mmap, i.ref->a, i.ref->b);
- print("#5", mmap);
+ // insert using initialization_list
+ c_forlist (i, pair, {{5, "one"}, {5, "two"}})
+ insert(&mmap, i.ref->a, i.ref->b);
+ print("#5", mmap);
- // FOLLOWING NOT IN ORIGINAL EXAMPLE:
- // erase all entries with key 5
- Multimap_erase(&mmap, 5);
- print("+5", mmap);
-
-
- Multimap_clear(&mmap);
- c_forlist (i, pair, {{1, "ä"}, {2, "ё"}, {2, "ö"}, {3, "ü"}})
- insert(&mmap, i.ref->a, i.ref->b);
- print("#6", mmap);
- }
+ // FOLLOWING NOT IN ORIGINAL EXAMPLE:
+ // erase all entries with key 5
+ Multimap_erase(&mmap, 5);
+ print("+5", mmap);
+
+ Multimap_drop(&mmap);
}