From a9ad1ad0d4f4d6b8f702b2a14bc9a508829da9ff Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Wed, 19 May 2021 11:50:40 +0200 Subject: More cleanups and refinements. --- README.md | 6 ++-- docs/carray_api.md | 3 +- docs/ccommon_api.md | 15 +++++---- docs/cmap_api.md | 42 ++++++++++++------------- docs/cpque_api.md | 24 +++++++------- docs/csmap_api.md | 20 ++++++------ examples/cpque.c | 14 ++++----- examples/demos.c | 26 ++++++++-------- examples/ex_gauss1.c | 35 ++++++++++----------- examples/ex_gauss2.c | 18 +++++------ examples/list_erase.c | 3 +- examples/multimap.c | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++ examples/olympics.c | 76 --------------------------------------------- stc/ccommon.h | 12 +++---- stc/clist.h | 2 +- stc/cmap.h | 2 +- stc/cpque.h | 2 +- stc/cqueue.h | 2 +- stc/csmap.h | 2 +- stc/cstack.h | 2 +- 20 files changed, 203 insertions(+), 189 deletions(-) create mode 100644 examples/multimap.c delete mode 100644 examples/olympics.c diff --git a/README.md b/README.md index 02cf93b9..bdd27c05 100644 --- a/README.md +++ b/README.md @@ -10,9 +10,9 @@ with a uniform API across the containers, and is similar to the c++ standard lib For an introduction to templated containers, please read the blog by Ian Fisher on [type-safe generic data structures in C](https://iafisher.com/blog/2020/06/type-safe-generics-in-c). -STC is a compact, header-only library with the all the major "standard" data containers, except for -the multi-map/set variants: -- [***carray*** - **multi-dim array** type](docs/carray_api.md) +STC is a compact, header-only library with the all the major "standard" data containers, except for the +multimap/set variants. However, there is an example how to create a multimap in the examples folder. +- [***carray*** - **multi-dim dynamic array** type](docs/carray_api.md) - [***cbits*** - **std::bitset** alike type](docs/cbits_api.md) - [***cdeq*** - **std::deque** alike type](docs/cdeq_api.md) - [***clist*** - **std::forward_list** alike type](docs/clist_api.md) diff --git a/docs/carray_api.md b/docs/carray_api.md index 3ae293c3..836726c2 100644 --- a/docs/carray_api.md +++ b/docs/carray_api.md @@ -84,7 +84,8 @@ int main() { // Ex1 int xd = 30, yd = 20, zd = 10; - carray3f arr3 = carray3f_init(xd, yd, zd, 0.0f); // define arr3[30][20][10], init with 0.0f. + // define arr3[30][20][10], initialized with zeros. + carray3f arr3 = carray3f_with_values(xd, yd, zd, 0.0f); arr3.data[5][4][3] = 3.14f; float *arr1 = arr3.data[5][4]; diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index 75e27327..bf79a757 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -56,14 +56,17 @@ constructors are defined. This ensures that resources are released after usage, **NB**: ***Only*** use `c_breakwith` to break out of the `c_with`-block if needed. ***Never*** use `return`, `break`, or `goto` inside the block. -| Usage | Description | -|:-------------------------------|:-------------------------------------------------| -| `c_with (acquire, release)` | Do `acquire`. Defer `release` to end of block | -| `c_defer (release)` | Defer `release` to end of defer block | -| `c_withvar (ctype, v)` | `c_with (ctype v = ctype_init(), ctype_del(&v))` | -| `c_withbuf (buf, type, n)` | Declare, allocate and free memory buffer | +| Usage | Description | +|:---------------------------------|:--------------------------------------------------| +| `c_with (acquire, release)` | Do `acquire`. Defer `release` to end of block | +| `c_defer (release...)` | Defer `release` to end of defer block | +| `c_withvar (type, var)` | `c_with (type var = type_init(), type_del(&var))` | +| `c_withbuf (buf, type, n)` | Declare, allocate and free memory buffer | The `acquire`argument must be of the form: `type var = get_resource`. +**c_with** and **c_defer** are general macros, whereas **c_withvar** requires that there +are methods *type_init()* to create and return an object of type, and *type_del()* +that destructs the object. These are available for all STC containers. ```c // Example: Load each line of a text file into a vector of strings diff --git a/docs/cmap_api.md b/docs/cmap_api.md index 9fd96e5c..590577a7 100644 --- a/docs/cmap_api.md +++ b/docs/cmap_api.md @@ -206,17 +206,17 @@ using_cmap(vi, Vec3i, int, c_memcmp_equals, // bitwise equals int main() { - cmap_vi vecs = cmap_vi_init(); - - cmap_vi_emplace(&vecs, (Vec3i){100, 0, 0}, 1); - cmap_vi_emplace(&vecs, (Vec3i){ 0, 100, 0}, 2); - cmap_vi_emplace(&vecs, (Vec3i){ 0, 0, 100}, 3); - cmap_vi_emplace(&vecs, (Vec3i){100, 100, 100}, 4); - - c_foreach (i, cmap_vi, vecs) - printf("{ %3d, %3d, %3d }: %d\n", i.ref->first.x, i.ref->first.y, i.ref->first.z, i.ref->second); - - cmap_vi_del(&vecs); + // Define map with defered destruct + c_with (cmap_vi vecs = cmap_vi_init(), cmap_vi_del(&vecs)) + { + cmap_vi_emplace(&vecs, (Vec3i){100, 0, 0}, 1); + cmap_vi_emplace(&vecs, (Vec3i){ 0, 100, 0}, 2); + cmap_vi_emplace(&vecs, (Vec3i){ 0, 0, 100}, 3); + cmap_vi_emplace(&vecs, (Vec3i){100, 100, 100}, 4); + + c_foreach (i, cmap_vi, vecs) + printf("{ %3d, %3d, %3d }: %d\n", i.ref->first.x, i.ref->first.y, i.ref->first.z, i.ref->second); + } } ``` Output: @@ -238,16 +238,16 @@ using_cmap(iv, int, Vec3i); int main() { - cmap_iv vecs = cmap_iv_init(); - cmap_iv_emplace(&vecs, 1, (Vec3i){100, 0, 0}); - cmap_iv_emplace(&vecs, 2, (Vec3i){ 0, 100, 0}); - cmap_iv_emplace(&vecs, 3, (Vec3i){ 0, 0, 100}); - cmap_iv_emplace(&vecs, 4, (Vec3i){100, 100, 100}); - - c_foreach (i, cmap_iv, vecs) - printf("%d: { %3d, %3d, %3d }\n", i.ref->first, i.ref->second.x, i.ref->second.y, i.ref->second.z); - - cmap_iv_del(&vecs); + c_with (cmap_iv vecs = cmap_iv_init(), cmap_iv_del(&vecs)) + { + cmap_iv_emplace(&vecs, 1, (Vec3i){100, 0, 0}); + cmap_iv_emplace(&vecs, 2, (Vec3i){ 0, 100, 0}); + cmap_iv_emplace(&vecs, 3, (Vec3i){ 0, 0, 100}); + cmap_iv_emplace(&vecs, 4, (Vec3i){100, 100, 100}); + + c_foreach (i, cmap_iv, vecs) + printf("%d: { %3d, %3d, %3d }\n", i.ref->first, i.ref->second.x, i.ref->second.y, i.ref->second.z); + } } ``` Output: diff --git a/docs/cpque_api.md b/docs/cpque_api.md index a5cd056e..6f7917f5 100644 --- a/docs/cpque_api.md +++ b/docs/cpque_api.md @@ -68,18 +68,20 @@ int main() stc64_t rng = stc64_init(1234); stc64_uniform_t dist = stc64_uniform_init(0, N * 10); - cpque_i heap = cpque_i_init(); - // Push ten million random numbers to priority queue, plus some negative ones. - c_forrange (N) - cpque_i_push(&heap, stc64_uniform(&rng, &dist)); - c_emplace(cpque_i, heap, {-231, -32, -873, -4, -343}); - - // Extract and display the fifty smallest. - c_forrange (50) { - printf("%zd ", *cpque_i_top(&heap)); - cpque_i_pop(&heap); + // Declare heap, with defered del() + c_with (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) + cpque_i_push(&heap, stc64_uniform(&rng, &dist)); + c_emplace(cpque_i, heap, {-231, -32, -873, -4, -343}); + + // Extract and display the fifty smallest. + c_forrange (50) { + printf("%zd ", *cpque_i_top(&heap)); + cpque_i_pop(&heap); + } } - cpque_i_del(&heap); } ``` Output: diff --git a/docs/csmap_api.md b/docs/csmap_api.md index 5b76f7bb..a5d0aeee 100644 --- a/docs/csmap_api.md +++ b/docs/csmap_api.md @@ -221,16 +221,16 @@ using_csmap(iv, int, Vec3i); int main() { - csmap_iv vecs = csmap_iv_init(); - csmap_iv_emplace(&vecs, 1, (Vec3i){100, 0, 0}); - csmap_iv_emplace(&vecs, 2, (Vec3i){ 0, 100, 0}); - csmap_iv_emplace(&vecs, 3, (Vec3i){ 0, 0, 100}); - csmap_iv_emplace(&vecs, 4, (Vec3i){100, 100, 100}); - - c_foreach (i, csmap_iv, vecs) - printf("%d: { %3d, %3d, %3d }\n", i.ref->first, i.ref->second.x, i.ref->second.y, i.ref->second.z); - - csmap_iv_del(&vecs); + c_with (csmap_iv vecs = csmap_iv_init(), csmap_iv_del(&vecs)) + { + csmap_iv_emplace(&vecs, 1, (Vec3i){100, 0, 0}); + csmap_iv_emplace(&vecs, 2, (Vec3i){ 0, 100, 0}); + csmap_iv_emplace(&vecs, 3, (Vec3i){ 0, 0, 100}); + csmap_iv_emplace(&vecs, 4, (Vec3i){100, 100, 100}); + + c_foreach (i, csmap_iv, vecs) + printf("%d: { %3d, %3d, %3d }\n", i.ref->first, i.ref->second.x, i.ref->second.y, i.ref->second.z); + } } ``` Output: diff --git a/examples/cpque.c b/examples/cpque.c index f9dc871c..d43ddef8 100644 --- a/examples/cpque.c +++ b/examples/cpque.c @@ -31,24 +31,22 @@ int main() { const int data[] = {1,8,5,6,3,4,0,9,7,2}; - c_withvar (cpque_imax, q) + c_withvar (cpque_imax, q) // init() and defered del() + c_withvar (cpque_imin, q2) + c_withvar (cpque_imix, q3) { c_forrange (n, c_arraylen(data)) cpque_imax_push(&q, n); + print_cpque_imax(q); - } - c_withvar (cpque_imin, q2) - { cpque_imin_emplace_items(&q2, data, c_arraylen(data)); print_cpque_imin(q2); - } - // Using imix_less to compare elements. - c_withvar (cpque_imix, q3) - { + // Using imix_less to compare elements. c_forrange (n, c_arraylen(data)) cpque_imix_push(&q3, n); + print_cpque_imix(q3); } } \ No newline at end of file diff --git a/examples/demos.c b/examples/demos.c index 388e3e46..91daf1b1 100644 --- a/examples/demos.c +++ b/examples/demos.c @@ -64,7 +64,7 @@ using_cvec_str(); void vectordemo2() { printf("\nVECTORDEMO2\n"); - c_withvar (cvec_str, names) { + c_with (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"); @@ -142,20 +142,20 @@ using_cmap_strkey(si, int); // Shorthand macro for the general using_cmap expans void mapdemo2() { printf("\nMAPDEMO2\n"); - cmap_si nums = cmap_si_init(); - cmap_si_emplace_or_assign(&nums, "Hello", 64); - cmap_si_emplace_or_assign(&nums, "Groovy", 121); - cmap_si_emplace_or_assign(&nums, "Groovy", 200); // overwrite previous - - // iterate the map: - for (cmap_si_iter_t i = cmap_si_begin(&nums); i.ref != cmap_si_end(&nums).ref; cmap_si_next(&i)) - printf("long: %s: %d\n", i.ref->first.str, i.ref->second); + c_with (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); + cmap_si_emplace_or_assign(&nums, "Groovy", 200); // overwrite previous - // or rather use the short form: - c_foreach (i, cmap_si, nums) - printf("short: %s: %d\n", i.ref->first.str, i.ref->second); + // iterate the map: + for (cmap_si_iter_t i = cmap_si_begin(&nums); i.ref != cmap_si_end(&nums).ref; cmap_si_next(&i)) + printf("long: %s: %d\n", i.ref->first.str, i.ref->second); - cmap_si_del(&nums); + // or rather use the short form: + c_foreach (i, cmap_si, nums) + printf("short: %s: %d\n", i.ref->first.str, i.ref->second); + } } diff --git a/examples/ex_gauss1.c b/examples/ex_gauss1.c index 4ab9e7a3..9b9285e6 100644 --- a/examples/ex_gauss1.c +++ b/examples/ex_gauss1.c @@ -29,29 +29,28 @@ int main() stc64_t rng = stc64_init(seed); stc64_normalf_t dist = stc64_normalf_init(Mean, StdDev); - c_withvar (cvec_e, vhist) { + // Create and init histogram vec and map with defered destructors: + c_withvar (cvec_e, vhist) + c_withvar (cmap_i, mhist) + { + c_forrange (N) { + int index = (int) round( stc64_normalf(&rng, &dist) ); + cmap_i_emplace(&mhist, index, 0).ref->second += 1; + } - // Create histogram map - c_withvar (cmap_i, mhist) { - c_forrange (N) { - int index = (int) round( stc64_normalf(&rng, &dist) ); - cmap_i_emplace(&mhist, index, 0).ref->second += 1; - } + // Transfer map to vec and sort it by map keys. + c_foreach (i, cmap_i, mhist) + cvec_e_push_back(&vhist, c_make(mapval){i.ref->first, i.ref->second}); - // Transfer map to vec and sort it by map keys. - c_foreach (i, cmap_i, mhist) - cvec_e_push_back(&vhist, c_make(mapval){i.ref->first, i.ref->second}); - } cvec_e_sort(&vhist); // Print the gaussian bar chart - c_withvar (cstr, bar) { - c_foreach (i, cvec_e, vhist) { - size_t n = (size_t) (i.ref->second * StdDev * Scale * 2.5 / (float)N); - if (n > 0) { - cstr_resize(&bar, n, '*'); - printf("%4d %s\n", i.ref->first, bar.str); - } + c_with (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) { + cstr_resize(&bar, n, '*'); + printf("%4d %s\n", i.ref->first, bar.str); } } } diff --git a/examples/ex_gauss2.c b/examples/ex_gauss2.c index bcf1e200..9ff42ebe 100644 --- a/examples/ex_gauss2.c +++ b/examples/ex_gauss2.c @@ -20,21 +20,21 @@ int main() stc64_t rng = stc64_init(seed); stc64_normalf_t dist = stc64_normalf_init(Mean, StdDev); - // Create histogram map - c_withvar (csmap_i, mhist) { + // Create and init histogram map with defered destruct + c_withvar (csmap_i, mhist) + { c_forrange (N) { int index = (int) round( stc64_normalf(&rng, &dist) ); csmap_i_emplace(&mhist, index, 0).ref->second += 1; } // Print the gaussian bar chart - c_withvar (cstr, bar) { - c_foreach (i, csmap_i, mhist) { - size_t n = (size_t) (i.ref->second * StdDev * Scale * 2.5 / (float)N); - if (n > 0) { - cstr_resize(&bar, n, '*'); - printf("%4d %s\n", i.ref->first, bar.str); - } + c_withvar (cstr, bar) + c_foreach (i, csmap_i, mhist) { + size_t n = (size_t) (i.ref->second * StdDev * Scale * 2.5 / (float)N); + if (n > 0) { + cstr_resize(&bar, n, '*'); + printf("%4d %s\n", i.ref->first, bar.str); } } } diff --git a/examples/list_erase.c b/examples/list_erase.c index d61edace..3526b5eb 100644 --- a/examples/list_erase.c +++ b/examples/list_erase.c @@ -6,7 +6,8 @@ using_clist(i, int); int main () { - c_withvar (clist_i, L) { + c_with (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 clist_i_iter_t it = clist_i_begin(&L); // ^ diff --git a/examples/multimap.c b/examples/multimap.c new file mode 100644 index 00000000..8d56119f --- /dev/null +++ b/examples/multimap.c @@ -0,0 +1,86 @@ +#include +#include +#include +#include + +// Olympics multimap example + +struct OlympicsData { int year; const char *city, *country, *date; } ol_data[] = { + {2026, "Milan and Cortina d'Ampezzo", "Italy", "February 6-22"}, + {2022, "Beijing", "China", "February 4-20"}, + {2018, "PyeongChang", "South Korea", "February 9-25"}, + {2014, "Sochi", "Russia", "February 7-23"}, + {2010, "Vancouver", "Canada", "February 12-28"}, + {2006, "Torino", "Italy", "February 10-26"}, + {2002, "Salt Lake City", "United States", "February 8-24"}, + {1998, "Nagano", "Japan", "February 7-22"}, + {1994, "Lillehammer", "Norway", "February 12-27"}, + {1992, "Albertville", "France", "February 8-23"}, + {1988, "Calgary", "Canada", "February 13-28"}, + {1984, "Sarajevo", "Yugoslavia", "February 8-19"}, + {1980, "Lake Placid", "United States", "February 13-24"}, + {1976, "Innsbruck", "Austria", "February 4-15"}, + {1972, "Sapporo", "Japan", "February 3-13"}, + {1968, "Grenoble", "France", "February 6-18"}, + {1964, "Innsbruck", "Austria", "January 29-February 9"}, + {1960, "Squaw Valley", "United States", "February 18-28"}, + {1956, "Cortina d'Ampezzo", "Italy", "January 26 - February 5"}, + {1952, "Oslo", "Norway", "February 14 - 25"}, + {1948, "St. Moritz", "Switzerland", "January 30 - February 8"}, + {1944, "canceled", "canceled", "canceled"}, + {1940, "canceled", "canceled", "canceled"}, + {1936, "Garmisch-Partenkirchen", "Germany", "February 6 - 16"}, + {1932, "Lake Placid", "United States", "February 4 - 15"}, + {1928, "St. Moritz", "Switzerland", "February 11 - 19"}, + {1924, "Chamonix", "France", "January 25 - February 5"}, +}; + + +typedef struct { int year; cstr city, date; } OlympicLocation; + +int OlympicLocation_compare(OlympicLocation* a, OlympicLocation* b) { + return a->year - b->year; +} +void OlympicLocation_del(OlympicLocation* self) { + c_del (cstr, &self->city, &self->date); +} + +// Create a clist, can be sorted by year. +using_clist(OL, OlympicLocation, OlympicLocation_compare, OlympicLocation_del); + +// Create a csmap where key is country +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_with (csmap_OL multimap = csmap_OL_init(), csmap_OL_del(&multimap)) + { + clist_OL empty = clist_OL_init(); + + for (int i = 0; i < c_arraylen(ol_data); ++i) + { + struct OlympicsData* it = &ol_data[i]; + OlympicLocation loc = {.year = it->year, + .city = cstr_from(it->city), + .date = cstr_from(it->date)}; + // Insert an empty list for each new country, and append the entry to the list. + // If list already exist in map, it returns it from the insert function. + clist_OL_push_back(&csmap_OL_insert(&multimap, cstr_from(it->country), empty).ref->second, loc); + } + + // Iterate the multimap: + c_foreach (country, csmap_OL, multimap) + { + // Sort locations by year for each country. + clist_OL_sort(&country.ref->second); + + // Loop the locations for a country sorted by year + c_foreach (loc, clist_OL, country.ref->second) + printf("%s: %d, %s, %s\n", country.ref->first.str, loc.ref->year, + loc.ref->city.str, + loc.ref->date.str); + } + } +} diff --git a/examples/olympics.c b/examples/olympics.c deleted file mode 100644 index 07388c1b..00000000 --- a/examples/olympics.c +++ /dev/null @@ -1,76 +0,0 @@ -#include -#include -#include -#include - -// Olympics multimap example - -struct OlympicsData { int year; const char *city, *country, *date; } ol_data[] = { - {2026, "Milan and Cortina d'Ampezzo", "Italy", "February 6-22"}, - {2022, "Beijing", "China", "February 4-20"}, - {2018, "PyeongChang", "South Korea", "February 9-25"}, - {2014, "Sochi", "Russia", "February 7-23"}, - {2010, "Vancouver", "Canada", "February 12-28"}, - {2006, "Torino", "Italy", "February 10-26"}, - {2002, "Salt Lake City", "United States", "February 8-24"}, - {1998, "Nagano", "Japan", "February 7-22"}, - {1994, "Lillehammer", "Norway", "February 12-27"}, - {1992, "Albertville", "France", "February 8-23"}, - {1988, "Calgary", "Canada", "February 13-28"}, - {1984, "Sarajevo", "Yugoslavia", "February 8-19"}, - {1980, "Lake Placid", "United States", "February 13-24"}, - {1976, "Innsbruck", "Austria", "February 4-15"}, - {1972, "Sapporo", "Japan", "February 3-13"}, - {1968, "Grenoble", "France", "February 6-18"}, - {1964, "Innsbruck", "Austria", "January 29-February 9"}, - {1960, "Squaw Valley", "United States", "February 18-28"}, - {1956, "Cortina d'Ampezzo", "Italy", "January 26 - February 5"}, - {1952, "Oslo", "Norway", "February 14 - 25"}, - {1948, "St. Moritz", "Switzerland", "January 30 - February 8"}, - {1944, "canceled", "canceled", "canceled"}, - {1940, "canceled", "canceled", "canceled"}, - {1936, "Garmisch-Partenkirchen", "Germany", "February 6 - 16"}, - {1932, "Lake Placid", "United States", "February 4 - 15"}, - {1928, "St. Moritz", "Switzerland", "February 11 - 19"}, - {1924, "Chamonix", "France", "January 25 - February 5"}, -}; - - -typedef struct { int year; cstr city, date; } OlympicLocation; - -int OlympicLocation_compare(OlympicLocation* a, OlympicLocation* b) { - return a->year - b->year; -} -void OlympicLocation_del(OlympicLocation* self) { - c_del (cstr, &self->city, &self->date); -} - -// Create a clist, can be sorted by year. -using_clist(ol, OlympicLocation, OlympicLocation_compare, OlympicLocation_del); - -// Create a csmap where key is country -using_csmap_strkey(ol, clist_ol, clist_ol_del, c_no_clone); - - -int main() -{ - c_withvar (csmap_ol, multimap) { - clist_ol empty = clist_ol_init(); - - for (int i = 0; iyear, .city=cstr_from(it->city), .date=cstr_from(it->date)}; - // Insert an empty list for each new country, and append the entry to the list. - // If list already exist in map, it returns it from the insert function. - clist_ol_push_back(&csmap_ol_insert(&multimap, cstr_from(it->country), empty).ref->second, loc); - } - - c_foreach (country, csmap_ol, multimap) { - clist_ol_sort(&country.ref->second); // Sort locations by year for each country. - c_foreach (loc, clist_ol, country.ref->second) - printf("%s: %d, %s, %s\n", country.ref->first.str, loc.ref->year, - loc.ref->city.str, - loc.ref->date.str); - } - } -} diff --git a/stc/ccommon.h b/stc/ccommon.h index 7e73dc5f..dc87471e 100644 --- a/stc/ccommon.h +++ b/stc/ccommon.h @@ -114,13 +114,13 @@ ; it.ref != it##_end_.ref; CX##_next(&it)) #define c_forrange(...) c_MACRO_OVERLOAD(c_forrange, __VA_ARGS__) -#define c_forrange_1(stop) for (size_t _c_i=0, _c_end_=stop; _c_i < _c_end_; ++_c_i) -#define c_forrange_2(i, stop) for (size_t i=0, i##_end_=stop; i < i##_end_; ++i) -#define c_forrange_3(i, type, stop) for (type i=0, i##_end_=stop; i < i##_end_; ++i) -#define c_forrange_4(i, type, start, stop) for (type i=start, i##_end_=stop; i < i##_end_; ++i) +#define c_forrange_1(stop) for (size_t _c_ii=0, _c_end=stop; _c_ii < _c_end; ++_c_ii) +#define c_forrange_2(i, stop) for (size_t i=0, _c_end=stop; i < _c_end; ++i) +#define c_forrange_3(i, type, stop) for (type i=0, _c_end=stop; i < _c_end; ++i) +#define c_forrange_4(i, type, start, stop) for (type i=start, _c_end=stop; i < _c_end; ++i) #define c_forrange_5(i, type, start, stop, step) \ - for (type i=start, i##_inc_=step, i##_end_=(stop) - (0 < i##_inc_) \ - ; (i <= i##_end_) == (0 < i##_inc_); i += i##_inc_) + for (type i=start, _c_inc=step, _c_end=(stop) - (0 < _c_inc) \ + ; (i <= _c_end) == (0 < _c_inc); i += _c_inc) #define c_defer(...) for (int _c_ii = 0; !_c_ii; ++_c_ii, __VA_ARGS__) #define c_with(start, end) for (start, *_c_ii = NULL; !_c_ii; ++_c_ii, end) diff --git a/stc/clist.h b/stc/clist.h index dd7ecc12..8e23a43e 100644 --- a/stc/clist.h +++ b/stc/clist.h @@ -35,7 +35,7 @@ using_clist(ix, int64_t); int main() { - c_withvar (clist_ix, list) + c_with (clist_ix list = clist_ix_init(), clist_ix_del(&list)) { stc64_t rng = stc64_init(12345); int n; diff --git a/stc/cmap.h b/stc/cmap.h index a6dbd7cc..937da0af 100644 --- a/stc/cmap.h +++ b/stc/cmap.h @@ -31,7 +31,7 @@ using_cmap(mx, int, char); // Map of int -> char int main(void) { - c_withvar (cmap_mx, m) + c_with (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/stc/cpque.h b/stc/cpque.h index f6fa67a2..20acf6f6 100644 --- a/stc/cpque.h +++ b/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_withvar (cpque_f, queue) + c_with (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/stc/cqueue.h b/stc/cqueue.h index af6cc7a2..37d18fba 100644 --- a/stc/cqueue.h +++ b/stc/cqueue.h @@ -35,7 +35,7 @@ stc64_t rng = stc64_init(1234); stc64_uniform_t dist = stc64_uniform_init(rng, 0, n); - c_withvar (cqueue_i, queue) + c_with (cqueue_i queue = cqueue_i_init(), cqueue_i_del(&queue)) { // Push ten million random numbers onto the queue. for (int i=0; i int main(void) { - c_withvar (csmap_mx, m) + c_with (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/stc/cstack.h b/stc/cstack.h index 3a5f491c..2184c89e 100644 --- a/stc/cstack.h +++ b/stc/cstack.h @@ -32,7 +32,7 @@ using_cstack(i, cvec_i); int main() { - c_withvar (cstack_i, stack) + c_with (cstack_i stack = cstack_i_init(), cstack_i_del(&stack)) { for (int i=0; i<100; ++i) cstack_i_push(&stack, i*i); -- cgit v1.2.3