From 131c47066cba5a8b59d5889415761d0300068bdc Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Mon, 14 Sep 2020 13:17:39 +0200 Subject: Got rid of c_items() macro. --- README.md | 2 +- examples/README.md | 4 ++-- examples/advanced.c | 4 ++-- examples/geek2.c | 4 ++-- examples/inits.c | 20 ++++++++++---------- examples/list.c | 2 +- examples/phonebook.c | 4 ++-- examples/priority.c | 2 +- examples/words.c | 8 ++++---- stc/cdefs.h | 5 ++--- stc/clist.h | 4 ++-- stc/cmap.h | 13 ++++++++----- 12 files changed, 37 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 4aeff7ae..7f672e92 100644 --- a/README.md +++ b/README.md @@ -300,7 +300,7 @@ int main() { clist_fx_clear(&list); // generic c_push() function, works on most containers: - c_push(&list, clist_fx, c_items(10, 20, 30, 40, 50)); + c_push(&list, clist_fx, {10, 20, 30, 40, 50}); c_foreach (i, clist_fx, list) printf("%f ", i.item->value); diff --git a/examples/README.md b/examples/README.md index 0cdefd55..26eb4bb1 100644 --- a/examples/README.md +++ b/examples/README.md @@ -61,11 +61,11 @@ Finally, main which also demos the generic c_push() of multiple elements: ``` int main() { cmap_vk vikings = cmap_ini; - c_push(&vikings, cmap_vk, c_items( + c_push(&vikings, cmap_vk, { { {"Einar", "Norway"}, 20 }, { {"Olaf", "Denmark"}, 24 }, { {"Harald", "Iceland"}, 12 }, - )); + }); VikingVw look = {"Einar", "Norway"}; cmap_vk_entry_t *e = cmap_vk_find(&vikings, look); e->value += 5; // update diff --git a/examples/advanced.c b/examples/advanced.c index 0aa1600e..d3c1ca21 100644 --- a/examples/advanced.c +++ b/examples/advanced.c @@ -62,11 +62,11 @@ declare_cmap(vk, Viking, int, c_default_destroy, vikingvw_equals, vikingvw_hash, int main() { cmap_vk vikings = cmap_ini; - c_push(&vikings, cmap_vk, c_items( + c_push(&vikings, cmap_vk, { {{"Einar", "Norway"}, 20}, {{"Olaf", "Denmark"}, 24}, {{"Harald", "Iceland"}, 12}, - )); + }); VikingVw einar = {"Einar", "Norway"}; cmap_vk_entry_t *e = cmap_vk_find(&vikings, einar); diff --git a/examples/geek2.c b/examples/geek2.c index c76d262f..a02ede25 100644 --- a/examples/geek2.c +++ b/examples/geek2.c @@ -20,12 +20,12 @@ int main() puts(""); // Review some books. - c_push(&book_reviews, cmap_str, c_items( + c_push(&book_reviews, cmap_str, { {"Adventures of Huckleberry Finn", "My favorite book."}, {"Grimms' Fairy Tales", "Masterpiece."}, {"Pride and Prejudice", "Very enjoyable."}, {"The Adventures of Sherlock Holmes", "Eye lyked it alot."}, - )); + }); /* cmap_str_emplace(&book_reviews, "Adventures of Huckleberry Finn", diff --git a/examples/inits.c b/examples/inits.c index ebb2e0e1..5a0ae04b 100644 --- a/examples/inits.c +++ b/examples/inits.c @@ -25,7 +25,7 @@ int main(void) { // CVEC FLOAT / PRIORITY QUEUE cvec_f floats = cvec_ini; - c_push(&floats, cvec_f, c_items(4.0f, 2.0f, 5.0f, 3.0f, 1.0f)); + c_push(&floats, cvec_f, {4.0f, 2.0f, 5.0f, 3.0f, 1.0f}); c_foreach (i, cvec_f, floats) printf("%.1f ", *i.item); puts(""); @@ -33,7 +33,7 @@ int main(void) { // CVEC PRIORITY QUEUE cpqueue_f_build(&floats); // reorganise vec - c_push(&floats, cpqueue_f, c_items(40.0f, 20.0f, 50.0f, 30.0f, 10.0f)); + c_push(&floats, cpqueue_f, {40.0f, 20.0f, 50.0f, 30.0f, 10.0f}); // sorted: while (cvec_size(floats) > 0) { @@ -47,11 +47,11 @@ int main(void) { int year = 2020; cmap_id idnames = cmap_ini; - c_push(&idnames, cmap_id, c_items( + c_push(&idnames, cmap_id, { {100, cstr_make("Hello")}, {110, cstr_make("World")}, {120, cstr_from("Howdy, -%d-", year)}, - )); + }); c_foreach (i, cmap_id, idnames) printf("%d: %s\n", i.item->key, i.item->value.str); @@ -61,7 +61,7 @@ int main(void) { // CMAP CNT cmap_cnt countries = cmap_ini; - c_push(&countries, cmap_cnt, c_items( + c_push(&countries, cmap_cnt, { {"Norway", 100}, {"Denmark", 50}, {"Iceland", 10}, @@ -70,7 +70,7 @@ int main(void) { {"Germany", 10}, {"Spain", 10}, {"France", 10}, - )); + }); cmap_cnt_emplace(&countries, "Greenland", 0).item->value += 20; cmap_cnt_emplace(&countries, "Sweden", 0).item->value += 20; cmap_cnt_emplace(&countries, "Norway", 0).item->value += 20; @@ -84,12 +84,12 @@ int main(void) { // CVEC PAIR cvec_ip pairs1 = cvec_ini; - c_push(&pairs1, cvec_ip, c_items( + c_push(&pairs1, cvec_ip, { {5, 6}, {3, 4}, {1, 2}, {7, 8}, - )); + }); cvec_ip_sort(&pairs1); c_foreach (i, cvec_ip, pairs1) @@ -100,12 +100,12 @@ int main(void) { // CLIST PAIR clist_ip pairs2 = clist_ini; - c_push(&pairs2, clist_ip, c_items( + c_push(&pairs2, clist_ip, { {5, 6}, {3, 4}, {1, 2}, {7, 8}, - )); + }); clist_ip_sort(&pairs2); c_foreach (i, clist_ip, pairs2) diff --git a/examples/list.c b/examples/list.c index dd8bba2f..d140d103 100644 --- a/examples/list.c +++ b/examples/list.c @@ -23,7 +23,7 @@ int main() { puts(""); clist_fx_clear(&list); - c_push(&list, clist_fx, c_items(10, 20, 30, 40, 30, 50)); + c_push(&list, clist_fx, {10, 20, 30, 40, 30, 50}); c_foreach (i, clist_fx, list) printf(" %g", i.item->value); puts(""); diff --git a/examples/phonebook.c b/examples/phonebook.c index 74105f25..983f4e74 100644 --- a/examples/phonebook.c +++ b/examples/phonebook.c @@ -37,12 +37,12 @@ int main(int argc, char **argv) { bool erased; cmap_str phone_book = cmap_ini; - c_push(&phone_book, cmap_str, c_items( + c_push(&phone_book, cmap_str, { {"Lilia Friedman", "(892) 670-4739"}, {"Tariq Beltran", "(489) 600-7575"}, {"Laiba Juarez", "(303) 885-5692"}, {"Elliott Mooney", "(945) 616-4482"}, - )); + }); printf("Phone book:\n"); print_phone_book(phone_book); diff --git a/examples/priority.c b/examples/priority.c index e857761c..8bd2abce 100644 --- a/examples/priority.c +++ b/examples/priority.c @@ -20,7 +20,7 @@ int main() { cpqueue_i_push(&heap, crand_uniform_i64(&pcg, &dist)); // push some negative numbers too. - c_push(&heap, cpqueue_i, c_items(-231, -32, -873, -4, -343)); + c_push(&heap, cpqueue_i, {-231, -32, -873, -4, -343}); for (int i=0; ivalue.str); puts(""); cvec_str words = cvec_ini; - c_push(&words, cvec_str, c_items( + c_push(&words, cvec_str, { "this", "sentence", "is", "not", "a", "sentence", "this", "sentence", "is", "a", "hoax" - )); + }); cmap_si word_map = cmap_ini; c_foreach (w, cvec_str, words) diff --git a/stc/cdefs.h b/stc/cdefs.h index bc7b0e1f..dc12b618 100644 --- a/stc/cdefs.h +++ b/stc/cdefs.h @@ -85,9 +85,8 @@ #define c_foreach_4(it, ctype, start, finish) \ for (ctype##_iter_t it = start, it##_end_ = finish; it.item != it##_end_.item; ctype##_next(&it)) -#define c_items(...) __VA_ARGS__ -#define c_push(self, ctype, items) do { \ - const ctype##_input_t __arr[] = {items}; \ +#define c_push(self, ctype, ...) do { \ + const ctype##_input_t __arr[] = __VA_ARGS__; \ ctype##_push_n(self, __arr, sizeof(__arr)/sizeof(__arr[0])); \ } while (0) diff --git a/stc/clist.h b/stc/clist.h index 9743a022..baba7ed3 100644 --- a/stc/clist.h +++ b/stc/clist.h @@ -89,10 +89,10 @@ #define clist_ini {NULL} #define clist_empty(list) ((list).last == NULL) -#define c_emplace_after(self, ctype, pos, items) do { \ +#define c_emplace_after(self, ctype, pos, ...) do { \ ctype* __self = self; \ ctype##_iter_t __pos = pos; \ - const ctype##_input_t __arr[] = {items}; \ + const ctype##_input_t __arr[] = __VA_ARGS__; \ for (size_t __i=0; __ivalue = val; \ } while (0) +#define c_insert(self, ctype, ...) do { \ + const ctype##_input_t __arr[] = __VA_ARGS__; \ + for (size_t i=0;i> 32)) @@ -231,12 +235,11 @@ typedef struct {size_t idx; uint32_t hx;} cmap_bucket_t, cset_bucket_t; CMAP_ONLY_##ctype( if (res.inserted) res.item->value = valueFromRaw(rawVal); ) \ return res; \ } \ -\ - CSET_ONLY_##ctype( \ STC_INLINE ctype##_##X##_result_t \ - ctype##_##X##_insert(ctype##_##X* self, RawKey rawKey) { \ - return ctype##_##X##_insert_key_(self, rawKey); \ - }) \ + ctype##_##X##_insert(ctype##_##X* self, ctype##_##X##_input_t in) { \ + return CSET_ONLY_##ctype(ctype##_##X##_insert_key_(self, in)) \ + CMAP_ONLY_##ctype(ctype##_##X##_emplace(self, in.first, in.second)); \ + } \ \ CMAP_ONLY_##ctype( \ STC_INLINE ctype##_##X##_result_t \ -- cgit v1.2.3