diff options
| author | Tyge Løvset <[email protected]> | 2022-05-22 20:52:18 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2022-05-22 20:52:18 +0200 |
| commit | 1c3e7b683469904d224f3f8f1ee0096f1c5b1b9d (patch) | |
| tree | 826ca6560cd952996eb9793cb3356a9791753f2e | |
| parent | 314a41be6b39b6c5967d79555dbf018dbc7eb5f6 (diff) | |
| download | STC-modified-1c3e7b683469904d224f3f8f1ee0096f1c5b1b9d.tar.gz STC-modified-1c3e7b683469904d224f3f8f1ee0096f1c5b1b9d.zip | |
Changed c_apply(v, ..) macro to make it more consistent with c_apply_arr(v, ..) and c_foreach (i, ..): v changed to a pointer - not value.
Note: also c_pair(v) is changed correspondingly, so usage with c_apply(v) is unchanged.
| -rw-r--r-- | README.md | 12 | ||||
| -rw-r--r-- | docs/cbox_api.md | 2 | ||||
| -rw-r--r-- | docs/cdeq_api.md | 2 | ||||
| -rw-r--r-- | docs/clist_api.md | 8 | ||||
| -rw-r--r-- | docs/cpque_api.md | 2 | ||||
| -rw-r--r-- | docs/cset_api.md | 4 | ||||
| -rw-r--r-- | docs/csset_api.md | 4 | ||||
| -rw-r--r-- | docs/cvec_api.md | 2 | ||||
| -rw-r--r-- | examples/box.c | 2 | ||||
| -rw-r--r-- | examples/city.c | 4 | ||||
| -rw-r--r-- | examples/csmap_find.c | 2 | ||||
| -rw-r--r-- | examples/csset_erase.c | 2 | ||||
| -rw-r--r-- | examples/inits.c | 4 | ||||
| -rw-r--r-- | examples/list.c | 2 | ||||
| -rw-r--r-- | examples/list_erase.c | 2 | ||||
| -rw-r--r-- | examples/list_splice.c | 4 | ||||
| -rw-r--r-- | examples/lower_bound.c | 4 | ||||
| -rw-r--r-- | examples/mmap.c | 2 | ||||
| -rw-r--r-- | examples/music_arc.c | 4 | ||||
| -rw-r--r-- | examples/new_list.c | 4 | ||||
| -rw-r--r-- | examples/new_map.c | 2 | ||||
| -rw-r--r-- | examples/person_arc.c | 2 | ||||
| -rw-r--r-- | examples/phonebook.c | 2 | ||||
| -rw-r--r-- | examples/prime.c | 1 | ||||
| -rw-r--r-- | examples/priority.c | 2 | ||||
| -rw-r--r-- | examples/shape.c | 4 | ||||
| -rw-r--r-- | examples/words.c | 2 | ||||
| -rw-r--r-- | include/stc/ccommon.h | 15 |
28 files changed, 52 insertions, 50 deletions
@@ -119,7 +119,7 @@ An alternative and often preferred way to write this code with STL is: int main(void) { c_auto (FVec, vec) // RAII - specify create and destruct at one place. { - c_apply(v, FVec_push_back(&vec, v), float, {10.f, 20.f, 30.f}); + c_apply(v, FVec_push_back(&vec, *v), float, {10.f, 20.f, 30.f}); c_foreach (i, FVec, vec) // generic iteration and element access printf(" %g", *i.ref); @@ -191,11 +191,11 @@ int main(void) { c_auto (csmap_int, map) { // add some elements to each container - c_apply(v, cset_int_insert(&set, v), int, {10, 20, 30}); - c_apply(v, cvec_pnt_push_back(&vec, v), struct Point, { {10, 1}, {20, 2}, {30, 3} }); - c_apply(v, cdeq_int_push_back(&deq, v), int, {10, 20, 30}); - c_apply(v, clist_int_push_back(&lst, v), int, {10, 20, 30}); - c_apply(v, cstack_int_push(&stk, v), int, {10, 20, 30}); + c_apply(v, cset_int_insert(&set, *v), int, {10, 20, 30}); + c_apply(v, cvec_pnt_push_back(&vec, *v), struct Point, { {10, 1}, {20, 2}, {30, 3} }); + c_apply(v, cdeq_int_push_back(&deq, *v), int, {10, 20, 30}); + c_apply(v, clist_int_push_back(&lst, *v), int, {10, 20, 30}); + c_apply(v, cstack_int_push(&stk, *v), int, {10, 20, 30}); c_apply(v, csmap_int_insert(&map, c_pair(v)), csmap_int_raw, { {20, 2}, {10, 1}, {30, 3} }); diff --git a/docs/cbox_api.md b/docs/cbox_api.md index 59c44e02..8709548a 100644 --- a/docs/cbox_api.md +++ b/docs/cbox_api.md @@ -84,7 +84,7 @@ int main() c_auto (IVec, vec) // declare and init vec, call drop at scope exit c_auto (ISet, set) // similar { - c_apply(v, IVec_push(&vec, v), IBox, { + c_apply(v, IVec_push(&vec, *v), IBox, { IBox_make(2021), IBox_make(2012), IBox_make(2022), IBox_make(2015), }); diff --git a/docs/cdeq_api.md b/docs/cdeq_api.md index 4a416d09..a1f1228f 100644 --- a/docs/cdeq_api.md +++ b/docs/cdeq_api.md @@ -110,7 +110,7 @@ int main() { printf(" %d", *i.ref); puts(""); - c_apply(v, cdeq_i_push_back(&q, v), int, {1, 4, 5, 22, 33, 2}); + c_apply(v, cdeq_i_push_back(&q, *v), int, {1, 4, 5, 22, 33, 2}); c_foreach (i, cdeq_i, q) printf(" %d", *i.ref); puts(""); diff --git a/docs/clist_api.md b/docs/clist_api.md index 5dd5d524..58970a19 100644 --- a/docs/clist_api.md +++ b/docs/clist_api.md @@ -109,7 +109,7 @@ Interleave *push_front()* / *push_back()* then *sort()*: int main() { clist_d list = clist_d_init(); - c_apply(v, clist_d_push_back(&list, v), double, { + c_apply(v, clist_d_push_back(&list, *v), double, { 10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0, 90.0 }); @@ -150,7 +150,7 @@ Use of *erase_at()* and *erase_range()*: int main () { clist_i L = clist_i_init(); - c_apply(v, clist_i_push_back(&L, v), int, {10, 20, 30, 40, 50}); + c_apply(v, clist_i_push_back(&L, *v), int, {10, 20, 30, 40, 50}); // 10 20 30 40 50 clist_i_iter it = clist_i_begin(&L); // ^ clist_i_next(&it); @@ -185,8 +185,8 @@ Splice `[30, 40]` from *L2* into *L1* before `3`: int main() { c_auto (clist_i, L1, L2) { - c_apply(v, clist_i_push_back(&L1, v), int, {1, 2, 3, 4, 5}); - c_apply(v, clist_i_push_back(&L2, v), int, {10, 20, 30, 40, 50}); + c_apply(v, clist_i_push_back(&L1, *v), int, {1, 2, 3, 4, 5}); + c_apply(v, clist_i_push_back(&L2, *v), int, {10, 20, 30, 40, 50}); 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/cpque_api.md b/docs/cpque_api.md index 1842c1b6..d9a381cc 100644 --- a/docs/cpque_api.md +++ b/docs/cpque_api.md @@ -78,7 +78,7 @@ int main() // Push ten million random numbers to priority queue, plus some negative ones. c_forrange (N) cpque_i_push(&heap, stc64_uniform(&rng, &dist)); - c_apply(v, cpque_i_push(&heap, v), int, {-231, -32, -873, -4, -343}); + c_apply(v, cpque_i_push(&heap, *v), int, {-231, -32, -873, -4, -343}); // Extract and display the fifty smallest. c_forrange (50) { diff --git a/docs/cset_api.md b/docs/cset_api.md index 1e361799..2d20c303 100644 --- a/docs/cset_api.md +++ b/docs/cset_api.md @@ -86,9 +86,9 @@ int main () c_auto (cset_str, first, second)
c_auto (cset_str, third, fourth)
{
- c_apply(v, cset_str_emplace(&second, v), const char*,
+ c_apply(v, cset_str_emplace(&second, *v), const char*,
{"red", "green", "blue"});
- c_apply(v, cset_str_emplace(&third, v), const char*,
+ c_apply(v, cset_str_emplace(&third, *v), const char*,
{"orange", "pink", "yellow"});
cset_str_emplace(&fourth, "potatoes");
diff --git a/docs/csset_api.md b/docs/csset_api.md index 1f252d13..a87bb6a2 100644 --- a/docs/csset_api.md +++ b/docs/csset_api.md @@ -85,9 +85,9 @@ c_auto (csset_str, fifth) c_auto (csset_str, first, second)
c_auto (csset_str, third, fourth)
{
- c_apply(v, csset_str_emplace(&second, v), const char*,
+ c_apply(v, csset_str_emplace(&second, *v), const char*,
{"red", "green", "blue"});
- c_apply(v, csset_str_emplace(&third, v), const char*,
+ c_apply(v, csset_str_emplace(&third, *v), const char*,
{"orange", "pink", "yellow"});
csset_str_emplace(&fourth, "potatoes");
diff --git a/docs/cvec_api.md b/docs/cvec_api.md index 3e12e5e2..2ea52e56 100644 --- a/docs/cvec_api.md +++ b/docs/cvec_api.md @@ -123,7 +123,7 @@ int main() cvec_int_push(&vec, 13); // Append a set of numbers - c_apply(v, cvec_int_push(&vec, v), int, {7, 5, 16, 8}); + c_apply(v, cvec_int_push(&vec, *v), int, {7, 5, 16, 8}); printf("initial:"); c_foreach (k, cvec_int, vec) { diff --git a/examples/box.c b/examples/box.c index 423e9cb1..2f82a694 100644 --- a/examples/box.c +++ b/examples/box.c @@ -52,7 +52,7 @@ int main() Persons_push_back(&vec, PBox_make(Person_new("Audrey", "Home")));
// NB! Clone p and q to the vector using emplace_back()
- c_apply(v, Persons_push_back(&vec, PBox_clone(v)), PBox, {p, q});
+ c_apply(v, Persons_push_back(&vec, PBox_clone(*v)), PBox, {p, q});
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/city.c b/examples/city.c index d2178494..017c1c20 100644 --- a/examples/city.c +++ b/examples/city.c @@ -52,8 +52,8 @@ int main(void) {
struct City_s { const char *name, *country; float lat, lon; int pop; };
- c_apply(c, Cities_push(&cities, CityArc_make((City){cstr_from(c.name), cstr_from(c.country),
- c.lat, c.lon, c.pop})), struct City_s, {
+ c_apply(c, Cities_push(&cities, CityArc_make((City){cstr_from(c->name), cstr_from(c->country),
+ c->lat, c->lon, c->pop})), struct City_s, {
{"New York", "US", 4.3, 23.2, 9000000},
{"Paris", "France", 4.3, 23.2, 9000000},
{"Berlin", "Germany", 4.3, 23.2, 9000000},
diff --git a/examples/csmap_find.c b/examples/csmap_find.c index 9e3aba83..c3047110 100644 --- a/examples/csmap_find.c +++ b/examples/csmap_find.c @@ -62,7 +62,7 @@ int main() print_collection_cvec_istr(v); c_foreach (i, cvec_istr, cvec_istr_begin(&v), cvec_istr_end(&v)) - csmap_istr_emplace(&m1, c_pair(*i.ref)); + csmap_istr_emplace(&m1, c_pair(i.ref)); puts("The modified map m1 is (key, value):"); print_collection_csmap_istr(m1); diff --git a/examples/csset_erase.c b/examples/csset_erase.c index fe9977a9..3cfaa7f1 100644 --- a/examples/csset_erase.c +++ b/examples/csset_erase.c @@ -7,7 +7,7 @@ int main() {
c_auto (csset_int, set)
{
- c_apply(v, csset_int_insert(&set, v),
+ c_apply(v, csset_int_insert(&set, *v),
int, {30, 20, 80, 40, 60, 90, 10, 70, 50});
c_foreach (k, csset_int, set)
printf(" %d", *k.ref);
diff --git a/examples/inits.c b/examples/inits.c index 97d86389..ae86cb87 100644 --- a/examples/inits.c +++ b/examples/inits.c @@ -90,7 +90,7 @@ int main(void) // CVEC PAIR
c_auto (cvec_ip, pairs1) {
- c_apply(p, cvec_ip_push_back(&pairs1, p), ipair_t,
+ c_apply(p, cvec_ip_push_back(&pairs1, *p), ipair_t,
{{5, 6}, {3, 4}, {1, 2}, {7, 8}});
cvec_ip_sort(&pairs1);
@@ -102,7 +102,7 @@ int main(void) // CLIST PAIR
c_auto (clist_ip, pairs2) {
- c_apply(p, clist_ip_push_back(&pairs2, p), ipair_t,
+ c_apply(p, clist_ip_push_back(&pairs2, *p), ipair_t,
{{5, 6}, {3, 4}, {1, 2}, {7, 8}});
clist_ip_sort(&pairs2);
diff --git a/examples/list.c b/examples/list.c index b9f56f13..1b3ccdba 100644 --- a/examples/list.c +++ b/examples/list.c @@ -36,7 +36,7 @@ int main() { puts("");
clist_fx_clear(&list);
- c_apply(v, clist_fx_push_back(&list, v), int, {10, 20, 30, 40, 30, 50});
+ c_apply(v, clist_fx_push_back(&list, *v), int, {10, 20, 30, 40, 30, 50});
const double* v = clist_fx_get(&list, 30);
printf("found: %f\n", *v);
c_foreach (i, clist_fx, list) printf(" %g", *i.ref);
diff --git a/examples/list_erase.c b/examples/list_erase.c index 1e5d2e9b..19a00299 100644 --- a/examples/list_erase.c +++ b/examples/list_erase.c @@ -8,7 +8,7 @@ int main () {
c_auto (clist_int, L)
{
- c_apply(i, clist_int_push_back(&L, i), int, {10, 20, 30, 40, 50});
+ c_apply(i, clist_int_push_back(&L, *i), int, {10, 20, 30, 40, 50});
c_foreach (x, clist_int, L)
printf("%d ", *x.ref);
puts("");
diff --git a/examples/list_splice.c b/examples/list_splice.c index 754ec6fb..aaec506a 100644 --- a/examples/list_splice.c +++ b/examples/list_splice.c @@ -17,8 +17,8 @@ int main () { c_auto (clist_i, list1, list2) { - c_apply(v, clist_i_push_back(&list1, v), int, {1, 2, 3, 4, 5}); - c_apply(v, clist_i_push_back(&list2, v), int, {10, 20, 30, 40, 50}); + c_apply(v, clist_i_push_back(&list1, *v), int, {1, 2, 3, 4, 5}); + c_apply(v, clist_i_push_back(&list2, *v), int, {10, 20, 30, 40, 50}); print_ilist("list1:", list1); print_ilist("list2:", list2); diff --git a/examples/lower_bound.c b/examples/lower_bound.c index bb8fdf66..6039dd48 100644 --- a/examples/lower_bound.c +++ b/examples/lower_bound.c @@ -13,7 +13,7 @@ int main() {
int key, *res;
- c_apply(t, cvec_int_push(&vec, t), int, {
+ c_apply(t, cvec_int_push(&vec, *t), int, {
40, 600, 1, 7000, 2, 500, 30,
});
@@ -41,7 +41,7 @@ int main() {
int key, *res;
- c_apply(t, csset_int_push(&set, t), int, {
+ c_apply(t, csset_int_push(&set, *t), int, {
40, 600, 1, 7000, 2, 500, 30,
});
diff --git a/examples/mmap.c b/examples/mmap.c index 6ffd3a5d..f8e56dc3 100644 --- a/examples/mmap.c +++ b/examples/mmap.c @@ -35,7 +35,7 @@ int main() // list-initialize
struct { int first; const char* second; } vals[] =
{{2, "foo"}, {2, "bar"}, {3, "baz"}, {1, "abc"}, {5, "def"}};
- c_forrange (i, c_arraylen(vals)) insert(&mmap, c_pair(vals[i]));
+ c_forrange (i, c_arraylen(vals)) insert(&mmap, c_pair(&vals[i]));
print("#1", mmap);
// insert using value_type
diff --git a/examples/music_arc.c b/examples/music_arc.c index 24cb25f1..489a136f 100644 --- a/examples/music_arc.c +++ b/examples/music_arc.c @@ -31,7 +31,7 @@ void example3() {
c_auto (SongVec, vec, vec2)
{
- c_apply(v, SongVec_push_back(&vec, v), SongPtr, {
+ c_apply(v, SongVec_push_back(&vec, *v), SongPtr, {
SongPtr_make(Song_new("Bob Dylan", "The Times They Are A Changing")),
SongPtr_make(Song_new("Aretha Franklin", "Bridge Over Troubled Water")),
SongPtr_make(Song_new("Thalia", "Entre El Mar y Una Estrella"))
@@ -41,7 +41,7 @@ void example3() if (!cstr_equals(s.ref->get->artist, "Bob Dylan"))
SongVec_push_back(&vec2, SongPtr_clone(*s.ref));
- c_apply(v, SongVec_push_back(&vec2, v), SongPtr, {
+ c_apply(v, SongVec_push_back(&vec2, *v), SongPtr, {
SongPtr_make(Song_new("Michael Jackson", "Billie Jean")),
SongPtr_make(Song_new("Rihanna", "Stay")),
});
diff --git a/examples/new_list.c b/examples/new_list.c index 2b6b4636..20a64ad3 100644 --- a/examples/new_list.c +++ b/examples/new_list.c @@ -39,7 +39,7 @@ int main() clist_i32_push_back(&lst, 123); c_auto (clist_pnt, plst) { - c_apply(v, clist_pnt_push_back(&plst, v), + c_apply(v, clist_pnt_push_back(&plst, *v), Point, {{42, 14}, {32, 94}, {62, 81}}); clist_pnt_sort(&plst); @@ -49,7 +49,7 @@ int main() } c_auto (clist_float, flst) { - c_apply(v, clist_float_push_back(&flst, v), + c_apply(v, clist_float_push_back(&flst, *v), float, {123.3f, 321.2f, -32.2f, 78.2f}); c_foreach (i, clist_float, flst) printf(" %g", *i.ref); } diff --git a/examples/new_map.c b/examples/new_map.c index 2671790a..32a4ce68 100644 --- a/examples/new_map.c +++ b/examples/new_map.c @@ -61,7 +61,7 @@ int main() {"So long, friend", "see you around"},
});
- c_apply(v, cset_str_emplace(&sset, v), const char*, {
+ c_apply(v, cset_str_emplace(&sset, *v), const char*, {
"Hello, friend",
"Nice to see you again",
"So long, friend",
diff --git a/examples/person_arc.c b/examples/person_arc.c index 40707740..c77ca315 100644 --- a/examples/person_arc.c +++ b/examples/person_arc.c @@ -54,7 +54,7 @@ int main() Persons_push_back(&vec, PSPtr_make(Person_new("Audrey", "Home")));
// Clone/share p and q to the vector
- c_apply(v, Persons_push_back(&vec, PSPtr_clone(v)), PSPtr, {p, q});
+ c_apply(v, Persons_push_back(&vec, PSPtr_clone(*v)), PSPtr, {p, q});
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 b00a49a8..26f4ff9e 100644 --- a/examples/phonebook.c +++ b/examples/phonebook.c @@ -40,7 +40,7 @@ void print_phone_book(cmap_str phone_book) int main(int argc, char **argv)
{
c_auto (cset_str, names) {
- c_apply(v, cset_str_emplace(&names, v), const char*,
+ c_apply(v, cset_str_emplace(&names, *v), const char*,
{"Hello", "Cool", "True"});
c_foreach (i, cset_str, names) printf("%s ", cstr_str(i.ref));
puts("");
diff --git a/examples/prime.c b/examples/prime.c index 4d70ee24..aee0a89e 100644 --- a/examples/prime.c +++ b/examples/prime.c @@ -1,6 +1,7 @@ #include <stdio.h>
#include <math.h>
#include <time.h>
+
#include <stc/cbits.h>
cbits sieveOfEratosthenes(size_t n)
diff --git a/examples/priority.c b/examples/priority.c index 6e033386..66e400dd 100644 --- a/examples/priority.c +++ b/examples/priority.c @@ -20,7 +20,7 @@ int main() { cpque_i_push(&heap, stc64_uniform(&rng, &dist));
// push some negative numbers too.
- c_apply(v, cpque_i_push(&heap, v), int, {-231, -32, -873, -4, -343});
+ c_apply(v, cpque_i_push(&heap, *v), int, {-231, -32, -873, -4, -343});
c_forrange (N)
cpque_i_push(&heap, stc64_uniform(&rng, &dist));
diff --git a/examples/shape.c b/examples/shape.c index 816fdea4..04aecaa1 100644 --- a/examples/shape.c +++ b/examples/shape.c @@ -147,10 +147,10 @@ int main(void) Polygon* pol1 = Polygon_new();
Polygon* pol2 = Polygon_new();
- c_apply(p, Polygon_addPoint(pol1, p), Point,
+ c_apply(p, Polygon_addPoint(pol1, *p), Point,
{{50, 72}, {123, 73}, {127, 201}, {828, 333}});
- c_apply(p, Polygon_addPoint(pol2, p), Point,
+ c_apply(p, Polygon_addPoint(pol2, *p), Point,
{{5, 7}, {12, 7}, {12, 20}, {82, 33}, {17, 56}});
Shapes_push(&shapes, &tri1->base);
diff --git a/examples/words.c b/examples/words.c index 28de2638..8a86ba7f 100644 --- a/examples/words.c +++ b/examples/words.c @@ -13,7 +13,7 @@ int main1() c_auto (cvec_str, words) c_auto (cmap_str, word_map) { - c_apply(v, cvec_str_emplace_back(&words, v), const char*, { + c_apply(v, cvec_str_emplace_back(&words, *v), const char*, { "this", "sentence", "is", "not", "a", "sentence", "this", "sentence", "is", "a", "hoax" }); diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index 850bc511..b6023f4b 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -204,17 +204,18 @@ STC_INLINE char* c_strnstrn(const char *s, const char *needle, #define c_apply(v, action, T, ...) do { \
typedef T _c_T; \
- const _c_T _c_arr[] = __VA_ARGS__; \
- for (size_t index = 0; index < c_arraylen(_c_arr); ++index) \
- { const _c_T v = _c_arr[index]; action; } \
+ const _c_T _c_arr[] = __VA_ARGS__, *v = _c_arr, \
+ *_c_end = v + c_arraylen(_c_arr); \
+ while (v != _c_end) { action; ++v; } \
} while (0)
+
#define c_apply_arr(v, action, T, arr, n) do { \
typedef T _c_T; \
- _c_T *_c_arr = arr; \
- for (size_t index = 0, _c_n = n; index < _c_n; ++index) \
- { _c_T *v = _c_arr + index; action; } \
+ _c_T *v = arr, *_c_end = v + (n); \
+ while (v != _c_end) { action; ++v; } \
} while (0)
-#define c_pair(v) (v).first, (v).second
+
+#define c_pair(v) (v)->first, (v)->second
#define c_find_if(C, cnt, it, pred) \
c_find_in(C, C##_begin(&cnt), C##_end(&cnt), it, pred)
|
