diff options
| author | Tyge Løvset <[email protected]> | 2022-09-23 07:15:41 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2022-09-23 07:15:41 +0200 |
| commit | 34bec4fdf406caff8492d53f0afc80df5d75bba4 (patch) | |
| tree | 94252139eb34cb37f561ec5a6b9513629b2a81a5 | |
| parent | d70bcf4d875c14da30a39acfc37d00391cc1e7a9 (diff) | |
| download | STC-modified-34bec4fdf406caff8492d53f0afc80df5d75bba4.tar.gz STC-modified-34bec4fdf406caff8492d53f0afc80df5d75bba4.zip | |
Deprecated c_forarray, c_forarray_p macros - both replaced by c_forlist, and is consistent with other c_for* macros.
37 files changed, 164 insertions, 157 deletions
@@ -117,17 +117,17 @@ int main(void) { } ``` Below is an alternative way to write this code with STC. It uses three -macros: `c_auto`, `c_forarray`, and `c_foreach`. These macro not only +macros: `c_auto`, `c_forlist`, and `c_foreach`. These macro not only simplifies the code, but more importantly makes it less prone to errors, while maintaining readability: ```c int main() { c_auto (FVec, vec) // RAII: init + free at one location in the code. { - c_forarray (float, v, {10.f, 20.f, 30.f}) // use array literals. - FVec_push(&vec, *v); // alias for push_back. + c_forlist (i, float, {10.f, 20.f, 30.f}) // use array literals. + FVec_push(&vec, *i.ref); // alias for push_back. - c_foreach (i, FVec, vec) // works for all containers. + c_foreach (i, FVec, vec) // works for all containers. printf(" %g", *i.ref); } } @@ -473,7 +473,7 @@ Memory efficiency - `c_scope`: macro renamed from `c_autoscope`, which is deprecated. - `c_defer`: macro renamed from `c_autodefer`, which is deprecated. Like Go's and Zig's **defer**. - `c_forrange` with 3 to 5 args: swapped 1st <-> 2nd arg. -- New `c_forarray` macro to replace usages of `c_apply`, which is removed. +- New `c_forlist` macro to replace usages of `c_forarray`/`c_apply`, which is removed. - Added back **coption** - command line argument parsing. - [See detailed changes for version 3](#version-3). diff --git a/docs/carc_api.md b/docs/carc_api.md index 56f8ff2e..d563f2f8 100644 --- a/docs/carc_api.md +++ b/docs/carc_api.md @@ -102,20 +102,20 @@ int main() // POPULATE s1 with shared pointers to Map: Map *map; - map = Stack_push(&s1, Arc_from(Map_init()))->get; // push empty map to s1. - c_forarray (Map_raw, v, { {"Joey", 1990}, {"Mary", 1995}, {"Joanna", 1992}}) { - Map_emplace(map, v->first, v->second); // populate it. + map = Stack_push(&s1, Arc_make(Map_init()))->get; // push empty map to s1. + c_forlist (i, Map_raw, {{"Joey", 1990}, {"Mary", 1995}, {"Joanna", 1992}}) { + Map_emplace(map, c_pair(i.ref)); // populate it. } - map = Stack_push(&s1, Arc_from(Map_init()))->get; - c_forarray (Map_raw, v, { {"Rosanna", 2001}, {"Brad", 1999}, {"Jack", 1980} }) { - Map_emplace(map, v->first, v->second); + map = Stack_push(&s1, Arc_make(Map_init()))->get; + c_forlist (i, Map_raw, {{"Rosanna", 2001}, {"Brad", 1999}, {"Jack", 1980}}) { + Map_emplace(map, c_pair(i.ref)); } // POPULATE s2: - map = Stack_push(&s2, Arc_from(Map_init()))->get; - c_forarray (Map_raw, v, { {"Steve", 1979}, {"Rick", 1974}, {"Tracy", 2003} }) { - Map_emplace(map, v->first, v->second); + map = Stack_push(&s2, Arc_make(Map_init()))->get; + c_forlist (i, Map_raw, {{"Steve", 1979}, {"Rick", 1974}, {"Tracy", 2003}}) { + Map_emplace(map, c_pair(i.ref)); } // Share two Maps from s1 with s2 by cloning(=sharing) the carcs: diff --git a/docs/cbox_api.md b/docs/cbox_api.md index 6686dde5..43fa46b0 100644 --- a/docs/cbox_api.md +++ b/docs/cbox_api.md @@ -91,8 +91,8 @@ int main() c_auto (IVec, vec) // declare and init vec, call drop at scope exit c_auto (ISet, set) // similar { - c_forarray (int, v, {2021, 2012, 2022, 2015}) - IVec_emplace(&vec, *v); // same as: IVec_push(&vec, IBox_from(*v)); + c_forlist (i, int, {2021, 2012, 2022, 2015}) + IVec_emplace(&vec, *i.ref); // same as: IVec_push(&vec, IBox_from(*i.ref)); printf("vec:"); c_foreach (i, IVec, vec) diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index 8a805cef..403d8ef9 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -138,24 +138,20 @@ from a `c_auto` scope: } // for ``` -### c_forarray, c_forarray_p +### c_forlist Iterate compound literal array elements ```c // apply multiple push_backs -c_forarray (int, v, {1, 2, 3}) - cvec_i_push_back(&vec, *v); +c_forlist (i, int, {1, 2, 3}) + cvec_i_push_back(&vec, *i.ref); // insert in existing map -c_forarray (cmap_ii_raw, v, { {4, 5}, {6, 7} }) - cmap_ii_insert(&map, v->first, v->second); +c_forlist (i, cmap_ii_raw, { {4, 5}, {6, 7} }) + cmap_ii_insert(&map, i.ref->first, i.ref->second); -// even define an anonymous struct inside it (no commas allowed) -c_forarray (struct { int a; int b; }, v, { {1, 2}, {3, 4}, {5, 6} }) - printf("(%d %d) ", v->a, v->b); - -// `c_forarray_p` is required for pointer type elements -c_forarray_p (const char*, v, {"Hello", "crazy", "world"}) - cstack_s_push(&stk, *v); +// even string literals +c_forlist (i, const char*, {"Hello", "crazy", "world"}) + cstack_s_push(&stk, *i.ref); ``` ### c_foreach, c_forpair @@ -172,8 +168,8 @@ c_forarray_p (const char*, v, {"Hello", "crazy", "world"}) #define i_tag ii #include <stc/csmap.h> ... -c_forarray (csmap_ii_value, v, { {23,1}, {3,2}, {7,3}, {5,4}, {12,5} }) - csmap_ii_insert(&map, v->first, v->second); +c_forlist (i, csmap_ii_value, { {23,1}, {3,2}, {7,3}, {5,4}, {12,5} }) + csmap_ii_insert(&map, i.ref->first, i.ref->second); c_foreach (i, csmap_ii, map) printf(" %d", i.ref->first); diff --git a/docs/cdeq_api.md b/docs/cdeq_api.md index bfe2447c..969cecc2 100644 --- a/docs/cdeq_api.md +++ b/docs/cdeq_api.md @@ -111,8 +111,8 @@ int main() { printf(" %d", *i.ref); puts(""); - c_forarray (int, v, {1, 4, 5, 22, 33, 2}) - cdeq_i_push_back(&q, *v) + c_forlist (i, int, {1, 4, 5, 22, 33, 2}) + cdeq_i_push_back(&q, *i.ref) c_foreach (i, cdeq_i, q) printf(" %d", *i.ref); diff --git a/docs/clist_api.md b/docs/clist_api.md index 45caeb93..1b3c5b0b 100644 --- a/docs/clist_api.md +++ b/docs/clist_api.md @@ -111,8 +111,8 @@ Interleave *push_front()* / *push_back()* then *sort()*: int main() { DList list = DList_init(); - c_forarray (double, v, {10., 20., 30., 40., 50., 60., 70., 80., 90.}) - DList_push_back(&list, *v); + c_forlist (i, double, {10., 20., 30., 40., 50., 60., 70., 80., 90.}) + DList_push_back(&list, *i.ref); c_forrange (int, i, 1, 10) { if (i & 1) DList_push_front(&list, (double) i); @@ -152,8 +152,8 @@ int main () { clist_i L = clist_i_init(); - c_forarray (int, v, {10, 20, 30, 40, 50}) - clist_i_push_back(&L, *v); + c_forlist (i, int, {10, 20, 30, 40, 50}) + clist_i_push_back(&L, *i.ref); // 10 20 30 40 50 clist_i_iter it = clist_i_begin(&L); // ^ clist_i_next(&it); @@ -189,10 +189,10 @@ Splice `[30, 40]` from *L2* into *L1* before `3`: int main() { c_auto (clist_i, L1, L2) { - c_forarray (int, v, {1, 2, 3, 4, 5}) - clist_i_push_back(&L1, *v); - c_forarray (int, v, {10, 20, 30, 40, 50}) - clist_i_push_back(&L2, *v); + c_forlist (i, int, {1, 2, 3, 4, 5}) + clist_i_push_back(&L1, *i.ref); + c_forlist (i, int, {10, 20, 30, 40, 50}) + clist_i_push_back(&L2, *i.ref); clist_i_iter i = clist_i_advance(clist_i_begin(&L1), 2); clist_i_iter j1 = clist_i_advance(clist_i_begin(&L2), 2), j2 = clist_i_advance(j1, 2); diff --git a/docs/cmap_api.md b/docs/cmap_api.md index a9676e68..25366792 100644 --- a/docs/cmap_api.md +++ b/docs/cmap_api.md @@ -127,11 +127,11 @@ int main() // Create an unordered_map of three strings (that map to strings) c_auto (cmap_str, u) { - c_forarray (cmap_str_raw, v, { + c_forlist (i, cmap_str_raw, { {"RED", "#FF0000"}, {"GREEN", "#00FF00"}, {"BLUE", "#0000FF"} - }) cmap_str_emplace(&u, v->first, v->second); + }) cmap_str_emplace(&u, c_pair(i.ref)); // Iterate and print keys and values of unordered map c_foreach (n, cmap_str, u) { @@ -173,8 +173,8 @@ int main() c_auto (cmap_id, idnames) { - c_forarray (cmap_id_raw, v, { {100, "Red"}, {110, "Blue"} }) - cmap_id_emplace(&idnames, v->first, v->second); + c_forlist (i, cmap_id_raw, { {100, "Red"}, {110, "Blue"} }) + cmap_id_emplace(&idnames, c_pair(i.ref)); // replace existing mapped value: cmap_id_emplace_or_assign(&idnames, 110, "White"); diff --git a/docs/cset_api.md b/docs/cset_api.md index 2d3ab6e7..b2e7d0c8 100644 --- a/docs/cset_api.md +++ b/docs/cset_api.md @@ -86,11 +86,11 @@ int main () c_auto (cset_str, first, second) c_auto (cset_str, third, fourth) { - c_forarray_p (const char*, v, {"red", "green", "blue"}) - cset_str_emplace(&second, *v); + c_forlist (i, const char*, {"red", "green", "blue"}) + cset_str_emplace(&second, *i.ref); - c_forarray_p (const char*, v, {"orange", "pink", "yellow"}) - cset_str_emplace(&third, *v); + c_forlist (i, const char*, {"orange", "pink", "yellow"}) + cset_str_emplace(&third, *i.ref); cset_str_emplace(&fourth, "potatoes"); cset_str_emplace(&fourth, "milk"); diff --git a/docs/csmap_api.md b/docs/csmap_api.md index 91abaae3..0be14605 100644 --- a/docs/csmap_api.md +++ b/docs/csmap_api.md @@ -113,11 +113,11 @@ int main() // Create a sorted map of three strings (maps to string) c_auto (csmap_str, colors) // RAII { - c_forarray (csmap_str_raw, v, { + c_forlist (i, csmap_str_raw, { {"RED", "#FF0000"}, {"GREEN", "#00FF00"}, {"BLUE", "#0000FF"} - }) csmap_str_emplace(&colors, v->first, v->second); + }) csmap_str_emplace(&colors, c_pair(i.ref)); // Iterate and print keys and values of sorted map c_foreach (i, csmap_str, colors) { @@ -159,8 +159,8 @@ int main() csmap_id idnames = csmap_id_init(); c_defer (csmap_id_drop(&idnames)) { - c_forarray (csmap_id_raw, v, { {100, "Red"}, {110, "Blue"} }) - csmap_id_emplace(&idnames, v->first, v->second); + c_forlist (i, csmap_id_raw, { {100, "Red"}, {110, "Blue"} }) + csmap_id_emplace(&idnames, c_pair(i.ref)); // put replaces existing mapped value: csmap_id_emplace_or_assign(&idnames, 110, "White"); diff --git a/docs/csset_api.md b/docs/csset_api.md index f2667376..82b8eb09 100644 --- a/docs/csset_api.md +++ b/docs/csset_api.md @@ -85,11 +85,11 @@ c_auto (csset_str, fifth) c_auto (csset_str, first, second) c_auto (csset_str, third, fourth) { - c_forarray_p (const char*, v, {"red", "green", "blue"}) - csset_str_emplace(&second, *v); + c_forlist (i, const char*, {"red", "green", "blue"}) + csset_str_emplace(&second, *i.ref); - c_forarray_p (const char*, v, {"orange", "pink", "yellow"}) - csset_str_emplace(&third, *v); + c_forlist (i, const char*, {"orange", "pink", "yellow"}) + csset_str_emplace(&third, *i.ref); csset_str_emplace(&fourth, "potatoes"); csset_str_emplace(&fourth, "milk"); diff --git a/docs/cvec_api.md b/docs/cvec_api.md index 588299fe..dca273aa 100644 --- a/docs/cvec_api.md +++ b/docs/cvec_api.md @@ -124,8 +124,8 @@ int main() cvec_int_push(&vec, 13); // Append a set of numbers - c_forarray (int, v, {7, 5, 16, 8}) - cvec_int_push(&vec, *v); + c_forlist (i, int, {7, 5, 16, 8}) + cvec_int_push(&vec, *i.ref); printf("initial:"); c_foreach (k, cvec_int, vec) { diff --git a/examples/arcvec_erase.c b/examples/arcvec_erase.c index 2c9a3daa..63b646a3 100644 --- a/examples/arcvec_erase.c +++ b/examples/arcvec_erase.c @@ -19,8 +19,8 @@ int main() { c_auto (Vec, vec) { - c_forarray (int, v, {2012, 1990, 2012, 2019, 2015}) - Vec_emplace(&vec, *v); + c_forlist (i, int, {2012, 1990, 2012, 2019, 2015}) + Vec_emplace(&vec, *i.ref); // clone the second 2012 and push it back. // note: cloning make sure that vec.data[2] has ref count 2. diff --git a/examples/city.c b/examples/city.c index c6a9417f..c98d3098 100644 --- a/examples/city.c +++ b/examples/city.c @@ -51,14 +51,12 @@ int main(void) c_auto (Cities, cities, copy) c_auto (CityMap, map) { - c_forarray (City, c, { + c_forlist (i, City, { {cstr_new("New York"), cstr_new("US"), 4.3, 23.2, 9000000}, {cstr_new("Paris"), cstr_new("France"), 4.3, 23.2, 9000000}, {cstr_new("Berlin"), cstr_new("Germany"), 4.3, 23.2, 9000000}, {cstr_new("London"), cstr_new("UK"), 4.3, 23.2, 9000000}, - }) { - Cities_emplace(&cities, *c); - } + }) Cities_emplace(&cities, *i.ref); copy = Cities_clone(cities); // share each element! diff --git a/examples/convert.c b/examples/convert.c index 5d58574d..74f4f865 100644 --- a/examples/convert.c +++ b/examples/convert.c @@ -17,11 +17,11 @@ int main() c_auto (cvec_str, keys, values) c_auto (clist_str, list) { - c_forarray (cmap_str_raw, v, { + c_forlist (i, cmap_str_raw, { {"green", "#00ff00"}, {"blue", "#0000ff"}, {"yellow", "#ffff00"}, - }) cmap_str_emplace(&map, c_pair(v)); + }) cmap_str_emplace(&map, c_pair(i.ref)); puts("MAP:"); c_foreach (i, cmap_str, map) diff --git a/examples/csmap_erase.c b/examples/csmap_erase.c index cbcc2607..e99a4d7a 100644 --- a/examples/csmap_erase.c +++ b/examples/csmap_erase.c @@ -37,13 +37,13 @@ int main() c_auto (mymap, m2) { // Fill in some data to test with, one at a time - c_forarray (mymap_raw, v, { + c_forlist (i, mymap_raw, { {10, "Bob"}, {11, "Rob"}, {12, "Robert"}, {13, "Bert"}, {14, "Bobby"}, - }) mymap_emplace(&m2, v->first, v->second); + }) mymap_emplace(&m2, c_pair(i.ref)); puts("Starting data of map m2 is:"); printmap(m2); diff --git a/examples/csmap_find.c b/examples/csmap_find.c index ae8aeb85..e7a7112c 100644 --- a/examples/csmap_find.c +++ b/examples/csmap_find.c @@ -45,8 +45,8 @@ int main() c_auto (csmap_istr, m1) c_auto (cvec_istr, v) { - c_forarray (csmap_istr_raw, v, {{40, "Zr"}, {45, "Rh"}}) - csmap_istr_emplace(&m1, c_pair(v)); + c_forlist (i, csmap_istr_raw, {{40, "Zr"}, {45, "Rh"}}) + csmap_istr_emplace(&m1, c_pair(i.ref)); 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 7652fd59..71c8cfe9 100644 --- a/examples/csmap_insert.c +++ b/examples/csmap_insert.c @@ -101,9 +101,9 @@ int main() c_auto (csmap_ii, m4) { // Insert the elements from an initializer_list - c_forarray (csmap_ii_raw, v, {{ 4, 44 }, { 2, 22 }, { 3, 33 }, - { 1, 11 }, { 5, 55 }}) - csmap_ii_insert(&m4, v->first, v->second); + c_forlist (i, csmap_ii_raw, {{ 4, 44 }, { 2, 22 }, { 3, 33 }, + { 1, 11 }, { 5, 55 }}) + csmap_ii_insert(&m4, c_pair(i.ref)); puts("After initializer_list insertion, m4 contains:"); print_ii(m4); diff --git a/examples/csset_erase.c b/examples/csset_erase.c index 9ca23aab..33eb2163 100644 --- a/examples/csset_erase.c +++ b/examples/csset_erase.c @@ -7,8 +7,8 @@ int main() { c_auto (csset_int, set) { - c_forarray (int, v, {30, 20, 80, 40, 60, 90, 10, 70, 50}) - csset_int_insert(&set, *v); + c_forlist (i, int, {30, 20, 80, 40, 60, 90, 10, 70, 50}) + csset_int_insert(&set, *i.ref); c_foreach (k, csset_int, set) printf(" %d", *k.ref); diff --git a/examples/forfilter.c b/examples/forfilter.c index c7d3fae0..409d83e3 100644 --- a/examples/forfilter.c +++ b/examples/forfilter.c @@ -11,27 +11,32 @@ #define i_val_bind csview #include <stc/cstack.h> +// filters and transforms: +#define flt_drop(i, n) (i.index >= (n)) +#define flt_remove(i, x) (*i.ref != (x)) +#define flt_take(i, n) (i.count < (n)) +#define flt_even(i) ((*i.ref & 1) == 0) +#define trf_square(i) (*i.ref * *i.ref) + void demo1(void) { c_auto (IVec, vec) { - c_forarray (int, v, {0, 1, 2, 3, 4, 5, 80, 6, 7, 80, 8, 9, 80, 10, 11, 12, 13}) - IVec_push(&vec, *v); + c_forlist (i, int, {0, 1, 2, 3, 4, 5, 80, 6, 7, 80, 8, 9, 80, 10, 11, 12, 13, 14, 15, 80, 16, 17}) + IVec_push(&vec, *i.ref); c_forfilter (i, IVec, vec, *i.ref != 80) printf(" %d", *i.ref); puts(""); - #define flt_drop(i, n) (i.index >= (n)) - #define flt_remove(i, x) (*i.ref != (x)) - #define flt_take(i, n) (i.taken < (n)) - #define flt_even(i) ((*i.ref & 1) == 0) - + int res, sum = 0; c_forfilter (i, IVec, vec, flt_drop(i, 3) && flt_even(i) && flt_remove(i, 80) - , flt_take(i, 5)) - printf(" %d", *i.ref); - puts(""); + , flt_take(i, 5)) { + sum += res = trf_square(i); + printf(" %d", res); + } + printf("\nsum: %d\n", sum); } } @@ -94,9 +99,12 @@ void demo4(void) { csview s = c_sv("ab123cReAghNGnΩoEp"); cstr out = cstr_null; - c_forfilter (i, csview, s, utf8_isupper(utf8_peek(i.ref))) - cstr_push(&out, i.ref); - //cstr_append_sv(&out, i.it.u8.chr); + + c_forfilter (i, csview, s, utf8_isupper(utf8_peek(i.ref))) { + char buf[4]; + utf8_encode(buf, utf8_tolower(utf8_peek(i.ref))); + cstr_push(&out, buf); + } printf("%s\n", cstr_str(&out)); } diff --git a/examples/inits.c b/examples/inits.c index 9ce96dc9..ab6cdce9 100644 --- a/examples/inits.c +++ b/examples/inits.c @@ -67,7 +67,7 @@ int main(void) // CMAP CNT c_auto (cmap_cnt, countries) { - c_forarray (cmap_cnt_raw, v, { + c_forlist (i, cmap_cnt_raw, { {"Norway", 100}, {"Denmark", 50}, {"Iceland", 10}, @@ -76,7 +76,7 @@ int main(void) {"Germany", 10}, {"Spain", 10}, {"France", 10}, - }) cmap_cnt_emplace(&countries, v->first, v->second); + }) cmap_cnt_emplace(&countries, c_pair(i.ref)); cmap_cnt_emplace(&countries, "Greenland", 0).ref->second += 20; cmap_cnt_emplace(&countries, "Sweden", 0).ref->second += 20; @@ -91,8 +91,8 @@ int main(void) // CVEC PAIR c_auto (cvec_ip, pairs1) { - c_forarray (ipair_t, p, {{5, 6}, {3, 4}, {1, 2}, {7, 8}}) - cvec_ip_push_back(&pairs1, *p); + c_forlist (i, ipair_t, {{5, 6}, {3, 4}, {1, 2}, {7, 8}}) + cvec_ip_push_back(&pairs1, *i.ref); cvec_ip_sort(&pairs1); @@ -104,8 +104,8 @@ int main(void) // CLIST PAIR c_auto (clist_ip, pairs2) { - c_forarray (ipair_t, p, {{5, 6}, {3, 4}, {1, 2}, {7, 8}}) - clist_ip_push_back(&pairs2, *p); + c_forlist (i, ipair_t, {{5, 6}, {3, 4}, {1, 2}, {7, 8}}) + clist_ip_push_back(&pairs2, *i.ref); clist_ip_sort(&pairs2); diff --git a/examples/list.c b/examples/list.c index d5f3996c..2f5fa964 100644 --- a/examples/list.c +++ b/examples/list.c @@ -37,8 +37,8 @@ int main() { puts(""); clist_fx_clear(&list); - c_forarray (int, v, {10, 20, 30, 40, 30, 50}) - clist_fx_push_back(&list, *v); + c_forlist (i, int, {10, 20, 30, 40, 30, 50}) + clist_fx_push_back(&list, *i.ref); const double* v = clist_fx_get(&list, 30); printf("found: %f\n", *v); diff --git a/examples/list_erase.c b/examples/list_erase.c index 71d1f55f..c1a2aa97 100644 --- a/examples/list_erase.c +++ b/examples/list_erase.c @@ -9,8 +9,8 @@ int main () { c_with (IList L = IList_init(), IList_drop(&L)) { - c_forarray (int, i, {10, 20, 30, 40, 50}) - IList_push(&L, *i); + c_forlist (i, int, {10, 20, 30, 40, 50}) + IList_push(&L, *i.ref); c_foreach (x, IList, L) printf("%d ", *x.ref); diff --git a/examples/list_splice.c b/examples/list_splice.c index 8ba022f8..f7bac0d6 100644 --- a/examples/list_splice.c +++ b/examples/list_splice.c @@ -18,11 +18,11 @@ int main () { c_auto (clist_i, list1, list2) { - c_forarray (int, v, {1, 2, 3, 4, 5}) - clist_i_push_back(&list1, *v); + c_forlist (i, int, {1, 2, 3, 4, 5}) + clist_i_push_back(&list1, *i.ref); - c_forarray (int, v, {10, 20, 30, 40, 50}) - clist_i_push_back(&list2, *v); + c_forlist (i, int, {10, 20, 30, 40, 50}) + clist_i_push_back(&list2, *i.ref); print_ilist("list1:", list1); print_ilist("list2:", list2); diff --git a/examples/lower_bound.c b/examples/lower_bound.c index c8beed6f..2477bc14 100644 --- a/examples/lower_bound.c +++ b/examples/lower_bound.c @@ -13,8 +13,8 @@ int main() { int key, *res; - c_forarray (int, t, {40, 600, 1, 7000, 2, 500, 30}) - cvec_int_push(&vec, *t); + c_forlist (i, int, {40, 600, 1, 7000, 2, 500, 30}) + cvec_int_push(&vec, *i.ref); cvec_int_sort(&vec); @@ -40,8 +40,8 @@ int main() { int key, *res; - c_forarray (int, t, {40, 600, 1, 7000, 2, 500, 30}) - csset_int_push(&set, *t); + c_forlist (i, int, {40, 600, 1, 7000, 2, 500, 30}) + csset_int_push(&set, *i.ref); key = 500; res = csset_int_lower_bound(&set, key).ref; diff --git a/examples/make.sh b/examples/make.sh index e5efde07..ff452ce8 100755 --- a/examples/make.sh +++ b/examples/make.sh @@ -1,8 +1,8 @@ #!/bin/bash cc='gcc -s -O2 -Wall -std=c99 -pedantic -Wfatal-errors' #cc='tcc -s -O2 -Wall -std=c99 -pedantic -Wfatal-errors' -#cc='clang -s -O2 -Wall -std=c99 -pedantic' -#cc='clang -s -O2 -Wall -std=c99 -pedantic -DSTC_CSTR_V1 -DSTC_CSMAP_V1' +#cc='clang -s -O2 -Wall -std=c99 -pedantic -Wfatal-errors' +#cc='clang -s -O2 -Wall -std=c99 -pedantic -Wfatal-errors -DSTC_CSTR_V1 -DSTC_CSMAP_V1' #cc='gcc -x c++ -s -O2 -Wall -std=c++20' #cc='g++ -x c++ -s -O2 -Wall' #cc='clang' diff --git a/examples/mmap.c b/examples/mmap.c index ed78d2af..b7fff59a 100644 --- a/examples/mmap.c +++ b/examples/mmap.c @@ -38,8 +38,8 @@ int main() typedef struct {int a; const char* b;} pair; // list-initialize - c_forarray (pair, v, {{2, "foo"}, {2, "bar"}, {3, "baz"}, {1, "abc"}, {5, "def"}}) - insert(&mmap, v->a, v->b); + c_forlist (i, pair, {{2, "foo"}, {2, "bar"}, {3, "baz"}, {1, "abc"}, {5, "def"}}) + insert(&mmap, i.ref->a, i.ref->b); print("#1", mmap); // insert using value_type @@ -54,8 +54,8 @@ int main() print("#4", mmap); // insert using initialization_list - c_forarray (pair, v, {{5, "one"}, {5, "two"}}) - insert(&mmap, v->a, v->b); + c_forlist (i, pair, {{5, "one"}, {5, "two"}}) + insert(&mmap, i.ref->a, i.ref->b); print("#5", mmap); // FOLLOWING NOT IN ORIGINAL EXAMPLE: @@ -65,8 +65,8 @@ int main() Multimap_clear(&mmap); - c_forarray (pair, v, {{1, "ä"}, {2, "ё"}, {2, "ö"}, {3, "ü"}}) - insert(&mmap, v->a, v->b); + c_forlist (i, pair, {{1, "ä"}, {2, "ё"}, {2, "ö"}, {3, "ü"}}) + insert(&mmap, i.ref->a, i.ref->b); print("#6", mmap); } } diff --git a/examples/music_arc.c b/examples/music_arc.c index a6b5d90f..616114fc 100644 --- a/examples/music_arc.c +++ b/examples/music_arc.c @@ -35,11 +35,11 @@ void example3() { c_auto (SongVec, vec1, vec2) { - c_forarray (Song, v, { + c_forlist (i, Song, { Song_from("Bob Dylan", "The Times They Are A Changing"), Song_from("Aretha Franklin", "Bridge Over Troubled Water"), Song_from("Thalia", "Entre El Mar y Una Estrella") - }) SongVec_emplace(&vec1, *v); + }) SongVec_emplace(&vec1, *i.ref); // Share all entries in vec with vec2, except Bob Dylan. c_foreach (s, SongVec, vec1) @@ -53,9 +53,9 @@ void example3() // SongVec_push(&vec2, SongArc_from(Song_from("Rihanna", "Stay"))); // We now have two vectors with some shared, some unique entries. - c_forarray (SongVec, v, {vec1, vec2}) { + c_forlist (i, SongVec, {vec1, vec2}) { puts("VEC:"); - c_foreach (s, SongVec, *v) + c_foreach (s, SongVec, *i.ref) printf(" %s - %s, REFS: %ld\n", cstr_str(&s.ref->get->artist), cstr_str(&s.ref->get->title), *s.ref->use_count); diff --git a/examples/new_list.c b/examples/new_list.c index e760a093..87260931 100644 --- a/examples/new_list.c +++ b/examples/new_list.c @@ -39,8 +39,8 @@ int main() clist_i32_push_back(&lst, 123); c_auto (clist_pnt, plst) { - c_forarray (Point, v, {{42, 14}, {32, 94}, {62, 81}}) - clist_pnt_push_back(&plst, *v); + c_forlist (i, Point, {{42, 14}, {32, 94}, {62, 81}}) + clist_pnt_push_back(&plst, *i.ref); clist_pnt_sort(&plst); @@ -50,8 +50,8 @@ int main() } c_auto (clist_float, flst) { - c_forarray (float, v, {123.3f, 321.2f, -32.2f, 78.2f}) - clist_float_push_back(&flst, *v); + c_forlist (i, float, {123.3f, 321.2f, -32.2f, 78.2f}) + clist_float_push_back(&flst, *i.ref); c_foreach (i, clist_float, flst) printf(" %g", *i.ref); } diff --git a/examples/new_map.c b/examples/new_map.c index c94c2b44..7f12c43c 100644 --- a/examples/new_map.c +++ b/examples/new_map.c @@ -49,23 +49,23 @@ int main() { cmap_int_insert(&map, 123, 321); - c_forarray (cmap_pnt_raw, v, {{{42, 14}, 1}, {{32, 94}, 2}, {{62, 81}, 3}}) - cmap_pnt_insert(&pmap, v->first, v->second); + c_forlist (i, cmap_pnt_raw, {{{42, 14}, 1}, {{32, 94}, 2}, {{62, 81}, 3}}) + cmap_pnt_insert(&pmap, c_pair(i.ref)); c_foreach (i, cmap_pnt, pmap) printf(" (%d, %d: %d)", i.ref->first.x, i.ref->first.y, i.ref->second); puts(""); - c_forarray (cmap_str_raw, v, { + c_forlist (i, cmap_str_raw, { {"Hello, friend", "long time no see"}, {"So long, friend", "see you around"}, - }) cmap_str_emplace(&smap, v->first, v->second); + }) cmap_str_emplace(&smap, c_pair(i.ref)); - c_forarray_p (const char*, v, { + c_forlist (i, const char*, { "Hello, friend", "Nice to see you again", "So long, friend", - }) cset_str_emplace(&sset, *v); + }) cset_str_emplace(&sset, *i.ref); c_foreach (i, cset_str, sset) printf(" %s\n", cstr_str(i.ref)); diff --git a/examples/new_smap.c b/examples/new_smap.c index 7c2ddb35..154b04a3 100644 --- a/examples/new_smap.c +++ b/examples/new_smap.c @@ -47,11 +47,11 @@ int main() } c_auto (PMap, pmap) { - c_forarray (PMap_value, v, { + c_forlist (i, PMap_value, { {{42, 14}, 1}, {{32, 94}, 2}, {{62, 81}, 3}, - }) PMap_insert(&pmap, c_pair(v)); + }) PMap_insert(&pmap, c_pair(i.ref)); c_forpair (p, i, PMap, pmap) printf(" (%d,%d: %d)", _.p->x, _.p->y, *_.i); @@ -59,11 +59,11 @@ int main() } c_auto (SMap, smap) { - c_forarray (SMap_raw, v, { + c_forlist (i, SMap_raw, { {"Hello, friend", "this is the mapped value"}, {"The brown fox", "jumped"}, {"This is the time", "for all good things"}, - }) SMap_emplace(&smap, c_pair(v)); + }) SMap_emplace(&smap, c_pair(i.ref)); c_forpair (i, j, SMap, smap) printf(" (%s: %s)\n", cstr_str(_.i), cstr_str(_.j)); diff --git a/examples/person_arc.c b/examples/person_arc.c index bddf7bd6..cd8d1f87 100644 --- a/examples/person_arc.c +++ b/examples/person_arc.c @@ -54,8 +54,8 @@ int main() Persons_push_back(&vec, PSPtr_make(Person_new("Audrey", "Home"))); // Clone/share p and q to the vector - c_forarray (PSPtr, v, {p, q}) - Persons_push_back(&vec, PSPtr_clone(*v)); + c_forlist (i, PSPtr, {p, q}) + Persons_push_back(&vec, PSPtr_clone(*i.ref)); c_foreach (i, Persons, vec) printf("%s %s\n", cstr_str(&i.ref->get->name), cstr_str(&i.ref->get->last)); diff --git a/examples/phonebook.c b/examples/phonebook.c index 1455c978..825ef61d 100644 --- a/examples/phonebook.c +++ b/examples/phonebook.c @@ -39,8 +39,8 @@ void print_phone_book(cmap_str phone_book) int main(int argc, char **argv) { c_auto (cset_str, names) { - c_forarray_p (const char*, v, {"Hello", "Cool", "True"}) - cset_str_emplace(&names, *v); + c_forlist (i, const char*, {"Hello", "Cool", "True"}) + cset_str_emplace(&names, *i.ref); c_foreach (i, cset_str, names) printf("%s ", cstr_str(i.ref)); @@ -48,12 +48,12 @@ int main(int argc, char **argv) } c_auto (cmap_str, phone_book) { - c_forarray (cmap_str_raw, v, { + c_forlist (i, cmap_str_raw, { {"Lilia Friedman", "(892) 670-4739"}, {"Tariq Beltran", "(489) 600-7575"}, {"Laiba Juarez", "(303) 885-5692"}, {"Elliott Mooney", "(945) 616-4482"}, - }) cmap_str_emplace(&phone_book, c_pair(v)); + }) cmap_str_emplace(&phone_book, c_pair(i.ref)); printf("Phone book:\n"); print_phone_book(phone_book); diff --git a/examples/priority.c b/examples/priority.c index b8766a77..d801c118 100644 --- a/examples/priority.c +++ b/examples/priority.c @@ -20,8 +20,8 @@ int main() { cpque_i_push(&heap, stc64_uniform(&rng, &dist)); // push some negative numbers too. - c_forarray (int, v, {-231, -32, -873, -4, -343}) - cpque_i_push(&heap, *v); + c_forlist (i, int, {-231, -32, -873, -4, -343}) + cpque_i_push(&heap, *i.ref); c_forrange (N) cpque_i_push(&heap, stc64_uniform(&rng, &dist)); diff --git a/examples/shape.c b/examples/shape.c index e72c8b7d..a17208ef 100644 --- a/examples/shape.c +++ b/examples/shape.c @@ -143,11 +143,11 @@ int main(void) Polygon* pol1 = c_new(Polygon, Polygon_init()); Polygon* pol2 = c_new(Polygon, Polygon_init()); - c_forarray (Point, p, {{50, 72}, {123, 73}, {127, 201}, {828, 333}}) - Polygon_addPoint(pol1, *p); + c_forlist (i, Point, {{50, 72}, {123, 73}, {127, 201}, {828, 333}}) + Polygon_addPoint(pol1, *i.ref); - c_forarray (Point, p, {{5, 7}, {12, 7}, {12, 20}, {82, 33}, {17, 56}}) - Polygon_addPoint(pol2, *p); + 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); diff --git a/examples/sidebyside.cpp b/examples/sidebyside.cpp index 282beefb..2578c120 100644 --- a/examples/sidebyside.cpp +++ b/examples/sidebyside.cpp @@ -47,8 +47,8 @@ int main() { c_auto (cmap_si, food) { - c_forarray (cmap_si_raw, v, {{"burger", 5}, {"pizza", 12}, {"steak", 15}}) - cmap_si_emplace(&food, c_pair(v)); + c_forlist (i, cmap_si_raw, {{"burger", 5}, {"pizza", 12}, {"steak", 15}}) + cmap_si_emplace(&food, c_pair(i.ref)); c_foreach (i, cmap_si, food) printf("%s, %d\n", cstr_str(&i.ref->first), i.ref->second); diff --git a/examples/words.c b/examples/words.c index 888f7abb..097447aa 100644 --- a/examples/words.c +++ b/examples/words.c @@ -13,10 +13,10 @@ int main1() c_auto (cvec_str, words) c_auto (cmap_str, word_map) { - c_forarray_p (const char*, v, { + c_forlist (i, const char*, { "this", "sentence", "is", "not", "a", "sentence", "this", "sentence", "is", "a", "hoax" - }) cvec_str_emplace_back(&words, *v); + }) cvec_str_emplace_back(&words, *i.ref); c_foreach (w, cvec_str, words) { cmap_str_emplace(&word_map, cstr_str(w.ref), 0).ref->second += 1; diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index 9dab76c9..d01254a6 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -176,12 +176,12 @@ STC_INLINE char* c_strnstrn(const char *s, const char *needle, #define c_forfilter5(it, C, cnt, filter, cond) \ c_forfilter_s(it, C, C##_begin(&cnt), filter, cond) #define c_forfilter_s(it, C, start, filter, cond) \ - c_forloop_s(it, C, start, cond) if (!((filter) && ++it.taken)) ; else + c_forloop_s(it, C, start, cond) if (!((filter) && ++it.count)) ; else #define c_forloop(i, C, cnt, cond) \ c_forloop_s(i, C, C##_begin(&cnt), cond) #define c_forloop_s(i, C, start, cond) \ - for (struct {C##_iter it; const C##_value *ref; size_t index, taken;} \ + for (struct {C##_iter it; const C##_value *ref; size_t index, count;} \ i = {.it=start, .ref=i.it.ref}; i.ref && (cond) \ ; C##_next(&i.it), i.ref = i.it.ref, ++i.index) @@ -199,6 +199,17 @@ STC_INLINE char* c_strnstrn(const char *s, const char *needle, for (itype i=start, _inc=step, _end=(stop) - (0 < _inc) \ ; (i <= _end) == (0 < _inc); i += _inc) +#define c_forlist(it, T, ...) \ + for (struct {T* data; T* ref; size_t index;} \ + it = {.data=(T[])__VA_ARGS__, .ref=it.data} \ + ; it.ref != &it.data[c_arraylen(((T[])__VA_ARGS__))] \ + ; ++it.ref, ++it.index) + +// [deprecated] - replaced by c_forlist(): +#define c_forarray(T, v, ...) \ + for (T _a[] = __VA_ARGS__, *v = _a; v != _a + c_arraylen(_a); ++v) +#define c_forarray_p(T, v, ...) \ + for (T _a[] = __VA_ARGS__, **v = _a; v != _a + c_arraylen(_a); ++v) #define c_autovar c_with // [deprecated] #define c_autoscope c_scope // [deprecated] #define c_autodefer c_defer // [deprecated] @@ -229,14 +240,8 @@ STC_INLINE char* c_strnstrn(const char *s, const char *needle, *b = (n)*sizeof *b > (BYTES) ? c_alloc_n(type, n) : _c_b \ ; b; b != _c_b ? c_free(b) : (void)0, b = NULL) -#define c_forarray(T, v, ...) \ - for (T _a[] = __VA_ARGS__, *v = _a; v != _a + c_arraylen(_a); ++v) - -#define c_forarray_p(T, v, ...) \ - for (T _a[] = __VA_ARGS__, **v = _a; v != _a + c_arraylen(_a); ++v) - #define c_pair(v) (v)->first, (v)->second -#define c_drop(C, ...) do { c_forarray_p(C*, _p, {__VA_ARGS__}) C##_drop(*_p); } while(0) +#define c_drop(C, ...) do { c_forlist (_i, C*, {__VA_ARGS__}) C##_drop(*_i.ref); } while(0) #define c_find_if(it, C, cnt, pred) do { \ size_t index = 0; \ |
