summaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2021-12-12 21:33:22 +0100
committerTyge Løvset <[email protected]>2021-12-12 21:33:22 +0100
commitd57b9bb7666753c7cf7ab5a0da6d7d11f303c2af (patch)
tree258690df4a5ab62055261579967d6dfffaf5b0fd /examples
parent9cd20ebfc4f1e10153ff814085499223265ef902 (diff)
downloadSTC-modified-d57b9bb7666753c7cf7ab5a0da6d7d11f303c2af.tar.gz
STC-modified-d57b9bb7666753c7cf7ab5a0da6d7d11f303c2af.zip
- Added **cbox** type: container of one element: similar to std::unique_ptr / Rust Box.
- Replaced example for **csptr** in docs. - Added [**c_forpair**](docs/ccommon_api.md) macro: for-loop with "structural binding" as in c++. - Deprecated *csptr_X_make()*. Renamed to *csptr_X_new()*. Corresponding **cbox** method is *cbox_X_new()*. - Deprecated *c_default_fromraw(raw)*. Renamed to *c_default_clone(raw)*. - Deprecated `i_key_csptr` / `i_val_csptr`. Use `i_key_ref` / `i_val_ref` when specifying containers with **csptr** or **cbox** elements. - Deprecated `i_cnt`. Use `i_type` instead to define the full container type name. - Bugfixes and docs updates.
Diffstat (limited to 'examples')
-rw-r--r--examples/cpque.c2
-rw-r--r--examples/csmap_erase.c2
-rw-r--r--examples/ex_gauss2.c6
-rw-r--r--examples/inits.c10
-rw-r--r--examples/new_map.c3
-rw-r--r--examples/new_sptr.c13
-rw-r--r--examples/ptr_elems.c53
-rw-r--r--examples/queue.c2
-rw-r--r--examples/sharedptr.c55
-rw-r--r--examples/splitstr.c2
-rw-r--r--examples/sptr_ex.c13
-rw-r--r--examples/sptr_pthread.c3
12 files changed, 90 insertions, 74 deletions
diff --git a/examples/cpque.c b/examples/cpque.c
index db7bc02c..b3ee4adf 100644
--- a/examples/cpque.c
+++ b/examples/cpque.c
@@ -7,7 +7,7 @@ static int (*icmp_fn)(const int* x, const int* y);
#define i_val int
#define i_cmp icmp_fn
-#define i_cnt ipque
+#define i_type ipque
#include <stc/cpque.h>
#define imix_less(left, right) ((*(left) ^ 1) < (*(right) ^ 1))
diff --git a/examples/csmap_erase.c b/examples/csmap_erase.c
index bbb69a35..627c11d4 100644
--- a/examples/csmap_erase.c
+++ b/examples/csmap_erase.c
@@ -5,7 +5,7 @@
#define i_key int
#define i_val_str
-#define i_cnt mymap
+#define i_type mymap
#include <stc/csmap.h>
void printmap(mymap m)
diff --git a/examples/ex_gauss2.c b/examples/ex_gauss2.c
index b3890ecf..702c8ff3 100644
--- a/examples/ex_gauss2.c
+++ b/examples/ex_gauss2.c
@@ -30,11 +30,11 @@ int main()
// Print the gaussian bar chart
c_auto (cstr, bar)
- c_foreach (i, csmap_int, mhist) {
- size_t n = (size_t) (i.ref->second * StdDev * Scale * 2.5 / (float)N);
+ c_forpair (index, count, csmap_int, mhist) {
+ size_t n = (size_t) (_.count * StdDev * Scale * 2.5 / (float)N);
if (n > 0) {
cstr_resize(&bar, n, '*');
- printf("%4d %s\n", i.ref->first, bar.str);
+ printf("%4d %s\n", _.index, bar.str);
}
}
}
diff --git a/examples/inits.c b/examples/inits.c
index be0914c2..a9751ba5 100644
--- a/examples/inits.c
+++ b/examples/inits.c
@@ -13,8 +13,8 @@
typedef struct {int x, y;} ipair_t;
inline static int ipair_compare(const ipair_t* a, const ipair_t* b) {
- int cx = c_default_compare(&a->x, &b->x);
- return cx == 0 ? c_default_compare(&a->y, &b->y) : cx;
+ int c = c_default_compare(&a->x, &b->x);
+ return c ? c : c_default_compare(&a->y, &b->y);
}
@@ -45,7 +45,7 @@ int main(void)
}
puts("");
- // CVEC PRIORITY QUEUE
+ // PRIORITY QUEUE
cpque_f_make_heap(&floats);
c_apply(cpque_f, push, &floats, {40.0f, 20.0f, 50.0f, 30.0f, 10.0f});
@@ -89,8 +89,8 @@ int main(void)
cmap_cnt_emplace(&countries, "Norway", 0).ref->second += 20;
cmap_cnt_emplace(&countries, "Finland", 0).ref->second += 20;
- c_foreach (i, cmap_cnt, countries)
- printf("%s: %d\n", i.ref->first.str, i.ref->second);
+ c_forpair (country, health, cmap_cnt, countries)
+ printf("%s: %d\n", _.country.str, _.health);
puts("");
}
diff --git a/examples/new_map.c b/examples/new_map.c
index 7f4d19ee..8975cd77 100644
--- a/examples/new_map.c
+++ b/examples/new_map.c
@@ -21,6 +21,7 @@ int point_compare(const Point* a, const Point* b) {
return c ? c : c_default_compare(&a->y, &b->y);
}
+// Point => int map
#define i_key Point
#define i_val int
#define i_cmp point_compare
@@ -28,7 +29,7 @@ int point_compare(const Point* a, const Point* b) {
#define i_tag pnt
#include <stc/cmap.h>
-// int => int map
+// cstr => cstr map
#define i_key_str
#define i_val_str
#include <stc/cmap.h>
diff --git a/examples/new_sptr.c b/examples/new_sptr.c
index 036ce581..4a715160 100644
--- a/examples/new_sptr.c
+++ b/examples/new_sptr.c
@@ -2,7 +2,7 @@
struct Person { cstr name, last; } typedef Person;
-Person Person_init(const char* name, const char* last) {
+Person Person_from(const char* name, const char* last) {
return (Person){.name = cstr_from(name), .last = cstr_from(last)};
}
void Person_del(Person* p) {
@@ -12,6 +12,7 @@ void Person_del(Person* p) {
#define i_val Person
#define i_del Person_del
+#define i_opt c_no_compare
#define i_tag person
#include <stc/csptr.h>
@@ -21,21 +22,21 @@ void Person_del(Person* p) {
#define i_del(x) printf("del: %d\n", *(x))
#include <stc/csptr.h>
-#define i_val_csptr csptr_int
+#define i_val_ref csptr_int
#define i_tag iptr
#include <stc/cstack.h>
int main(void) {
- c_autovar (csptr_person p = csptr_person_make(Person_init("John", "Smiths")), csptr_person_del(&p))
+ c_autovar (csptr_person p = csptr_person_new(Person_from("John", "Smiths")), csptr_person_del(&p))
c_autovar (csptr_person q = csptr_person_clone(p), csptr_person_del(&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_make(10));
- cstack_iptr_push(&stk, csptr_int_make(20));
- cstack_iptr_push(&stk, csptr_int_make(30));
+ 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_emplace(&stk, *cstack_iptr_top(&stk));
cstack_iptr_emplace(&stk, *cstack_iptr_begin(&stk).ref);
diff --git a/examples/ptr_elems.c b/examples/ptr_elems.c
index 1b4f881a..7cb35043 100644
--- a/examples/ptr_elems.c
+++ b/examples/ptr_elems.c
@@ -3,38 +3,45 @@
struct { double x, y; } typedef Point;
-#define i_val Point*
-#define i_from(val) c_new(Point, *(val))
-#define i_del(pval) c_free(*(pval))
+// Set of Point pointers: define all template parameters "in-line"
+// Note it may be simpler to use a cbox for this.
+#define i_key Point*
+#define i_keydel(x) c_free(*(x))
+#define i_keyfrom(x) c_new(Point, *(x))
+#define i_hash(x, n) c_default_hash(*(x), sizeof *(x))
+#define i_equ(x, y) c_memcmp_equalto(*(x), *(y))
#define i_tag pnt
-#include <stc/cvec.h>
+#include <stc/cset.h>
+// Map of int64 pointers: For fun, define valraw as int64_t for easy emplace call!
#define i_key_str
-#define i_val int*
-#define i_valraw int
-#define i_valfrom(raw) c_new(int, raw)
-#define i_valto(pval) **(pval)
-#define i_valdel(pval) c_free(*(pval))
+#define i_valraw int64_t
+#define i_val i_valraw*
+#define i_valdel(x) c_free(*(x))
+#define i_valfrom(raw) c_new(i_valraw, raw)
+#define i_valto(x) **(x)
#include <stc/cmap.h>
int main()
{
- c_auto (cvec_pnt, vec, cpy)
+ c_auto (cset_pnt, set, cpy)
{
- printf("Vector with pointer elements:\n");
- // c++: vec.push_back(new Point{1.2, 3.4});
- cvec_pnt_push_back(&vec, c_new(Point, {1.2, 3.4}));
- cvec_pnt_push_back(&vec, c_new(Point, {6.1, 4.7}));
+ printf("Set with pointer elements:\n");
+ // c++: set.insert(new Point{1.2, 3.4});
+ cset_pnt_insert(&set, c_new(Point, {1.2, 3.4}));
+ Point* q = *cset_pnt_insert(&set, c_new(Point, {6.1, 4.7})).ref;
+ cset_pnt_insert(&set, c_new(Point, {5.7, 2.3}));
- cpy = cvec_pnt_clone(vec);
- cpy.data[1]->x = 100;
+ cpy = cset_pnt_clone(set);
+ cset_pnt_erase(&cpy, q);
+
+ printf("set:");
+ c_foreach (i, cset_pnt, set)
+ printf(" (%g %g)", i.ref[0]->x, i.ref[0]->y);
- printf("vec:");
- c_foreach (i, cvec_pnt, vec)
- printf(" (%g %g)", (*i.ref)->x, (*i.ref)->y);
printf("\ncpy:");
- c_foreach (i, cvec_pnt, cpy)
- printf(" (%g %g)", (*i.ref)->x, (*i.ref)->y);
+ c_foreach (i, cset_pnt, cpy)
+ printf(" (%g %g)", i.ref[0]->x, i.ref[0]->y);
puts("");
}
@@ -48,7 +55,7 @@ int main()
cmap_str_emplace(&map, "hello", 200);
cmap_str_emplace(&map, "goodbye", 400);
- c_foreach (i, cmap_str, map)
- printf("%s: %d\n", i.ref->first.str, *i.ref->second);
+ c_forpair (name, number, cmap_str, map)
+ printf("%s: %d\n", _.name.str, *_.number);
}
}
diff --git a/examples/queue.c b/examples/queue.c
index 2c2052e5..4acaa4d6 100644
--- a/examples/queue.c
+++ b/examples/queue.c
@@ -2,6 +2,8 @@
#include <stdio.h>
#define i_val int
+#define i_del(x) printf("drop %d\n", *(x))
+#define i_from c_default_clone
#define i_tag i
#include <stc/cqueue.h>
diff --git a/examples/sharedptr.c b/examples/sharedptr.c
index dbb3746f..a8e3e3ae 100644
--- a/examples/sharedptr.c
+++ b/examples/sharedptr.c
@@ -1,53 +1,56 @@
#include <stdio.h>
+#include <string.h>
-void int_del(int* x) { printf("del: %d\n", *x); }
+void int_del(int* x) {
+ printf("del: %d\n", *x);
+}
+// csptr implements its own clone method using ref counting, so 'i_valfrom'
+// should not be defined (ignored).
#define i_val int
-#define i_valdel int_del // optional func to show elements destroyed
-#include <stc/csptr.h> // csptr_int: shared pointer to int
+#define i_del int_del // optional func, just to display elements destroyed
+#include <stc/csptr.h> // csptr_int
-#define i_key_csptr csptr_int
-#define i_tag intp
-#include <stc/csset.h> // csset_intp: csset<csptr_int>
+#define i_key_ref csptr_int // note: use i_key_ref instead of i_key
+#define i_tag int // tag otherwise defaults to 'ref'
+#include <stc/csset.h> // csset_int (like: std::set<std::shared_ptr<int>>)
-#define i_val_csptr csptr_int
-#define i_tag intp
-#include <stc/cvec.h> // cvec_intp: cvec<csptr_int>
+#define i_val_ref csptr_int // Note: use i_val_ref instead of i_val
+#define i_tag int // tag otherwise defaults to 'ref'
+#include <stc/cvec.h> // cvec_int (like: std::vector<std::shared_ptr<int>>)
int main()
{
- c_auto (cvec_intp, vec)
- c_auto (csset_intp, set) // declare and init set, call del at scope exit
+ c_auto (cvec_int, vec) // declare and init vec, call del at scope exit
+ c_auto (csset_int, set) // declare and init set, call del at scope exit
{
- c_apply(cvec_intp, push_back, &vec, {
- csptr_int_make(2021),
- csptr_int_make(2012),
- csptr_int_make(2022),
- csptr_int_make(2015),
- });
+ const int years[] = {2021, 2012, 2022, 2015};
+ c_forrange (i, c_arraylen(years))
+ cvec_int_push_back(&vec, csptr_int_new(years[i]));
+
printf("vec:");
- c_foreach (i, cvec_intp, vec) printf(" %d", *i.ref->get);
+ c_foreach (i, cvec_int, vec) printf(" %d", *i.ref->get);
puts("");
// add odd numbers from vec to set
- c_foreach (i, cvec_intp, vec)
+ c_foreach (i, cvec_int, vec)
if (*i.ref->get & 1)
- csset_intp_emplace(&set, *i.ref); // copy shared pointer => increments counter.
+ csset_int_emplace(&set, *i.ref); // copy shared pointer => increments counter.
// erase the two last elements in vec
- cvec_intp_pop_back(&vec);
- cvec_intp_pop_back(&vec);
+ cvec_int_pop_back(&vec);
+ cvec_int_pop_back(&vec);
printf("vec:");
- c_foreach (i, cvec_intp, vec) printf(" %d", *i.ref->get);
+ c_foreach (i, cvec_int, vec) printf(" %d", *i.ref->get);
printf("\nset:");
- c_foreach (i, csset_intp, set) printf(" %d", *i.ref->get);
+ c_foreach (i, csset_int, set) printf(" %d", *i.ref->get);
c_autovar (csptr_int p = csptr_int_clone(vec.data[0]), csptr_int_del(&p)) {
- printf("\n%d is now owned by %lu objects\n", *p.get, *p.use_count);
+ printf("\n%d is now owned by %zu objects\n", *p.get, *p.use_count);
}
- puts("Done");
+ puts("\nDone");
}
}
diff --git a/examples/splitstr.c b/examples/splitstr.c
index 82914485..baa48d21 100644
--- a/examples/splitstr.c
+++ b/examples/splitstr.c
@@ -5,7 +5,7 @@ void print_split(csview str, csview sep)
csview token = csview_first_token(str, sep);
for (;;) {
// print non-null-terminated csview
- printf("\t\"%.*s\"\n", csview_ARG(token));
+ printf("\t\"%.*s\"\n", c_svarg(token));
if (csview_end(&token).ref == csview_end(&str).ref) break;
token = csview_next_token(str, sep, token);
}
diff --git a/examples/sptr_ex.c b/examples/sptr_ex.c
index 702edef9..e8e08b5f 100644
--- a/examples/sptr_ex.c
+++ b/examples/sptr_ex.c
@@ -20,9 +20,10 @@ void Song_del(Song* s) {
#define i_val Song
#define i_del Song_del
#define i_tag song
+#define i_opt c_no_compare
#include <stc/csptr.h> // define csptr_song
-#define i_val_csptr csptr_song
+#define i_val_ref csptr_song
#define i_tag song
#include <stc/cvec.h>
@@ -31,9 +32,9 @@ void example3()
c_auto (cvec_song, v, v2)
{
c_apply(cvec_song, push_back, &v, {
- csptr_song_make(Song_from("Bob Dylan", "The Times They Are A Changing")),
- csptr_song_make(Song_from("Aretha Franklin", "Bridge Over Troubled Water")),
- csptr_song_make(Song_from("Thalia", "Entre El Mar y Una Estrella"))
+ csptr_song_new(Song_from("Bob Dylan", "The Times They Are A Changing")),
+ csptr_song_new(Song_from("Aretha Franklin", "Bridge Over Troubled Water")),
+ csptr_song_new(Song_from("Thalia", "Entre El Mar y Una Estrella"))
});
c_foreach (s, cvec_song, v)
@@ -41,8 +42,8 @@ void example3()
cvec_song_emplace_back(&v2, *s.ref); // note: calls csptr_song_clone()
c_apply(cvec_song, push_back, &v2, {
- csptr_song_make(Song_from("Michael Jackson", "Billie Jean")),
- csptr_song_make(Song_from("Rihanna", "Stay")),
+ csptr_song_new(Song_from("Michael Jackson", "Billie Jean")),
+ csptr_song_new(Song_from("Rihanna", "Stay")),
});
c_foreach (s, cvec_song, v2)
diff --git a/examples/sptr_pthread.c b/examples/sptr_pthread.c
index 0a1b7c79..d82ea938 100644
--- a/examples/sptr_pthread.c
+++ b/examples/sptr_pthread.c
@@ -15,6 +15,7 @@ void Base_del(Base* b) { printf("Base::~Base()\n"); }
#define i_val Base
#define i_del Base_del
+#define i_opt c_no_compare
#define i_tag base
#include <stc/csptr.h>
@@ -34,7 +35,7 @@ void* thr(csptr_base* lp)
int main()
{
- csptr_base p = csptr_base_make((Base){42});
+ csptr_base p = csptr_base_new((Base){42});
printf("Created a Base\n"
" p.get() = %p, p.use_count() = %ld\n", (void*)p.get, *p.use_count);