summaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2021-05-19 11:50:40 +0200
committerTyge Løvset <[email protected]>2021-05-19 11:50:40 +0200
commita9ad1ad0d4f4d6b8f702b2a14bc9a508829da9ff (patch)
tree2b5eee8e0747a706c57133fab773d43358df4635 /examples
parent353ed880f92403d29c32af8d36c4bb8b0cb9c821 (diff)
downloadSTC-modified-a9ad1ad0d4f4d6b8f702b2a14bc9a508829da9ff.tar.gz
STC-modified-a9ad1ad0d4f4d6b8f702b2a14bc9a508829da9ff.zip
More cleanups and refinements.
Diffstat (limited to 'examples')
-rw-r--r--examples/cpque.c14
-rw-r--r--examples/demos.c26
-rw-r--r--examples/ex_gauss1.c35
-rw-r--r--examples/ex_gauss2.c18
-rw-r--r--examples/list_erase.c3
-rw-r--r--examples/multimap.c (renamed from examples/olympics.c)32
6 files changed, 68 insertions, 60 deletions
diff --git a/examples/cpque.c b/examples/cpque.c
index f9dc871c..d43ddef8 100644
--- a/examples/cpque.c
+++ b/examples/cpque.c
@@ -31,24 +31,22 @@ int main()
{
const int data[] = {1,8,5,6,3,4,0,9,7,2};
- c_withvar (cpque_imax, q)
+ c_withvar (cpque_imax, q) // init() and defered del()
+ c_withvar (cpque_imin, q2)
+ c_withvar (cpque_imix, q3)
{
c_forrange (n, c_arraylen(data))
cpque_imax_push(&q, n);
+
print_cpque_imax(q);
- }
- c_withvar (cpque_imin, q2)
- {
cpque_imin_emplace_items(&q2, data, c_arraylen(data));
print_cpque_imin(q2);
- }
- // Using imix_less to compare elements.
- c_withvar (cpque_imix, q3)
- {
+ // Using imix_less to compare elements.
c_forrange (n, c_arraylen(data))
cpque_imix_push(&q3, n);
+
print_cpque_imix(q3);
}
} \ No newline at end of file
diff --git a/examples/demos.c b/examples/demos.c
index 388e3e46..91daf1b1 100644
--- a/examples/demos.c
+++ b/examples/demos.c
@@ -64,7 +64,7 @@ using_cvec_str();
void vectordemo2()
{
printf("\nVECTORDEMO2\n");
- c_withvar (cvec_str, names) {
+ c_with (cvec_str names = cvec_str_init(), cvec_str_del(&names)) {
cvec_str_emplace_back(&names, "Mary");
cvec_str_emplace_back(&names, "Joe");
cvec_str_emplace_back(&names, "Chris");
@@ -142,20 +142,20 @@ using_cmap_strkey(si, int); // Shorthand macro for the general using_cmap expans
void mapdemo2()
{
printf("\nMAPDEMO2\n");
- cmap_si nums = cmap_si_init();
- cmap_si_emplace_or_assign(&nums, "Hello", 64);
- cmap_si_emplace_or_assign(&nums, "Groovy", 121);
- cmap_si_emplace_or_assign(&nums, "Groovy", 200); // overwrite previous
-
- // iterate the map:
- for (cmap_si_iter_t i = cmap_si_begin(&nums); i.ref != cmap_si_end(&nums).ref; cmap_si_next(&i))
- printf("long: %s: %d\n", i.ref->first.str, i.ref->second);
+ c_with (cmap_si nums = cmap_si_init(), cmap_si_del(&nums))
+ {
+ cmap_si_emplace_or_assign(&nums, "Hello", 64);
+ cmap_si_emplace_or_assign(&nums, "Groovy", 121);
+ cmap_si_emplace_or_assign(&nums, "Groovy", 200); // overwrite previous
- // or rather use the short form:
- c_foreach (i, cmap_si, nums)
- printf("short: %s: %d\n", i.ref->first.str, i.ref->second);
+ // iterate the map:
+ for (cmap_si_iter_t i = cmap_si_begin(&nums); i.ref != cmap_si_end(&nums).ref; cmap_si_next(&i))
+ printf("long: %s: %d\n", i.ref->first.str, i.ref->second);
- cmap_si_del(&nums);
+ // or rather use the short form:
+ c_foreach (i, cmap_si, nums)
+ printf("short: %s: %d\n", i.ref->first.str, i.ref->second);
+ }
}
diff --git a/examples/ex_gauss1.c b/examples/ex_gauss1.c
index 4ab9e7a3..9b9285e6 100644
--- a/examples/ex_gauss1.c
+++ b/examples/ex_gauss1.c
@@ -29,29 +29,28 @@ int main()
stc64_t rng = stc64_init(seed);
stc64_normalf_t dist = stc64_normalf_init(Mean, StdDev);
- c_withvar (cvec_e, vhist) {
+ // Create and init histogram vec and map with defered destructors:
+ c_withvar (cvec_e, vhist)
+ c_withvar (cmap_i, mhist)
+ {
+ c_forrange (N) {
+ int index = (int) round( stc64_normalf(&rng, &dist) );
+ cmap_i_emplace(&mhist, index, 0).ref->second += 1;
+ }
- // Create histogram map
- c_withvar (cmap_i, mhist) {
- c_forrange (N) {
- int index = (int) round( stc64_normalf(&rng, &dist) );
- cmap_i_emplace(&mhist, index, 0).ref->second += 1;
- }
+ // Transfer map to vec and sort it by map keys.
+ c_foreach (i, cmap_i, mhist)
+ cvec_e_push_back(&vhist, c_make(mapval){i.ref->first, i.ref->second});
- // Transfer map to vec and sort it by map keys.
- c_foreach (i, cmap_i, mhist)
- cvec_e_push_back(&vhist, c_make(mapval){i.ref->first, i.ref->second});
- }
cvec_e_sort(&vhist);
// Print the gaussian bar chart
- c_withvar (cstr, bar) {
- c_foreach (i, cvec_e, vhist) {
- size_t n = (size_t) (i.ref->second * StdDev * Scale * 2.5 / (float)N);
- if (n > 0) {
- cstr_resize(&bar, n, '*');
- printf("%4d %s\n", i.ref->first, bar.str);
- }
+ c_with (cstr bar = cstr_null, cstr_del(&bar))
+ c_foreach (i, cvec_e, vhist) {
+ size_t n = (size_t) (i.ref->second * StdDev * Scale * 2.5 / (float)N);
+ if (n > 0) {
+ cstr_resize(&bar, n, '*');
+ printf("%4d %s\n", i.ref->first, bar.str);
}
}
}
diff --git a/examples/ex_gauss2.c b/examples/ex_gauss2.c
index bcf1e200..9ff42ebe 100644
--- a/examples/ex_gauss2.c
+++ b/examples/ex_gauss2.c
@@ -20,21 +20,21 @@ int main()
stc64_t rng = stc64_init(seed);
stc64_normalf_t dist = stc64_normalf_init(Mean, StdDev);
- // Create histogram map
- c_withvar (csmap_i, mhist) {
+ // Create and init histogram map with defered destruct
+ c_withvar (csmap_i, mhist)
+ {
c_forrange (N) {
int index = (int) round( stc64_normalf(&rng, &dist) );
csmap_i_emplace(&mhist, index, 0).ref->second += 1;
}
// Print the gaussian bar chart
- c_withvar (cstr, bar) {
- c_foreach (i, csmap_i, mhist) {
- size_t n = (size_t) (i.ref->second * StdDev * Scale * 2.5 / (float)N);
- if (n > 0) {
- cstr_resize(&bar, n, '*');
- printf("%4d %s\n", i.ref->first, bar.str);
- }
+ c_withvar (cstr, bar)
+ c_foreach (i, csmap_i, mhist) {
+ size_t n = (size_t) (i.ref->second * StdDev * Scale * 2.5 / (float)N);
+ if (n > 0) {
+ cstr_resize(&bar, n, '*');
+ printf("%4d %s\n", i.ref->first, bar.str);
}
}
}
diff --git a/examples/list_erase.c b/examples/list_erase.c
index d61edace..3526b5eb 100644
--- a/examples/list_erase.c
+++ b/examples/list_erase.c
@@ -6,7 +6,8 @@ using_clist(i, int);
int main ()
{
- c_withvar (clist_i, L) {
+ c_with (clist_i L = clist_i_init(), clist_i_del(&L))
+ {
c_emplace(clist_i, L, {10, 20, 30, 40, 50});
// 10 20 30 40 50
clist_i_iter_t it = clist_i_begin(&L); // ^
diff --git a/examples/olympics.c b/examples/multimap.c
index 07388c1b..8d56119f 100644
--- a/examples/olympics.c
+++ b/examples/multimap.c
@@ -46,28 +46,38 @@ void OlympicLocation_del(OlympicLocation* self) {
}
// Create a clist<OlympicLocation>, can be sorted by year.
-using_clist(ol, OlympicLocation, OlympicLocation_compare, OlympicLocation_del);
+using_clist(OL, OlympicLocation, OlympicLocation_compare, OlympicLocation_del);
-// Create a csmap<cstr, clist_ol> where key is country
-using_csmap_strkey(ol, clist_ol, clist_ol_del, c_no_clone);
+// Create a csmap<cstr, clist_OL> where key is country
+using_csmap_strkey(OL, clist_OL, clist_OL_del, c_no_clone);
int main()
{
- c_withvar (csmap_ol, multimap) {
- clist_ol empty = clist_ol_init();
+ // Define the multimap with destructor defered to when block is completed.
+ c_with (csmap_OL multimap = csmap_OL_init(), csmap_OL_del(&multimap))
+ {
+ clist_OL empty = clist_OL_init();
- for (int i = 0; i<c_arraylen(ol_data); ++i) {
+ for (int i = 0; i < c_arraylen(ol_data); ++i)
+ {
struct OlympicsData* it = &ol_data[i];
- OlympicLocation loc = {.year=it->year, .city=cstr_from(it->city), .date=cstr_from(it->date)};
+ OlympicLocation loc = {.year = it->year,
+ .city = cstr_from(it->city),
+ .date = cstr_from(it->date)};
// Insert an empty list for each new country, and append the entry to the list.
// If list already exist in map, it returns it from the insert function.
- clist_ol_push_back(&csmap_ol_insert(&multimap, cstr_from(it->country), empty).ref->second, loc);
+ clist_OL_push_back(&csmap_OL_insert(&multimap, cstr_from(it->country), empty).ref->second, loc);
}
- c_foreach (country, csmap_ol, multimap) {
- clist_ol_sort(&country.ref->second); // Sort locations by year for each country.
- c_foreach (loc, clist_ol, country.ref->second)
+ // Iterate the multimap:
+ c_foreach (country, csmap_OL, multimap)
+ {
+ // Sort locations by year for each country.
+ clist_OL_sort(&country.ref->second);
+
+ // Loop the locations for a country sorted by year
+ c_foreach (loc, clist_OL, country.ref->second)
printf("%s: %d, %s, %s\n", country.ref->first.str, loc.ref->year,
loc.ref->city.str,
loc.ref->date.str);