diff options
| author | Tyge Løvset <[email protected]> | 2021-10-02 12:54:52 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2021-10-02 12:54:52 +0200 |
| commit | 67f49709f60ce063b6a1c84364232d19eb71f800 (patch) | |
| tree | 31fca3680f5add75a11b77ac51ed45dd9ab80906 | |
| parent | ce9ef95a3beb195b5f29fde80a3d7b0f57f84c22 (diff) | |
| download | STC-modified-67f49709f60ce063b6a1c84364232d19eb71f800.tar.gz STC-modified-67f49709f60ce063b6a1c84364232d19eb71f800.zip | |
Some example improvements.
| -rw-r--r-- | examples/new_list.c | 40 | ||||
| -rw-r--r-- | examples/sharedptr.c | 42 | ||||
| -rw-r--r-- | include/stc/cvec.h | 3 |
3 files changed, 42 insertions, 43 deletions
diff --git a/examples/new_list.c b/examples/new_list.c index a46e6350..879f26aa 100644 --- a/examples/new_list.c +++ b/examples/new_list.c @@ -35,25 +35,23 @@ int point_compare(const Point* a, const Point* b) { int main() { - clist_i32 lst = clist_i32_init(); - clist_i32_push_back(&lst, 123); - clist_i32_del(&lst); - - clist_float flst = clist_float_init(); - clist_float_push_back(&flst, 123.3); - clist_float_del(&flst); - - clist_pnt plst = clist_pnt_init(); - clist_pnt_push_back(&plst, (Point){42, 14}); - clist_pnt_push_back(&plst, (Point){32, 94}); - clist_pnt_push_back(&plst, (Point){62, 81}); - clist_pnt_sort(&plst); - c_foreach (i, clist_pnt, plst) - printf(" (%d %d)", i.ref->x, i.ref->y); - puts(""); - clist_pnt_del(&plst); - - clist_str slst = clist_str_init(); - clist_str_emplace_back(&slst, "Hello, friend"); - clist_str_del(&slst); + c_auto (clist_i32, lst) + clist_i32_push_back(&lst, 123); + + c_auto (clist_pnt, plst) { + c_apply(clist_pnt, push_back, &plst, {{42, 14}, {32, 94}, {62, 81}}); + clist_pnt_sort(&plst); + + c_foreach (i, clist_pnt, plst) + printf(" (%d %d)", i.ref->x, i.ref->y); + puts(""); + } + + c_auto (clist_float, flst) { + c_apply(clist_float, push_back, &flst, {123.3, 321.2, -32.2, 78.2}); + c_foreach (i, clist_float, flst) printf(" %g", *i.ref); + } + + c_auto (clist_str, slst) + clist_str_emplace_back(&slst, "Hello, friend"); }
\ No newline at end of file diff --git a/examples/sharedptr.c b/examples/sharedptr.c index e062025a..94de3d38 100644 --- a/examples/sharedptr.c +++ b/examples/sharedptr.c @@ -1,48 +1,48 @@ #include <stdio.h>
-void int_del(int* x) {
- printf("del: %d\n", *x);
-}
+void int_del(int* x) { printf("del: %d\n", *x); }
#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_key_csptr csptr_int
-#define i_tag int
-#include <stc/csset.h> // csset_int: csset<csptr_int>
+#define i_tag intp
+#include <stc/csset.h> // csset_intp: csset<csptr_int>
#define i_val_csptr csptr_int
-#define i_tag int
-#include <stc/cvec.h> // cvec_int: cvec<csptr_int>
+#define i_tag intp
+#include <stc/cvec.h> // cvec_intp: cvec<csptr_int>
+
int main()
{
- c_auto (cvec_int, vec)
- c_auto (csset_int, set) // declare and init set, call del at scope exit
+ c_auto (cvec_intp, vec)
+ c_auto (csset_intp, set) // declare and init set, call del at scope exit
{
- cvec_int_push_back(&vec, csptr_int_make(2021));
- cvec_int_push_back(&vec, csptr_int_make(2012));
- cvec_int_push_back(&vec, csptr_int_make(2022));
- cvec_int_push_back(&vec, csptr_int_make(2015));
-
+ c_apply(cvec_intp, push_back, &vec, {
+ csptr_int_make(2021),
+ csptr_int_make(2012),
+ csptr_int_make(2022),
+ csptr_int_make(2015),
+ });
printf("vec:");
- c_foreach (i, cvec_int, vec) printf(" %d", *i.ref->get);
+ c_foreach (i, cvec_intp, vec) printf(" %d", *i.ref->get);
puts("");
// add odd numbers from vec to set
- c_foreach (i, cvec_int, vec)
+ c_foreach (i, cvec_intp, vec)
if (*i.ref->get & 1)
- csset_int_emplace(&set, *i.ref); // copy shared pointer => increments counter.
+ csset_intp_emplace(&set, *i.ref); // copy shared pointer => increments counter.
// erase the two last elements in vec
- cvec_int_pop_back(&vec);
- cvec_int_pop_back(&vec);
+ cvec_intp_pop_back(&vec);
+ cvec_intp_pop_back(&vec);
printf("vec:");
- c_foreach (i, cvec_int, vec) printf(" %d", *i.ref->get);
+ c_foreach (i, cvec_intp, vec) printf(" %d", *i.ref->get);
printf("\nset:");
- c_foreach (i, csset_int, set) printf(" %d", *i.ref->get);
+ c_foreach (i, csset_intp, 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 %u objects\n", *p.get, *p.use_count);
diff --git a/include/stc/cvec.h b/include/stc/cvec.h index 11de465c..27aae8c3 100644 --- a/include/stc/cvec.h +++ b/include/stc/cvec.h @@ -237,7 +237,8 @@ cx_memb(_init)(void) { STC_DEF void
cx_memb(_clear)(Self* self) {
- struct cvec_rep* rep = cvec_rep_(self); if (rep->cap) {
+ struct cvec_rep* rep = cvec_rep_(self);
+ if (rep->cap) {
for (cx_value_t *p = self->data, *q = p + rep->size; p != q; ++p)
i_valdel(p);
rep->size = 0;
|
