diff options
| author | Tyge Løvset <[email protected]> | 2022-06-01 16:28:07 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2022-06-01 16:28:07 +0200 |
| commit | de629774cb912aa3d563f24d99258142713c3fcd (patch) | |
| tree | c37e2851d6cb049bc0863a59b6ecf5945fb88619 /examples/mapmap.c | |
| parent | 7fb43a24a17da787dd809114ca26c1231b058493 (diff) | |
| download | STC-modified-de629774cb912aa3d563f24d99258142713c3fcd.tar.gz STC-modified-de629774cb912aa3d563f24d99258142713c3fcd.zip | |
Converted all files with DOS line endings to LINUX.
Diffstat (limited to 'examples/mapmap.c')
| -rw-r--r-- | examples/mapmap.c | 146 |
1 files changed, 73 insertions, 73 deletions
diff --git a/examples/mapmap.c b/examples/mapmap.c index 9ac22371..ab48ecc6 100644 --- a/examples/mapmap.c +++ b/examples/mapmap.c @@ -1,73 +1,73 @@ -// unordered_map<string, unordered_map<string, string>>:
-#define i_implement
-#include <stc/cstr.h>
-#define i_type People
-#define i_key_str
-#define i_val_str
-#define i_keydrop(p) (printf("kdrop: %s\n", cstr_str(p)), cstr_drop(p)) // override
-#include <stc/csmap.h>
-
-#define i_type Departments
-#define i_key_str
-#define i_val_bind People
-// Shorthand for:
-// #define i_val People
-// #define i_cmp People_cmp
-// #define i_valclone People_clone
-// #define i_valdrop People_drop
-#include <stc/csmap.h>
-
-#define i_type Stack
-#define i_val_bind People_value
-// Shorthand for:
-// #define i_val People_value (pair of cstr)
-// #define i_cmp People_value_cmp
-// #define i_valclone People_value_clone
-// #define i_valdrop People_value_drop
-#include <stc/cvec.h>
-
-void add(Departments* deps, const char* name, const char* email, const char* dep)
-{
- People *people = &Departments_insert(deps, cstr_from(dep), People_init()).ref->second;
- People_emplace_or_assign(people, name, email);
-}
-
-int contains(Departments* map, const char* name)
-{
- int count = 0;
- c_foreach (i, Departments, *map)
- if (People_contains(&i.ref->second, name))
- ++count;
- return count;
-}
-
-int main(void)
-{
- c_auto (Departments, map)
- {
- add(&map, "Anna Kendro", "[email protected]", "Support");
- add(&map, "Terry Dane", "[email protected]", "Development");
- add(&map, "Kik Winston", "[email protected]", "Finance");
- add(&map, "Nancy Drew", "[email protected]", "Development");
- add(&map, "Nick Denton", "[email protected]", "Finance");
- add(&map, "Stan Whiteword", "[email protected]", "Marketing");
- add(&map, "Serena Bath", "[email protected]", "Support");
- add(&map, "Patrick Dust", "[email protected]", "Finance");
- add(&map, "Red Winger", "[email protected]", "Marketing");
- add(&map, "Nick Denton", "[email protected]", "Support");
- add(&map, "Colin Turth", "[email protected]", "Support");
- add(&map, "Dennis Kay", "[email protected]", "Marketing");
- add(&map, "Anne Dickens", "[email protected]", "Development");
-
- c_foreach (i, Departments, map)
- c_forpair (name, email, People, i.ref->second)
- printf("%s: %s - %s\n", cstr_str(&i.ref->first), cstr_str(_.name), cstr_str(_.email));
- puts("");
-
- printf("found: %d\n", contains(&map, "Nick Denton"));
- printf("found: %d\n", contains(&map, "Patrick Dust"));
- printf("found: %d\n", contains(&map, "Dennis Kay"));
- printf("found: %d\n", contains(&map, "Serena Bath"));
- puts("Done");
- }
-}
+// unordered_map<string, unordered_map<string, string>>: +#define i_implement +#include <stc/cstr.h> +#define i_type People +#define i_key_str +#define i_val_str +#define i_keydrop(p) (printf("kdrop: %s\n", cstr_str(p)), cstr_drop(p)) // override +#include <stc/csmap.h> + +#define i_type Departments +#define i_key_str +#define i_val_bind People +// Shorthand for: +// #define i_val People +// #define i_cmp People_cmp +// #define i_valclone People_clone +// #define i_valdrop People_drop +#include <stc/csmap.h> + +#define i_type Stack +#define i_val_bind People_value +// Shorthand for: +// #define i_val People_value (pair of cstr) +// #define i_cmp People_value_cmp +// #define i_valclone People_value_clone +// #define i_valdrop People_value_drop +#include <stc/cvec.h> + +void add(Departments* deps, const char* name, const char* email, const char* dep) +{ + People *people = &Departments_insert(deps, cstr_from(dep), People_init()).ref->second; + People_emplace_or_assign(people, name, email); +} + +int contains(Departments* map, const char* name) +{ + int count = 0; + c_foreach (i, Departments, *map) + if (People_contains(&i.ref->second, name)) + ++count; + return count; +} + +int main(void) +{ + c_auto (Departments, map) + { + add(&map, "Anna Kendro", "[email protected]", "Support"); + add(&map, "Terry Dane", "[email protected]", "Development"); + add(&map, "Kik Winston", "[email protected]", "Finance"); + add(&map, "Nancy Drew", "[email protected]", "Development"); + add(&map, "Nick Denton", "[email protected]", "Finance"); + add(&map, "Stan Whiteword", "[email protected]", "Marketing"); + add(&map, "Serena Bath", "[email protected]", "Support"); + add(&map, "Patrick Dust", "[email protected]", "Finance"); + add(&map, "Red Winger", "[email protected]", "Marketing"); + add(&map, "Nick Denton", "[email protected]", "Support"); + add(&map, "Colin Turth", "[email protected]", "Support"); + add(&map, "Dennis Kay", "[email protected]", "Marketing"); + add(&map, "Anne Dickens", "[email protected]", "Development"); + + c_foreach (i, Departments, map) + c_forpair (name, email, People, i.ref->second) + printf("%s: %s - %s\n", cstr_str(&i.ref->first), cstr_str(_.name), cstr_str(_.email)); + puts(""); + + printf("found: %d\n", contains(&map, "Nick Denton")); + printf("found: %d\n", contains(&map, "Patrick Dust")); + printf("found: %d\n", contains(&map, "Dennis Kay")); + printf("found: %d\n", contains(&map, "Serena Bath")); + puts("Done"); + } +} |
