summaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2021-12-22 08:49:19 +0100
committerTyge Løvset <[email protected]>2021-12-22 08:50:15 +0100
commit6e65e30c6e3c2bc271d3885e749ceb0289bbd9cf (patch)
treeccffa7b7dde19fb551f99c7eed529bdc7308e494 /examples
parent1b90b6c3eea0f3e07f8a2d2abe3686917d1d86a8 (diff)
downloadSTC-modified-6e65e30c6e3c2bc271d3885e749ceb0289bbd9cf.tar.gz
STC-modified-6e65e30c6e3c2bc271d3885e749ceb0289bbd9cf.zip
Changed the c_apply() and c_apply_pair() to one new c_apply() API. Added c_pair(v) for convenience.
Diffstat (limited to 'examples')
-rw-r--r--examples/box.c12
-rw-r--r--examples/convert.c2
-rw-r--r--examples/cpque.c4
-rw-r--r--examples/csmap_erase.c4
-rw-r--r--examples/csmap_find.c3
-rw-r--r--examples/csmap_insert.c3
-rw-r--r--examples/csset_erase.c3
-rw-r--r--examples/inits.c10
-rw-r--r--examples/list.c2
-rw-r--r--examples/list_erase.c2
-rw-r--r--examples/list_splice.c4
-rw-r--r--examples/new_list.c6
-rw-r--r--examples/new_map.c6
-rw-r--r--examples/new_sptr.c8
-rw-r--r--examples/phonebook.c5
-rw-r--r--examples/priority.c2
-rw-r--r--examples/sidebyside.cpp3
-rw-r--r--examples/sptr.c23
-rw-r--r--examples/sptr_demo.c8
-rw-r--r--examples/sptr_erase.c11
-rw-r--r--examples/sptr_music.c30
-rw-r--r--examples/sptr_pthread.c2
-rw-r--r--examples/sptr_to_maps.c22
-rw-r--r--examples/vikings.c2
-rw-r--r--examples/words.c2
25 files changed, 94 insertions, 85 deletions
diff --git a/examples/box.c b/examples/box.c
index 598d0bdc..ae10a89f 100644
--- a/examples/box.c
+++ b/examples/box.c
@@ -28,7 +28,7 @@ void Person_drop(Person* p) {
#include <stc/cbox.h>
#define i_type Persons
-#define i_val_bind PBox // binds PBox_cmp, ...
+#define i_val_ref PBox // binds PBox_cmp, ...
#include <stc/cvec.h>
@@ -37,7 +37,7 @@ int main()
c_auto (Persons, vec)
c_auto (PBox, p, q)
{
- p = PBox_new(Person_new("Laura", "Palmer"));
+ p = PBox_from(Person_new("Laura", "Palmer"));
q = PBox_clone(p);
cstr_assign(&q.get->name, "Leland");
@@ -45,11 +45,11 @@ int main()
printf("orig: %s %s\n", p.get->name.str, p.get->last.str);
printf("copy: %s %s\n", q.get->name.str, q.get->last.str);
- Persons_push_back(&vec, PBox_new(Person_new("Dale", "Cooper")));
- Persons_push_back(&vec, PBox_new(Person_new("Audrey", "Home")));
+ Persons_push_back(&vec, PBox_from(Person_new("Dale", "Cooper")));
+ Persons_push_back(&vec, PBox_from(Person_new("Audrey", "Home")));
// NB! Clone p and q to the vector using emplace_back()
- c_apply(Persons, emplace_back, &vec, {p, q});
+ c_apply(v, Persons_push_back(&vec, PBox_clone(v)), PBox, {p, q});
c_foreach (i, Persons, vec)
printf("%s %s\n", i.ref->get->name.str, i.ref->get->last.str);
@@ -57,7 +57,7 @@ int main()
// Look-up Audrey! Use a (fake) temporary PBox for lookup.
c_autovar (Person a = Person_new("Audrey", "Home"), Person_drop(&a)) {
- const PBox *v = Persons_get(&vec, (PBox){&a});
+ const PBox *v = Persons_get(&vec, a);
if (v) printf("found: %s %s\n", v->get->name.str, v->get->last.str);
}
puts("");
diff --git a/examples/convert.c b/examples/convert.c
index 83dc1b37..034657db 100644
--- a/examples/convert.c
+++ b/examples/convert.c
@@ -16,7 +16,7 @@ int main()
c_auto (cvec_str, keys, values)
c_auto (clist_str, list)
{
- c_apply_pair(cmap_str, emplace, &map, {
+ c_apply(v, cmap_str_emplace(&map, c_pair(v)), cmap_str_rawvalue, {
{"green", "#00ff00"},
{"blue", "#0000ff"},
{"yellow", "#ffff00"},
diff --git a/examples/cpque.c b/examples/cpque.c
index 0b8a610c..d3ad2d0c 100644
--- a/examples/cpque.c
+++ b/examples/cpque.c
@@ -37,11 +37,11 @@ int main()
print_queue(q);
icmp_fn = imin_cmp;
- c_apply_n(ipque, push, &q2, data, n);
+ c_apply_arr(v, ipque_push(&q2, v), int, data, n);
print_queue(q2);
icmp_fn = imix_cmp;
- c_apply_n(ipque, push, &q3, data, n);
+ c_apply_arr(v, ipque_push(&q3, v), int, data, n);
print_queue(q3);
}
}
diff --git a/examples/csmap_erase.c b/examples/csmap_erase.c
index 9cf65389..7b179bef 100644
--- a/examples/csmap_erase.c
+++ b/examples/csmap_erase.c
@@ -36,8 +36,8 @@ int main()
c_auto (mymap, m2)
{
- // Fill in some data to test with, one at a time, using c_apply_pair()
- c_apply_pair(mymap, emplace, &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_rawvalue, {
{10, "Bob"},
{11, "Rob"},
{12, "Robert"},
diff --git a/examples/csmap_find.c b/examples/csmap_find.c
index 3ca7ff87..c70e0523 100644
--- a/examples/csmap_find.c
+++ b/examples/csmap_find.c
@@ -45,7 +45,8 @@ int main()
c_auto (csmap_istr, m1)
c_auto (cvec_istr, v)
{
- c_apply_pair(csmap_istr, emplace, &m1, {{40, "Zr"}, {45, "Rh"}});
+ c_apply(v, csmap_istr_emplace(&m1, c_pair(v)), csmap_istr_rawvalue,
+ {{40, "Zr"}, {45, "Rh"}});
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 c3690c83..1b89915e 100644
--- a/examples/csmap_insert.c
+++ b/examples/csmap_insert.c
@@ -99,7 +99,8 @@ int main()
c_auto (csmap_ii, m4) {
// Insert the elements from an initializer_list
- c_apply_pair(csmap_ii, insert, &m4, { { 4, 44 }, { 2, 22 }, { 3, 33 }, { 1, 11 }, { 5, 55 } });
+ c_apply(v, csmap_ii_insert(&m4, c_pair(v)), csmap_ii_rawvalue,
+ { { 4, 44 }, { 2, 22 }, { 3, 33 }, { 1, 11 }, { 5, 55 } });
puts("After initializer_list insertion, m4 contains:");
print_ii(m4);
puts("");
diff --git a/examples/csset_erase.c b/examples/csset_erase.c
index 95145ed8..485e78de 100644
--- a/examples/csset_erase.c
+++ b/examples/csset_erase.c
@@ -7,7 +7,8 @@ int main()
{
c_auto (csset_int, set)
{
- c_apply(csset_int, insert, &set, {30, 20, 80, 40, 60, 90, 10, 70, 50});
+ c_apply(v, csset_int_insert(&set, v),
+ int, {30, 20, 80, 40, 60, 90, 10, 70, 50});
c_foreach (k, csset_int, set)
printf(" %d", *k.ref);
puts("");
diff --git a/examples/inits.c b/examples/inits.c
index a1733caf..3f62b9fc 100644
--- a/examples/inits.c
+++ b/examples/inits.c
@@ -41,7 +41,7 @@ int main(void)
// PRIORITY QUEUE
- c_apply_n(cpque_f, push, &floats, nums, c_arraylen(nums));
+ c_apply_arr(v, cpque_f_push(&floats, v), float, nums, c_arraylen(nums));
puts("\npop and show high priorites first:");
while (! cpque_f_empty(floats)) {
@@ -67,7 +67,7 @@ int main(void)
// CMAP CNT
c_auto (cmap_cnt, countries) {
- c_apply_pair(cmap_cnt, emplace, &countries, {
+ c_apply(v, cmap_cnt_emplace(&countries, c_pair(v)), cmap_cnt_rawvalue, {
{"Norway", 100},
{"Denmark", 50},
{"Iceland", 10},
@@ -90,7 +90,8 @@ int main(void)
// CVEC PAIR
c_auto (cvec_ip, pairs1) {
- c_apply(cvec_ip, push_back, &pairs1, {{5, 6}, {3, 4}, {1, 2}, {7, 8}});
+ c_apply(p, cvec_ip_push_back(&pairs1, p), ipair_t,
+ {{5, 6}, {3, 4}, {1, 2}, {7, 8}});
cvec_ip_sort(&pairs1);
c_foreach (i, cvec_ip, pairs1)
@@ -101,7 +102,8 @@ int main(void)
// CLIST PAIR
c_auto (clist_ip, pairs2) {
- c_apply(clist_ip, push_back, &pairs2, {{5, 6}, {3, 4}, {1, 2}, {7, 8}});
+ c_apply(p, clist_ip_push_back(&pairs2, p), ipair_t,
+ {{5, 6}, {3, 4}, {1, 2}, {7, 8}});
clist_ip_sort(&pairs2);
c_foreach (i, clist_ip, pairs2)
diff --git a/examples/list.c b/examples/list.c
index d0d377f3..84f4e24c 100644
--- a/examples/list.c
+++ b/examples/list.c
@@ -36,7 +36,7 @@ int main() {
puts("");
clist_fx_clear(&list);
- c_apply(clist_fx, push_back, &list, {10, 20, 30, 40, 30, 50});
+ c_apply(v, clist_fx_push_back(&list, v), int, {10, 20, 30, 40, 30, 50});
c_foreach (i, clist_fx, list) printf(" %g", *i.ref);
puts("");
diff --git a/examples/list_erase.c b/examples/list_erase.c
index c567f043..b20f3fb1 100644
--- a/examples/list_erase.c
+++ b/examples/list_erase.c
@@ -8,7 +8,7 @@ int main ()
{
c_auto (clist_int, L)
{
- c_apply(clist_int, push_back, &L, {10, 20, 30, 40, 50});
+ c_apply(i, clist_int_push_back(&L, i), int, {10, 20, 30, 40, 50});
c_foreach (x, clist_int, L) printf("%d ", *x.ref);
puts("");
// 10 20 30 40 50
diff --git a/examples/list_splice.c b/examples/list_splice.c
index f1049288..0738e9d3 100644
--- a/examples/list_splice.c
+++ b/examples/list_splice.c
@@ -17,8 +17,8 @@ int main ()
{
c_auto (clist_i, list1, list2)
{
- c_apply(clist_i, push_back, &list1, {1, 2, 3, 4, 5});
- c_apply(clist_i, push_back, &list2, {10, 20, 30, 40, 50});
+ 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});
print_ilist("list1:", list1);
print_ilist("list2:", list2);
diff --git a/examples/new_list.c b/examples/new_list.c
index 0867c7a6..b6718d34 100644
--- a/examples/new_list.c
+++ b/examples/new_list.c
@@ -39,7 +39,8 @@ int main()
clist_i32_push_back(&lst, 123);
c_auto (clist_pnt, plst) {
- c_apply(clist_pnt, push_back, &plst, {{42, 14}, {32, 94}, {62, 81}});
+ c_apply(v, clist_pnt_push_back(&plst, v),
+ Point, {{42, 14}, {32, 94}, {62, 81}});
clist_pnt_sort(&plst);
c_foreach (i, clist_pnt, plst)
@@ -48,7 +49,8 @@ int main()
}
c_auto (clist_float, flst) {
- c_apply(clist_float, push_back, &flst, {123.3, 321.2, -32.2, 78.2});
+ c_apply(v, clist_float_push_back(&flst, v),
+ float, {123.3f, 321.2f, -32.2f, 78.2f});
c_foreach (i, clist_float, flst) printf(" %g", *i.ref);
}
diff --git a/examples/new_map.c b/examples/new_map.c
index 953ca793..1c27d5bf 100644
--- a/examples/new_map.c
+++ b/examples/new_map.c
@@ -48,19 +48,19 @@ int main()
{
cmap_int_insert(&map, 123, 321);
- c_apply_pair(cmap_pnt, insert, &pmap, {
+ c_apply(v, cmap_pnt_insert(&pmap, c_pair(v)), cmap_pnt_rawvalue, {
{{42, 14}, 1}, {{32, 94}, 2}, {{62, 81}, 3}
});
c_foreach (i, cmap_pnt, pmap)
printf(" (%d, %d: %d)", i.ref->first.x, i.ref->first.y, i.ref->second);
puts("");
- c_apply_pair(cmap_str, emplace, &smap, {
+ c_apply(v, cmap_str_emplace(&smap, c_pair(v)), cmap_str_rawvalue, {
{"Hello, friend", "long time no see"},
{"So long, friend", "see you around"},
});
- c_apply(cset_str, emplace, &sset, {
+ c_apply(v, cset_str_emplace(&sset, v), const char*, {
"Hello, friend",
"Nice to see you again",
"So long, friend",
diff --git a/examples/new_sptr.c b/examples/new_sptr.c
index d1eed636..ce31478e 100644
--- a/examples/new_sptr.c
+++ b/examples/new_sptr.c
@@ -27,16 +27,16 @@ void Person_drop(Person* p) {
#include <stc/cstack.h>
int main(void) {
- c_autovar (csptr_person p = csptr_person_new(Person_new("John", "Smiths")), csptr_person_drop(&p))
+ c_autovar (csptr_person p = csptr_person_from(Person_new("John", "Smiths")), csptr_person_drop(&p))
c_autovar (csptr_person q = csptr_person_clone(p), csptr_person_drop(&q)) // share the pointer
{
printf("%s %s. uses: %lu\n", q.get->name.str, q.get->last.str, *q.use_count);
}
c_auto (cstack_iptr, stk) {
- cstack_iptr_push(&stk, csptr_int_new(10));
- cstack_iptr_push(&stk, csptr_int_new(20));
- cstack_iptr_push(&stk, csptr_int_new(30));
+ cstack_iptr_push(&stk, csptr_int_from(10));
+ cstack_iptr_push(&stk, csptr_int_from(20));
+ cstack_iptr_push(&stk, csptr_int_from(30));
cstack_iptr_emplace(&stk, *cstack_iptr_top(&stk));
cstack_iptr_emplace(&stk, *cstack_iptr_begin(&stk).ref);
diff --git a/examples/phonebook.c b/examples/phonebook.c
index 640aaec3..064de93f 100644
--- a/examples/phonebook.c
+++ b/examples/phonebook.c
@@ -42,14 +42,15 @@ int main(int argc, char **argv)
c_static_assert(sizeof argc == 4);
c_auto (cset_str, names) {
- c_apply(cset_str, emplace, &names, {"Hello", "Cool", "True"});
+ c_apply(v, cset_str_emplace(&names, v), const char*,
+ {"Hello", "Cool", "True"});
c_foreach (i, cset_str, names) printf("%s ", i.ref->str);
puts("");
}
bool erased;
c_auto (cmap_str, phone_book) {
- c_apply_pair (cmap_str, emplace, &phone_book, {
+ c_apply(v, cmap_str_emplace(&phone_book, c_pair(v)), cmap_str_rawvalue, {
{"Lilia Friedman", "(892) 670-4739"},
{"Tariq Beltran", "(489) 600-7575"},
{"Laiba Juarez", "(303) 885-5692"},
diff --git a/examples/priority.c b/examples/priority.c
index 5164d85d..184cad9f 100644
--- a/examples/priority.c
+++ b/examples/priority.c
@@ -20,7 +20,7 @@ int main() {
cpque_i_push(&heap, stc64_uniform(&rng, &dist));
// push some negative numbers too.
- c_apply(cpque_i, push, &heap, {-231, -32, -873, -4, -343});
+ c_apply(v, cpque_i_push(&heap, v), int, {-231, -32, -873, -4, -343});
c_forrange (N)
cpque_i_push(&heap, stc64_uniform(&rng, &dist));
diff --git a/examples/sidebyside.cpp b/examples/sidebyside.cpp
index 0f645991..cf4b2f70 100644
--- a/examples/sidebyside.cpp
+++ b/examples/sidebyside.cpp
@@ -19,7 +19,8 @@ int main() {
}
c_auto (cmap_str, food)
{
- c_apply_pair(cmap_str, emplace, &food, {{"burger", 5}, {"pizza", 12}, {"steak", 15}});
+ c_apply(v, cmap_str_emplace(&food, c_pair(v)), cmap_str_rawvalue,
+ {{"burger", 5}, {"pizza", 12}, {"steak", 15}});
c_foreach (i, cmap_str, food)
printf("%s, %d\n", i.ref->first.str, i.ref->second);
puts("");
diff --git a/examples/sptr.c b/examples/sptr.c
index 7e0f7357..e1d734e0 100644
--- a/examples/sptr.c
+++ b/examples/sptr.c
@@ -28,7 +28,7 @@ void Person_drop(Person* p) {
#include <stc/csptr.h>
#define i_type Persons
-#define i_val_bind PSPtr // binds PSPtr_cmp, ...
+#define i_val_ref PSPtr // binds PSPtr_cmp, ...
#include <stc/cvec.h>
@@ -37,30 +37,31 @@ int main()
c_auto (Persons, vec)
c_auto (PSPtr, p, q)
{
- p = PSPtr_new(Person_new("Laura", "Palmer"));
+ p = PSPtr_from(Person_new("Laura", "Palmer"));
// We want a deep copy -- PSPtr_clone(p) only shares!
- q = PSPtr_new(Person_clone(*p.get));
+ q = PSPtr_from(Person_clone(*p.get));
cstr_assign(&q.get->name, "Leland");
printf("orig: %s %s\n", p.get->name.str, p.get->last.str);
printf("copy: %s %s\n", q.get->name.str, q.get->last.str);
- Persons_push_back(&vec, PSPtr_new(Person_new("Dale", "Cooper")));
- Persons_push_back(&vec, PSPtr_new(Person_new("Audrey", "Home")));
-
- // NB! Clone/share p and q to the vector using emplace_back()
- c_apply(Persons, emplace_back, &vec, {p, q});
+ Persons_push_back(&vec, PSPtr_from(Person_new("Dale", "Cooper")));
+ Persons_push_back(&vec, PSPtr_from(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_foreach (i, Persons, vec)
printf("%s %s\n", i.ref->get->name.str, i.ref->get->last.str);
puts("");
-
- // Look-up Audrey! Use a (fake) temporary PSPtr for lookup.
+
+ // Look-up Audrey!
c_autovar (Person a = Person_new("Audrey", "Home"), Person_drop(&a)) {
- const PSPtr *v = Persons_get(&vec, (PSPtr){&a});
+ const PSPtr *v = Persons_get(&vec, a);
if (v) printf("found: %s %s\n", v->get->name.str, v->get->last.str);
}
+
puts("");
}
}
diff --git a/examples/sptr_demo.c b/examples/sptr_demo.c
index a6b33de6..a5c642b0 100644
--- a/examples/sptr_demo.c
+++ b/examples/sptr_demo.c
@@ -13,10 +13,10 @@ void int_drop(int* x) {
#define i_drop int_drop // optional, just to display the elements destroyed
#include <stc/csptr.h> // iref
-#define i_key_bind iref // note: use i_key_bind instead of i_key for csptr/cbox elements
+#define i_key_ref iref // note: use i_key_bind instead of i_key for csptr/cbox elements
#include <stc/csset.h> // csset_iref (like: std::set<std::shared_ptr<int>>)
-#define i_val_bind iref // note: as above.
+#define i_val_ref iref // note: as above.
#include <stc/cvec.h> // cvec_iref (like: std::vector<std::shared_ptr<int>>)
int main()
@@ -26,7 +26,7 @@ int main()
{
const int years[] = {2021, 2012, 2022, 2015};
c_forrange (i, c_arraylen(years))
- cvec_iref_push_back(&vec, iref_new(years[i]));
+ cvec_iref_push_back(&vec, iref_from(years[i]));
printf("vec:");
c_foreach (i, cvec_iref, vec) printf(" %d", *i.ref->get);
@@ -35,7 +35,7 @@ int main()
// add odd numbers from vec to set
c_foreach (i, cvec_iref, vec)
if (*i.ref->get & 1)
- csset_iref_emplace(&set, *i.ref); // copy shared pointer => increments counter.
+ csset_iref_insert(&set, iref_clone(*i.ref)); // copy shared pointer => increments counter.
// erase the two last elements in vec
cvec_iref_pop_back(&vec);
diff --git a/examples/sptr_erase.c b/examples/sptr_erase.c
index 240b9a58..fbb141b4 100644
--- a/examples/sptr_erase.c
+++ b/examples/sptr_erase.c
@@ -9,11 +9,10 @@ void show_drop(int* x) { printf("drop: %d\n", *x); }
// if 'i_opt c_no_cmp' is defined, otherwise i_cmp is used
// to compare object values. See the twodifferences by
// commenting out the next line.
-//#define i_opt c_no_cmp
#include <stc/csptr.h> // Shared pointer to int
#define i_type Vec
-#define i_val_bind Arc
+#define i_val_ref Arc
#include <stc/cvec.h> // Vec: cvec<Arc>
@@ -23,11 +22,11 @@ int main()
{
const int v[] = {2012, 1990, 2012, 2019, 2015};
c_forrange (i, c_arraylen(v))
- Vec_push_back(&vec, Arc_new(v[i]));
+ Vec_push_back(&vec, Arc_from(v[i]));
// clone the second 2012 and push it back.
// note: cloning make sure that vec.data[2] has ref count 2.
- Vec_emplace_back(&vec, vec.data[2]);
+ Vec_push_back(&vec, Arc_clone(vec.data[2]));
printf("vec before erase :");
c_foreach (i, Vec, vec)
@@ -36,12 +35,12 @@ int main()
printf("erase vec.data[2]; or first matching value depending on compare.\n");
Vec_iter it;
- it = Vec_find(&vec, vec.data[2]);
+ it = Vec_find(&vec, *vec.data[2].get);
if (it.ref != Vec_end(&vec).ref)
Vec_erase_at(&vec, it);
int year = 2015;
- it = Vec_find(&vec, (Arc){&year}); // Ok as tmp only.
+ it = Vec_find(&vec, year); // Ok as tmp only.
if (it.ref != Vec_end(&vec).ref)
Vec_erase_at(&vec, it);
diff --git a/examples/sptr_music.c b/examples/sptr_music.c
index 04ec28cc..1d58cc46 100644
--- a/examples/sptr_music.c
+++ b/examples/sptr_music.c
@@ -17,36 +17,36 @@ void Song_drop(Song* s) {
c_drop(cstr, &s->artist, &s->title);
}
+#define i_type SongPtr
#define i_val Song
#define i_drop Song_drop
#define i_opt c_no_cmp
-#define i_tag song
-#include <stc/csptr.h> // define csptr_song
+#include <stc/csptr.h>
-#define i_val_bind csptr_song
-#define i_tag song
+#define i_type SongVec
+#define i_val_ref SongPtr
#include <stc/cvec.h>
void example3()
{
- c_auto (cvec_song, v, v2)
+ c_auto (SongVec, vec, vec2)
{
- c_apply(cvec_song, push_back, &v, {
- csptr_song_new(Song_new("Bob Dylan", "The Times They Are A Changing")),
- csptr_song_new(Song_new("Aretha Franklin", "Bridge Over Troubled Water")),
- csptr_song_new(Song_new("Thalia", "Entre El Mar y Una Estrella"))
+ c_apply(v, SongVec_push_back(&vec, v), SongPtr, {
+ SongPtr_from(Song_new("Bob Dylan", "The Times They Are A Changing")),
+ SongPtr_from(Song_new("Aretha Franklin", "Bridge Over Troubled Water")),
+ SongPtr_from(Song_new("Thalia", "Entre El Mar y Una Estrella"))
});
- c_foreach (s, cvec_song, v)
+ c_foreach (s, SongVec, vec)
if (!cstr_equals(s.ref->get->artist, "Bob Dylan"))
- cvec_song_emplace_back(&v2, *s.ref); // note: calls csptr_song_clone()
+ SongVec_push_back(&vec2, SongPtr_clone(*s.ref));
- c_apply(cvec_song, push_back, &v2, {
- csptr_song_new(Song_new("Michael Jackson", "Billie Jean")),
- csptr_song_new(Song_new("Rihanna", "Stay")),
+ c_apply(v, SongVec_push_back(&vec2, v), SongPtr, {
+ SongPtr_from(Song_new("Michael Jackson", "Billie Jean")),
+ SongPtr_from(Song_new("Rihanna", "Stay")),
});
- c_foreach (s, cvec_song, v2)
+ c_foreach (s, SongVec, vec2)
printf("%s - %s: refs %lu\n", s.ref->get->artist.str, s.ref->get->title.str,
*s.ref->use_count);
}
diff --git a/examples/sptr_pthread.c b/examples/sptr_pthread.c
index 501fdcef..d71ce97d 100644
--- a/examples/sptr_pthread.c
+++ b/examples/sptr_pthread.c
@@ -33,7 +33,7 @@ void* thr(csptr_base* lp)
int main()
{
- csptr_base p = csptr_base_new((Base){42});
+ csptr_base p = csptr_base_from((Base){42});
printf("Created a Base\n"
" p.get() = %p, p.use_count() = %ld\n", (void*)p.get, *p.use_count);
diff --git a/examples/sptr_to_maps.c b/examples/sptr_to_maps.c
index 69798563..6b4dd359 100644
--- a/examples/sptr_to_maps.c
+++ b/examples/sptr_to_maps.c
@@ -15,11 +15,11 @@
#include <stc/csptr.h>
#define i_type Stack
-#define i_val_bind Arc // define i_val_bind for csptr/cbox value (not i_val)
+#define i_val_ref Arc // define i_val_bind for csptr/cbox value (not i_val)
#include <stc/cstack.h>
#define i_type List
-#define i_val_bind Arc // as above
+#define i_val_ref Arc // as above
#include <stc/clist.h>
int main()
@@ -29,28 +29,28 @@ int main()
{
// POPULATE stack with shared pointers to Maps:
Map *map;
- map = Stack_push(&stack, Arc_new(Map_init()))->get;
- c_apply_pair (Map, emplace, map, {
+ map = Stack_push(&stack, Arc_from(Map_init()))->get;
+ c_apply(v, Map_emplace(map, c_pair(v)), Map_rawvalue, {
{"Joey", 1990}, {"Mary", 1995}, {"Joanna", 1992}
});
- map = Stack_push(&stack, Arc_new(Map_init()))->get;
- c_apply_pair (Map, emplace, map, {
+ map = Stack_push(&stack, Arc_from(Map_init()))->get;
+ c_apply(v, Map_emplace(map, c_pair(v)), Map_rawvalue, {
{"Rosanna", 2001}, {"Brad", 1999}, {"Jack", 1980}
});
// POPULATE list:
- map = List_push_back(&list, Arc_new(Map_init()))->get;
- c_apply_pair (Map, emplace, map, {
+ map = List_push_back(&list, Arc_from(Map_init()))->get;
+ c_apply(v, Map_emplace(map, c_pair(v)), Map_rawvalue, {
{"Steve", 1979}, {"Rick", 1974}, {"Tracy", 2003}
});
// Share two Maps from the stack with the list using emplace (clone the csptr):
- List_emplace_back(&list, stack.data[0]);
- List_emplace_back(&list, stack.data[1]);
+ List_push_back(&list, Arc_clone(stack.data[0]));
+ List_push_back(&list, Arc_clone(stack.data[1]));
// Clone (deep copy) a Map from the stack to the list
// List will contain two shared and two unshared maps.
- map = List_push_back(&list, Arc_new(Map_clone(*stack.data[1].get)))->get;
+ map = List_push_back(&list, Arc_from(Map_clone(*stack.data[1].get)))->get;
// Add one more element to the cloned map:
Map_emplace_or_assign(map, "CLONED", 2021);
diff --git a/examples/vikings.c b/examples/vikings.c
index 7a953059..9900e9b6 100644
--- a/examples/vikings.c
+++ b/examples/vikings.c
@@ -49,7 +49,7 @@ static inline RViking Viking_toraw(const Viking* vk) {
int main()
{
c_auto (Vikings, vikings) {
- c_apply_pair(Vikings, emplace, &vikings, {
+ c_apply(v, Vikings_emplace(&vikings, c_pair(v)), Vikings_rawvalue, {
{{"Einar", "Norway"}, 20},
{{"Olaf", "Denmark"}, 24},
{{"Harald", "Iceland"}, 12},
diff --git a/examples/words.c b/examples/words.c
index 6c0636dc..568f9557 100644
--- a/examples/words.c
+++ b/examples/words.c
@@ -13,7 +13,7 @@ int main1()
c_auto (cvec_str, words)
c_auto (cmap_str, word_map)
{
- c_apply(cvec_str, emplace_back, &words, {
+ c_apply(v, cvec_str_emplace_back(&words, v), const char*, {
"this", "sentence", "is", "not", "a", "sentence",
"this", "sentence", "is", "a", "hoax"
});