From 0dfe69fa5dd7d4d9dded93d35106537e1ea76243 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Sat, 17 Apr 2021 08:02:39 +0200 Subject: Changed c_emplace_items(&cont, ctype, {...}) macro with c_emplace(ctype, cont, {...}): consistent with c_init(ctype, cont, {...}). --- README.md | 4 ++-- docs/ccommon_api.md | 8 ++++---- docs/cdeq_api.md | 2 +- docs/cpque_api.md | 2 +- docs/cvec_api.md | 2 +- examples/advanced.c | 2 +- examples/birthday.c | 4 ++-- examples/inits.c | 6 +++--- examples/list.c | 2 +- examples/priority.c | 2 +- examples/read.c | 2 +- stc/ccommon.h | 6 +++--- 12 files changed, 21 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index d484235d..d6724d38 100644 --- a/README.md +++ b/README.md @@ -256,10 +256,10 @@ Erase methods ------------- | Name | Description | Container | |:--------------------------|:-----------------------------|:--------------------------------------------| -| erase() | key based | csmap, csset, cmap, cset | +| erase() | key based | csmap, csset, cmap, cset, cstr | | erase_it() | iterator based | csmap, csset, cmap, cset, cvec, cdeq, clist | | erase_range() | iterator based | csmap, csset, cvec, cdeq, clist | -| erase_n() | index based | cvec, cdeq | +| erase_n() | index based | cvec, cdeq, cstr | | remove() | remove all matching values | clist | Memory efficiency diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index 4c40c998..891d0ec6 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -2,11 +2,11 @@ The following handy macros are safe to use, i.e. have no side-effects. -### c_init, c_emplace_items -**c_init** declares and initializes any container with an array of elements. **c_emplace_items** adds elements to any existing container: +### c_init, c_emplace +**c_init** declares and initializes any container with an array of elements. **c_emplace** adds elements to any existing container: ```c -c_init (cvec_i, vec, {1, 2, 3}); -c_emplace_items(&vec, cvec_i, {4, 5, 6}); +c_init (cvec_i, vec, {1, 2, 3}); // declare and emplace +c_emplace(cvec_i, vec, {4, 5, 6}); // adds to existing vec ``` ### c_forrange diff --git a/docs/cdeq_api.md b/docs/cdeq_api.md index d921a5fb..d4de155b 100644 --- a/docs/cdeq_api.md +++ b/docs/cdeq_api.md @@ -107,7 +107,7 @@ int main() { printf(" %d", *i.ref); puts(""); - c_emplace_items(&q, cdeq_i, {1, 4, 5, 22, 33, 2}); + c_emplace(cdeq_i, q, {1, 4, 5, 22, 33, 2}); c_foreach (i, cdeq_i, q) printf(" %d", *i.ref); puts(""); diff --git a/docs/cpque_api.md b/docs/cpque_api.md index 261c2909..07f60709 100644 --- a/docs/cpque_api.md +++ b/docs/cpque_api.md @@ -72,7 +72,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_emplace_items(&heap, cpque_i, {-231, -32, -873, -4, -343}); + c_emplace(cpque_i, heap, {-231, -32, -873, -4, -343}); // Extract and display the fifty smallest. c_forrange (50) { diff --git a/docs/cvec_api.md b/docs/cvec_api.md index 14bce940..2f084704 100644 --- a/docs/cvec_api.md +++ b/docs/cvec_api.md @@ -106,7 +106,7 @@ int main() { // Create a vector containing integers cvec_i vec = cvec_i_init(); - c_emplace_items(&vec, cvec_i, {7, 5, 16, 8}); + c_emplace(cvec_i, vec, {7, 5, 16, 8}); // Add two more integers to vector cvec_i_push_back(&vec, 25); diff --git a/examples/advanced.c b/examples/advanced.c index 33ceebbe..795f3f63 100644 --- a/examples/advanced.c +++ b/examples/advanced.c @@ -42,7 +42,7 @@ using_cmap_keydef(vk, Viking, int, vikingraw_equals, vikingraw_hash, int main() { cmap_vk vikings = cmap_vk_init(); - c_emplace_items(&vikings, cmap_vk, { + c_emplace(cmap_vk, vikings, { { {"Einar", "Norway"}, 20}, { {"Olaf", "Denmark"}, 24}, { {"Harald", "Iceland"}, 12}, diff --git a/examples/birthday.c b/examples/birthday.c index 5c5e0426..b2fc834c 100644 --- a/examples/birthday.c +++ b/examples/birthday.c @@ -20,7 +20,7 @@ static void test_repeats(void) cmap_ic_reserve(&m, N); c_forrange (i, N) { uint64_t k = stc64_rand(&rng) & mask; - int v = ++cmap_ic_emplace(&m, k, 0).ref->second; + int v = cmap_ic_emplace(&m, k, 0).ref->second += 1; if (v > 1) printf("repeated value %llx (%d) at 2^%d\n", k, v, (int) log2(i)); } } @@ -38,7 +38,7 @@ void test_distribution(void) c_forrange (N) { uint64_t k = stc64_rand(&rng); - ++cmap_x_emplace(&map, k & 0xf, 0).ref->second; + cmap_x_emplace(&map, k & 0xf, 0).ref->second += 1; } uint64_t sum = 0; diff --git a/examples/inits.c b/examples/inits.c index 26518e33..003acfa3 100644 --- a/examples/inits.c +++ b/examples/inits.c @@ -24,7 +24,7 @@ int main(void) // CVEC FLOAT / PRIORITY QUEUE cvec_f floats = cvec_f_init(); - c_emplace_items(&floats, cvec_f, {4.0f, 2.0f, 5.0f, 3.0f, 1.0f}); + 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); puts(""); @@ -32,7 +32,7 @@ int main(void) // CVEC PRIORITY QUEUE cpque_f_make_heap(&floats); - c_emplace_items(&floats, cpque_f, {40.0f, 20.0f, 50.0f, 30.0f, 10.0f}); + c_emplace(cpque_f, floats, {40.0f, 20.0f, 50.0f, 30.0f, 10.0f}); puts("\npop and show high priorites first:"); while (! cpque_f_empty(floats)) { @@ -58,7 +58,7 @@ int main(void) // CMAP CNT cmap_cnt countries = cmap_cnt_init(); - c_emplace_items(&countries, cmap_cnt, { + c_emplace(cmap_cnt, countries, { {"Norway", 100}, {"Denmark", 50}, {"Iceland", 10}, diff --git a/examples/list.c b/examples/list.c index 2bc493b9..4b75468d 100644 --- a/examples/list.c +++ b/examples/list.c @@ -33,7 +33,7 @@ int main() { puts(""); clist_fx_clear(&list); - c_emplace_items(&list, clist_fx, {10, 20, 30, 40, 30, 50}); + c_emplace(clist_fx, list, {10, 20, 30, 40, 30, 50}); c_foreach (i, clist_fx, list) printf(" %g", *i.ref); puts(""); diff --git a/examples/priority.c b/examples/priority.c index ed7db584..ec26cb15 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_emplace_items(&heap, cpque_i, {-231, -32, -873, -4, -343}); + c_emplace(cpque_i, heap, {-231, -32, -873, -4, -343}); c_forrange (N) cpque_i_push(&heap, stc64_uniform(&rng, &dist)); diff --git a/examples/read.c b/examples/read.c index b3ddcc00..2324d1df 100644 --- a/examples/read.c +++ b/examples/read.c @@ -6,7 +6,7 @@ using_cvec_str(); cvec_str read_file(const char* name) { cvec_str vec = cvec_str_init(); - c_withfile (f, fopen(name, "r")) { + for (FILE* f = fopen(name, "r"); f; fclose(f), f=NULL) { cstr line = cstr_init(); while (cstr_getline(&line, f)) cvec_str_emplace_back(&vec, line.str); diff --git a/stc/ccommon.h b/stc/ccommon.h index ec01d731..684c0f90 100644 --- a/stc/ccommon.h +++ b/stc/ccommon.h @@ -121,11 +121,11 @@ #define c_breakwith continue #define c_init(ctype, c, ...) \ - ctype c = ctype##_init(); c_emplace_items(&c, ctype, __VA_ARGS__) + ctype c = ctype##_init(); c_emplace(ctype, c, __VA_ARGS__) -#define c_emplace_items(self, ctype, ...) do { \ +#define c_emplace(ctype, c, ...) do { \ const ctype##_rawvalue_t _c_arr[] = __VA_ARGS__; \ - ctype##_emplace_n(self, _c_arr, c_arraylen(_c_arr)); \ + ctype##_emplace_n(&(c), _c_arr, c_arraylen(_c_arr)); \ } while (0) #define c_del(ctype, ...) do { \ -- cgit v1.2.3