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 /docs | |
| 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.
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/cbox_api.md | 17 | ||||
| -rw-r--r-- | docs/ccommon_api.md | 18 | ||||
| -rw-r--r-- | docs/cdeq_api.md | 2 | ||||
| -rw-r--r-- | docs/clist_api.md | 8 | ||||
| -rw-r--r-- | docs/cmap_api.md | 6 | ||||
| -rw-r--r-- | docs/cpque_api.md | 2 | ||||
| -rw-r--r-- | docs/cset_api.md | 6 | ||||
| -rw-r--r-- | docs/csmap_api.md | 4 | ||||
| -rw-r--r-- | docs/csptr_api.md | 42 | ||||
| -rw-r--r-- | docs/csset_api.md | 6 | ||||
| -rw-r--r-- | docs/cvec_api.md | 2 |
11 files changed, 61 insertions, 52 deletions
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) { |
