diff options
| author | Tyge Løvset <[email protected]> | 2023-03-27 19:57:09 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2023-03-27 19:57:09 +0200 |
| commit | e35036deef4fc8f17cc9221e2e666dfdb832ba78 (patch) | |
| tree | b41e45015be7454ef3f82092b558da6b365d6a57 | |
| parent | ede39bc98a758674485796174ea860515ec281e6 (diff) | |
| download | STC-modified-e35036deef4fc8f17cc9221e2e666dfdb832ba78.tar.gz STC-modified-e35036deef4fc8f17cc9221e2e666dfdb832ba78.zip | |
More RAII cleanup in examples. Also removed gauss1.c and new_deq.c
| -rw-r--r-- | misc/benchmarks/various/prng_bench.cpp | 2 | ||||
| -rw-r--r-- | misc/examples/arc_demo.c | 2 | ||||
| -rw-r--r-- | misc/examples/astar.c | 2 | ||||
| -rw-r--r-- | misc/examples/books.c | 2 | ||||
| -rw-r--r-- | misc/examples/coread.c | 13 | ||||
| -rw-r--r-- | misc/examples/forfilter.c | 39 | ||||
| -rw-r--r-- | misc/examples/forloops.c | 23 | ||||
| -rw-r--r-- | misc/examples/functor.c | 6 | ||||
| -rw-r--r-- | misc/examples/gauss1.c | 58 | ||||
| -rw-r--r-- | misc/examples/gauss2.c | 12 | ||||
| -rw-r--r-- | misc/examples/inits.c | 2 | ||||
| -rw-r--r-- | misc/examples/mapmap.c | 15 | ||||
| -rw-r--r-- | misc/examples/multimap.c | 2 | ||||
| -rw-r--r-- | misc/examples/new_deq.c | 46 | ||||
| -rw-r--r-- | misc/examples/prime.c | 5 | ||||
| -rw-r--r-- | misc/examples/printspan.c | 52 | ||||
| -rw-r--r-- | misc/examples/rawptr_elements.c | 78 | ||||
| -rw-r--r-- | misc/examples/regex2.c | 2 | ||||
| -rw-r--r-- | misc/examples/replace.c | 35 | ||||
| -rw-r--r-- | misc/examples/shape.c | 38 | ||||
| -rw-r--r-- | misc/examples/sidebyside.cpp | 11 | ||||
| -rw-r--r-- | misc/examples/splitstr.c | 1 |
22 files changed, 164 insertions, 282 deletions
diff --git a/misc/benchmarks/various/prng_bench.cpp b/misc/benchmarks/various/prng_bench.cpp index 6f4e0e47..be07f799 100644 --- a/misc/benchmarks/various/prng_bench.cpp +++ b/misc/benchmarks/various/prng_bench.cpp @@ -122,7 +122,7 @@ using namespace std; int main(void) { - enum {N = 2000000000}; + enum {N = 500000000}; uint16_t* recipient = new uint16_t[N]; static stc64_t rng; init_state(rng.state, 12345123); diff --git a/misc/examples/arc_demo.c b/misc/examples/arc_demo.c index 88555177..2339adbb 100644 --- a/misc/examples/arc_demo.c +++ b/misc/examples/arc_demo.c @@ -25,7 +25,7 @@ int main() const int years[] = {2021, 2012, 2022, 2015}; cvec_Arc vec = {0}; - c_forrange (i, c_ARRAYLEN(years)) + c_forrange (i, c_arraylen(years)) cvec_Arc_push(&vec, Arc_from(years[i])); printf("vec:"); diff --git a/misc/examples/astar.c b/misc/examples/astar.c index 6c850b08..7dd12d50 100644 --- a/misc/examples/astar.c +++ b/misc/examples/astar.c @@ -103,7 +103,7 @@ astar(cstr* maze, int width) { -1, 0, 0, width }, /* ~ ~ ~ ~ ~ ~ ~ */ { 1, 0, 0, width }, { -1, -1, 0, width }, { 0, -1, 0, width }, { 1, -1, 0, width }, }; - for (size_t i = 0; i < c_ARRAYLEN(deltas); i++) + for (size_t i = 0; i < c_arraylen(deltas); i++) { point delta = deltas[i]; point next = point_init(current.x + delta.x, current.y + delta.y, width); diff --git a/misc/examples/books.c b/misc/examples/books.c index 40fe877f..a62769b0 100644 --- a/misc/examples/books.c +++ b/misc/examples/books.c @@ -41,7 +41,7 @@ int main() // Look up the values associated with some keys. const char* to_find[] = {"Pride and Prejudice", "Alice's Adventure in Wonderland"}; - c_forrange (i, c_ARRAYLEN(to_find)) { + c_forrange (i, c_arraylen(to_find)) { const cmap_str_value* b = cmap_str_get(&book_reviews, to_find[i]); if (b) printf("%s: %s\n", cstr_str(&b->first), cstr_str(&b->second)); diff --git a/misc/examples/coread.c b/misc/examples/coread.c index c5f85e08..0a7f4816 100644 --- a/misc/examples/coread.c +++ b/misc/examples/coread.c @@ -20,17 +20,20 @@ bool file_nextline(struct file_nextline* U) while (cstr_getline(&U->line, U->fp)) cco_yield(true); - cco_final: // required label. + cco_final: // this label is required. printf("finish\n"); cstr_drop(&U->line); fclose(U->fp); cco_end(false); } -int main(void) { - struct file_nextline z = {__FILE__}; +int main(void) +{ + struct file_nextline it = {__FILE__}; int n = 0; - while (file_nextline(&z)) { - printf("%3d %s\n", ++n, cstr_str(&z.line)); + while (file_nextline(&it)) + { + printf("%3d %s\n", ++n, cstr_str(&it.line)); + //if (n == 10) cco_stop(&it); } } diff --git a/misc/examples/forfilter.c b/misc/examples/forfilter.c index fdf8be6f..0d72bd1b 100644 --- a/misc/examples/forfilter.c +++ b/misc/examples/forfilter.c @@ -28,17 +28,15 @@ void demo1(void) printf(" %d", *i.ref); puts(""); - int res, sum = 0; + int sum = 0; c_forfilter (i, IVec, vec, - c_flt_skipwhile(i, *i.ref != 80) && - c_flt_skip(i, 1) && - c_flt_skipwhile(i, *i.ref != 80) && - flt_isEven(i) && - flt_skipValue(i, 80) && - c_flt_take(i, 5) // short-circuit + c_flt_skipwhile(i, *i.ref != 80) && + c_flt_skip(i, 1) && + flt_isEven(i) && + flt_skipValue(i, 80) && + c_flt_take(i, 5) // short-circuit ){ - sum += res = flt_square(i); - printf(" %d", res); + sum += flt_square(i); } printf("\n sum: %d\n", sum); @@ -62,11 +60,13 @@ void demo2(void) { IVec vector = {0}; - c_forfilter (x, crange, crange_obj(INT64_MAX), - c_flt_skipwhile(x, *x.ref != 11) && - *x.ref % 2 != 0 && - c_flt_take(x, 5)) + c_forfilter (x, crange, crange_object(INT64_MAX), + c_flt_skipwhile(x, *x.ref != 11) && + (*x.ref % 2) != 0 && + c_flt_take(x, 5) + ){ IVec_push(&vector, (int)(*x.ref * *x.ref)); + } c_foreach (x, IVec, vector) printf(" %d", *x.ref); @@ -130,13 +130,14 @@ void demo5(void) crange R = crange_make(1963, INT32_MAX); c_forfilter (i, crange, R, - c_flt_skip(i,15) && - c_flt_skipwhile(i, flt_mid_decade(i)) && - c_flt_skip(i,30) && - flt_even(i) && - c_flt_take(i,5)) + c_flt_skip(i,15) && + c_flt_skipwhile(i, flt_mid_decade(i)) && + c_flt_skip(i,30) && + flt_even(i) && + c_flt_take(i,5) + ){ printf(" %lld", *i.ref); - + } puts(""); } diff --git a/misc/examples/forloops.c b/misc/examples/forloops.c index 54b49485..82126456 100644 --- a/misc/examples/forloops.c +++ b/misc/examples/forloops.c @@ -52,24 +52,17 @@ int main() puts("\n\nc_forpair:");
c_forpair (key, val, IMap, map)
printf(" (%d %d)", *_.key, *_.val);
-
- puts("\n\nc_forfilter 1:");
- c_forfilter (i, IVec, vec, c_flt_take(i, 3))
- printf(" %d", *i.ref);
-
+
#define isOdd(i) (*i.ref & 1)
- puts("\n\nc_forfilter 2:");
- c_forfilter (i, IVec, vec,
- c_flt_skipwhile(i, *i.ref != 65) &&
- c_flt_takewhile(i, *i.ref != 280) &&
- c_flt_skipwhile(i, isOdd(i)) &&
- isOdd(i) &&
- c_flt_skip(i, 2) &&
- c_flt_take(i, 2))
+ puts("\n\nc_forfilter:");
+ c_forfilter (i, IVec, vec,
+ isOdd(i) &&
+ c_flt_skip(i, 4) &&
+ c_flt_take(i, 4)
+ ){
printf(" %d", *i.ref);
- puts("");
- // 189
+ }
IVec_drop(&vec);
IMap_drop(&map);
diff --git a/misc/examples/functor.c b/misc/examples/functor.c index 8952c710..f37e41d5 100644 --- a/misc/examples/functor.c +++ b/misc/examples/functor.c @@ -43,14 +43,16 @@ static bool int_lambda(const int* x, const int* y) { return (*x ^ 1) < (*y ^ 1); int main() { - const int data[] = {1,8,5,6,3,4,0,9,7,2}, n = c_ARRAYLEN(data); + const int data[] = {1,8,5,6,3,4,0,9,7,2}, n = c_arraylen(data); printf("data: \t"); - c_forrange (i, n) printf("%d ", data[i]); + c_forrange (i, n) + printf("%d ", data[i]); puts(""); IPQueue q1 = {ipque_init(), int_less}; // Max priority queue IPQueue minq1 = {ipque_init(), int_greater}; // Min priority queue IPQueue q5 = {ipque_init(), int_lambda}; // Using lambda to compare elements. + c_forrange (i, n) ipque_push(&q1.Q, data[i]); print_queue("q1", q1); diff --git a/misc/examples/gauss1.c b/misc/examples/gauss1.c deleted file mode 100644 index 67871aa9..00000000 --- a/misc/examples/gauss1.c +++ /dev/null @@ -1,58 +0,0 @@ -#include <time.h> -#include <math.h> - -#include <stc/crandom.h> -#include <stc/cstr.h> - -// Declare int -> int hashmap. Uses typetag 'ii' for ints. -#define i_key int -#define i_val int -#define i_tag ii -#include <stc/cmap.h> - -// Declare int vector with entries from the cmap. -#define i_val cmap_ii_value -#define i_less(x, y) x->first < y->first -#define i_tag ii -#include <stc/cvec.h> - -int main() -{ - enum {N = 10000000}; - const double Mean = -12.0, StdDev = 6.0, Scale = 74; - - printf("Demo of gaussian / normal distribution of %d random samples\n", N); - - // Setup random engine with normal distribution. - uint64_t seed = (uint64_t)time(NULL); - stc64_t rng = stc64_new(seed); - stc64_normalf_t dist = stc64_normalf_new(Mean, StdDev); - - // Create and init histogram vec and map with defered destructors: - cvec_ii histvec = {0}; - cmap_ii histmap = {0}; - - c_forrange (N) { - int index = (int)round( stc64_normalf(&rng, &dist) ); - cmap_ii_insert(&histmap, index, 0).ref->second += 1; - } - - // Transfer map to vec and sort it by map keys. - c_foreach (i, cmap_ii, histmap) - cvec_ii_push(&histvec, (cmap_ii_value){i.ref->first, i.ref->second}); - - cvec_ii_sort(&histvec); - - // Print the gaussian bar chart - c_foreach (i, cvec_ii, histvec) { - int n = (int)(i.ref->second * StdDev * Scale * 2.5 / (double)N); - if (n > 0) { - printf("%4d ", i.ref->first); - c_forrange (n) printf("*"); - puts(""); - } - } - - cvec_ii_drop(&histvec); - cmap_ii_drop(&histmap); -} diff --git a/misc/examples/gauss2.c b/misc/examples/gauss2.c index 79397f0c..be514c12 100644 --- a/misc/examples/gauss2.c +++ b/misc/examples/gauss2.c @@ -6,7 +6,7 @@ // Declare int -> int sorted map. #define i_key int -#define i_val size_t +#define i_val int #include <stc/csmap.h> int main() @@ -22,16 +22,16 @@ int main() stc64_normalf_t dist = stc64_normalf_new(Mean, StdDev); // Create and init histogram map with defered destruct - csmap_int mhist = {0}; + csmap_int hist = {0}; cstr bar = {0}; c_forrange (N) { - int index = (int) round( stc64_normalf(&rng, &dist) ); - csmap_int_insert(&mhist, index, 0).ref->second += 1; + int index = (int)round( stc64_normalf(&rng, &dist) ); + csmap_int_insert(&hist, index, 0).ref->second += 1; } // Print the gaussian bar chart - c_forpair (index, count, csmap_int, mhist) { + c_forpair (index, count, csmap_int, hist) { int n = (int)((float)*_.count * StdDev * Scale * 2.5f / (float)N); if (n > 0) { cstr_resize(&bar, n, '*'); @@ -39,5 +39,5 @@ int main() } } cstr_drop(&bar); - csmap_int_drop(&mhist); + csmap_int_drop(&hist); } diff --git a/misc/examples/inits.c b/misc/examples/inits.c index 7530dd08..81bcdd3e 100644 --- a/misc/examples/inits.c +++ b/misc/examples/inits.c @@ -40,7 +40,7 @@ int main(void) const float nums[] = {4.0f, 2.0f, 5.0f, 3.0f, 1.0f}; // PRIORITY QUEUE - c_forrange (i, c_ARRAYLEN(nums)) + c_forrange (i, c_arraylen(nums)) cpque_f_push(&floats, nums[i]); puts("\npop and show high priorites first:"); diff --git a/misc/examples/mapmap.c b/misc/examples/mapmap.c index 8fc307ab..668da5de 100644 --- a/misc/examples/mapmap.c +++ b/misc/examples/mapmap.c @@ -7,24 +7,13 @@ #define i_key_str // name #define i_val_str // email #define i_keydrop(p) (printf("kdrop: %s\n", cstr_str(p)), cstr_drop(p)) // override -#include <stc/csmap.h> +#include <stc/cmap.h> // Departments: std::map<std::string, People> #define i_type Departments #define i_key_str // dep. name #define i_valclass People -// i_key_str implies: -// #define i_tag str -// #define i_key cstr -// #define i_keyclone cstr_clone -// #define i_keydrop cstr_drop -// #define i_cmp cstr_cmp -// #define i_hash cstr_hash -// i_valclass implies: -// #define i_val People -// #define i_valclone People_clone -// #define i_valdrop People_drop -#include <stc/csmap.h> +#include <stc/cmap.h> void add(Departments* deps, const char* name, const char* email, const char* dep) diff --git a/misc/examples/multimap.c b/misc/examples/multimap.c index 9c37db6c..d8981a81 100644 --- a/misc/examples/multimap.c +++ b/misc/examples/multimap.c @@ -71,7 +71,7 @@ int main() csmap_OL multimap = {0}; const clist_OL empty = clist_OL_init(); - for (size_t i = 0; i < c_ARRAYLEN(ol_data); ++i) + for (size_t i = 0; i < c_arraylen(ol_data); ++i) { struct OlympicsData* d = &ol_data[i]; OlympicLoc loc = {.year = d->year, diff --git a/misc/examples/new_deq.c b/misc/examples/new_deq.c deleted file mode 100644 index 467fd4e5..00000000 --- a/misc/examples/new_deq.c +++ /dev/null @@ -1,46 +0,0 @@ -#include <stc/cstr.h> -#include <stc/forward.h> - -forward_cdeq(cdeq_i32, int); -forward_cdeq(cdeq_pnt, struct Point); - -struct MyStruct { - cdeq_i32 intvec; - cdeq_pnt pntvec; -} typedef MyStruct; - - -#define i_val int -#define i_opt c_is_forward -#define i_tag i32 -#include <stc/cdeq.h> - -struct Point { int x, y; } typedef Point; -int point_cmp(const Point* a, const Point* b) { - int c = a->x - b->x; - return c ? c : a->y - b->y; -} - -#define i_val Point -#define i_cmp point_cmp -#define i_opt c_is_forward -#define i_tag pnt -#include <stc/cdeq.h> - - -int main() -{ - cdeq_pnt pvec = {0}; - - cdeq_pnt_push_back(&pvec, (Point){42, 14}); - cdeq_pnt_push_back(&pvec, (Point){32, 94}); - cdeq_pnt_push_front(&pvec, (Point){62, 81}); - - cdeq_pnt_sort(&pvec); - - c_foreach (i, cdeq_pnt, pvec) - printf(" (%d %d)", i.ref->x, i.ref->y); - puts(""); - - cdeq_pnt_drop(&pvec); -} diff --git a/misc/examples/prime.c b/misc/examples/prime.c index 18bf3490..9ffb2f53 100644 --- a/misc/examples/prime.c +++ b/misc/examples/prime.c @@ -44,8 +44,9 @@ int main(void) puts("Show the last 50 primes using a temporary crange generator:"); crange R = crange_make(n - 1, 0, -2); c_forfilter (i, crange, R, - cbits_test(&primes, *i.ref>>1) && - c_flt_take(i, 50)) { + cbits_test(&primes, *i.ref>>1) && + c_flt_take(i, 50) + ){ printf("%lld ", *i.ref); if (c_flt_last(i) % 10 == 0) puts(""); } diff --git a/misc/examples/printspan.c b/misc/examples/printspan.c index 127b07b9..7459ac77 100644 --- a/misc/examples/printspan.c +++ b/misc/examples/printspan.c @@ -16,7 +16,8 @@ using_cspan(intspan, int, 1); void printMe(intspan container) { printf("%d:", (int)cspan_size(&container)); - c_foreach (e, intspan, container) printf(" %d", *e.ref); + c_foreach (e, intspan, container) + printf(" %d", *e.ref); puts(""); } @@ -31,31 +32,26 @@ int main() intspan sp2 = cspan_from_array(arr); printMe( (intspan)cspan_subspan(&sp2, 1, 4) ); - cvec_int vec; - cstack_int stk; - cdeq_int deq; - csset_str set; - c_defer( - cvec_int_drop(&vec), - cstack_int_drop(&stk), - cdeq_int_drop(&deq), - csset_str_drop(&set) - ) { - vec = c_make(cvec_int, {1, 2, 3, 4, 5}); - printMe( (intspan)cspan_from(&vec) ); - - printMe( sp2 ); - - stk = c_make(cstack_int, {1, 2, 3, 4, 5, 6, 7}); - printMe( (intspan)cspan_from(&stk) ); - - deq = c_make(cdeq_int, {1, 2, 3, 4, 5, 6, 7, 8}); - printMe( (intspan)cspan_from(&deq) ); - - set = c_make(csset_str, {"5", "7", "4", "3", "8", "2", "1", "9", "6"}); - printf("%d:", (int)csset_str_size(&set)); - c_foreach (e, csset_str, set) - printf(" %s", cstr_str(e.ref)); - puts(""); - } + cvec_int vec = c_make(cvec_int, {1, 2, 3, 4, 5}); + printMe( (intspan)cspan_from(&vec) ); + + printMe( sp2 ); + + cstack_int stk = c_make(cstack_int, {1, 2, 3, 4, 5, 6, 7}); + printMe( (intspan)cspan_from(&stk) ); + + cdeq_int deq = c_make(cdeq_int, {1, 2, 3, 4, 5, 6, 7, 8}); + printMe( (intspan)cspan_from(&deq) ); + + csset_str set = c_make(csset_str, {"5", "7", "4", "3", "8", "2", "1", "9", "6"}); + printf("%d:", (int)csset_str_size(&set)); + c_foreach (e, csset_str, set) + printf(" %s", cstr_str(e.ref)); + puts(""); + + // cleanup + cvec_int_drop(&vec); + cstack_int_drop(&stk); + cdeq_int_drop(&deq); + csset_str_drop(&set); } diff --git a/misc/examples/rawptr_elements.c b/misc/examples/rawptr_elements.c index f46a7f5e..01bcdc44 100644 --- a/misc/examples/rawptr_elements.c +++ b/misc/examples/rawptr_elements.c @@ -2,58 +2,58 @@ #include <stdio.h> #include <stc/cstr.h> -// Map of cstr => int64 pointers -typedef int64_t inttype; -// Do it without cbox: +// Create cmap of cstr => long* #define i_type SIPtrMap #define i_key_str -#define i_val inttype* -#define i_valraw inttype -#define i_valfrom(raw) c_new(inttype, raw) +#define i_val long* +#define i_valraw long +#define i_valfrom(raw) c_new(long, raw) #define i_valto(x) **x -#define i_valclone(x) c_new(inttype, *x) +#define i_valclone(x) c_new(long, *x) #define i_valdrop(x) c_free(*x) #include <stc/cmap.h> -// With cbox: +// Alternatively, using cbox: #define i_type IBox -#define i_val int -#include <stc/cbox.h> //<stc/carc.h> +#define i_val long +#include <stc/cbox.h> // unique_ptr<long> alike. +// cmap of cstr => IBox #define i_type SIBoxMap #define i_key_str -#define i_valboxed IBox +#define i_valboxed IBox // i_valboxed: use properties from IBox automatically #include <stc/cmap.h> int main() { - SIPtrMap map = {0}, m1; - SIBoxMap m2 = {0}; - c_defer( - SIPtrMap_drop(&map), - SIPtrMap_drop(&m1), - SIBoxMap_drop(&m2) - ){ - printf("\nMap with pointer elements:\n"); - SIPtrMap_insert(&map, cstr_from("testing"), c_new(inttype, 1)); - SIPtrMap_insert(&map, cstr_from("done"), c_new(inttype, 2)); - - // Emplace: implicit key, val construction: - SIPtrMap_emplace(&map, "hello", 3); - SIPtrMap_emplace(&map, "goodbye", 4); - - m1 = SIPtrMap_clone(map); - - c_forpair (name, number, SIPtrMap, m1) - printf("%s: %" PRId64 "\n", cstr_str(_.name), **_.number); - - - puts("\nIBox map:"); - SIBoxMap_insert(&m2, cstr_from("Hello"), IBox_make(123)); - SIBoxMap_emplace(&m2, "World", 999); - c_forpair (name, number, SIBoxMap, m2) - printf("%s: %d\n", cstr_str(_.name), *_.number->get); - puts(""); - } + // These have the same behaviour, except IBox has a get member: + SIPtrMap map1 = {0}; + SIBoxMap map2 = {0}; + + printf("\nMap cstr => long*:\n"); + SIPtrMap_insert(&map1, cstr_from("Test1"), c_new(long, 1)); + SIPtrMap_insert(&map1, cstr_from("Test2"), c_new(long, 2)); + + // Emplace implicitly creates cstr from const char* and an owned long* from long! + SIPtrMap_emplace(&map1, "Test3", 3); + SIPtrMap_emplace(&map1, "Test4", 4); + + c_forpair (name, number, SIPtrMap, map1) + printf("%s: %ld\n", cstr_str(_.name), **_.number); + + puts("\nMap cstr => IBox:"); + SIBoxMap_insert(&map2, cstr_from("Test1"), IBox_make(1)); + SIBoxMap_insert(&map2, cstr_from("Test2"), IBox_make(2)); + + // Emplace implicitly creates cstr from const char* and IBox from long! + SIBoxMap_emplace(&map2, "Test3", 3); + SIBoxMap_emplace(&map2, "Test4", 4); + + c_forpair (name, number, SIBoxMap, map2) + printf("%s: %ld\n", cstr_str(_.name), *_.number->get); + puts(""); + + SIPtrMap_drop(&map1); + SIBoxMap_drop(&map2); } diff --git a/misc/examples/regex2.c b/misc/examples/regex2.c index 883dd112..3133f7c2 100644 --- a/misc/examples/regex2.c +++ b/misc/examples/regex2.c @@ -16,7 +16,7 @@ int main() }; cregex re = {0}; - c_forrange (i, c_ARRAYLEN(s)) + c_forrange (i, c_arraylen(s)) { int res = cregex_compile(&re, s[i].pattern); if (res < 0) { diff --git a/misc/examples/replace.c b/misc/examples/replace.c index c22c71ff..cf5b45cb 100644 --- a/misc/examples/replace.c +++ b/misc/examples/replace.c @@ -8,27 +8,28 @@ int main () // replace signatures used in the same order as described above: - // Ustring positions: 0123456789*123456789*12345 - cstr s = cstr_from(base); // "this is a test string." + // Ustring positions: 0123456789*123456789*12345 + cstr s = cstr_from(base); // "this is a test string." cstr m = cstr_clone(s); - c_defer (cstr_drop(&s), cstr_drop(&m)) { - cstr_append(&m, cstr_str(&m)); - cstr_append(&m, cstr_str(&m)); - printf("%s\n", cstr_str(&m)); - cstr_replace_at(&s, 9, 5, s2); // "this is an example string." (1) - printf("(1) %s\n", cstr_str(&s)); + cstr_append(&m, cstr_str(&m)); + cstr_append(&m, cstr_str(&m)); + printf("%s\n", cstr_str(&m)); - cstr_replace_at_sv(&s, 19, 6, c_sv(s3+7, 6)); // "this is an example phrase." (2) - printf("(2) %s\n", cstr_str(&s)); + cstr_replace_at(&s, 9, 5, s2); // "this is an example string." (1) + printf("(1) %s\n", cstr_str(&s)); - cstr_replace_at(&s, 8, 10, "just a"); // "this is just a phrase." (3) - printf("(3) %s\n", cstr_str(&s)); + cstr_replace_at_sv(&s, 19, 6, c_sv(s3+7, 6)); // "this is an example phrase." (2) + printf("(2) %s\n", cstr_str(&s)); - cstr_replace_at_sv(&s, 8, 6, c_sv("a shorty", 7)); // "this is a short phrase." (4) - printf("(4) %s\n", cstr_str(&s)); + cstr_replace_at(&s, 8, 10, "just a"); // "this is just a phrase." (3) + printf("(3) %s\n", cstr_str(&s)); - cstr_replace_at(&s, 22, 1, "!!!"); // "this is a short phrase!!!" (5) - printf("(5) %s\n", cstr_str(&s)); - } + cstr_replace_at_sv(&s, 8, 6, c_sv("a shorty", 7)); // "this is a short phrase." (4) + printf("(4) %s\n", cstr_str(&s)); + + cstr_replace_at(&s, 22, 1, "!!!"); // "this is a short phrase!!!" (5) + printf("(5) %s\n", cstr_str(&s)); + + c_drop(cstr, &s, &m); } diff --git a/misc/examples/shape.c b/misc/examples/shape.c index 2aabdcc2..e24f7fd7 100644 --- a/misc/examples/shape.c +++ b/misc/examples/shape.c @@ -139,23 +139,23 @@ void testShape(const Shape* shape) int main(void) { Shapes shapes = {0}; - c_defer (Shapes_drop(&shapes)) - { - Triangle* tri1 = c_new(Triangle, Triangle_from((Point){5, 7}, (Point){12, 7}, (Point){12, 20})); - Polygon* pol1 = c_new(Polygon, Polygon_init()); - Polygon* pol2 = c_new(Polygon, Polygon_init()); - - c_forlist (i, Point, {{50, 72}, {123, 73}, {127, 201}, {828, 333}}) - Polygon_addPoint(pol1, *i.ref); - - c_forlist (i, Point, {{5, 7}, {12, 7}, {12, 20}, {82, 33}, {17, 56}}) - Polygon_addPoint(pol2, *i.ref); - - Shapes_push(&shapes, &tri1->shape); - Shapes_push(&shapes, &pol1->shape); - Shapes_push(&shapes, &pol2->shape); - - c_foreach (i, Shapes, shapes) - testShape(*i.ref); - } + + Triangle* tri1 = c_new(Triangle, Triangle_from((Point){5, 7}, (Point){12, 7}, (Point){12, 20})); + Polygon* pol1 = c_new(Polygon, Polygon_init()); + Polygon* pol2 = c_new(Polygon, Polygon_init()); + + c_forlist (i, Point, {{50, 72}, {123, 73}, {127, 201}, {828, 333}}) + Polygon_addPoint(pol1, *i.ref); + + c_forlist (i, Point, {{5, 7}, {12, 7}, {12, 20}, {82, 33}, {17, 56}}) + Polygon_addPoint(pol2, *i.ref); + + Shapes_push(&shapes, &tri1->shape); + Shapes_push(&shapes, &pol1->shape); + Shapes_push(&shapes, &pol2->shape); + + c_foreach (i, Shapes, shapes) + testShape(*i.ref); + + Shapes_drop(&shapes); } diff --git a/misc/examples/sidebyside.cpp b/misc/examples/sidebyside.cpp index a817b5a5..a7c1008c 100644 --- a/misc/examples/sidebyside.cpp +++ b/misc/examples/sidebyside.cpp @@ -24,9 +24,8 @@ int main() { std::cout << i.first << ", " << i.second << std::endl; std::cout << std::endl; } - - IIMap hist = {0}; - c_defer (IIMap_drop(&hist)) { + { + IIMap hist = {0}; IIMap_insert(&hist, 12, 100).ref->second += 1; IIMap_insert(&hist, 13, 100).ref->second += 1; IIMap_insert(&hist, 12, 100).ref->second += 1; @@ -34,6 +33,7 @@ int main() { c_foreach (i, IIMap, hist) printf("%d, %d\n", i.ref->first, i.ref->second); puts(""); + IIMap_drop(&hist); } // =================================================== { @@ -44,15 +44,14 @@ int main() { std::cout << i.first << ", " << i.second << std::endl; std::cout << std::endl; } - - SIMap food = {0}; - c_defer (SIMap_drop(&food)) { + SIMap food = {0}; c_forlist (i, SIMap_raw, {{"burger", 5}, {"pizza", 12}, {"steak", 15}}) SIMap_emplace(&food, i.ref->first, i.ref->second); c_foreach (i, SIMap, food) printf("%s, %d\n", cstr_str(&i.ref->first), i.ref->second); puts(""); + SIMap_drop(&food); } } diff --git a/misc/examples/splitstr.c b/misc/examples/splitstr.c index bf90b5d4..2bc6fc07 100644 --- a/misc/examples/splitstr.c +++ b/misc/examples/splitstr.c @@ -15,5 +15,6 @@ int main() cregex re = cregex_from("[^ ]+"); c_formatch (i, &re, " Hello World C99! ") printf("'%.*s'\n", c_SV(i.match[0])); + cregex_drop(&re); } |
