1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
// https://doc.rust-lang.org/std/collections/struct.HashMap.html
#define i_implement
#include <stc/cstr.h>
#define i_key_str
#define i_val_str
#include <stc/cmap.h>
// Type inference lets us omit an explicit type signature (which
// would be `HashMap<String, String>` in this example).
int main(void)
{
cmap_str book_reviews = {0};
// Review some books.
cmap_str_emplace(&book_reviews,
"Adventures of Huckleberry Finn",
"My favorite book."
);
cmap_str_emplace(&book_reviews,
"Grimms' Fairy Tales",
"Masterpiece."
);
cmap_str_emplace(&book_reviews,
"Pride and Prejudice",
"Very enjoyable"
);
cmap_str_insert(&book_reviews,
cstr_lit("The Adventures of Sherlock Holmes"),
cstr_lit("Eye lyked it alot.")
);
// Check for a specific one.
// When collections store owned values (String), they can still be
// queried using references (&str).
if (cmap_str_contains(&book_reviews, "Les Misérables")) {
printf("We've got %" c_ZI " reviews, but Les Misérables ain't one.",
cmap_str_size(&book_reviews));
}
// oops, this review has a lot of spelling mistakes, let's delete it.
cmap_str_erase(&book_reviews, "The Adventures of Sherlock Holmes");
// Look up the values associated with some keys.
const char* to_find[] = {"Pride and Prejudice", "Alice's Adventure in Wonderland"};
c_forrange (i, c_arraylen(to_find)) {
const cmap_str_value* b = cmap_str_get(&book_reviews, to_find[i]);
if (b)
printf("%s: %s\n", cstr_str(&b->first), cstr_str(&b->second));
else
printf("%s is unreviewed.\n", to_find[i]);
}
// Look up the value for a key (will panic if the key is not found).
printf("Review for Jane: %s\n", cstr_str(cmap_str_at(&book_reviews, "Pride and Prejudice")));
// Iterate over everything.
c_forpair (book, review, cmap_str, book_reviews) {
printf("%s: \"%s\"\n", cstr_str(_.book), cstr_str(_.review));
}
cmap_str_drop(&book_reviews);
}
|