diff options
| author | Tyge Løvset <[email protected]> | 2021-05-24 19:12:22 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2021-05-24 19:12:22 +0200 |
| commit | 1490bbe1aa98b4081c226060834d54d05bf7e6e3 (patch) | |
| tree | 5c3cd1aa6cee6730a9923c34ee3bdf5d60456be2 | |
| parent | b2f0250fd70c54ca55bd430f748b3ad21c24a86a (diff) | |
| download | STC-modified-1490bbe1aa98b4081c226060834d54d05bf7e6e3.tar.gz STC-modified-1490bbe1aa98b4081c226060834d54d05bf7e6e3.zip | |
Split c_fordefer() into c_forvar() and c_fordefer() macros.
39 files changed, 90 insertions, 89 deletions
diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index 08b92396..f26d55b7 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -2,23 +2,25 @@ The following handy macros are safe to use, i.e. have no side-effects. -### c_fordefer, c_forbuffer +### c_forvar, c_fordefer, c_forbuffer General ***defer*** mechanics for resource acquisition. These macros allows to specify the release of the resource where the resource acquisition is made. This ensures that resources are released after usage. -**NB**: These macros are one-time **for**-loops. ***Do only*** use `continue` in order to break out of a -`c_fordefer` / `c_forbuffer`-block. ***Do not*** use `return`, `goto` or `break`, as it will prevent the -`release`-statement to be executed at the end. +**Note**: These macros are one-time executed **for**-loops. ***Only*** use `continue` in order to break out +of a `c_forvar` / `c_fordefer`-block. ***Do not*** use `return`, `goto` or `break`, as it will prevent the +`release`-statement to be executed at the end. The same applies for `c_forbuffer`. | Usage | Description | |:---------------------------------|:--------------------------------------------------| -| `c_fordefer (acquire, release)` | Do `acquire`. Defer `release` to end of block | -| `c_fordefer (release)` | Defer execution of `release` to end of block | +| `c_forvar (acquire, release...)` | Do `acquire`. Defer `release` to end of block | +| `c_fordefer (release...)` | Defer execution of `release` to end of block | | `c_forbuffer (buf, type, n)` | Declare, allocate and free memory buffer | -The `acquire`argument must be of the form: `type var = getResource`. Note that the `release` argument can be -a list of statements enclosed in parathesises. **c_forbuffer** uses stack memory if buffer is <= to 256 bytes, -othewise it uses more expensive heap memory. +The `acquire`argument must be of the form: `type var = GetResource`. Note that the `release` argument can be +a list of statements enclosed in parathesises. `c_forvar` can handle multiple variables of same type by using +the `c_arg` macro: `c_forvar (c_arg(cstr s1 = cstr_lit("Hello"), s2 = cstr_lit("world"), cstr_del(&s1), cstr_del(&s2))`. + +**c_forbuffer** uses stack memory if buffer is <= to 256 bytes, othewise it uses the slower heap memory. ```c // Example: Load each line of a text file into a vector of strings @@ -33,8 +35,8 @@ cvec_str readFile(const char* name) // receiver should check errno variable cvec_str vec = cvec_str_init(); - c_fordefer (FILE* fp = fopen(name, "r"), fclose(fp)) { - c_fordefer (cstr line = cstr_null, cstr_del(&line)) + c_forvar (FILE* fp = fopen(name, "r"), fclose(fp)) { + c_forvar (cstr line = cstr_null, cstr_del(&line)) while (cstr_getline(&line, fp)) cvec_str_emplace_back(&vec, line.str); } @@ -43,7 +45,7 @@ cvec_str readFile(const char* name) int main() { - c_fordefer (cvec_str x = readFile(__FILE__), cvec_str_del(&x)) + c_forvar (cvec_str x = readFile(__FILE__), cvec_str_del(&x)) c_foreach (i, cvec_str, x) printf("%s\n", i.ref->str); } diff --git a/docs/cmap_api.md b/docs/cmap_api.md index 6bd998ba..ea9efb39 100644 --- a/docs/cmap_api.md +++ b/docs/cmap_api.md @@ -208,7 +208,7 @@ using_cmap(vi, Vec3i, int, c_memcmp_equals, // bitwise equals int main() { // Define map with defered destruct - c_fordefer (cmap_vi vecs = cmap_vi_init(), cmap_vi_del(&vecs)) + c_forvar (cmap_vi vecs = cmap_vi_init(), cmap_vi_del(&vecs)) { cmap_vi_insert(&vecs, (Vec3i){100, 0, 0}, 1); cmap_vi_insert(&vecs, (Vec3i){ 0, 100, 0}, 2); @@ -239,7 +239,7 @@ using_cmap(iv, int, Vec3i); int main() { - c_fordefer (cmap_iv vecs = cmap_iv_init(), cmap_iv_del(&vecs)) + c_forvar (cmap_iv vecs = cmap_iv_init(), cmap_iv_del(&vecs)) { cmap_iv_insert(&vecs, 1, (Vec3i){100, 0, 0}); cmap_iv_insert(&vecs, 2, (Vec3i){ 0, 100, 0}); diff --git a/docs/cpque_api.md b/docs/cpque_api.md index 503e322e..441afc0a 100644 --- a/docs/cpque_api.md +++ b/docs/cpque_api.md @@ -69,7 +69,7 @@ int main() stc64_uniform_t dist = stc64_uniform_init(0, N * 10); // Declare heap, with defered del() - c_fordefer (cpque_i heap = cpque_i_init(), cpque_i_del(&heap)) + c_forvar (cpque_i heap = cpque_i_init(), cpque_i_del(&heap)) { // Push ten million random numbers to priority queue, plus some negative ones. c_forrange (N) diff --git a/docs/csmap_api.md b/docs/csmap_api.md index 8b6e1a59..1402bd78 100644 --- a/docs/csmap_api.md +++ b/docs/csmap_api.md @@ -152,7 +152,7 @@ int main() {100, "Red"}, {110, "Blue"}, }); - c_fordefer (csmap_id_del(&idnames)) + c_forvar (csmap_id_del(&idnames)) { /* put replaces existing mapped value: */ csmap_id_emplace_or_assign(&idnames, 110, "White"); @@ -192,7 +192,7 @@ using_csmap(vi, Vec3i, int, Vec3i_compare); int main() { - c_fordefer (csmap_vi vecs = csmap_vi_init(), csmap_vi_del(&vecs)) + c_forvar (csmap_vi vecs = csmap_vi_init(), csmap_vi_del(&vecs)) { csmap_vi_insert(&vecs, (Vec3i){100, 0, 0}, 1); csmap_vi_insert(&vecs, (Vec3i){ 0, 100, 0}, 2); @@ -223,7 +223,7 @@ using_csmap(iv, int, Vec3i); int main() { - c_fordefer (csmap_iv vecs = csmap_iv_init(), csmap_iv_del(&vecs)) + c_forvar (csmap_iv vecs = csmap_iv_init(), csmap_iv_del(&vecs)) { csmap_iv_insert(&vecs, 1, (Vec3i){100, 0, 0}); csmap_iv_insert(&vecs, 2, (Vec3i){ 0, 100, 0}); diff --git a/docs/csview_api.md b/docs/csview_api.md index 26857938..97ae5694 100644 --- a/docs/csview_api.md +++ b/docs/csview_api.md @@ -128,7 +128,7 @@ int main() csview text = c_lit("The length of this literal is evaluated at compile time and stored in csview text."); printf("%s\nLength: %zu\n\n", text.str, text.size); - c_fordefer (cmap_si map = csmap_si_init(), csmap_si_del(&map)) + c_forvar (cmap_si map = csmap_si_init(), csmap_si_del(&map)) { cmap_si_emplace(&map, c_lit("hello"), 100); cmap_si_emplace(&map, c_lit("world"), 200); @@ -187,7 +187,7 @@ int main() print_split(c_lit("This has no matching separator"), c_lit("xx")); puts(""); - c_fordefer (cvec_str v = string_split(c_lit("Split,this,,string,now,"), c_lit(",")), cvec_str_del(&v)) + c_forvar (cvec_str v = string_split(c_lit("Split,this,,string,now,"), c_lit(",")), cvec_str_del(&v)) c_foreach (i, cvec_str, v) printf("\"%s\"\n", i.ref->str); } diff --git a/examples/advanced.c b/examples/advanced.c index 1d8a8cf2..147ba041 100644 --- a/examples/advanced.c +++ b/examples/advanced.c @@ -41,7 +41,7 @@ using_cmap_keydef(vk, Viking, int, vikingraw_equals, vikingraw_hash, int main() { - c_fordefer (cmap_vk vikings = cmap_vk_init(), cmap_vk_del(&vikings)) { + c_forvar (cmap_vk vikings = cmap_vk_init(), cmap_vk_del(&vikings)) { c_emplace(cmap_vk, vikings, { { {"Einar", "Norway"}, 20}, { {"Olaf", "Denmark"}, 24}, diff --git a/examples/birthday.c b/examples/birthday.c index 80802572..0dbb544a 100644 --- a/examples/birthday.c +++ b/examples/birthday.c @@ -35,7 +35,7 @@ void test_distribution(void) stc64_t rng = stc64_init(seed);
const size_t N = 1ull << BITS ;
- c_fordefer (cmap_x map = cmap_x_init(), cmap_x_del(&map)) {
+ c_forvar (cmap_x map = cmap_x_init(), cmap_x_del(&map)) {
c_forrange (N) {
uint64_t k = stc64_rand(&rng);
cmap_x_emplace(&map, k & 0xf, 0).ref->second += 1;
diff --git a/examples/bits.c b/examples/bits.c index e478321c..84bdbb6b 100644 --- a/examples/bits.c +++ b/examples/bits.c @@ -3,7 +3,7 @@ int main()
{
- c_fordefer (cbits set = cbits_with_size(23, true), cbits_del(&set)) {
+ c_forvar (cbits set = cbits_with_size(23, true), cbits_del(&set)) {
printf("count %zu, %zu\n", cbits_count(set), set.size);
cbits s1 = cbits_from_str("1110100110111");
char buf[256];
@@ -35,7 +35,7 @@ int main() printf("%d", cbits_test(set, i));
puts("");
- c_fordefer (cbits s2 = cbits_clone(set), cbits_del(&s2)) {
+ c_forvar (cbits s2 = cbits_clone(set), cbits_del(&s2)) {
cbits_flip_all(&s2);
cbits_set(&s2, 16);
cbits_set(&s2, 17);
diff --git a/examples/cpque.c b/examples/cpque.c index 937b7ac9..3c22fdfd 100644 --- a/examples/cpque.c +++ b/examples/cpque.c @@ -31,9 +31,9 @@ int main() {
const int data[] = {1,8,5,6,3,4,0,9,7,2};
- c_fordefer (cpque_imax q = cpque_imax_init(), cpque_imax_del(&q)) // init() and defered del()
- c_fordefer (cpque_imin q2 = cpque_imin_init(), cpque_imin_del(&q2))
- c_fordefer (cpque_imix q3 = cpque_imix_init(), cpque_imix_del(&q3))
+ c_forvar (cpque_imax q = cpque_imax_init(), cpque_imax_del(&q)) // init() and defered del()
+ c_forvar (cpque_imin q2 = cpque_imin_init(), cpque_imin_del(&q2))
+ c_forvar (cpque_imix q3 = cpque_imix_init(), cpque_imix_del(&q3))
{
c_forrange (n, c_arraylen(data))
cpque_imax_push(&q, n);
diff --git a/examples/csmap_erase.c b/examples/csmap_erase.c index 58a90d4b..35afbe3a 100644 --- a/examples/csmap_erase.c +++ b/examples/csmap_erase.c @@ -15,7 +15,7 @@ void printmap(csmap_my map) int main()
{
- c_fordefer (csmap_my m1 = csmap_my_init(), csmap_my_del(&m1))
+ c_forvar (csmap_my m1 = csmap_my_init(), csmap_my_del(&m1))
{
// Fill in some data to test with, one at a time
csmap_my_emplace(&m1, 1, "A");
@@ -32,7 +32,7 @@ int main() printmap(m1);
}
- c_fordefer (csmap_my m2 = csmap_my_init(), csmap_my_del(&m2))
+ c_forvar (csmap_my m2 = csmap_my_init(), csmap_my_del(&m2))
{
// Fill in some data to test with, one at a time, using emplace
c_emplace(csmap_my, m2, {
@@ -54,7 +54,7 @@ int main() printmap(m2);
}
- c_fordefer (csmap_my m3 = csmap_my_init(), csmap_my_del(&m3))
+ c_forvar (csmap_my m3 = csmap_my_init(), csmap_my_del(&m3))
{
// Fill in some data to test with, one at a time, using emplace
csmap_my_emplace(&m3, 1, "red");
diff --git a/examples/csmap_find.c b/examples/csmap_find.c index 4fcedb43..314e702e 100644 --- a/examples/csmap_find.c +++ b/examples/csmap_find.c @@ -38,8 +38,8 @@ void findit(csmap_istr c, csmap_istr_key_t val) { int main()
{
- c_fordefer (csmap_istr m1 = csmap_istr_init(), csmap_istr_del(&m1))
- c_fordefer (cvec_istr v = cvec_istr_init(), cvec_istr_del(&v)) {
+ c_forvar (csmap_istr m1 = csmap_istr_init(), csmap_istr_del(&m1))
+ c_forvar (cvec_istr v = cvec_istr_init(), cvec_istr_del(&v)) {
c_emplace(csmap_istr, m1, { { 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 0f32e586..684d499a 100644 --- a/examples/csmap_insert.c +++ b/examples/csmap_insert.c @@ -25,7 +25,7 @@ void print_istr(csmap_istr map) { int main()
{
// insert single values
- c_fordefer (csmap_ii m1 = csmap_ii_init(), csmap_ii_del(&m1)) {
+ c_forvar (csmap_ii m1 = csmap_ii_init(), csmap_ii_del(&m1)) {
csmap_ii_insert(&m1, 1, 10);
csmap_ii_insert(&m1, 2, 20);
@@ -52,9 +52,8 @@ int main() }
// The templatized version inserting a jumbled range
- // c_fordefer_var(type, v) is shorthand for: c_fordefer (type v = type_init(), type_del(&v))
- c_fordefer (csmap_ii m2 = csmap_ii_init(), csmap_ii_del(&m2))
- c_fordefer (cvec_ii v = cvec_ii_init(), cvec_ii_del(&v)) {
+ c_forvar (csmap_ii m2 = csmap_ii_init(), csmap_ii_del(&m2))
+ c_forvar (cvec_ii v = cvec_ii_init(), cvec_ii_del(&v)) {
cvec_ii_push_back(&v, (cvec_ii_value_t){43, 294});
cvec_ii_push_back(&v, (cvec_ii_value_t){41, 262});
cvec_ii_push_back(&v, (cvec_ii_value_t){45, 330});
@@ -73,7 +72,7 @@ int main() }
// The templatized versions move-constructing elements
- c_fordefer (csmap_istr m3 = csmap_istr_init(), csmap_istr_del(&m3)) {
+ c_forvar (csmap_istr m3 = csmap_istr_init(), csmap_istr_del(&m3)) {
csmap_istr_value_t ip1 = {475, cstr_lit("blue")}, ip2 = {510, cstr_lit("green")};
// single element
@@ -88,7 +87,7 @@ int main() puts("");
}
- c_fordefer (csmap_ii m4 = csmap_ii_init(), csmap_ii_del(&m4)) {
+ c_forvar (csmap_ii m4 = csmap_ii_init(), csmap_ii_del(&m4)) {
// Insert the elements from an initializer_list
c_emplace(csmap_ii, m4, { { 4, 44 }, { 2, 22 }, { 3, 33 }, { 1, 11 }, { 5, 55 } });
puts("After initializer_list insertion, m4 contains:");
diff --git a/examples/demos.c b/examples/demos.c index a9fad4ab..bb4deb22 100644 --- a/examples/demos.c +++ b/examples/demos.c @@ -9,7 +9,7 @@ void stringdemo1()
{
printf("\nSTRINGDEMO1\n");
- c_fordefer (cstr cs = cstr_from("one-nine-three-seven-five"), cstr_del(&cs))
+ c_forvar (cstr cs = cstr_from("one-nine-three-seven-five"), cstr_del(&cs))
{
printf("%s.\n", cs.str);
@@ -40,7 +40,7 @@ using_cvec(ix, int64_t); // ix is just an example tag name. void vectordemo1()
{
printf("\nVECTORDEMO1\n");
- c_fordefer (cvec_ix bignums = cvec_ix_with_capacity(100), cvec_ix_del(&bignums))
+ c_forvar (cvec_ix bignums = cvec_ix_with_capacity(100), cvec_ix_del(&bignums))
{
cvec_ix_reserve(&bignums, 100);
for (size_t i = 10; i <= 100; i += 10)
@@ -64,7 +64,7 @@ using_cvec_str(); void vectordemo2()
{
printf("\nVECTORDEMO2\n");
- c_fordefer (cvec_str names = cvec_str_init(), cvec_str_del(&names)) {
+ c_forvar (cvec_str names = cvec_str_init(), cvec_str_del(&names)) {
cvec_str_emplace_back(&names, "Mary");
cvec_str_emplace_back(&names, "Joe");
cvec_str_emplace_back(&names, "Chris");
@@ -82,8 +82,8 @@ using_clist(ix, int); void listdemo1()
{
printf("\nLISTDEMO1\n");
- c_fordefer (clist_ix nums = clist_ix_init(), clist_ix_del(&nums))
- c_fordefer (clist_ix nums2 = clist_ix_init(), clist_ix_del(&nums2))
+ c_forvar (clist_ix nums = clist_ix_init(), clist_ix_del(&nums))
+ c_forvar (clist_ix nums2 = clist_ix_init(), clist_ix_del(&nums2))
{
for (int i = 0; i < 10; ++i)
clist_ix_push_back(&nums, i);
@@ -142,7 +142,7 @@ using_cmap_strkey(si, int); // Shorthand macro for the general using_cmap expans void mapdemo2()
{
printf("\nMAPDEMO2\n");
- c_fordefer (cmap_si nums = cmap_si_init(), cmap_si_del(&nums))
+ c_forvar (cmap_si nums = cmap_si_init(), cmap_si_del(&nums))
{
cmap_si_emplace_or_assign(&nums, "Hello", 64);
cmap_si_emplace_or_assign(&nums, "Groovy", 121);
diff --git a/examples/ex_gauss1.c b/examples/ex_gauss1.c index d44c2b4e..1365d27c 100644 --- a/examples/ex_gauss1.c +++ b/examples/ex_gauss1.c @@ -30,8 +30,8 @@ int main() stc64_normalf_t dist = stc64_normalf_init(Mean, StdDev);
// Create and init histogram vec and map with defered destructors:
- c_fordefer (cvec_e vhist = cvec_e_init(), cvec_e_del(&vhist))
- c_fordefer (cmap_i mhist = cmap_i_init(), cmap_i_del(&mhist))
+ c_forvar (cvec_e vhist = cvec_e_init(), cvec_e_del(&vhist))
+ c_forvar (cmap_i mhist = cmap_i_init(), cmap_i_del(&mhist))
{
c_forrange (N) {
int index = (int) round( stc64_normalf(&rng, &dist) );
@@ -45,7 +45,7 @@ int main() cvec_e_sort(&vhist);
// Print the gaussian bar chart
- c_fordefer (cstr bar = cstr_null, cstr_del(&bar))
+ c_forvar (cstr bar = cstr_null, cstr_del(&bar))
c_foreach (i, cvec_e, vhist) {
size_t n = (size_t) (i.ref->second * StdDev * Scale * 2.5 / (float)N);
if (n > 0) {
diff --git a/examples/ex_gauss2.c b/examples/ex_gauss2.c index 23d8dd7a..553846aa 100644 --- a/examples/ex_gauss2.c +++ b/examples/ex_gauss2.c @@ -21,8 +21,8 @@ int main() stc64_normalf_t dist = stc64_normalf_init(Mean, StdDev);
// Create and init histogram map with defered destruct
- c_fordefer (csmap_i mhist = csmap_i_init(), csmap_i_del(&mhist))
- c_fordefer (cstr bar = cstr_init(), cstr_del(&bar))
+ c_forvar (csmap_i mhist = csmap_i_init(), csmap_i_del(&mhist))
+ c_forvar (cstr bar = cstr_init(), cstr_del(&bar))
{
c_forrange (N) {
int index = (int) round( stc64_normalf(&rng, &dist) );
diff --git a/examples/inits.c b/examples/inits.c index 91f5c3c1..40e89139 100644 --- a/examples/inits.c +++ b/examples/inits.c @@ -23,7 +23,7 @@ int main(void) {
// CVEC FLOAT / PRIORITY QUEUE
- c_fordefer (cvec_f floats = cvec_f_init(), cvec_f_del(&floats)) {
+ c_forvar (cvec_f floats = cvec_f_init(), cvec_f_del(&floats)) {
c_emplace(cvec_f, floats, {4.0f, 2.0f, 5.0f, 3.0f, 1.0f});
c_foreach (i, cvec_f, floats) printf("%.1f ", *i.ref);
@@ -45,7 +45,7 @@ int main(void) // CMAP ID
int year = 2020;
- c_fordefer (cmap_id idnames = cmap_id_init(), cmap_id_del(&idnames)) {
+ c_forvar (cmap_id idnames = cmap_id_init(), cmap_id_del(&idnames)) {
cmap_id_emplace(&idnames, 100, "Hello");
cmap_id_insert(&idnames, 110, cstr_from("World"));
cmap_id_insert(&idnames, 120, cstr_from_fmt("Howdy, -%d-", year));
@@ -57,7 +57,7 @@ int main(void) // CMAP CNT
- c_fordefer (cmap_cnt countries = cmap_cnt_init(), cmap_cnt_del(&countries)) {
+ c_forvar (cmap_cnt countries = cmap_cnt_init(), cmap_cnt_del(&countries)) {
c_emplace(cmap_cnt, countries, {
{"Norway", 100},
{"Denmark", 50},
@@ -80,7 +80,7 @@ int main(void) // CVEC PAIR
- c_fordefer (cvec_ip pairs1 = cvec_ip_init(), cvec_ip_del(&pairs1)) {
+ c_forvar (cvec_ip pairs1 = cvec_ip_init(), cvec_ip_del(&pairs1)) {
c_emplace (cvec_ip, pairs1, {{5, 6}, {3, 4}, {1, 2}, {7, 8}});
cvec_ip_sort(&pairs1);
@@ -91,7 +91,7 @@ int main(void) // CLIST PAIR
- c_fordefer (clist_ip pairs2 = clist_ip_init(), clist_ip_del(&pairs2)) {
+ c_forvar (clist_ip pairs2 = clist_ip_init(), clist_ip_del(&pairs2)) {
c_emplace(clist_ip, pairs2, {{5, 6}, {3, 4}, {1, 2}, {7, 8}});
clist_ip_sort(&pairs2);
diff --git a/examples/list.c b/examples/list.c index e4b027a9..327b477c 100644 --- a/examples/list.c +++ b/examples/list.c @@ -8,7 +8,7 @@ int main() { int k;
const int n = 2000000;
- c_fordefer (clist_fx list = clist_fx_init(), clist_fx_del(&list)) {
+ c_forvar (clist_fx list = clist_fx_init(), clist_fx_del(&list)) {
stc64_t rng = stc64_init(1234);
stc64_uniformf_t dist = stc64_uniformf_init(100.0f, n);
int m = 0;
diff --git a/examples/list_erase.c b/examples/list_erase.c index a9d2b9d3..523ee0b8 100644 --- a/examples/list_erase.c +++ b/examples/list_erase.c @@ -6,7 +6,7 @@ using_clist(i, int); int main ()
{
- c_fordefer (clist_i L = clist_i_init(), clist_i_del(&L))
+ c_forvar (clist_i L = clist_i_init(), clist_i_del(&L))
{
c_emplace(clist_i, L, {10, 20, 30, 40, 50});
// 10 20 30 40 50
diff --git a/examples/list_splice.c b/examples/list_splice.c index f80ac860..8379dbde 100644 --- a/examples/list_splice.c +++ b/examples/list_splice.c @@ -14,8 +14,8 @@ void print_ilist(const char* s, clist_i list) int main () { - c_fordefer (clist_i list1 = clist_i_init(), clist_i_del(&list1)) - c_fordefer (clist_i list2 = clist_i_init(), clist_i_del(&list2)) + c_forvar (clist_i list1 = clist_i_init(), clist_i_del(&list1)) + c_forvar (clist_i list2 = clist_i_init(), clist_i_del(&list2)) { c_emplace(clist_i, list1, {1, 2, 3, 4, 5}); c_emplace(clist_i, list2, {10, 20, 30, 40, 50}); diff --git a/examples/mapmap.c b/examples/mapmap.c index 21b2cac9..e582ab64 100644 --- a/examples/mapmap.c +++ b/examples/mapmap.c @@ -9,7 +9,7 @@ using_cmap_str(); using_cmap_strkey(cfg, cmap_str, cmap_str_del, c_no_clone);
int main(void) {
- c_fordefer (cmap_cfg cfg = cmap_cfg_init(), cmap_cfg_del(&cfg)) {
+ c_forvar (cmap_cfg cfg = cmap_cfg_init(), cmap_cfg_del(&cfg)) {
cmap_str init = cmap_str_init();
cmap_str_emplace(&cmap_cfg_insert(&cfg, cstr_from("user"), init).ref->second, "name", "Joe");
cmap_str_emplace(&cmap_cfg_insert(&cfg, cstr_from("user"), init).ref->second, "groups", "proj1,proj3");
diff --git a/examples/mmap.c b/examples/mmap.c index 58511c37..04e501aa 100644 --- a/examples/mmap.c +++ b/examples/mmap.c @@ -26,7 +26,7 @@ void insert(csmap_m* mmap, int key, const char* str) int main()
{
- c_fordefer (csmap_m mmap = csmap_m_init(), csmap_m_del(&mmap))
+ c_forvar (csmap_m mmap = csmap_m_init(), csmap_m_del(&mmap))
{
// list-initialize
struct {int i; const char* s;} vals[] = {{2, "foo"}, {2, "bar"}, {3, "baz"}, {1, "abc"}, {5, "def"}};
diff --git a/examples/multimap.c b/examples/multimap.c index 4b192e29..03d4712d 100644 --- a/examples/multimap.c +++ b/examples/multimap.c @@ -55,7 +55,7 @@ using_csmap_strkey(OL, clist_OL, clist_OL_del, c_no_clone); int main()
{
// Define the multimap with destructor defered to when block is completed.
- c_fordefer (csmap_OL multimap = csmap_OL_init(), csmap_OL_del(&multimap))
+ c_forvar (csmap_OL multimap = csmap_OL_init(), csmap_OL_del(&multimap))
{
clist_OL empty = clist_OL_init();
diff --git a/examples/phonebook.c b/examples/phonebook.c index e755923f..a69e701b 100644 --- a/examples/phonebook.c +++ b/examples/phonebook.c @@ -39,14 +39,14 @@ int main(int argc, char **argv) {
c_static_assert(3 == 3, "hello");
- c_fordefer (cset_str names = cset_str_init(), cset_str_del(&names)) {
+ c_forvar (cset_str names = cset_str_init(), cset_str_del(&names)) {
c_emplace (cset_str, names, {"Hello", "Cool", "True"});
c_foreach (i, cset_str, names) printf("%s ", i.ref->str);
puts("");
}
bool erased;
- c_fordefer (cmap_str phone_book = cmap_str_init(), cmap_str_del(&phone_book)) {
+ c_forvar (cmap_str phone_book = cmap_str_init(), cmap_str_del(&phone_book)) {
c_emplace (cmap_str, phone_book, {
{"Lilia Friedman", "(892) 670-4739"},
{"Tariq Beltran", "(489) 600-7575"},
diff --git a/examples/prime.c b/examples/prime.c index 67f92c7b..7ac19eb0 100644 --- a/examples/prime.c +++ b/examples/prime.c @@ -27,7 +27,7 @@ int main(void) printf("computing prime numbers up to %zu\n", n);
clock_t t1 = clock();
- c_fordefer (cbits primes = sieveOfEratosthenes(n + 1), cbits_del(&primes)) {
+ c_forvar (cbits primes = sieveOfEratosthenes(n + 1), cbits_del(&primes)) {
size_t np = cbits_count(primes);
clock_t t2 = clock();
diff --git a/examples/priority.c b/examples/priority.c index e0865054..ee64bf42 100644 --- a/examples/priority.c +++ b/examples/priority.c @@ -13,7 +13,7 @@ int main() { size_t N = 10000000;
stc64_t rng = stc64_init(time(NULL));
stc64_uniform_t dist = stc64_uniform_init(0, N * 10);
- c_fordefer (cpque_i heap = cpque_i_init(), cpque_i_del(&heap))
+ c_forvar (cpque_i heap = cpque_i_init(), cpque_i_del(&heap))
{
// Push ten million random numbers to priority queue
c_forrange (N)
diff --git a/examples/queue.c b/examples/queue.c index d2687507..1e96b53a 100644 --- a/examples/queue.c +++ b/examples/queue.c @@ -17,7 +17,7 @@ int main() { stc64_t rng = stc64_init(1234);
dist = stc64_uniform_init(0, n);
- c_fordefer (cqueue_i queue = cqueue_i_init(), cqueue_i_del(&queue))
+ c_forvar (cqueue_i queue = cqueue_i_init(), cqueue_i_del(&queue))
{
// Push ten million random numbers onto the queue.
c_forrange (n)
diff --git a/examples/replace.c b/examples/replace.c index 69928917..cfbcc1e5 100644 --- a/examples/replace.c +++ b/examples/replace.c @@ -13,7 +13,7 @@ int main () // Ustring positions: 0123456789*123456789*12345
cstr s = cstr_from(base); // "this is a test string."
cstr m = cstr_clone(s);
- c_fordefer ((cstr_del(&s), cstr_del(&m))) {
+ c_fordefer (cstr_del(&s), cstr_del(&m)) {
cstr_append(&m, m.str);
cstr_append(&m, m.str);
printf("%s\n", m.str);
@@ -25,8 +25,8 @@ int main () cstr_replace(&s, 8, 10, "just a"); // "this is just a phrase." (3)
printf("(3) %s\n", s.str);
cstr_replace_n(&s, 8, 6,"a shorty", 7); // "this is a short phrase." (4)
- printf("(4) %s\n", s.str);
+ printf("(4) %s\n", s.str);
cstr_replace(&s, 22, 1, "!!!"); // "this is a short phrase!!!" (5)
- printf("(5) %s\n", s.str);
- }
+ printf("(5) %s\n", s.str);
+ }
}
diff --git a/examples/stack.c b/examples/stack.c index 83c8cc08..7ae53e0f 100644 --- a/examples/stack.c +++ b/examples/stack.c @@ -11,16 +11,16 @@ using_cstack(c, cvec_c); int main() {
cstack_i stack = cstack_i_init();
cstack_c chars = cstack_c_init();
- c_fordefer ((cstack_i_del(&stack), cstack_c_del(&chars)))
+ c_fordefer (cstack_i_del(&stack), cstack_c_del(&chars))
{
- c_forrange (i, int, 101)
+ c_forrange (i, int, 101)
cstack_i_push(&stack, i*i);
printf("%d\n", *cstack_i_top(&stack));
- c_forrange (i, int, 90)
+ c_forrange (i, int, 90)
cstack_i_pop(&stack);
-
+
c_foreach (i, cstack_i, stack)
printf(" %d", *i.ref);
puts("");
diff --git a/examples/stc_astar.c b/examples/stc_astar.c index ecca939f..6122aad4 100644 --- a/examples/stc_astar.c +++ b/examples/stc_astar.c @@ -83,9 +83,9 @@ astar(cstr maze, int width) cpque_pnt frontier = cpque_pnt_init();
csmap_step came_from = csmap_step_init();
csmap_cost cost_so_far = csmap_cost_init();
- c_fordefer ((cpque_pnt_del(&frontier),
- csmap_step_del(&came_from),
- csmap_cost_del(&cost_so_far)))
+ c_fordefer (cpque_pnt_del(&frontier),
+ csmap_step_del(&came_from),
+ csmap_cost_del(&cost_so_far))
{
cpque_pnt_push(&frontier, start);
csmap_cost_insert(&cost_so_far, start, 0);
diff --git a/examples/svmap.c b/examples/svmap.c index 3087706e..8cacf95a 100644 --- a/examples/svmap.c +++ b/examples/svmap.c @@ -9,7 +9,7 @@ int main() { csview fox = c_lit("The quick brown fox jumps over the lazy dog.");
printf("\"%s\", length=%zu\n", fox.str, fox.size);
- c_fordefer (cmap_si frequencies = cmap_si_init(), cmap_si_del(&frequencies))
+ c_forvar (cmap_si frequencies = cmap_si_init(), cmap_si_del(&frequencies))
{
// Non-emplace: cstr element API
cmap_si_insert(&frequencies, cstr_lit("thousand"), 1000);
diff --git a/include/stc/carray.h b/include/stc/carray.h index fa794238..d67c1410 100644 --- a/include/stc/carray.h +++ b/include/stc/carray.h @@ -33,7 +33,7 @@ using_carray2(i, int); int main() {
int w = 7, h = 5;
- c_fordefer (carray2i image = carray2i_init(w, h), carray2i_del(&image))
+ c_forvar (carray2i image = carray2i_init(w, h), carray2i_del(&image))
{
int *dat = carray2i_data(&image);
for (int i = 0; i < carray2i_size(image); ++i)
diff --git a/include/stc/cbits.h b/include/stc/cbits.h index 803fe2d5..2424f70f 100644 --- a/include/stc/cbits.h +++ b/include/stc/cbits.h @@ -30,7 +30,7 @@ Similar to boost::dynamic_bitset / std::bitset #include "cbits.h"
int main() {
- c_fordefer (cbits bset = cbits_with_size(23, true), cbits_del(&bset))
+ c_forvar (cbits bset = cbits_with_size(23, true), cbits_del(&bset))
{
cbits_reset(&bset, 9);
cbits_resize(&bset, 43, false);
diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index 62b8a172..d15d5951 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -123,9 +123,9 @@ for (type i=start, _c_inc=step, _c_end=(stop) - (0 < _c_inc) \
; (i <= _c_end) == (0 < _c_inc); i += _c_inc)
-#define c_fordefer(...) c_MACRO_OVERLOAD(c_fordefer, __VA_ARGS__)
-#define c_fordefer_1(release) for (int _c_ii = 0; !_c_ii; ++_c_ii, release)
-#define c_fordefer_2(acquire, release) for (acquire, *_c_ii = NULL; !_c_ii; ++_c_ii, release)
+#define c_forvar(acquire, ...) for (acquire, *_c_ii = NULL; !_c_ii; ++_c_ii, __VA_ARGS__)
+#define c_arg(...) __VA_ARGS__
+#define c_fordefer(...) for (int _c_ii = 0; !_c_ii; ++_c_ii, __VA_ARGS__)
#define c_forbuffer(b, type, n) c_forbuffer_N(b, type, n, 256)
#define c_forbuffer_N(b, type, n, BYTES) \
diff --git a/include/stc/clist.h b/include/stc/clist.h index 538c25b4..f07aa9d6 100644 --- a/include/stc/clist.h +++ b/include/stc/clist.h @@ -35,7 +35,7 @@ using_clist(ix, int64_t);
int main() {
- c_fordefer (clist_ix list = clist_ix_init(), clist_ix_del(&list))
+ c_forvar (clist_ix list = clist_ix_init(), clist_ix_del(&list))
{
stc64_t rng = stc64_init(12345);
int n;
diff --git a/include/stc/cmap.h b/include/stc/cmap.h index 9377aadd..8c226aa2 100644 --- a/include/stc/cmap.h +++ b/include/stc/cmap.h @@ -31,7 +31,7 @@ using_cmap(mx, int, char); // Map of int -> char
int main(void) {
- c_fordefer (cmap_mx m = cmap_mx_init(), cmap_mx_del(&m))
+ c_forvar (cmap_mx m = cmap_mx_init(), cmap_mx_del(&m))
{
cmap_mx_emplace(&m, 5, 'a');
cmap_mx_emplace(&m, 8, 'b');
diff --git a/include/stc/cpque.h b/include/stc/cpque.h index afdd41ac..c6e0ed61 100644 --- a/include/stc/cpque.h +++ b/include/stc/cpque.h @@ -32,7 +32,7 @@ stc64_t rng = stc64_init(1234);
stc64_uniformf_t dist = stc64_uniformf_init(10.0f, 100.0f);
- c_fordefer (cpque_f queue = cpque_f_init(), cpque_f_del(&queue))
+ c_forvar (cpque_f queue = cpque_f_init(), cpque_f_del(&queue))
{
// Push ten million random numbers onto the queue.
for (int i=0; i<10000000; ++i)
diff --git a/include/stc/cqueue.h b/include/stc/cqueue.h index 99afc296..464b6383 100644 --- a/include/stc/cqueue.h +++ b/include/stc/cqueue.h @@ -35,7 +35,7 @@ stc64_t rng = stc64_init(1234);
stc64_uniform_t dist = stc64_uniform_init(rng, 0, n);
- c_fordefer (cqueue_i queue = cqueue_i_init(), cqueue_i_del(&queue))
+ c_forvar (cqueue_i queue = cqueue_i_init(), cqueue_i_del(&queue))
{
// Push ten million random numbers onto the queue.
for (int i=0; i<n; ++i)
diff --git a/include/stc/csmap.h b/include/stc/csmap.h index 2909e0a1..3535897c 100644 --- a/include/stc/csmap.h +++ b/include/stc/csmap.h @@ -30,7 +30,7 @@ using_csmap(mx, int, char); // Sorted map<int, char>
int main(void) {
- c_fordefer (csmap_mx m = csmap_mx_init(), csmap_mx_del(&m))
+ c_forvar (csmap_mx m = csmap_mx_init(), csmap_mx_del(&m))
{
csmap_mx_insert(&m, 5, 'a');
csmap_mx_insert(&m, 8, 'b');
diff --git a/include/stc/cstack.h b/include/stc/cstack.h index 575e9012..d6b1b3ad 100644 --- a/include/stc/cstack.h +++ b/include/stc/cstack.h @@ -32,7 +32,7 @@ using_cstack(i, cvec_i);
int main() {
- c_fordefer (cstack_i stack = cstack_i_init(), cstack_i_del(&stack))
+ c_forvar (cstack_i stack = cstack_i_init(), cstack_i_del(&stack))
{
for (int i=0; i<100; ++i)
cstack_i_push(&stack, i*i);
|
