diff options
| author | Tyge Løvset <[email protected]> | 2023-02-20 14:44:25 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2023-02-20 14:44:25 +0100 |
| commit | a8fc8ac4e8c1481300e0d46bbd376f32ebeb4635 (patch) | |
| tree | 68dad18a7cf235916b5a1a2257bb80e69bb5d23e /misc/examples | |
| parent | 0e3d07dbd991c1f1a691b24655c37ddab660a9d9 (diff) | |
| download | STC-modified-a8fc8ac4e8c1481300e0d46bbd376f32ebeb4635.tar.gz STC-modified-a8fc8ac4e8c1481300e0d46bbd376f32ebeb4635.zip | |
Added c_eraseremove_if() for cvec, cdeq, cstack, cqueue in ccommon.h. Some cleanup.
Diffstat (limited to 'misc/examples')
| -rw-r--r-- | misc/examples/box2.c | 74 | ||||
| -rw-r--r-- | misc/examples/list.c | 8 |
2 files changed, 42 insertions, 40 deletions
diff --git a/misc/examples/box2.c b/misc/examples/box2.c index 943b2ae8..f7d21976 100644 --- a/misc/examples/box2.c +++ b/misc/examples/box2.c @@ -1,36 +1,31 @@ -// https://doc.rust-lang.org/rust-by-example/std/box.html - -#include <stdlib.h> +// example: https://doc.rust-lang.org/rust-by-example/std/box.html #include <stdio.h> -#include <string.h> -#include <stc/ccommon.h> -struct { +typedef struct { double x; double y; -} typedef Point; +} Point; // A Rectangle can be specified by where its top left and bottom right // corners are in space -struct { +typedef struct { Point top_left; Point bottom_right; -} typedef Rectangle; +} Rectangle; #define i_val Point -#define i_opt c_no_cmp +#define i_no_cmp #include <stc/cbox.h> // cbox_Point #define i_val Rectangle -#define i_opt c_no_cmp +#define i_no_cmp #include <stc/cbox.h> // cbox_Rectangle // Box in box: -#define i_valboxed cbox_Point // NB: use i_valboxed when value is a cbox or carc! - // it will auto define i_valdrop, i_valfrom, and i_cmp. -#define i_tag BoxPoint -#define i_opt c_no_cmp -#include <stc/cbox.h> // cbox_BoxPoint +#define i_valboxed cbox_Point // NB: use i_valboxed when value is a cbox or carc! +#define i_type BoxBoxPoint +#define i_no_cmp +#include <stc/cbox.h> // BoxBoxPoint Point origin(void) { return (Point){ .x=1.0, .y=2.0 }; @@ -38,7 +33,7 @@ Point origin(void) { cbox_Point boxed_origin(void) { // Allocate this point on the heap, and return a pointer to it - return cbox_Point_make((Point){ .x=2.0, .y=3.0 }); + return cbox_Point_make((Point){ .x=1.0, .y=2.0 }); } @@ -47,44 +42,47 @@ int main(void) { Point point = origin(); Rectangle rectangle = (Rectangle){ .top_left = origin(), - .bottom_right = (Point){ .x=3.0, .y=-4.0 } + .bottom_right = { .x=3.0, .y=-4.0 } }; - // Declare auto-deleted box objects + // Declare RAII'ed box objects c_auto (cbox_Rectangle, boxed_rectangle) c_auto (cbox_Point, boxed_point) - c_auto (cbox_BoxPoint, box_in_a_box, boxbox2) + c_auto (BoxBoxPoint, box_in_a_box) { // Heap allocated rectangle boxed_rectangle = cbox_Rectangle_make((Rectangle){ .top_left = origin(), - .bottom_right = (Point){ .x=3.0, .y=-4.0 } + .bottom_right = { .x=3.0, .y=-4.0 } }); // The output of functions can be boxed - boxed_point = cbox_Point_from(origin()); + boxed_point = cbox_Point_make(origin()); // Double indirection - box_in_a_box = cbox_BoxPoint_make(boxed_origin()); - boxbox2 = cbox_BoxPoint_from(point); // !! - printf("boxbox2: x=%f\n", boxbox2.get->get->x); - - printf("Point occupies %" c_ZU " bytes on the stack\n", - sizeof(point)); - printf("Rectangle occupies %" c_ZU " bytes on the stack\n", - sizeof(rectangle)); + //box_in_a_box = BoxBoxPoint_make(boxed_origin()); + //printf("box_in_a_box: x = %g\n", box_in_a_box.get->get->x); + + // Can use from(raw) and toraw instead: + box_in_a_box = BoxBoxPoint_from(origin()); + printf("box_in_a_box: x = %g\n", BoxBoxPoint_toraw(&box_in_a_box).x); + + printf("Point occupies %d bytes on the stack\n", + (int)sizeof(point)); + printf("Rectangle occupies %d bytes on the stack\n", + (int)sizeof(rectangle)); // box size == pointer size - printf("Boxed point occupies %" c_ZU " bytes on the stack\n", - sizeof(boxed_point)); - printf("Boxed rectangle occupies %" c_ZU " bytes on the stack\n", - sizeof(boxed_rectangle)); - printf("Boxed box occupies %" c_ZU " bytes on the stack\n", - sizeof(box_in_a_box)); + printf("Boxed point occupies %d bytes on the stack\n", + (int)sizeof(boxed_point)); + printf("Boxed rectangle occupies %d bytes on the stack\n", + (int)sizeof(boxed_rectangle)); + printf("Boxed box occupies %d bytes on the stack\n", + (int)sizeof(box_in_a_box)); // Copy the data contained in `boxed_point` into `unboxed_point` Point unboxed_point = *boxed_point.get; - printf("Unboxed point occupies %" c_ZU " bytes on the stack\n", - sizeof(unboxed_point)); + printf("Unboxed point occupies %d bytes on the stack\n", + (int)sizeof(unboxed_point)); } } diff --git a/misc/examples/list.c b/misc/examples/list.c index 6a655800..b345bd16 100644 --- a/misc/examples/list.c +++ b/misc/examples/list.c @@ -17,6 +17,7 @@ int main() { int m = 0; c_forrange (n) clist_fx_push_back(&list, stc64_uniformf(&rng, &dist)), ++m; + double sum = 0.0; printf("sumarize %d:\n", m); c_foreach (i, clist_fx, list) @@ -46,18 +47,21 @@ int main() { const double* v = clist_fx_get(&list, 30); printf("found: %f\n", *v); - c_foreach (i, clist_fx, list) printf(" %g", *i.ref); + c_foreach (i, clist_fx, list) + printf(" %g", *i.ref); puts(""); clist_fx_remove(&list, 30); clist_fx_insert_at(&list, clist_fx_begin(&list), 5); // same as push_front() clist_fx_push_back(&list, 500); clist_fx_push_front(&list, 1964); - clist_fx_iter it = clist_fx_begin(&list); + printf("Full: "); c_foreach (i, clist_fx, list) printf(" %g", *i.ref); + printf("\nSubs: "); + clist_fx_iter it = clist_fx_begin(&list); c_foreach (i, clist_fx, clist_fx_advance(it, 4), clist_fx_end(&list)) printf(" %g", *i.ref); puts(""); |
