summaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2022-08-06 17:48:06 +0200
committerTyge Løvset <[email protected]>2022-08-06 17:48:06 +0200
commit618b5704e6f85cfe1b6e5c9c9373abe76a8bb628 (patch)
tree4e25a538c8ba3fd58bb25b90ff6dc54adfb31aa4 /examples
parent927fa8093ea0bc1e25586e60c47cf1dd8a311d9e (diff)
downloadSTC-modified-618b5704e6f85cfe1b6e5c9c9373abe76a8bb628.tar.gz
STC-modified-618b5704e6f85cfe1b6e5c9c9373abe76a8bb628.zip
c_apply() deprecated: replaced with c_forarray() macro. Updated and improved README.md docs.
Diffstat (limited to 'examples')
-rw-r--r--examples/city.c8
-rw-r--r--examples/convert.c5
-rw-r--r--examples/cpque.c8
-rw-r--r--examples/csmap_erase.c8
-rw-r--r--examples/csmap_find.c5
-rw-r--r--examples/csmap_insert.c7
-rw-r--r--examples/csset_erase.c5
-rw-r--r--examples/inits.c15
-rw-r--r--examples/list.c4
-rw-r--r--examples/list_erase.c4
-rw-r--r--examples/list_splice.c8
-rw-r--r--examples/lower_bound.c10
-rw-r--r--examples/music_arc.c8
-rw-r--r--examples/new_list.c10
-rw-r--r--examples/new_map.c15
-rw-r--r--examples/new_smap.c10
-rw-r--r--examples/person_arc.c3
-rw-r--r--examples/phonebook.c12
-rw-r--r--examples/priority.c3
-rw-r--r--examples/shape.c8
-rw-r--r--examples/sidebyside.cpp4
-rw-r--r--examples/vikings.c5
-rw-r--r--examples/words.c4
23 files changed, 96 insertions, 73 deletions
diff --git a/examples/city.c b/examples/city.c
index d70edbf7..c22693f9 100644
--- a/examples/city.c
+++ b/examples/city.c
@@ -52,13 +52,15 @@ int main(void)
{
struct City_s { const char *name, *country; float lat, lon; int pop; };
- c_apply(c, Cities_push(&cities, CityArc_make((City){cstr_from(c->name), cstr_from(c->country),
- c->lat, c->lon, c->pop})), struct City_s, {
+ c_forarray (struct City_s, c, {
{"New York", "US", 4.3, 23.2, 9000000},
{"Paris", "France", 4.3, 23.2, 9000000},
{"Berlin", "Germany", 4.3, 23.2, 9000000},
{"London", "UK", 4.3, 23.2, 9000000},
- });
+ }) {
+ Cities_push(&cities, CityArc_make((City){cstr_from(c->name), cstr_from(c->country),
+ c->lat, c->lon, c->pop}));
+ }
copy = Cities_clone(cities); // share each element!
diff --git a/examples/convert.c b/examples/convert.c
index 56cd1eca..5d58574d 100644
--- a/examples/convert.c
+++ b/examples/convert.c
@@ -17,11 +17,12 @@ int main()
c_auto (cvec_str, keys, values)
c_auto (clist_str, list)
{
- c_apply(v, cmap_str_emplace(&map, c_pair(v)), cmap_str_raw, {
+ c_forarray (cmap_str_raw, v, {
{"green", "#00ff00"},
{"blue", "#0000ff"},
{"yellow", "#ffff00"},
- });
+ }) cmap_str_emplace(&map, c_pair(v));
+
puts("MAP:");
c_foreach (i, cmap_str, map)
printf(" %s: %s\n", cstr_str(&i.ref->first), cstr_str(&i.ref->second));
diff --git a/examples/cpque.c b/examples/cpque.c
index 00d2697e..5866f17b 100644
--- a/examples/cpque.c
+++ b/examples/cpque.c
@@ -31,17 +31,15 @@ int main()
c_auto (ipque, q, q2, q3) // init() and defered drop()
{
less_fn = int_less;
- c_forrange (i, n)
- ipque_push(&q, data[i]);
-
+ c_forrange (i, n) ipque_push(&q, data[i]);
print_queue(q);
less_fn = int_greater;
- c_apply_array(v, ipque_push(&q2, *v), const int, data, n);
+ c_forrange (i, n) ipque_push(&q2, data[i]);
print_queue(q2);
less_fn = int_lambda;
- c_apply_array(v, ipque_push(&q3, *v), const int, data, n);
+ c_forrange (i, n) ipque_push(&q3, data[i]);
print_queue(q3);
}
}
diff --git a/examples/csmap_erase.c b/examples/csmap_erase.c
index 37d25f37..1c533a99 100644
--- a/examples/csmap_erase.c
+++ b/examples/csmap_erase.c
@@ -36,14 +36,14 @@ int main()
c_auto (mymap, m2)
{
- // Fill in some data to test with, one at a time, using c_apply()
- c_apply(v, mymap_emplace(&m2, c_pair(v)), mymap_raw, {
+ // Fill in some data to test with, one at a time
+ c_forarray (mymap_raw, v, {
{10, "Bob"},
{11, "Rob"},
{12, "Robert"},
{13, "Bert"},
- {14, "Bobby"}
- });
+ {14, "Bobby"},
+ }) mymap_emplace(&m2, v->first, v->second);
puts("Starting data of map m2 is:");
printmap(m2);
diff --git a/examples/csmap_find.c b/examples/csmap_find.c
index f74f7fb9..ae8aeb85 100644
--- a/examples/csmap_find.c
+++ b/examples/csmap_find.c
@@ -45,8 +45,9 @@ int main()
c_auto (csmap_istr, m1)
c_auto (cvec_istr, v)
{
- c_apply(v, csmap_istr_emplace(&m1, c_pair(v)), csmap_istr_raw,
- {{40, "Zr"}, {45, "Rh"}});
+ c_forarray (csmap_istr_raw, v, {{40, "Zr"}, {45, "Rh"}})
+ csmap_istr_emplace(&m1, c_pair(v));
+
puts("The starting map m1 is (key, value):");
print_collection_csmap_istr(&m1);
diff --git a/examples/csmap_insert.c b/examples/csmap_insert.c
index 3a739cd8..7652fd59 100644
--- a/examples/csmap_insert.c
+++ b/examples/csmap_insert.c
@@ -101,9 +101,10 @@ int main()
c_auto (csmap_ii, m4) {
// Insert the elements from an initializer_list
- c_apply(v, csmap_ii_insert(&m4, c_pair(v)), csmap_ii_raw, {
- { 4, 44 }, { 2, 22 }, { 3, 33 }, { 1, 11 }, { 5, 55 }
- });
+ c_forarray (csmap_ii_raw, v, {{ 4, 44 }, { 2, 22 }, { 3, 33 },
+ { 1, 11 }, { 5, 55 }})
+ csmap_ii_insert(&m4, v->first, v->second);
+
puts("After initializer_list insertion, m4 contains:");
print_ii(m4);
puts("");
diff --git a/examples/csset_erase.c b/examples/csset_erase.c
index 7c8c1d97..9ca23aab 100644
--- a/examples/csset_erase.c
+++ b/examples/csset_erase.c
@@ -7,8 +7,9 @@ int main()
{
c_auto (csset_int, set)
{
- c_apply(v, csset_int_insert(&set, *v),
- int, {30, 20, 80, 40, 60, 90, 10, 70, 50});
+ c_forarray (int, v, {30, 20, 80, 40, 60, 90, 10, 70, 50})
+ csset_int_insert(&set, *v);
+
c_foreach (k, csset_int, set)
printf(" %d", *k.ref);
puts("");
diff --git a/examples/inits.c b/examples/inits.c
index a2fb51cb..9ce96dc9 100644
--- a/examples/inits.c
+++ b/examples/inits.c
@@ -67,7 +67,7 @@ int main(void)
// CMAP CNT
c_auto (cmap_cnt, countries) {
- c_apply(v, cmap_cnt_emplace(&countries, c_pair(v)), cmap_cnt_raw, {
+ c_forarray (cmap_cnt_raw, v, {
{"Norway", 100},
{"Denmark", 50},
{"Iceland", 10},
@@ -76,7 +76,8 @@ int main(void)
{"Germany", 10},
{"Spain", 10},
{"France", 10},
- });
+ }) cmap_cnt_emplace(&countries, v->first, v->second);
+
cmap_cnt_emplace(&countries, "Greenland", 0).ref->second += 20;
cmap_cnt_emplace(&countries, "Sweden", 0).ref->second += 20;
cmap_cnt_emplace(&countries, "Norway", 0).ref->second += 20;
@@ -90,8 +91,9 @@ int main(void)
// CVEC PAIR
c_auto (cvec_ip, pairs1) {
- c_apply(p, cvec_ip_push_back(&pairs1, *p), ipair_t,
- {{5, 6}, {3, 4}, {1, 2}, {7, 8}});
+ c_forarray (ipair_t, p, {{5, 6}, {3, 4}, {1, 2}, {7, 8}})
+ cvec_ip_push_back(&pairs1, *p);
+
cvec_ip_sort(&pairs1);
c_foreach (i, cvec_ip, pairs1)
@@ -102,8 +104,9 @@ int main(void)
// CLIST PAIR
c_auto (clist_ip, pairs2) {
- c_apply(p, clist_ip_push_back(&pairs2, *p), ipair_t,
- {{5, 6}, {3, 4}, {1, 2}, {7, 8}});
+ c_forarray (ipair_t, p, {{5, 6}, {3, 4}, {1, 2}, {7, 8}})
+ clist_ip_push_back(&pairs2, *p);
+
clist_ip_sort(&pairs2);
c_foreach (i, clist_ip, pairs2)
diff --git a/examples/list.c b/examples/list.c
index d76490f8..2dc19704 100644
--- a/examples/list.c
+++ b/examples/list.c
@@ -39,7 +39,9 @@ int main() {
puts("");
clist_fx_clear(&list);
- c_apply(v, clist_fx_push_back(&list, *v), int, {10, 20, 30, 40, 30, 50});
+ c_forarray (int, v, {10, 20, 30, 40, 30, 50})
+ clist_fx_push_back(&list, *v);
+
const double* v = clist_fx_get(&list, 30);
printf("found: %f\n", *v);
c_foreach (i, clist_fx, list) printf(" %g", *i.ref);
diff --git a/examples/list_erase.c b/examples/list_erase.c
index ad062131..9155e38d 100644
--- a/examples/list_erase.c
+++ b/examples/list_erase.c
@@ -8,7 +8,9 @@ int main ()
{
c_auto (clist_int, L)
{
- c_apply(i, clist_int_push_back(&L, *i), int, {10, 20, 30, 40, 50});
+ c_forarray (int, i, {10, 20, 30, 40, 50})
+ clist_int_push_back(&L, *i);
+
c_foreach (x, clist_int, L)
printf("%d ", *x.ref);
puts("");
diff --git a/examples/list_splice.c b/examples/list_splice.c
index cc041a73..8ba022f8 100644
--- a/examples/list_splice.c
+++ b/examples/list_splice.c
@@ -18,8 +18,12 @@ int main ()
{
c_auto (clist_i, list1, list2)
{
- c_apply(v, clist_i_push_back(&list1, *v), int, {1, 2, 3, 4, 5});
- c_apply(v, clist_i_push_back(&list2, *v), int, {10, 20, 30, 40, 50});
+ c_forarray (int, v, {1, 2, 3, 4, 5})
+ clist_i_push_back(&list1, *v);
+
+ c_forarray (int, v, {10, 20, 30, 40, 50})
+ clist_i_push_back(&list2, *v);
+
print_ilist("list1:", list1);
print_ilist("list2:", list2);
diff --git a/examples/lower_bound.c b/examples/lower_bound.c
index a1de1cfd..c8beed6f 100644
--- a/examples/lower_bound.c
+++ b/examples/lower_bound.c
@@ -13,9 +13,8 @@ int main()
{
int key, *res;
- c_apply(t, cvec_int_push(&vec, *t), int, {
- 40, 600, 1, 7000, 2, 500, 30,
- });
+ c_forarray (int, t, {40, 600, 1, 7000, 2, 500, 30})
+ cvec_int_push(&vec, *t);
cvec_int_sort(&vec);
@@ -41,9 +40,8 @@ int main()
{
int key, *res;
- c_apply(t, csset_int_push(&set, *t), int, {
- 40, 600, 1, 7000, 2, 500, 30,
- });
+ c_forarray (int, t, {40, 600, 1, 7000, 2, 500, 30})
+ csset_int_push(&set, *t);
key = 500;
res = csset_int_lower_bound(&set, key).ref;
diff --git a/examples/music_arc.c b/examples/music_arc.c
index e1e715a1..ac730bc3 100644
--- a/examples/music_arc.c
+++ b/examples/music_arc.c
@@ -30,20 +30,20 @@ void example3()
{
c_auto (SongVec, vec, vec2)
{
- c_apply(v, SongVec_push_back(&vec, *v), SongPtr, {
+ c_forarray (SongPtr, v, {
SongPtr_make(Song_new("Bob Dylan", "The Times They Are A Changing")),
SongPtr_make(Song_new("Aretha Franklin", "Bridge Over Troubled Water")),
SongPtr_make(Song_new("Thalia", "Entre El Mar y Una Estrella"))
- });
+ }) SongVec_push_back(&vec, *v);
c_foreach (s, SongVec, vec)
if (!cstr_equals(s.ref->get->artist, "Bob Dylan"))
SongVec_push_back(&vec2, SongPtr_clone(*s.ref));
- c_apply(v, SongVec_push_back(&vec2, *v), SongPtr, {
+ c_forarray (SongPtr, v, {
SongPtr_make(Song_new("Michael Jackson", "Billie Jean")),
SongPtr_make(Song_new("Rihanna", "Stay")),
- });
+ }) SongVec_push_back(&vec2, *v);
c_foreach (s, SongVec, vec2)
printf("%s - %s: refs %lu\n", cstr_str(&s.ref->get->artist),
diff --git a/examples/new_list.c b/examples/new_list.c
index 9bbbd8ce..e760a093 100644
--- a/examples/new_list.c
+++ b/examples/new_list.c
@@ -39,8 +39,9 @@ int main()
clist_i32_push_back(&lst, 123);
c_auto (clist_pnt, plst) {
- c_apply(v, clist_pnt_push_back(&plst, *v),
- Point, {{42, 14}, {32, 94}, {62, 81}});
+ c_forarray (Point, v, {{42, 14}, {32, 94}, {62, 81}})
+ clist_pnt_push_back(&plst, *v);
+
clist_pnt_sort(&plst);
c_foreach (i, clist_pnt, plst)
@@ -49,8 +50,9 @@ int main()
}
c_auto (clist_float, flst) {
- c_apply(v, clist_float_push_back(&flst, *v),
- float, {123.3f, 321.2f, -32.2f, 78.2f});
+ c_forarray (float, v, {123.3f, 321.2f, -32.2f, 78.2f})
+ clist_float_push_back(&flst, *v);
+
c_foreach (i, clist_float, flst) printf(" %g", *i.ref);
}
diff --git a/examples/new_map.c b/examples/new_map.c
index b0752d53..c94c2b44 100644
--- a/examples/new_map.c
+++ b/examples/new_map.c
@@ -49,23 +49,24 @@ int main()
{
cmap_int_insert(&map, 123, 321);
- c_apply(v, cmap_pnt_insert(&pmap, c_pair(v)), cmap_pnt_raw, {
- {{42, 14}, 1}, {{32, 94}, 2}, {{62, 81}, 3}
- });
+ c_forarray (cmap_pnt_raw, v, {{{42, 14}, 1}, {{32, 94}, 2}, {{62, 81}, 3}})
+ cmap_pnt_insert(&pmap, v->first, v->second);
+
c_foreach (i, cmap_pnt, pmap)
printf(" (%d, %d: %d)", i.ref->first.x, i.ref->first.y, i.ref->second);
puts("");
- c_apply(v, cmap_str_emplace(&smap, c_pair(v)), cmap_str_raw, {
+ c_forarray (cmap_str_raw, v, {
{"Hello, friend", "long time no see"},
{"So long, friend", "see you around"},
- });
+ }) cmap_str_emplace(&smap, v->first, v->second);
- c_apply(v, cset_str_emplace(&sset, *v), const char*, {
+ c_forarray_p (const char*, v, {
"Hello, friend",
"Nice to see you again",
"So long, friend",
- });
+ }) cset_str_emplace(&sset, *v);
+
c_foreach (i, cset_str, sset)
printf(" %s\n", cstr_str(i.ref));
}
diff --git a/examples/new_smap.c b/examples/new_smap.c
index 368775dc..7c2ddb35 100644
--- a/examples/new_smap.c
+++ b/examples/new_smap.c
@@ -47,22 +47,24 @@ int main()
}
c_auto (PMap, pmap) {
- c_apply(v, PMap_insert(&pmap, c_pair(v)), PMap_value, {
+ c_forarray (PMap_value, v, {
{{42, 14}, 1},
{{32, 94}, 2},
{{62, 81}, 3},
- });
+ }) PMap_insert(&pmap, c_pair(v));
+
c_forpair (p, i, PMap, pmap)
printf(" (%d,%d: %d)", _.p->x, _.p->y, *_.i);
puts("");
}
c_auto (SMap, smap) {
- c_apply(v, SMap_emplace(&smap, c_pair(v)), SMap_raw, {
+ c_forarray (SMap_raw, v, {
{"Hello, friend", "this is the mapped value"},
{"The brown fox", "jumped"},
{"This is the time", "for all good things"},
- });
+ }) SMap_emplace(&smap, c_pair(v));
+
c_forpair (i, j, SMap, smap)
printf(" (%s: %s)\n", cstr_str(_.i), cstr_str(_.j));
}
diff --git a/examples/person_arc.c b/examples/person_arc.c
index d0af690e..272a3f72 100644
--- a/examples/person_arc.c
+++ b/examples/person_arc.c
@@ -54,7 +54,8 @@ int main()
Persons_push_back(&vec, PSPtr_make(Person_new("Audrey", "Home")));
// Clone/share p and q to the vector
- c_apply(v, Persons_push_back(&vec, PSPtr_clone(*v)), PSPtr, {p, q});
+ c_forarray (PSPtr, v, {p, q})
+ Persons_push_back(&vec, PSPtr_clone(*v));
c_foreach (i, Persons, vec)
printf("%s %s\n", cstr_str(&i.ref->get->name), cstr_str(&i.ref->get->last));
diff --git a/examples/phonebook.c b/examples/phonebook.c
index eebc8008..1455c978 100644
--- a/examples/phonebook.c
+++ b/examples/phonebook.c
@@ -39,19 +39,21 @@ void print_phone_book(cmap_str phone_book)
int main(int argc, char **argv)
{
c_auto (cset_str, names) {
- c_apply(v, cset_str_emplace(&names, *v), const char*,
- {"Hello", "Cool", "True"});
- c_foreach (i, cset_str, names) printf("%s ", cstr_str(i.ref));
+ c_forarray_p (const char*, v, {"Hello", "Cool", "True"})
+ cset_str_emplace(&names, *v);
+
+ c_foreach (i, cset_str, names)
+ printf("%s ", cstr_str(i.ref));
puts("");
}
c_auto (cmap_str, phone_book) {
- c_apply(v, cmap_str_emplace(&phone_book, c_pair(v)), cmap_str_raw, {
+ c_forarray (cmap_str_raw, v, {
{"Lilia Friedman", "(892) 670-4739"},
{"Tariq Beltran", "(489) 600-7575"},
{"Laiba Juarez", "(303) 885-5692"},
{"Elliott Mooney", "(945) 616-4482"},
- });
+ }) cmap_str_emplace(&phone_book, c_pair(v));
printf("Phone book:\n");
print_phone_book(phone_book);
diff --git a/examples/priority.c b/examples/priority.c
index 7ba9e59b..b8766a77 100644
--- a/examples/priority.c
+++ b/examples/priority.c
@@ -20,7 +20,8 @@ int main() {
cpque_i_push(&heap, stc64_uniform(&rng, &dist));
// push some negative numbers too.
- c_apply(v, cpque_i_push(&heap, *v), int, {-231, -32, -873, -4, -343});
+ c_forarray (int, v, {-231, -32, -873, -4, -343})
+ cpque_i_push(&heap, *v);
c_forrange (N)
cpque_i_push(&heap, stc64_uniform(&rng, &dist));
diff --git a/examples/shape.c b/examples/shape.c
index b052d921..4c2a7542 100644
--- a/examples/shape.c
+++ b/examples/shape.c
@@ -145,11 +145,11 @@ int main(void)
Polygon* pol1 = Polygon_new();
Polygon* pol2 = Polygon_new();
- c_apply(p, Polygon_addPoint(pol1, *p), Point,
- {{50, 72}, {123, 73}, {127, 201}, {828, 333}});
+ c_forarray (Point, p, {{50, 72}, {123, 73}, {127, 201}, {828, 333}})
+ Polygon_addPoint(pol1, *p);
- c_apply(p, Polygon_addPoint(pol2, *p), Point,
- {{5, 7}, {12, 7}, {12, 20}, {82, 33}, {17, 56}});
+ c_forarray (Point, p, {{5, 7}, {12, 7}, {12, 20}, {82, 33}, {17, 56}})
+ Polygon_addPoint(pol2, *p);
Shapes_push(&shapes, &tri1->shape);
Shapes_push(&shapes, &pol1->shape);
diff --git a/examples/sidebyside.cpp b/examples/sidebyside.cpp
index f2021436..282beefb 100644
--- a/examples/sidebyside.cpp
+++ b/examples/sidebyside.cpp
@@ -47,8 +47,8 @@ int main() {
c_auto (cmap_si, food)
{
- c_apply(v, cmap_si_emplace(&food, c_pair(v)), cmap_si_raw,
- {{"burger", 5}, {"pizza", 12}, {"steak", 15}});
+ c_forarray (cmap_si_raw, v, {{"burger", 5}, {"pizza", 12}, {"steak", 15}})
+ cmap_si_emplace(&food, c_pair(v));
c_foreach (i, cmap_si, food)
printf("%s, %d\n", cstr_str(&i.ref->first), i.ref->second);
diff --git a/examples/vikings.c b/examples/vikings.c
index 2cef1991..667c4426 100644
--- a/examples/vikings.c
+++ b/examples/vikings.c
@@ -51,11 +51,12 @@ static inline RViking Viking_toraw(const Viking* vp) {
int main()
{
c_auto (Vikings, vikings) {
- c_apply(v, Vikings_emplace(&vikings, c_pair(v)), Vikings_raw, {
+ c_forarray (Vikings_raw, v, {
{{"Einar", "Norway"}, 20},
{{"Olaf", "Denmark"}, 24},
{{"Harald", "Iceland"}, 12},
- });
+ }) Vikings_emplace(&vikings, c_pair(v));
+
RViking bjorn = {"Bjorn", "Sweden"};
Vikings_emplace_or_assign(&vikings, bjorn, 10);
diff --git a/examples/words.c b/examples/words.c
index 8a86ba7f..888f7abb 100644
--- a/examples/words.c
+++ b/examples/words.c
@@ -13,10 +13,10 @@ int main1()
c_auto (cvec_str, words)
c_auto (cmap_str, word_map)
{
- c_apply(v, cvec_str_emplace_back(&words, *v), const char*, {
+ c_forarray_p (const char*, v, {
"this", "sentence", "is", "not", "a", "sentence",
"this", "sentence", "is", "a", "hoax"
- });
+ }) cvec_str_emplace_back(&words, *v);
c_foreach (w, cvec_str, words) {
cmap_str_emplace(&word_map, cstr_str(w.ref), 0).ref->second += 1;