diff options
| author | Tyge Løvset <[email protected]> | 2021-12-22 08:49:19 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2021-12-22 08:50:15 +0100 |
| commit | 6e65e30c6e3c2bc271d3885e749ceb0289bbd9cf (patch) | |
| tree | ccffa7b7dde19fb551f99c7eed529bdc7308e494 | |
| parent | 1b90b6c3eea0f3e07f8a2d2abe3686917d1d86a8 (diff) | |
| download | STC-modified-6e65e30c6e3c2bc271d3885e749ceb0289bbd9cf.tar.gz STC-modified-6e65e30c6e3c2bc271d3885e749ceb0289bbd9cf.zip | |
Changed the c_apply() and c_apply_pair() to one new c_apply() API. Added c_pair(v) for convenience.
41 files changed, 241 insertions, 191 deletions
@@ -17,6 +17,8 @@ still be defined as before, which is often easier for simple element types. Migration guide from version 2 to 3. Replace (regular expresion) in VS Code: - `_del\b` → `_drop` - `_compare\b` → `_cmp` +- `csptr` → `cref` +- `_rawvalue\b` → `_raw` - `_equ\b` → `_eq` Replace (whole word + match case): @@ -25,10 +27,10 @@ Replace (whole word + match case): - `i_cnt` → `i_type` - `cstr_lit` → `cstr_new` - `csptr_X_make` → `csptr_X_new` -- `i_key_csptr` → `i_key_bind` -- `i_val_csptr` → `i_val_bind` -- `i_key_ref` → `i_key_bind` -- `i_val_ref` → `i_val_bind` +- `i_key_csptr` → `i_key_sptr` +- `i_val_csptr` → `i_val_sptr` +- `c_apply` → `c_apply_OLD` // replaced by new `c_apply` +- `c_apply_pair` → `c_apply_pair_OLD` // replaced by new `c_apply` ### Final version 2.1 - Strings: Renamed constructor *cstr_lit()* to `cstr_new(lit)`. Renamed *cstr_assign_fmt()* to `cstr_printf()`. @@ -192,12 +194,13 @@ int main(void) { c_auto (csmap_int, map) { // add some elements to each container - c_apply(cset_int, insert, &set, {10, 20, 30}); - c_apply(cvec_pnt, push_back, &vec, { {10, 1}, {20, 2}, {30, 3} }); - c_apply(cdeq_int, push_back, &deq, {10, 20, 30}); - c_apply(clist_int, push_back, &lst, {10, 20, 30}); - c_apply(cstack_int, push, &stk, {10, 20, 30}); - c_apply_pair(csmap_int, insert, &map, { {20, 2}, {10, 1}, {30, 3} }); + c_apply(v, cset_int_insert(&set, v), int, {10, 20, 30}); + c_apply(v, cvec_pnt_push_back(&vec, v), int, { {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_rawvalue, { {20, 2}, {10, 1}, {30, 3} }); // add one more element to each container cset_int_insert(&set, 40); diff --git a/docs/cbox_api.md b/docs/cbox_api.md index 8580e7d7..011beea2 100644 --- a/docs/cbox_api.md +++ b/docs/cbox_api.md @@ -33,24 +33,23 @@ compare the pointer addresses when used. Additionally, `c_no_clone` or `i_is_fwd ## Methods ```c cbox_X cbox_X_init(); // return an empty cbox -cbox_X cbox_X_new(i_val val); // allocate new heap object with val. Take ownership of val. -cbox_X cbox_X_from(i_valraw raw); // like cbox_X_new(), but create owned value from raw. -cbox_X cbox_X_with(i_val* p); // create a cbox from a pointer. Takes ownership of p. +cbox_X cbox_X_new(i_valraw raw); // like cbox_X_from(), but create owned value from raw. +cbox_X cbox_X_from(i_val val); // allocate new heap object with val. Take ownership of val. +cbox_X cbox_X_from_ptr(i_val* p); // create a cbox from a pointer. Takes ownership of p. cbox_X cbox_X_clone(cbox_X other); // return deep copied clone cbox_X cbox_X_move(cbox_X* self); // transfer ownership to another cbox. void cbox_X_take(cbox_X* self, cbox_X other); // take ownership of other. void cbox_X_copy(cbox_X* self, cbox_X other); // deep copy to self - void cbox_X_drop(cbox_X* self); // destruct the contained object and free's it. void cbox_X_reset(cbox_X* self); -void cbox_X_reset_new(cbox_X* self, i_val val); // assign new cbox with value. Takes ownership of val. -void cbox_X_reset_from(cbox_X* self, i_valraw raw); // make and assign new cbox from raw value. -void cbox_X_reset_with(cbox_X* self, i_val* p); // create cbox with pointer p. Takes ownership of p. +void cbox_X_reset_from(cbox_X* self, i_val val); // assign new cbox with value. Takes ownership of val. -int cbox_X_cmp(const cbox_X* x, const cbox_X* y); // compares pointer addresses if 'i_opt c_no_cmp' +uint64_t cbox_X_value_hash(const i_val* x, size_t n); // hash value +int cbox_X_value_cmp(const i_val* x, const i_val* y); // compares pointer addresses if 'i_opt c_no_cmp' // is defined. Otherwise uses 'i_cmp' or default compare. +bool cbox_X_value_eq(const i_val* x, const i_val* y); // cbox_X_value_cmp == 0 ``` ## Types and constants @@ -98,7 +97,7 @@ int main() c_auto (cvec_int, vec) // declare and init vec, call drop at scope exit c_auto (csset_int, set) // declare and init set, call drop at scope exit { - c_apply(cvec_int, push_back, &vec, { + c_apply(v, cvec_int_push_back(&vec, v), cbox_int, { cbox_int_new(2021), cbox_int_new(2012), cbox_int_new(2022), diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index 9318b8e3..eec56490 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -93,7 +93,8 @@ int main() #define i_tag ii #include <stc/csmap.h> ... -c_apply_pair(csmap_ii, insert, &map, { {23,1}, {3,2}, {7,3}, {5,4}, {12,5} }); +c_apply(v, csmap_ii_insert(&map, c_pair(v)), csmap_ii_value, + { {23,1}, {3,2}, {7,3}, {5,4}, {12,5} }); c_foreach (i, csmap_ii, map) printf(" %d", i.ref->first); // out: 3 5 7 12 23 @@ -130,14 +131,19 @@ c_forrange (i, int, 30, 0, -5) printf(" %d", i); // 30 25 20 15 10 5 ``` -### c_apply, c_apply_pair, c_apply_n -**c_apply** will apply a method on a container with each of the elements in the given array: +### c_apply, c_apply_arr, c_apply_cnt, c_pair +**c_apply** applies an expression on a container with each of the elements in the given array: ```c -c_apply(cvec_i, push_back, &vec, {1, 2, 3}); // apply multiple push_backs -c_apply_pair(cmap_i, insert, &map, { {4, 5}, {6, 7} }); // inserts to existing map +// apply multiple push_backs +c_apply(v, cvec_i_push_back(&vec, v), int, {1, 2, 3}); +// inserts to existing map +c_apply(v, cmap_i_insert(&map, c_pair(v)), cmap_i_rawvalue, { {4, 5}, {6, 7} }); int arr[] = {1, 2, 3}; -c_apply_n(cvec_i, push_back, &vec, arr, c_arraylen(arr)); +c_apply_arr(v, cvec_i_push_back(&vec, v), int, arr, c_arraylen(arr)); + +// add keys from a map to a vec. +c_apply_cnt(v, cvec_i_push_back(&vec, v.first), cmap_i, map); ``` ### c_new, c_alloc, c_alloc_n, c_drop, c_make diff --git a/docs/cdeq_api.md b/docs/cdeq_api.md index 1985c2ab..ce14184f 100644 --- a/docs/cdeq_api.md +++ b/docs/cdeq_api.md @@ -106,7 +106,7 @@ int main() { printf(" %d", *i.ref); puts(""); - c_apply(cdeq_i, push_back, &q, {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 0817737b..41fbd274 100644 --- a/docs/clist_api.md +++ b/docs/clist_api.md @@ -106,7 +106,7 @@ Interleave *push_front()* / *push_back()* then *sort()*: int main() { clist_d list = clist_d_init(); - c_apply(clist_d, push_back, &list, { + 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 }); @@ -147,7 +147,7 @@ Use of *erase_at()* and *erase_range()*: int main () { clist_i L = clist_i_init(); - c_apply(clist_i, push_back, &L, {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); @@ -182,8 +182,8 @@ Splice `[30, 40]` from *L2* into *L1* before `3`: int main() { c_auto (clist_i, L1, L2) { - c_apply(clist_i, push_back, &L1, {1, 2, 3, 4, 5}); - c_apply(clist_i, push_back, &L2, {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/cmap_api.md b/docs/cmap_api.md index b05ae5a8..b7a08e3e 100644 --- a/docs/cmap_api.md +++ b/docs/cmap_api.md @@ -122,7 +122,7 @@ int main() // Create an unordered_map of three strings (that map to strings) c_auto (cmap_str, u) { - c_apply_pair(cmap_str, emplace, &u, { + c_apply(v, cmap_str_emplace(&u, c_pair(v)), cmap_str_rawvalue, { {"RED", "#FF0000"}, {"GREEN", "#00FF00"}, {"BLUE", "#0000FF"} @@ -168,7 +168,7 @@ int main() c_auto (cmap_id, idnames) { - c_apply_pair(cmap_id, emplace, &idnames, { + c_apply(v, cmap_id_emplace(&idnames, c_pair(v)), cmap_id_rawvalue, { {100, "Red"}, {110, "Blue"} }); // replace existing mapped value: @@ -384,7 +384,7 @@ static inline RViking Viking_toraw(const Viking* vk) { int main() { c_auto (Vikings, vikings) { - c_apply_pair(Vikings, emplace, &vikings, { + c_apply(v, Vikings_emplace(&vikings, v), c_pair(v), Vikings_rawvalue, { {{"Einar", "Norway"}, 20}, {{"Olaf", "Denmark"}, 24}, {{"Harald", "Iceland"}, 12}, diff --git a/docs/cpque_api.md b/docs/cpque_api.md index e891b7d2..7a6ef00f 100644 --- a/docs/cpque_api.md +++ b/docs/cpque_api.md @@ -75,7 +75,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(cpque_i, push, &heap, {-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 2ce43309..479cf3f3 100644 --- a/docs/cset_api.md +++ b/docs/cset_api.md @@ -85,8 +85,10 @@ int main () c_auto (cset_str, first, second)
c_auto (cset_str, third, fourth)
{
- c_apply(cset_str, emplace, &second, {"red", "green", "blue"});
- c_apply(cset_str, emplace, &third, {"orange", "pink", "yellow"});
+ c_apply(v, cset_str_emplace(&second, v), const char*,
+ {"red", "green", "blue"});
+ c_apply(v, cset_str_emplace(&third, v), const char*,
+ {"orange", "pink", "yellow"});
cset_str_emplace(&fourth, "potatoes");
cset_str_emplace(&fourth, "milk");
diff --git a/docs/csmap_api.md b/docs/csmap_api.md index a6e06b54..3791347b 100644 --- a/docs/csmap_api.md +++ b/docs/csmap_api.md @@ -100,7 +100,7 @@ int main() // Create a sorted map of three strings (maps to string) c_auto (csmap_str, colors) // RAII { - c_apply_pair(csmap_str, emplace, &colors, { + c_apply(v, csmap_str_emplace(&colors, c_pair(v)), csmap_str_rawvalue, { {"RED", "#FF0000"}, {"GREEN", "#00FF00"}, {"BLUE", "#0000FF"} @@ -146,7 +146,7 @@ int main() csmap_id idnames = csmap_id_init(); c_autodefer (csmap_id_drop(&idnames)) { - c_apply_pair(csmap_id, emplace, &idnames, { + c_apply(v, csmap_id_emplace(&idnames, c_pair(v)), csmap_id_rawvalue, { {100, "Red"}, {110, "Blue"}, }); diff --git a/docs/csptr_api.md b/docs/csptr_api.md index 5cbe746f..6a48f91a 100644 --- a/docs/csptr_api.md +++ b/docs/csptr_api.md @@ -35,35 +35,35 @@ See similar c++ class [std::shared_ptr](https://en.cppreference.com/w/cpp/memory ## Methods ```c -csptr_X csptr_X_init(); // empty shared pointer -csptr_X csptr_X_new(i_val val); // create new heap allocated object. Take ownership of val. -csptr_X csptr_X_from(i_valraw raw); // like csptr_X_new(), but construct owned value from raw. -csptr_X csptr_X_with(i_val* p); // create a csptr from raw pointer. Takes ownership of p. +csptr_X csptr_X_init(); // empty shared pointer +csptr_X csptr_X_new(i_valraw raw); // like csptr_X_from(), but construct owned value from raw. +csptr_X csptr_X_from(i_val val); // create new heap allocated object. Take ownership of val. +csptr_X csptr_X_from_ptr(i_val* p); // create a csptr from raw pointer. Takes ownership of p. -csptr_X csptr_X_clone(csptr_X other); // return other with increased use count -csptr_X csptr_X_move(csptr_X* self); // transfer ownership to another csptr. -void csptr_X_take(csptr_X* self, csptr_X other); // take ownership of other. -void csptr_X_copy(csptr_X* self, csptr_X other); // copy shared (increase use count) +csptr_X csptr_X_clone(csptr_X other); // return other with increased use count +csptr_X csptr_X_move(csptr_X* self); // transfer ownership to another csptr. +void csptr_X_take(csptr_X* self, csptr_X other); // take ownership of other. +void csptr_X_copy(csptr_X* self, csptr_X other); // copy shared (increase use count) void csptr_X_drop(csptr_X* self); // destruct (decrease use count, free at 0) long csptr_X_use_count(csptr_X ptr); void csptr_X_reset(csptr_X* self); -void csptr_X_reset_new(csptr_X* self, i_val val); // assign new csptr with value. Takes ownership of val. -void csptr_X_reset_from(csptr_X* self, i_valraw raw); // make and assign new csptr from raw value. -void csptr_X_reset_with(csptr_X* self, i_val* p); // create csptr with pointer p. Takes ownership of p. +void csptr_X_reset_from(csptr_X* self, i_val val); // assign new csptr with value. Takes ownership of val. -int csptr_X_cmp(const csptr_X* x, const csptr_X* y); // compares pointer addresses if 'i_opt c_no_cmp' - // is defined. Otherwise uses 'i_cmp' or default compare. +uint64_t csptr_X_value_hash(const i_val* x, size_t n); // hash value +int csptr_X_value_cmp(const i_val* x, const i_val* y); // compares pointer addresses if 'i_opt c_no_cmp' + // is defined. Otherwise uses 'i_cmp' or default compare. +bool csptr_X_value_eq(const i_val* x, const i_val* y); // cbox_X_value_cmp == 0 ``` ## Types and constants -| Type name | Type definition | Used to represent... | -|:-------------------|:--------------------------------------------------|:------------------------| -| `csptr_null` | `{NULL, NULL}` | Init nullptr const | -| `csptr_X` | `struct { csptr_X_value* get; long* use_count; }` | The csptr type | -| `csptr_X_value` | `i_val` | The csptr element type | +| Type name | Type definition | Used to represent... | +|:-------------------|:---------------------------------------------------|:------------------------| +| `csptr_null` | `{NULL, NULL}` | Init nullptr const | +| `csptr_X` | `struct { csptr_X_value* get; long* use_count; }` | The csptr type | +| `csptr_X_value` | `i_val` | The csptr element type | ## Example @@ -101,17 +101,17 @@ int main() // POPULATE the stack with shared pointers to Map: Map *map; map = Stack_push(&stack, Arc_new(Map_init()))->get; - c_apply_pair (Map, emplace, map, { + c_apply(v, Map_emplace(map, c_pair(v)), Map_rawvalue, { {"Joey", 1990}, {"Mary", 1995}, {"Joanna", 1992} }); map = Stack_push(&stack, Arc_new(Map_init()))->get; - c_apply_pair (Map, emplace, map, { + c_apply(v, Map_emplace(map, c_pair(v)), Map_rawvalue, { {"Rosanna", 2001}, {"Brad", 1999}, {"Jack", 1980} }); // POPULATE the list: map = List_push_back(&list, Arc_new(Map_init()))->get; - c_apply_pair (Map, emplace, map, { + c_apply(v, Map_emplace(map, c_pair(v)), Map_rawvalue, { {"Steve", 1979}, {"Rick", 1974}, {"Tracy", 2003} }); diff --git a/docs/csset_api.md b/docs/csset_api.md index c5cd0f88..62ccb638 100644 --- a/docs/csset_api.md +++ b/docs/csset_api.md @@ -80,8 +80,10 @@ c_auto (csset_str, fifth) c_auto (csset_str, first, second)
c_auto (csset_str, third, fourth)
{
- c_apply(csset_str, emplace, &second, {"red", "green", "blue"});
- c_apply(csset_str, emplace, &third, {"orange", "pink", "yellow"});
+ c_apply(v, csset_str_emplace(&second, v), const char*,
+ {"red", "green", "blue"});
+ c_apply(v, csset_str_emplace(&third, v), const char*,
+ {"orange", "pink", "yellow"});
csset_str_emplace(&fourth, "potatoes");
csset_str_emplace(&fourth, "milk");
diff --git a/docs/cvec_api.md b/docs/cvec_api.md index 29a3efcb..2a4ceaf6 100644 --- a/docs/cvec_api.md +++ b/docs/cvec_api.md @@ -116,7 +116,7 @@ int main() cvec_int_push_back(&vec, 13); // Append a set of numbers - c_apply(cvec_int, push_back, &vec, {7, 5, 16, 8}); + c_apply(v, cvec_int_push_back(&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 598d0bdc..ae10a89f 100644 --- a/examples/box.c +++ b/examples/box.c @@ -28,7 +28,7 @@ void Person_drop(Person* p) { #include <stc/cbox.h>
#define i_type Persons
-#define i_val_bind PBox // binds PBox_cmp, ...
+#define i_val_ref PBox // binds PBox_cmp, ...
#include <stc/cvec.h>
@@ -37,7 +37,7 @@ int main() c_auto (Persons, vec)
c_auto (PBox, p, q)
{
- p = PBox_new(Person_new("Laura", "Palmer"));
+ p = PBox_from(Person_new("Laura", "Palmer"));
q = PBox_clone(p);
cstr_assign(&q.get->name, "Leland");
@@ -45,11 +45,11 @@ int main() printf("orig: %s %s\n", p.get->name.str, p.get->last.str);
printf("copy: %s %s\n", q.get->name.str, q.get->last.str);
- Persons_push_back(&vec, PBox_new(Person_new("Dale", "Cooper")));
- Persons_push_back(&vec, PBox_new(Person_new("Audrey", "Home")));
+ Persons_push_back(&vec, PBox_from(Person_new("Dale", "Cooper")));
+ Persons_push_back(&vec, PBox_from(Person_new("Audrey", "Home")));
// NB! Clone p and q to the vector using emplace_back()
- c_apply(Persons, emplace_back, &vec, {p, q});
+ c_apply(v, Persons_push_back(&vec, PBox_clone(v)), PBox, {p, q});
c_foreach (i, Persons, vec)
printf("%s %s\n", i.ref->get->name.str, i.ref->get->last.str);
@@ -57,7 +57,7 @@ int main() // Look-up Audrey! Use a (fake) temporary PBox for lookup.
c_autovar (Person a = Person_new("Audrey", "Home"), Person_drop(&a)) {
- const PBox *v = Persons_get(&vec, (PBox){&a});
+ const PBox *v = Persons_get(&vec, a);
if (v) printf("found: %s %s\n", v->get->name.str, v->get->last.str);
}
puts("");
diff --git a/examples/convert.c b/examples/convert.c index 83dc1b37..034657db 100644 --- a/examples/convert.c +++ b/examples/convert.c @@ -16,7 +16,7 @@ int main() c_auto (cvec_str, keys, values) c_auto (clist_str, list) { - c_apply_pair(cmap_str, emplace, &map, { + c_apply(v, cmap_str_emplace(&map, c_pair(v)), cmap_str_rawvalue, { {"green", "#00ff00"}, {"blue", "#0000ff"}, {"yellow", "#ffff00"}, diff --git a/examples/cpque.c b/examples/cpque.c index 0b8a610c..d3ad2d0c 100644 --- a/examples/cpque.c +++ b/examples/cpque.c @@ -37,11 +37,11 @@ int main() print_queue(q); icmp_fn = imin_cmp; - c_apply_n(ipque, push, &q2, data, n); + c_apply_arr(v, ipque_push(&q2, v), int, data, n); print_queue(q2); icmp_fn = imix_cmp; - c_apply_n(ipque, push, &q3, data, n); + c_apply_arr(v, ipque_push(&q3, v), int, data, n); print_queue(q3); } } diff --git a/examples/csmap_erase.c b/examples/csmap_erase.c index 9cf65389..7b179bef 100644 --- a/examples/csmap_erase.c +++ b/examples/csmap_erase.c @@ -36,8 +36,8 @@ int main() c_auto (mymap, m2) { - // Fill in some data to test with, one at a time, using c_apply_pair() - c_apply_pair(mymap, emplace, &m2, { + // Fill in some data to test with, one at a time, using c_apply() + c_apply(v, mymap_emplace(&m2, c_pair(v)), mymap_rawvalue, { {10, "Bob"}, {11, "Rob"}, {12, "Robert"}, diff --git a/examples/csmap_find.c b/examples/csmap_find.c index 3ca7ff87..c70e0523 100644 --- a/examples/csmap_find.c +++ b/examples/csmap_find.c @@ -45,7 +45,8 @@ int main() c_auto (csmap_istr, m1) c_auto (cvec_istr, v) { - c_apply_pair(csmap_istr, emplace, &m1, {{40, "Zr"}, {45, "Rh"}}); + c_apply(v, csmap_istr_emplace(&m1, c_pair(v)), csmap_istr_rawvalue, + {{40, "Zr"}, {45, "Rh"}}); 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 c3690c83..1b89915e 100644 --- a/examples/csmap_insert.c +++ b/examples/csmap_insert.c @@ -99,7 +99,8 @@ int main() c_auto (csmap_ii, m4) {
// Insert the elements from an initializer_list
- c_apply_pair(csmap_ii, insert, &m4, { { 4, 44 }, { 2, 22 }, { 3, 33 }, { 1, 11 }, { 5, 55 } });
+ c_apply(v, csmap_ii_insert(&m4, c_pair(v)), csmap_ii_rawvalue,
+ { { 4, 44 }, { 2, 22 }, { 3, 33 }, { 1, 11 }, { 5, 55 } });
puts("After initializer_list insertion, m4 contains:");
print_ii(m4);
puts("");
diff --git a/examples/csset_erase.c b/examples/csset_erase.c index 95145ed8..485e78de 100644 --- a/examples/csset_erase.c +++ b/examples/csset_erase.c @@ -7,7 +7,8 @@ int main() {
c_auto (csset_int, set)
{
- c_apply(csset_int, insert, &set, {30, 20, 80, 40, 60, 90, 10, 70, 50});
+ 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);
puts("");
diff --git a/examples/inits.c b/examples/inits.c index a1733caf..3f62b9fc 100644 --- a/examples/inits.c +++ b/examples/inits.c @@ -41,7 +41,7 @@ int main(void) // PRIORITY QUEUE
- c_apply_n(cpque_f, push, &floats, nums, c_arraylen(nums));
+ c_apply_arr(v, cpque_f_push(&floats, v), float, nums, c_arraylen(nums));
puts("\npop and show high priorites first:");
while (! cpque_f_empty(floats)) {
@@ -67,7 +67,7 @@ int main(void) // CMAP CNT
c_auto (cmap_cnt, countries) {
- c_apply_pair(cmap_cnt, emplace, &countries, {
+ c_apply(v, cmap_cnt_emplace(&countries, c_pair(v)), cmap_cnt_rawvalue, {
{"Norway", 100},
{"Denmark", 50},
{"Iceland", 10},
@@ -90,7 +90,8 @@ int main(void) // CVEC PAIR
c_auto (cvec_ip, pairs1) {
- c_apply(cvec_ip, push_back, &pairs1, {{5, 6}, {3, 4}, {1, 2}, {7, 8}});
+ c_apply(p, cvec_ip_push_back(&pairs1, p), ipair_t,
+ {{5, 6}, {3, 4}, {1, 2}, {7, 8}});
cvec_ip_sort(&pairs1);
c_foreach (i, cvec_ip, pairs1)
@@ -101,7 +102,8 @@ int main(void) // CLIST PAIR
c_auto (clist_ip, pairs2) {
- c_apply(clist_ip, push_back, &pairs2, {{5, 6}, {3, 4}, {1, 2}, {7, 8}});
+ c_apply(p, clist_ip_push_back(&pairs2, p), ipair_t,
+ {{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 d0d377f3..84f4e24c 100644 --- a/examples/list.c +++ b/examples/list.c @@ -36,7 +36,7 @@ int main() { puts("");
clist_fx_clear(&list);
- c_apply(clist_fx, push_back, &list, {10, 20, 30, 40, 30, 50});
+ c_apply(v, clist_fx_push_back(&list, v), int, {10, 20, 30, 40, 30, 50});
c_foreach (i, clist_fx, list) printf(" %g", *i.ref);
puts("");
diff --git a/examples/list_erase.c b/examples/list_erase.c index c567f043..b20f3fb1 100644 --- a/examples/list_erase.c +++ b/examples/list_erase.c @@ -8,7 +8,7 @@ int main () {
c_auto (clist_int, L)
{
- c_apply(clist_int, push_back, &L, {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("");
// 10 20 30 40 50
diff --git a/examples/list_splice.c b/examples/list_splice.c index f1049288..0738e9d3 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(clist_i, push_back, &list1, {1, 2, 3, 4, 5}); - c_apply(clist_i, push_back, &list2, {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/new_list.c b/examples/new_list.c index 0867c7a6..b6718d34 100644 --- a/examples/new_list.c +++ b/examples/new_list.c @@ -39,7 +39,8 @@ int main() clist_i32_push_back(&lst, 123); c_auto (clist_pnt, plst) { - c_apply(clist_pnt, push_back, &plst, {{42, 14}, {32, 94}, {62, 81}}); + c_apply(v, clist_pnt_push_back(&plst, v), + Point, {{42, 14}, {32, 94}, {62, 81}}); clist_pnt_sort(&plst); c_foreach (i, clist_pnt, plst) @@ -48,7 +49,8 @@ int main() } c_auto (clist_float, flst) { - c_apply(clist_float, push_back, &flst, {123.3, 321.2, -32.2, 78.2}); + 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 953ca793..1c27d5bf 100644 --- a/examples/new_map.c +++ b/examples/new_map.c @@ -48,19 +48,19 @@ int main() {
cmap_int_insert(&map, 123, 321);
- c_apply_pair(cmap_pnt, insert, &pmap, {
+ c_apply(v, cmap_pnt_insert(&pmap, c_pair(v)), cmap_pnt_rawvalue, {
{{42, 14}, 1}, {{32, 94}, 2}, {{62, 81}, 3}
});
c_foreach (i, cmap_pnt, pmap)
printf(" (%d, %d: %d)", i.ref->first.x, i.ref->first.y, i.ref->second);
puts("");
- c_apply_pair(cmap_str, emplace, &smap, {
+ c_apply(v, cmap_str_emplace(&smap, c_pair(v)), cmap_str_rawvalue, {
{"Hello, friend", "long time no see"},
{"So long, friend", "see you around"},
});
- c_apply(cset_str, emplace, &sset, {
+ c_apply(v, cset_str_emplace(&sset, v), const char*, {
"Hello, friend",
"Nice to see you again",
"So long, friend",
diff --git a/examples/new_sptr.c b/examples/new_sptr.c index d1eed636..ce31478e 100644 --- a/examples/new_sptr.c +++ b/examples/new_sptr.c @@ -27,16 +27,16 @@ void Person_drop(Person* p) { #include <stc/cstack.h>
int main(void) {
- c_autovar (csptr_person p = csptr_person_new(Person_new("John", "Smiths")), csptr_person_drop(&p))
+ c_autovar (csptr_person p = csptr_person_from(Person_new("John", "Smiths")), csptr_person_drop(&p))
c_autovar (csptr_person q = csptr_person_clone(p), csptr_person_drop(&q)) // share the pointer
{
printf("%s %s. uses: %lu\n", q.get->name.str, q.get->last.str, *q.use_count);
}
c_auto (cstack_iptr, stk) {
- cstack_iptr_push(&stk, csptr_int_new(10));
- cstack_iptr_push(&stk, csptr_int_new(20));
- cstack_iptr_push(&stk, csptr_int_new(30));
+ cstack_iptr_push(&stk, csptr_int_from(10));
+ cstack_iptr_push(&stk, csptr_int_from(20));
+ cstack_iptr_push(&stk, csptr_int_from(30));
cstack_iptr_emplace(&stk, *cstack_iptr_top(&stk));
cstack_iptr_emplace(&stk, *cstack_iptr_begin(&stk).ref);
diff --git a/examples/phonebook.c b/examples/phonebook.c index 640aaec3..064de93f 100644 --- a/examples/phonebook.c +++ b/examples/phonebook.c @@ -42,14 +42,15 @@ int main(int argc, char **argv) c_static_assert(sizeof argc == 4);
c_auto (cset_str, names) {
- c_apply(cset_str, emplace, &names, {"Hello", "Cool", "True"});
+ c_apply(v, cset_str_emplace(&names, v), const char*,
+ {"Hello", "Cool", "True"});
c_foreach (i, cset_str, names) printf("%s ", i.ref->str);
puts("");
}
bool erased;
c_auto (cmap_str, phone_book) {
- c_apply_pair (cmap_str, emplace, &phone_book, {
+ c_apply(v, cmap_str_emplace(&phone_book, c_pair(v)), cmap_str_rawvalue, {
{"Lilia Friedman", "(892) 670-4739"},
{"Tariq Beltran", "(489) 600-7575"},
{"Laiba Juarez", "(303) 885-5692"},
diff --git a/examples/priority.c b/examples/priority.c index 5164d85d..184cad9f 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(cpque_i, push, &heap, {-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/sidebyside.cpp b/examples/sidebyside.cpp index 0f645991..cf4b2f70 100644 --- a/examples/sidebyside.cpp +++ b/examples/sidebyside.cpp @@ -19,7 +19,8 @@ int main() { }
c_auto (cmap_str, food)
{
- c_apply_pair(cmap_str, emplace, &food, {{"burger", 5}, {"pizza", 12}, {"steak", 15}});
+ c_apply(v, cmap_str_emplace(&food, c_pair(v)), cmap_str_rawvalue,
+ {{"burger", 5}, {"pizza", 12}, {"steak", 15}});
c_foreach (i, cmap_str, food)
printf("%s, %d\n", i.ref->first.str, i.ref->second);
puts("");
diff --git a/examples/sptr.c b/examples/sptr.c index 7e0f7357..e1d734e0 100644 --- a/examples/sptr.c +++ b/examples/sptr.c @@ -28,7 +28,7 @@ void Person_drop(Person* p) { #include <stc/csptr.h>
#define i_type Persons
-#define i_val_bind PSPtr // binds PSPtr_cmp, ...
+#define i_val_ref PSPtr // binds PSPtr_cmp, ...
#include <stc/cvec.h>
@@ -37,30 +37,31 @@ int main() c_auto (Persons, vec)
c_auto (PSPtr, p, q)
{
- p = PSPtr_new(Person_new("Laura", "Palmer"));
+ p = PSPtr_from(Person_new("Laura", "Palmer"));
// We want a deep copy -- PSPtr_clone(p) only shares!
- q = PSPtr_new(Person_clone(*p.get));
+ q = PSPtr_from(Person_clone(*p.get));
cstr_assign(&q.get->name, "Leland");
printf("orig: %s %s\n", p.get->name.str, p.get->last.str);
printf("copy: %s %s\n", q.get->name.str, q.get->last.str);
- Persons_push_back(&vec, PSPtr_new(Person_new("Dale", "Cooper")));
- Persons_push_back(&vec, PSPtr_new(Person_new("Audrey", "Home")));
-
- // NB! Clone/share p and q to the vector using emplace_back()
- c_apply(Persons, emplace_back, &vec, {p, q});
+ Persons_push_back(&vec, PSPtr_from(Person_new("Dale", "Cooper")));
+ Persons_push_back(&vec, PSPtr_from(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_foreach (i, Persons, vec)
printf("%s %s\n", i.ref->get->name.str, i.ref->get->last.str);
puts("");
-
- // Look-up Audrey! Use a (fake) temporary PSPtr for lookup.
+
+ // Look-up Audrey!
c_autovar (Person a = Person_new("Audrey", "Home"), Person_drop(&a)) {
- const PSPtr *v = Persons_get(&vec, (PSPtr){&a});
+ const PSPtr *v = Persons_get(&vec, a);
if (v) printf("found: %s %s\n", v->get->name.str, v->get->last.str);
}
+
puts("");
}
}
diff --git a/examples/sptr_demo.c b/examples/sptr_demo.c index a6b33de6..a5c642b0 100644 --- a/examples/sptr_demo.c +++ b/examples/sptr_demo.c @@ -13,10 +13,10 @@ void int_drop(int* x) { #define i_drop int_drop // optional, just to display the elements destroyed
#include <stc/csptr.h> // iref
-#define i_key_bind iref // note: use i_key_bind instead of i_key for csptr/cbox elements
+#define i_key_ref iref // note: use i_key_bind instead of i_key for csptr/cbox elements
#include <stc/csset.h> // csset_iref (like: std::set<std::shared_ptr<int>>)
-#define i_val_bind iref // note: as above.
+#define i_val_ref iref // note: as above.
#include <stc/cvec.h> // cvec_iref (like: std::vector<std::shared_ptr<int>>)
int main()
@@ -26,7 +26,7 @@ int main() {
const int years[] = {2021, 2012, 2022, 2015};
c_forrange (i, c_arraylen(years))
- cvec_iref_push_back(&vec, iref_new(years[i]));
+ cvec_iref_push_back(&vec, iref_from(years[i]));
printf("vec:");
c_foreach (i, cvec_iref, vec) printf(" %d", *i.ref->get);
@@ -35,7 +35,7 @@ int main() // add odd numbers from vec to set
c_foreach (i, cvec_iref, vec)
if (*i.ref->get & 1)
- csset_iref_emplace(&set, *i.ref); // copy shared pointer => increments counter.
+ csset_iref_insert(&set, iref_clone(*i.ref)); // copy shared pointer => increments counter.
// erase the two last elements in vec
cvec_iref_pop_back(&vec);
diff --git a/examples/sptr_erase.c b/examples/sptr_erase.c index 240b9a58..fbb141b4 100644 --- a/examples/sptr_erase.c +++ b/examples/sptr_erase.c @@ -9,11 +9,10 @@ void show_drop(int* x) { printf("drop: %d\n", *x); } // if 'i_opt c_no_cmp' is defined, otherwise i_cmp is used
// to compare object values. See the twodifferences by
// commenting out the next line.
-//#define i_opt c_no_cmp
#include <stc/csptr.h> // Shared pointer to int
#define i_type Vec
-#define i_val_bind Arc
+#define i_val_ref Arc
#include <stc/cvec.h> // Vec: cvec<Arc>
@@ -23,11 +22,11 @@ int main() {
const int v[] = {2012, 1990, 2012, 2019, 2015};
c_forrange (i, c_arraylen(v))
- Vec_push_back(&vec, Arc_new(v[i]));
+ Vec_push_back(&vec, Arc_from(v[i]));
// clone the second 2012 and push it back.
// note: cloning make sure that vec.data[2] has ref count 2.
- Vec_emplace_back(&vec, vec.data[2]);
+ Vec_push_back(&vec, Arc_clone(vec.data[2]));
printf("vec before erase :");
c_foreach (i, Vec, vec)
@@ -36,12 +35,12 @@ int main() printf("erase vec.data[2]; or first matching value depending on compare.\n");
Vec_iter it;
- it = Vec_find(&vec, vec.data[2]);
+ it = Vec_find(&vec, *vec.data[2].get);
if (it.ref != Vec_end(&vec).ref)
Vec_erase_at(&vec, it);
int year = 2015;
- it = Vec_find(&vec, (Arc){&year}); // Ok as tmp only.
+ it = Vec_find(&vec, year); // Ok as tmp only.
if (it.ref != Vec_end(&vec).ref)
Vec_erase_at(&vec, it);
diff --git a/examples/sptr_music.c b/examples/sptr_music.c index 04ec28cc..1d58cc46 100644 --- a/examples/sptr_music.c +++ b/examples/sptr_music.c @@ -17,36 +17,36 @@ void Song_drop(Song* s) { c_drop(cstr, &s->artist, &s->title);
}
+#define i_type SongPtr
#define i_val Song
#define i_drop Song_drop
#define i_opt c_no_cmp
-#define i_tag song
-#include <stc/csptr.h> // define csptr_song
+#include <stc/csptr.h>
-#define i_val_bind csptr_song
-#define i_tag song
+#define i_type SongVec
+#define i_val_ref SongPtr
#include <stc/cvec.h>
void example3()
{
- c_auto (cvec_song, v, v2)
+ c_auto (SongVec, vec, vec2)
{
- c_apply(cvec_song, push_back, &v, {
- csptr_song_new(Song_new("Bob Dylan", "The Times They Are A Changing")),
- csptr_song_new(Song_new("Aretha Franklin", "Bridge Over Troubled Water")),
- csptr_song_new(Song_new("Thalia", "Entre El Mar y Una Estrella"))
+ c_apply(v, SongVec_push_back(&vec, v), SongPtr, {
+ SongPtr_from(Song_new("Bob Dylan", "The Times They Are A Changing")),
+ SongPtr_from(Song_new("Aretha Franklin", "Bridge Over Troubled Water")),
+ SongPtr_from(Song_new("Thalia", "Entre El Mar y Una Estrella"))
});
- c_foreach (s, cvec_song, v)
+ c_foreach (s, SongVec, vec)
if (!cstr_equals(s.ref->get->artist, "Bob Dylan"))
- cvec_song_emplace_back(&v2, *s.ref); // note: calls csptr_song_clone()
+ SongVec_push_back(&vec2, SongPtr_clone(*s.ref));
- c_apply(cvec_song, push_back, &v2, {
- csptr_song_new(Song_new("Michael Jackson", "Billie Jean")),
- csptr_song_new(Song_new("Rihanna", "Stay")),
+ c_apply(v, SongVec_push_back(&vec2, v), SongPtr, {
+ SongPtr_from(Song_new("Michael Jackson", "Billie Jean")),
+ SongPtr_from(Song_new("Rihanna", "Stay")),
});
- c_foreach (s, cvec_song, v2)
+ c_foreach (s, SongVec, vec2)
printf("%s - %s: refs %lu\n", s.ref->get->artist.str, s.ref->get->title.str,
*s.ref->use_count);
}
diff --git a/examples/sptr_pthread.c b/examples/sptr_pthread.c index 501fdcef..d71ce97d 100644 --- a/examples/sptr_pthread.c +++ b/examples/sptr_pthread.c @@ -33,7 +33,7 @@ void* thr(csptr_base* lp) int main()
{
- csptr_base p = csptr_base_new((Base){42});
+ csptr_base p = csptr_base_from((Base){42});
printf("Created a Base\n"
" p.get() = %p, p.use_count() = %ld\n", (void*)p.get, *p.use_count);
diff --git a/examples/sptr_to_maps.c b/examples/sptr_to_maps.c index 69798563..6b4dd359 100644 --- a/examples/sptr_to_maps.c +++ b/examples/sptr_to_maps.c @@ -15,11 +15,11 @@ #include <stc/csptr.h>
#define i_type Stack
-#define i_val_bind Arc // define i_val_bind for csptr/cbox value (not i_val)
+#define i_val_ref Arc // define i_val_bind for csptr/cbox value (not i_val)
#include <stc/cstack.h>
#define i_type List
-#define i_val_bind Arc // as above
+#define i_val_ref Arc // as above
#include <stc/clist.h>
int main()
@@ -29,28 +29,28 @@ int main() {
// POPULATE stack with shared pointers to Maps:
Map *map;
- map = Stack_push(&stack, Arc_new(Map_init()))->get;
- c_apply_pair (Map, emplace, map, {
+ map = Stack_push(&stack, Arc_from(Map_init()))->get;
+ c_apply(v, Map_emplace(map, c_pair(v)), Map_rawvalue, {
{"Joey", 1990}, {"Mary", 1995}, {"Joanna", 1992}
});
- map = Stack_push(&stack, Arc_new(Map_init()))->get;
- c_apply_pair (Map, emplace, map, {
+ map = Stack_push(&stack, Arc_from(Map_init()))->get;
+ c_apply(v, Map_emplace(map, c_pair(v)), Map_rawvalue, {
{"Rosanna", 2001}, {"Brad", 1999}, {"Jack", 1980}
});
// POPULATE list:
- map = List_push_back(&list, Arc_new(Map_init()))->get;
- c_apply_pair (Map, emplace, map, {
+ map = List_push_back(&list, Arc_from(Map_init()))->get;
+ c_apply(v, Map_emplace(map, c_pair(v)), Map_rawvalue, {
{"Steve", 1979}, {"Rick", 1974}, {"Tracy", 2003}
});
// Share two Maps from the stack with the list using emplace (clone the csptr):
- List_emplace_back(&list, stack.data[0]);
- List_emplace_back(&list, stack.data[1]);
+ List_push_back(&list, Arc_clone(stack.data[0]));
+ List_push_back(&list, Arc_clone(stack.data[1]));
// Clone (deep copy) a Map from the stack to the list
// List will contain two shared and two unshared maps.
- map = List_push_back(&list, Arc_new(Map_clone(*stack.data[1].get)))->get;
+ map = List_push_back(&list, Arc_from(Map_clone(*stack.data[1].get)))->get;
// Add one more element to the cloned map:
Map_emplace_or_assign(map, "CLONED", 2021);
diff --git a/examples/vikings.c b/examples/vikings.c index 7a953059..9900e9b6 100644 --- a/examples/vikings.c +++ b/examples/vikings.c @@ -49,7 +49,7 @@ static inline RViking Viking_toraw(const Viking* vk) { int main() { c_auto (Vikings, vikings) { - c_apply_pair(Vikings, emplace, &vikings, { + c_apply(v, Vikings_emplace(&vikings, c_pair(v)), Vikings_rawvalue, { {{"Einar", "Norway"}, 20}, {{"Olaf", "Denmark"}, 24}, {{"Harald", "Iceland"}, 12}, diff --git a/examples/words.c b/examples/words.c index 6c0636dc..568f9557 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(cvec_str, emplace_back, &words, { + 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/cbox.h b/include/stc/cbox.h index bcd9bd8c..37687134 100644 --- a/include/stc/cbox.h +++ b/include/stc/cbox.h @@ -86,10 +86,15 @@ STC_INLINE _cx_self _cx_memb(_from_ptr)(i_val* p) { return c_make(_cx_self){p}; }
STC_INLINE _cx_self
-_cx_memb(_new)(i_val val) {
+_cx_memb(_from)(i_val val) {
return c_make(_cx_self){c_new(i_val, val)};
}
+STC_INLINE i_val
+_cx_memb(_toraw)(const _cx_self* self) {
+ return *self->get;
+}
+
// destructor
STC_INLINE void
_cx_memb(_drop)(_cx_self* self) {
@@ -109,16 +114,18 @@ _cx_memb(_reset)(_cx_self* self) { // take ownership of val
STC_INLINE void
-_cx_memb(_reset_new)(_cx_self* self, i_val val) {
+_cx_memb(_reset_from)(_cx_self* self, i_val val) {
if (self->get) { i_valdrop(self->get); *self->get = val; }
else self->get = c_new(i_val, val);
}
-#if !c_option(c_no_clone)
+#if !c_option(c_no_clone)
+ //#ifndef _i_valraw_default
STC_INLINE _cx_self
- _cx_memb(_from)(i_valraw raw) {
+ _cx_memb(_new)(i_valraw raw) {
return c_make(_cx_self){c_new(i_val, i_valfrom(raw))};
}
+ //#endif
STC_INLINE _cx_self
_cx_memb(_clone)(_cx_self other) {
@@ -141,29 +148,29 @@ _cx_memb(_take)(_cx_self* self, _cx_self other) { }
STC_INLINE uint64_t
-_cx_memb(_hash)(const _cx_self* self, size_t n) {
+_cx_memb(_value_hash)(const _cx_value* x, size_t n) {
#if c_option(c_no_cmp) && UINTPTR_MAX == UINT64_MAX
- return c_hash64(&self->get, 8);
+ return c_hash64(&x, 8);
#elif c_option(c_no_cmp)
- return c_hash32(&self->get, 4);
+ return c_hash32(&x, 4);
#else
- i_valraw rx = i_valto(self->get);
+ i_valraw rx = i_valto(x);
return i_hash(&rx, sizeof rx);
#endif
}
STC_INLINE int
-_cx_memb(_cmp)(const _cx_self* x, const _cx_self* y) {
+_cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y) {
#if c_option(c_no_cmp)
- return c_default_cmp(&x->get, &y->get);
+ return c_default_cmp(&x, &y);
#else
- i_valraw rx = i_valto(x->get), ry = i_valto(x->get);
+ i_valraw rx = i_valto(x), ry = i_valto(x);
return i_cmp(&rx, &ry);
#endif
}
STC_INLINE bool
-_cx_memb(_eq)(const _cx_self* x, const _cx_self* y) {
- return !_cx_memb(_cmp)(x, y);
+_cx_memb(_value_eq)(const _cx_value* x, const _cx_value* y) {
+ return !_cx_memb(_value_cmp)(x, y);
}
#include "template.h"
diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index 9cbe24a4..e11fa3be 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -189,23 +189,37 @@ STC_INLINE uint64_t c_default_hash(const void* key, size_t len) { *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_apply(C, method, cx, ...) do { \
+#define c_apply(v, method, T, ...) do { \
+ const T _c_arr[] = __VA_ARGS__; \
+ for (size_t index = 0; index < c_arraylen(_c_arr); ++index) \
+ { const T v = _c_arr[index]; method; } \
+} while (0)
+#define c_apply_arr(v, method, T, arr, n) do { \
+ const T* _c_arr = arr; size_t _n = n; \
+ for (size_t index = 0; index < _n; ++index) \
+ { const T v = _c_arr[index]; method; } \
+} while (0)
+#define c_apply_it(v, method, C, ...) do { \
+ size_t index = 0; \
+ c_foreach (_it, C, __VA_ARGS__) \
+ { const C##_value v = *_it.ref; method; ++index; } \
+} while (0)
+#define c_pair(v) (v).first, (v).second
+
+// [deprecated]
+#define c_apply_OLD(C, method, cx, ...) do { \
const C##_rawvalue _c_arr[] = __VA_ARGS__; \
C* _c_cx = cx; \
for (size_t _c_i = 0; _c_i < c_arraylen(_c_arr); ++_c_i) \
C##_##method(_c_cx, _c_arr[_c_i]); \
} while (0)
-#define c_apply_pair(C, method, cx, ...) do { \
+// [deprecated]
+#define c_apply_pair_OLD(C, method, cx, ...) do { \
const C##_rawvalue _c_arr[] = __VA_ARGS__; \
C* _c_cx = cx; \
for (size_t _c_i = 0; _c_i < c_arraylen(_c_arr); ++_c_i) \
C##_##method(_c_cx, _c_arr[_c_i].first, _c_arr[_c_i].second); \
} while (0)
-#define c_apply_n(C, method, cx, arr, n) do { \
- C* _c_cx = cx; \
- for (const C##_rawvalue *_c_i = arr, *_c_end = _c_i+(n); _c_i != _c_end; ++_c_i) \
- C##_##method(_c_cx, *_c_i); \
-} while (0)
#define c_drop(C, ...) do { \
C* _c_arr[] = {__VA_ARGS__}; \
diff --git a/include/stc/csptr.h b/include/stc/csptr.h index 92ed05ef..4e9b4687 100644 --- a/include/stc/csptr.h +++ b/include/stc/csptr.h @@ -104,13 +104,17 @@ _cx_memb(_from_ptr)(_cx_value* p) { }
STC_INLINE _cx_self
-_cx_memb(_new)(i_val val) {
+_cx_memb(_from)(i_val val) {
_cx_self ptr; _cx_csptr_rep *rep = c_alloc(_cx_csptr_rep);
*(ptr.use_count = &rep->counter) = 1;
*(ptr.get = &rep->value) = val;
return ptr;
}
+STC_INLINE i_val _cx_memb(_toraw)(const _cx_self* self) {
+ return *self->get;
+}
+
STC_INLINE _cx_self
_cx_memb(_move)(_cx_self* self) {
_cx_self ptr = *self;
@@ -135,15 +139,15 @@ _cx_memb(_reset)(_cx_self* self) { }
STC_INLINE void
-_cx_memb(_reset_new)(_cx_self* self, i_val val) {
+_cx_memb(_reset_from)(_cx_self* self, i_val val) {
_cx_memb(_drop)(self);
- *self = _cx_memb(_new)(val);
+ *self = _cx_memb(_from)(val);
}
-#if !c_option(c_no_clone)
- STC_INLINE _cx_self _cx_memb(_from)(i_valraw raw) {
- return _cx_memb(_new)(i_valfrom(raw));
-}
+#if !c_option(c_no_clone) && !defined _i_valraw_default
+ STC_INLINE _cx_self _cx_memb(_new)(i_valraw raw) {
+ return _cx_memb(_from)(i_valfrom(raw));
+ }
#endif
// does not use i_valfrom, so we can bypass c_no_clone
@@ -167,30 +171,30 @@ _cx_memb(_take)(_cx_self* self, _cx_self ptr) { }
STC_INLINE uint64_t
-_cx_memb(_hash)(const _cx_self* self, size_t n) {
+_cx_memb(_value_hash)(const _cx_value* x, size_t n) {
#if c_option(c_no_cmp) && UINTPTR_MAX == UINT64_MAX
- return c_hash64(&self->get, 8);
+ return c_hash64(&x, 8);
#elif c_option(c_no_cmp)
- return c_hash32(&self->get, 4);
+ return c_hash32(&x, 4);
#else
- i_valraw rx = i_valto(self->get);
+ i_valraw rx = i_valto(x);
return i_hash(&rx, sizeof rx);
#endif
}
STC_INLINE int
-_cx_memb(_cmp)(const _cx_self* x, const _cx_self* y) {
+_cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y) {
#if c_option(c_no_cmp)
- return c_default_cmp(&x->get, &y->get);
+ return c_default_cmp(&x, &y);
#else
- i_valraw rx = i_valto(x->get), ry = i_valto(x->get);
+ i_valraw rx = i_valto(x), ry = i_valto(x);
return i_cmp(&rx, &ry);
#endif
}
STC_INLINE bool
-_cx_memb(_eq)(const _cx_self* x, const _cx_self* y) {
- return !_cx_memb(_cmp)(x, y);
+_cx_memb(_value_eq)(const _cx_value* x, const _cx_value* y) {
+ return !_cx_memb(_value_cmp)(x, y);
}
#undef _i_atomic_inc
#undef _i_atomic_dec_and_test
diff --git a/include/stc/template.h b/include/stc/template.h index 83a362d7..675baa47 100644 --- a/include/stc/template.h +++ b/include/stc/template.h @@ -40,14 +40,6 @@ #define _cx_size _cx_memb(_size_t)
#endif
-#if defined i_key_csptr || defined i_key_ref // [deprecated]
- #define i_key_bind i_key_csptr
- #error "i_key_csptr/ref no longer supported: use new name i_key_bind"
-#endif
-#if defined i_val_csptr || defined i_val_ref // [deprecated]
- #define i_val_bind i_val_csptr
- #error "i_val_sptr/ref no longer supported: use new name i_val_bind"
-#endif
#if defined i_cnt || defined i_equ // [deprecated]
#define i_type i_cnt
#error "i_cnt and i_equ no longer supported: use new name i_type / i_eq"
@@ -69,6 +61,9 @@ #ifndef i_tag
#define i_tag str
#endif
+#elif defined i_key_ref
+ #define i_key_bind i_key_ref
+ #define i_keyraw c_PASTE(i_key_ref, _value)
#endif
#ifdef i_key_bind
@@ -125,6 +120,9 @@ #if !defined i_tag && !defined i_key
#define i_tag str
#endif
+#elif defined i_val_ref
+ #define i_val_bind i_val_ref
+ #define i_valraw c_PASTE(i_val_ref, _value)
#endif
#ifdef i_val_bind
@@ -167,6 +165,7 @@ #define i_keyfrom c_default_clone
#endif
#ifndef i_keyraw
+ #define _i_keyraw_default
#define i_keyraw i_key
#define i_keyto c_default_toraw
#endif
@@ -192,6 +191,7 @@ #define i_valfrom c_default_clone
#endif
#ifndef i_valraw
+ #define _i_valraw_default
#define i_valraw i_val
#define i_valto c_default_toraw
#endif
@@ -219,6 +219,7 @@ #undef i_val
#undef i_val_str
+#undef i_val_ref
#undef i_val_bind
#undef i_valraw
#undef i_valfrom
@@ -227,6 +228,7 @@ #undef i_key
#undef i_key_str
+#undef i_key_ref
#undef i_key_bind
#undef i_keyraw
#undef i_keyfrom
@@ -234,6 +236,8 @@ #undef i_keydrop
#undef _i_prefix
+#undef _i_valraw_default
+#undef _i_keyraw_default
#undef _i_has_internal_clone
#undef _i_template
#endif
|
