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
63
64
65
66
67
68
69
70
71
72
|
// This implements the c++ std::map::find example at:
// https://docs.microsoft.com/en-us/cpp/standard-library/map-class?view=msvc-160#example-17
#include <stc/cstr.h>
#define i_tag istr
#define i_key int
#define i_val_str
#include <stc/csmap.h>
#define i_tag istr
#define i_val csmap_istr_rawvalue_t
#define i_cmp c_no_compare
#include <stc/cvec.h>
void print_elem(csmap_istr_rawvalue_t p) {
printf("(%d, %s) ", p.first, p.second);
}
#define using_print_collection(CX) \
void print_collection_##CX(CX t) { \
printf("%zu elements: ", CX##_size(t)); \
\
c_foreach (p, CX, t) { \
print_elem(CX##_value_toraw(p.ref)); \
} \
puts(""); \
}
using_print_collection(csmap_istr)
using_print_collection(cvec_istr)
void findit(csmap_istr c, csmap_istr_key_t val)
{
printf("Trying find() on value %d\n", val);
csmap_istr_value_t* result = csmap_istr_get(&c, val); // easier with get than find.
if (result) {
printf("Element found: "); print_elem(csmap_istr_value_toraw(result)); puts("");
} else {
puts("Element not found.");
}
}
int main()
{
c_auto (csmap_istr, m1)
c_auto (cvec_istr, v)
{
c_apply_pair(csmap_istr, emplace, &m1, {{40, "Zr"}, {45, "Rh"}});
puts("The starting map m1 is (key, value):");
print_collection_csmap_istr(m1);
typedef cvec_istr_value_t pair;
cvec_istr_emplace_back(&v, (pair){43, "Tc"});
cvec_istr_emplace_back(&v, (pair){41, "Nb"});
cvec_istr_emplace_back(&v, (pair){46, "Pd"});
cvec_istr_emplace_back(&v, (pair){42, "Mo"});
cvec_istr_emplace_back(&v, (pair){44, "Ru"});
cvec_istr_emplace_back(&v, (pair){44, "Ru"}); // attempt a duplicate
puts("Inserting the following vector data into m1:");
print_collection_cvec_istr(v);
c_foreach (i, cvec_istr, v) csmap_istr_emplace(&m1, i.ref->first, i.ref->second);
puts("The modified map m1 is (key, value):");
print_collection_csmap_istr(m1);
puts("");
findit(m1, 45);
findit(m1, 6);
}
}
|