diff options
| author | Tyge Løvset <[email protected]> | 2021-12-19 12:21:44 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2021-12-19 12:21:44 +0100 |
| commit | 92b950333c6c7002bdbf1b60af44a249dc0cef9c (patch) | |
| tree | 4b1acfcdba0bd940f829c53910587e27b5e0af90 /docs | |
| parent | 183a89859ba9914ee0546e4482b40be199e52292 (diff) | |
| download | STC-modified-92b950333c6c7002bdbf1b60af44a249dc0cef9c.tar.gz STC-modified-92b950333c6c7002bdbf1b60af44a249dc0cef9c.zip | |
First commit for Version 3 of STC. Main changes are consistent rename of '_del' to '_drop' and '_compare' to '_cmp'.
Also i_key_ref (earlier i_key_sptr) and i_val_ref replaced by more general i_key_bind/i_val_bind.
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/carray_api.md | 12 | ||||
| -rw-r--r-- | docs/cbits_api.md | 4 | ||||
| -rw-r--r-- | docs/cbox_api.md | 66 | ||||
| -rw-r--r-- | docs/ccommon_api.md | 22 | ||||
| -rw-r--r-- | docs/cdeq_api.md | 6 | ||||
| -rw-r--r-- | docs/clist_api.md | 10 | ||||
| -rw-r--r-- | docs/cmap_api.md | 161 | ||||
| -rw-r--r-- | docs/cpque_api.md | 8 | ||||
| -rw-r--r-- | docs/cqueue_api.md | 6 | ||||
| -rw-r--r-- | docs/crandom_api.md | 4 | ||||
| -rw-r--r-- | docs/cset_api.md | 8 | ||||
| -rw-r--r-- | docs/csmap_api.md | 16 | ||||
| -rw-r--r-- | docs/csptr_api.md | 94 | ||||
| -rw-r--r-- | docs/csset_api.md | 12 | ||||
| -rw-r--r-- | docs/cstack_api.md | 6 | ||||
| -rw-r--r-- | docs/cstr_api.md | 13 | ||||
| -rw-r--r-- | docs/csview_api.md | 6 | ||||
| -rw-r--r-- | docs/cvec_api.md | 26 |
18 files changed, 246 insertions, 234 deletions
diff --git a/docs/carray_api.md b/docs/carray_api.md index 5e26f658..3ac6d650 100644 --- a/docs/carray_api.md +++ b/docs/carray_api.md @@ -10,8 +10,8 @@ See the c++ class [boost::multi_array](https://www.boost.org/doc/libs/release/li ```c #define i_val // value: REQUIRED -#define i_del // destroy value func - defaults to empty destruct -#define i_valfrom // func Raw => i_val - defaults to plain copy +#define i_drop // destroy value func - defaults to empty destruct +#define i_from // func Raw => i_val - defaults to plain copy #define i_valto // func i_val => Raw - defaults to plain copy #define i_tag // defaults to i_val @@ -29,7 +29,7 @@ carr2_X carr2_X_with_storage(size_t xdim, size_t ydim, i_val* array) carr2_X carr2_X_clone(carr2_X arr); void carr2_X_copy(carr2_X* self, carr2_X other); i_val* carr2_X_release(carr2_X* self); // release storage (not freed) -void carr2_X_del(carr2_X* self); +void carr2_X_drop(carr2_X* self); size_t carr2_X_size(carr2_X arr); i_val* carr2_X_data(carr2_X* self); // access storage data @@ -48,7 +48,7 @@ carr3_X carr3_X_with_storage(size_t xdim, size_t ydim, size_t zdim, carr3_X carr3_X_clone(carr3_X arr); void carr3_X_copy(carr3_X* self, carr3_X other); i_val* carr3_X_release(carr3_X* self); // release storage (not freed) -void carr3_X_del(carr3_X* self); +void carr3_X_drop(carr3_X* self); size_t carr3_X_size(carr3_X arr); i_val* carr3_X_data(carr3_X* self); // storage data @@ -91,7 +91,7 @@ int main() int xd = 30, yd = 20, zd = 10; // define arr3[30][20][10], initialized with zeros. c_autovar (carr3_f arr3 = carr3_f_with_values(xd, yd, zd, 0.0f), - carr3_f_del(&arr3)) { + carr3_f_drop(&arr3)) { arr3.data[5][4][3] = 3.14f; float *arr1 = arr3.data[5][4]; @@ -104,7 +104,7 @@ int main() // Ex2 int w = 256, h = 128; - c_autovar (carr2_i image = carr2_i_init(w, h), carr2_i_del(&image)) { + c_autovar (carr2_i image = carr2_i_init(w, h), carr2_i_drop(&image)) { int n = 0; c_foreach (i, carr2_i, image) { uint32_t t = n++ % 256; diff --git a/docs/cbits_api.md b/docs/cbits_api.md index b369b5d1..f6077d20 100644 --- a/docs/cbits_api.md +++ b/docs/cbits_api.md @@ -27,7 +27,7 @@ cbits cbits_clone(cbits other); void cbits_clear(cbits* self); cbits* cbits_copy(cbits* self, cbits other); void cbits_resize(cbits* self, size_t size, bool value); -void cbits_del(cbits* self); +void cbits_drop(cbits* self); cbits* cbits_take(cbits* self, cbits other); // give other to self cbits cbits_move(cbits* self); // transfer self to caller @@ -103,7 +103,7 @@ int main(void) if (cbits_test(primes, i>>1)) printf(" %zu", i); puts(""); - cbits_del(&primes); + cbits_drop(&primes); } ``` Output: diff --git a/docs/cbox_api.md b/docs/cbox_api.md index 83cec830..8580e7d7 100644 --- a/docs/cbox_api.md +++ b/docs/cbox_api.md @@ -1,12 +1,12 @@ # STC [cbox](../include/stc/cbox.h): (Boxed) Heap Allocated Objects **cbox** is a A box is a smart pointer to a heap allocated value of type X. A **cbox** can -be empty. The *cbox_X_compare()*, *cbox_X_del()* methods are defined based on the `i_cmp` -and `i_valdel` macros specified. Use *cbox_X_clone(p)* to make a deep copy, which uses the +be empty. The *cbox_X_cmp()*, *cbox_X_drop()* methods are defined based on the `i_cmp` +and `i_valdrop` macros specified. Use *cbox_X_clone(p)* to make a deep copy, which uses the `i_valfrom` macro if defined. -When declaring a container of **cbox** values, it is recommended to define `i_val_ref` to the -cbox type instead of defining `i_val`. This will auto-set `i_del`, `i_from`, and `i_cmp` using +When declaring a container of **cbox** values, it is recommended to define `i_val_bind` to the +cbox type instead of defining `i_val`. This will auto-set `i_drop`, `i_from`, and `i_cmp` using functions defined by the specified **cbox**. For containers, make sure to pass the result of create functions like *cbox_X_new()* **only** to @@ -20,21 +20,21 @@ See similar c++ class [std::unique_ptr](https://en.cppreference.com/w/cpp/memory ```c #define i_val // value: REQUIRED #define i_cmp // three-way compare two i_val* : REQUIRED IF i_val is a non-integral type -#define i_del // destroy value func - defaults to empty destruct -#define i_from // create from raw/clone func - REQUIRED if i_del is defined, +#define i_drop // destroy value func - defaults to empty destruct +#define i_from // create from raw/clone func - REQUIRED if i_drop is defined, // unless 'i_opt c_no_clone' is defined. #define i_tag // type name tag, defaults to i_val #include <stc/cbox.h> ``` `X` should be replaced by the value of `i_tag` in all of the following documentation. -Define `i_opt` with `c_no_compare` if comparison between i_val's is not needed/available. Will then +Define `i_opt` with `c_no_cmp` if comparison between i_val's is not needed/available. Will then compare the pointer addresses when used. Additionally, `c_no_clone` or `i_is_fwd` may be defined. ## 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_rawval raw); // like cbox_X_new(), but create owned value from raw. +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_clone(cbox_X other); // return deep copied clone @@ -42,14 +42,14 @@ cbox_X cbox_X_move(cbox_X* self); // transfer owners 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_del(cbox_X* self); // destruct the contained object and free's it. +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_rawval raw); // make and assign new cbox from raw value. +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. -int cbox_X_compare(const cbox_X* x, const cbox_X* y); // compares pointer addresses if 'i_opt c_no_compare' +int cbox_X_cmp(const cbox_X* x, const cbox_X* y); // compares pointer addresses if 'i_opt c_no_cmp' // is defined. Otherwise uses 'i_cmp' or default compare. ``` ## Types and constants @@ -66,14 +66,14 @@ int cbox_X_compare(const cbox_X* x, const cbox_X* y); // compares pointe #include <stdio.h> #include <string.h> -void int_del(int* x) { - printf("del: %d\n", *x); +void int_drop(int* x) { + printf("drop: %d\n", *x); } -// When 'i_del' is defined, you are also forced to define a clone function with -// 'i_valfrom', as it is normally required when i_del destroys resources. +// When 'i_drop' is defined, you are also forced to define a clone function with +// 'i_from', as it is normally required when i_drop destroys resources. // -// If cloning is not needed, define 'i_opt c_no_clone' instead of 'i_valfrom' +// If cloning is not needed, define 'i_opt c_no_clone' instead of 'i_from' // both for the cbox type and the container of cbox elements. It will also // disable emplace container functions. // @@ -81,22 +81,22 @@ void int_del(int* x) { // define cloning internally. #define i_val int -#define i_del int_del // optional func, just to display elements destroyed -#define i_valfrom c_default_clone -#include <stc/cbox.h> // cbox_int +#define i_drop int_drop // optional func, just to display elements destroyed +#define i_from c_default_clone +#include <stc/cbox.h> // cbox_int -#define i_key_ref cbox_int // note: use i_key_ref instead of i_key -#define i_tag int // tag otherwise defaults to 'ref' -#include <stc/csset.h> // csset_int (like: std::set<std::unique_ptr<int>>) +#define i_key_bind cbox_int // note: use i_key_bind instead of i_key +#define i_tag int // tag otherwise defaults to 'ref' +#include <stc/csset.h> // csset_int (like: std::set<std::unique_ptr<int>>) -#define i_val_ref cbox_int // note: use i_val_ref instead of i_val -#define i_tag int // tag otherwise defaults to 'ref' -#include <stc/cvec.h> // cvec_int (like: std::vector<std::unique_ptr<int>>) +#define i_val_bind cbox_int // note: use i_val_bind instead of i_val +#define i_tag int // tag otherwise defaults to 'ref' +#include <stc/cvec.h> // cvec_int (like: std::vector<std::unique_ptr<int>>) int main() { - c_auto (cvec_int, vec) // declare and init vec, call del at scope exit - c_auto (csset_int, set) // declare and init set, call del at scope exit + 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, { cbox_int_new(2021), @@ -130,13 +130,13 @@ int main() Output: ``` vec: 2021 2012 2022 2015 -del: 2015 -del: 2022 +drop: 2015 +drop: 2022 vec: 2021 2012 set: 2015 2021 Done -del: 2021 -del: 2015 -del: 2021 -del: 2012 +drop: 2021 +drop: 2015 +drop: 2021 +drop: 2012 ``` diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index 740df9a4..cc021001 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -13,7 +13,7 @@ macros, as one must always make sure to unwind temporary allocated resources bef | Usage | Description | |:---------------------------------------|:---------------------------------------------------| -| `c_auto (Type, var...)` | `c_autovar (Type var=Type_init(), Type_del(&var))` | +| `c_auto (Type, var...)` | `c_autovar (Type var=Type_init(), Type_drop(&var))` | | `c_autovar (Type var=init, end...)` | Declare `var`. Defer `end...` to end of block | | `c_autoscope (init, end...)` | Execute `init`. Defer `end...` to end of block | | `c_autodefer (end...)` | Defer `end...` to end of block | @@ -22,7 +22,7 @@ macros, as one must always make sure to unwind temporary allocated resources bef For multiple variables, use either multiple **c_autovar** in sequence, or declare variable outside scope and use **c_autoscope**. Also, **c_auto** support up to 3 variables. ```c -c_autovar (cstr s = cstr_new("Hello"), cstr_del(&s)) +c_autovar (cstr s = cstr_new("Hello"), cstr_drop(&s)) { cstr_append(&s, " world"); printf("%s\n", s.str); @@ -46,7 +46,7 @@ c_autoscope (mydata_init(&data), mydata_destroy(&data)) } cstr s1 = cstr_new("Hello"), s2 = cstr_new("world"); -c_autodefer (cstr_del(&s1), cstr_del(&s2)) +c_autodefer (cstr_drop(&s1), cstr_drop(&s2)) { printf("%s %s\n", s1.str, s2.str); } @@ -65,7 +65,7 @@ cvec_str readFile(const char* name) cvec_str vec = cvec_str_init(); // returned c_autovar (FILE* fp = fopen(name, "r"), fclose(fp)) - c_autovar (cstr line = cstr_null, cstr_del(&line)) + c_autovar (cstr line = cstr_null, cstr_drop(&line)) while (cstr_getline(&line, fp)) cvec_str_emplace_back(&vec, line.str); return vec; @@ -73,7 +73,7 @@ cvec_str readFile(const char* name) int main() { - c_autovar (cvec_str x = readFile(__FILE__), cvec_str_del(&x)) + c_autovar (cvec_str x = readFile(__FILE__), cvec_str_drop(&x)) c_foreach (i, cvec_str, x) printf("%s\n", i.ref->str); } @@ -140,14 +140,14 @@ int arr[] = {1, 2, 3}; c_apply_n(cvec_i, push_back, &vec, arr, c_arraylen(arr)); ``` -### c_new, c_alloc, c_alloc_n, c_del, c_make +### c_new, c_alloc, c_alloc_n, c_drop, c_make | Usage | Meaning | |:-------------------------------|:----------------------------------------| | `c_new (type, value)` | Move value to a new object on the heap | | `c_alloc (type)` | `(type *) c_malloc(sizeof(type))` | | `c_alloc_n (type, N)` | `(type *) c_malloc((N)*sizeof(type))` | -| `c_del (ctype, &c1, ..., &cN)` | `ctype_del(&c1); ... ctype_del(&cN)` | +| `c_drop (ctype, &c1, ..., &cN)` | `ctype_drop(&c1); ... ctype_drop(&cN)` | | `c_make(type){value...}` | `(type){value...}` // c++ compatability | ```c @@ -159,17 +159,17 @@ int* array = c_alloc_n (int, 100); c_free(array); cstr a = cstr_new("Hello"), b = cstr_new("World"); -c_del(cstr, &a, &b); +c_drop(cstr, &a, &b); ``` ### General predefined template parameter functions ``` -int c_default_compare(const Type*, const Type*); +int c_default_cmp(const Type*, const Type*); Type c_default_clone(Type val); // simple copy Type c_default_toraw(const Type* val); // dereference val -void c_default_del(Type* val); // does nothing +void c_default_drop(Type* val); // does nothing -int c_rawstr_compare(const char* const* a, const char* const* b); +int c_rawstr_cmp(const char* const* a, const char* const* b); bool c_rawstr_equalto(const char* const* a, const char* const* b); ``` diff --git a/docs/cdeq_api.md b/docs/cdeq_api.md index 554b4c7b..1985c2ab 100644 --- a/docs/cdeq_api.md +++ b/docs/cdeq_api.md @@ -10,7 +10,7 @@ See the c++ class [std::deque](https://en.cppreference.com/w/cpp/container/deque ```c #define i_val // value: REQUIRED #define i_cmp // three-way compare two i_valraw* : REQUIRED IF i_valraw is a non-integral type -#define i_del // destroy value func - defaults to empty destruct +#define i_drop // destroy value func - defaults to empty destruct #define i_valraw // convertion "raw" type - defaults to i_val #define i_valfrom // convertion func i_valraw => i_val - defaults to plain copy #define i_valto // convertion func i_val* => i_valraw - defaults to plain copy @@ -31,7 +31,7 @@ void cdeq_X_copy(cdeq_X* self, cdeq_X other); bool cdeq_X_reserve(cdeq_X* self, size_t cap); void cdeq_X_shrink_to_fit(cdeq_X* self); void cdeq_X_swap(cdeq_X* a, cdeq_X* b); -void cdeq_X_del(cdeq_X* self); // destructor +void cdeq_X_drop(cdeq_X* self); // destructor bool cdeq_X_empty(cdeq_X deq); size_t cdeq_X_size(cdeq_X deq); @@ -119,7 +119,7 @@ int main() { c_foreach (i, cdeq_i, q) printf(" %d", *i.ref); puts(""); - cdeq_i_del(&q); + cdeq_i_drop(&q); } ``` Output: diff --git a/docs/clist_api.md b/docs/clist_api.md index b45108a7..0817737b 100644 --- a/docs/clist_api.md +++ b/docs/clist_api.md @@ -24,10 +24,10 @@ See the c++ class [std::list](https://en.cppreference.com/w/cpp/container/list) ```c #define i_val // value: REQUIRED #define i_cmp // three-way compare two i_valraw* : REQUIRED IF i_valraw is a non-integral type -#define i_del // destroy value func - defaults to empty destruct +#define i_drop // destroy value func - defaults to empty destruct #define i_valraw // convertion "raw" type - defaults to i_val -#define i_valfrom // convertion func i_valraw => i_val - defaults to plain copy #define i_valto // convertion func i_val* => i_valraw - defaults to plain copy +#define i_from // convertion func i_valraw => i_val - defaults to plain copy #define i_tag // defaults to i_val #include <stc/clist.h> ``` @@ -42,7 +42,7 @@ clist_X clist_X_clone(clist_X list); void clist_X_clear(clist_X* self); void clist_X_copy(clist_X* self, clist_X other); -void clist_X_del(clist_X* self); // destructor +void clist_X_drop(clist_X* self); // destructor bool clist_X_empty(clist_X list); size_t clist_X_count(clist_X list); // size() in O(n) time @@ -125,7 +125,7 @@ int main() { c_foreach (i, clist_d, list) printf(" %g", *i.ref); - clist_d_del(&list); + clist_d_drop(&list); } ``` Output: @@ -161,7 +161,7 @@ int main () c_foreach (x, clist_i, L) printf(" %d", *x.ref); puts(""); - clist_i_del(&L); + clist_i_drop(&L); } ``` Output: diff --git a/docs/cmap_api.md b/docs/cmap_api.md index 0fec365c..e2cb668f 100644 --- a/docs/cmap_api.md +++ b/docs/cmap_api.md @@ -21,13 +21,13 @@ See the c++ class [std::unordered_map](https://en.cppreference.com/w/cpp/contain #define i_val // value: REQUIRED #define i_cmp // three-way compare two i_keyraw*: REQUIRED IF i_keyraw is non-integral type #define i_equ // equality comparison two i_keyraw*: ALTERNATIVE to i_cmp -#define i_keydel // destroy key func - defaults to empty destruct +#define i_keydrop // destroy key func - defaults to empty destruct #define i_keyraw // convertion "raw" type - defaults to i_key -#define i_keyfrom // convertion func i_keyraw => i_key - defaults to plain copy +#define i_keyfrom // convertion func i_keyraw => i_key - defaults to plain copy #define i_keyto // convertion func i_key* => i_keyraw - defaults to plain copy -#define i_valdel // destroy value func - defaults to empty destruct +#define i_valdrop // destroy value func - defaults to empty destruct #define i_valraw // convertion "raw" type - defaults to i_val -#define i_valfrom // convertion func i_valraw => i_val - defaults to plain copy +#define i_valfrom // convertion func i_valraw => i_val - defaults to plain copy #define i_valto // convertion func i_val* => i_valraw - defaults to plain copy #define i_tag // defaults to i_key #include <stc/cmap.h> @@ -47,7 +47,7 @@ void cmap_X_max_load_factor(cmap_X* self, float max_load); bool cmap_X_reserve(cmap_X* self, size_t size); void cmap_X_shrink_to_fit(cmap_X* self); void cmap_X_swap(cmap_X* a, cmap_X* b); -void cmap_X_del(cmap_X* self); // destructor +void cmap_X_drop(cmap_X* self); // destructor size_t cmap_X_size(cmap_X map); size_t cmap_X_capacity(cmap_X map); // buckets * max_load_factor @@ -207,15 +207,15 @@ typedef struct { int x, y, z; } Vec3i; int main() { // Define map with defered destruct - c_autovar (cmap_vi vecs = cmap_vi_init(), cmap_vi_del(&vecs)) + c_autovar (cmap_vi vecs = cmap_vi_init(), cmap_vi_drop(&vecs)) { cmap_vi_insert(&vecs, (Vec3i){100, 0, 0}, 1); cmap_vi_insert(&vecs, (Vec3i){ 0, 100, 0}, 2); cmap_vi_insert(&vecs, (Vec3i){ 0, 0, 100}, 3); cmap_vi_insert(&vecs, (Vec3i){100, 100, 100}, 4); - c_foreach (i, cmap_vi, vecs) - printf("{ %3d, %3d, %3d }: %d\n", i.ref->first.x, i.ref->first.y, i.ref->first.z, i.ref->second); + c_forpair (vec, num, cmap_vi, vecs) + printf("{ %3d, %3d, %3d }: %d\n", _.vec.x, _.vec.y, _.vec.z, _.num); } } ``` @@ -240,15 +240,15 @@ typedef struct { int x, y, z; } Vec3i; int main() { - c_autovar (cmap_iv vecs = cmap_iv_init(), cmap_iv_del(&vecs)) + c_auto (cmap_iv, vecs) // shorthand for c_autovar with _init(), _drop(). { cmap_iv_insert(&vecs, 1, (Vec3i){100, 0, 0}); cmap_iv_insert(&vecs, 2, (Vec3i){ 0, 100, 0}); cmap_iv_insert(&vecs, 3, (Vec3i){ 0, 0, 100}); cmap_iv_insert(&vecs, 4, (Vec3i){100, 100, 100}); - c_foreach (i, cmap_iv, vecs) - printf("%d: { %3d, %3d, %3d }\n", i.ref->first, i.ref->second.x, i.ref->second.y, i.ref->second.z); + c_forpair (num, vec, cmap_iv, vecs) + printf("%d: { %3d, %3d, %3d }\n", _.num_, _.vec.x, _.vec.y, _.vec.z); } } ``` @@ -270,45 +270,56 @@ typedef struct { cstr country; } Viking; -static bool Viking_equalto(const Viking* a, const Viking* b) { +#define Viking_init() ((Viking){cstr_null, cstr_null}) + +static inline bool RViking_equalto(const Viking* a, const Viking* b) { return cstr_equals_s(a->name, b->name) && cstr_equals_s(a->country, b->country); } -static uint32_t Viking_hash(const Viking* a, int ignored) { +static inline uint32_t RViking_hash(const Viking* a, int ignored) { return c_strhash(a->name.str) ^ (c_strhash(a->country.str) >> 15); } -static void Viking_del(Viking* v) { - c_del(cstr, &v->name, &v->country); +static inline Viking Viking_clone(Viking v) { + v.name = cstr_clone(v.name); + v.country = cstr_clone(v.country); +} + +void inline Viking_drop(Viking* vk) { + cstr_drop(&vk->name); + cstr_drop(&vk->country); } -#define i_key Viking +#define i_type Vikings +#define i_key_bind Viking #define i_val int -#define i_equ Viking_equalto -#define i_hash Viking_hash -#define i_del Viking_del -#define i_tag vk +// i_key_bind auto-binds: +// #define i_equ RViking_equalto +// #define i_hash RViking_hash +// #define i_keyfrom Viking_clone +// #define i_drop Viking_drop #include <stc/cmap.h> int main() { // Use a HashMap to store the vikings' health points. - cmap_vk vikings = cmap_vk_init(); - - cmap_vk_insert(&vikings, (Viking){cstr_new("Einar"), cstr_new("Norway")}, 25); - cmap_vk_insert(&vikings, (Viking){cstr_new("Olaf"), cstr_new("Denmark")}, 24); - cmap_vk_insert(&vikings, (Viking){cstr_new("Harald"), cstr_new("Iceland")}, 12); - cmap_vk_insert(&vikings, (Viking){cstr_new("Einar"), cstr_new("Denmark")}, 21); - - Viking lookup = (Viking){cstr_new("Einar"), cstr_new("Norway")}; - printf("Lookup: Einar of Norway has %d hp\n\n", *cmap_vk_at(&vikings, lookup)); - Viking_del(&lookup); - - // Print the status of the vikings. - c_foreach (i, cmap_vk, vikings) { - printf("%s of %s has %d hp\n", i.ref->first.name.str, i.ref->first.country.str, i.ref->second); + c_auto (Vikings, vikings) // uses Vikings_init(), Vikings_drop() + { + Vikings_insert(&vikings, (Viking){cstr_new("Einar"), cstr_new("Norway")}, 25); + Vikings_insert(&vikings, (Viking){cstr_new("Olaf"), cstr_new("Denmark")}, 24); + Vikings_insert(&vikings, (Viking){cstr_new("Harald"), cstr_new("Iceland")}, 12); + Vikings_insert(&vikings, (Viking){cstr_new("Einar"), cstr_new("Denmark")}, 21); + + c_auto (Viking, lookup) { + lookup = (Viking){cstr_new("Einar"), cstr_new("Norway")}; + printf("Lookup: Einar of Norway has %d hp\n\n", *Vikings_at(&vikings, lookup)); + } + + // Print the status of the vikings. + c_forpair (viking, hp, Vikings, vikings) { + printf("%s of %s has %d hp\n", _.viking.name.str, _.viking.country.str, _.hp); + } } - cmap_vk_del(&vikings); } ``` Output: @@ -326,65 +337,65 @@ to add "raw" type entries (otherwise compile error): ```c #include <stc/cstr.h> -typedef struct { +typedef struct Viking { cstr name; cstr country; } Viking; -static void Viking_del(Viking* v) { - c_del(cstr, &v->name, &v->country); +static inline void Viking_drop(Viking* v) { + c_drop(cstr, &v->name, &v->country); } -// Define a "raw" type that does not need allocations. -// Define equals, hash, fromraw, toraw functions: +// Define Viking raw struct with hash, equalto, and convertion functions between Viking and RViking structs: -typedef struct { +typedef struct RViking { const char* name; const char* country; } RViking; -static bool RViking_equalto(const RViking* r1, const RViking* r2) - { return !strcmp(r1->name, r2->name) && !strcmp(r1->country, r2->country); } - -static uint32_t RViking_hash(const RViking* r, int ignored) - { return c_strhash(r->name) ^ (c_strhash(r->country) >> 15); } - -static Viking Viking_fromR(RViking r) - { return (Viking){cstr_from(r.name), cstr_from(r.country)}; } +static inline uint64_t RViking_hash(const RViking* raw, size_t ignore) { + uint64_t hash = c_strhash(raw->name) ^ (c_strhash(raw->country) >> 15); + return hash; +} +static inline bool RViking_equalto(const RViking* rx, const RViking* ry) { + return strcmp(rx->name, ry->name) == 0 && strcmp(rx->country, ry->country) == 0; +} -static RViking Viking_toR(const Viking* v) - { return (RViking){v->name.str, v->country.str}; } +static inline Viking Viking_from(RViking raw) { + return (Viking){cstr_from(raw.name), cstr_from(raw.country)}; +} +static inline RViking Viking_toraw(const Viking* vk) { + return (RViking){vk->name.str, vk->country.str}; +} -#define i_key Viking -#define i_val int -#define i_keydel Viking_del -#define i_keyraw RViking -#define i_equ RViking_equalto -#define i_hash RViking_hash -#define i_keyfrom Viking_fromR -#define i_keyto Viking_toR -#define i_tag vk +// With this in place, we define the Viking => int hash map type: +#define i_type Vikings +#define i_key_bind Viking +#define i_val int +#define i_keyraw RViking +// i_key_bind macro will make these functions auto-bind: +// #define i_hash RViking_hash +// #define i_equ RViking_equalto +// #define i_keyfrom Viking_from // uses _from because i_keyraw is defined +// #define i_keyto Viking_toraw +// #define i_keydrop Viking_drop #include <stc/cmap.h> int main() { - c_auto (cmap_vk, vikings) // RAII - { - // Insert works as before, takes a constructed Viking object - cmap_vk_insert(&vikings, (Viking){cstr_new("Einar"), cstr_new("Norway")}, 25); - cmap_vk_insert(&vikings, (Viking){cstr_new("Olaf"), cstr_new("Denmark")}, 24); - - // Emplace is simpler to use now - takes rawkey argument - cmap_vk_emplace(&vikings, (RViking){"Harald", "Iceland"}, 12); - cmap_vk_emplace(&vikings, (RViking){"Einar", "Denmark"}, 21); + c_auto (Vikings, vikings) { + c_apply_pair(Vikings, emplace, &vikings, { + {{"Einar", "Norway"}, 20}, + {{"Olaf", "Denmark"}, 24}, + {{"Harald", "Iceland"}, 12}, + }); + Vikings_emplace_or_assign(&vikings, (RViking){"Bjorn", "Sweden"}, 10); - // Lookup also uses rawkey args, no need construct/destruct key: - printf("Lookup: Einar of Norway has %d hp\n\n", *cmap_vk_at(&vikings, (RViking){"Einar", "Norway"})); + Vikings_value *einar = Vikings_find(&vikings, (RViking){"Einar", "Norway"}).ref; + if (einar) einar->second += 3; // add 3 hp points to Einar - // Print the status of the vikings. - c_foreach (i, cmap_vk, vikings) { - printf("%s of %s has %d hp\n", i.ref->first.name.str, - i.ref->first.country.str, i.ref->second); + c_forpair (viking, health, Vikings, vikings) { + printf("%s of %s has %d hp\n", _.viking.name.str, _.viking.country.str, _.health); } } } diff --git a/docs/cpque_api.md b/docs/cpque_api.md index 5c0c9ec4..e891b7d2 100644 --- a/docs/cpque_api.md +++ b/docs/cpque_api.md @@ -12,7 +12,7 @@ See the c++ class [std::priority_queue](https://en.cppreference.com/w/cpp/contai ```c #define i_val // value: REQUIRED #define i_cmp // three-way compare two i_val* : REQUIRED IF i_val is a non-integral type -#define i_del // destroy value func - defaults to empty destruct +#define i_drop // destroy value func - defaults to empty destruct #define i_valfrom // convertion func i_val => i_val - defaults to plain copy #define i_tag // defaults to i_val #include <stc/cpque.h> @@ -29,7 +29,7 @@ void cpque_X_clear(cpque_X* self); bool cpque_X_reserve(cpque_X* self, size_t n); void cpque_X_shrink_to_fit(cpque_X* self); void cpque_X_copy(cpque_X* self, cpque_X other); -void cpque_X_del(cpque_X* self); // destructor +void cpque_X_drop(cpque_X* self); // destructor size_t cpque_X_size(cpque_X pq); bool cpque_X_empty(cpque_X pq); @@ -59,7 +59,7 @@ cpque_X_value cpque_X_value_clone(cpque_X_value val); #include <stdio.h> #define i_val int64_t -#define i_cmp -c_default_compare // min-heap +#define i_cmp -c_default_cmp // min-heap #define i_tag i #include <stc/cpque.h> @@ -69,7 +69,7 @@ int main() stc64_t rng = stc64_init(1234); stc64_uniform_t dist = stc64_uniform_init(0, N * 10); - // Declare heap, with defered del() + // Declare heap, with defered drop() c_auto (cpque_i, heap) { // Push ten million random numbers to priority queue, plus some negative ones. diff --git a/docs/cqueue_api.md b/docs/cqueue_api.md index 469b7c99..9fd2c4e0 100644 --- a/docs/cqueue_api.md +++ b/docs/cqueue_api.md @@ -9,7 +9,7 @@ See the c++ class [std::queue](https://en.cppreference.com/w/cpp/container/queue ```c #define i_val // value: REQUIRED #define i_cmp // three-way compare two i_valraw* : REQUIRED IF i_valraw is a non-integral type -#define i_del // destroy value func - defaults to empty destruct +#define i_drop // destroy value func - defaults to empty destruct #define i_valraw // convertion "raw" type - defaults to i_val #define i_valfrom // convertion func i_valraw => i_val - defaults to plain copy #define i_valto // convertion func i_val* => i_valraw - defaults to plain copy @@ -27,7 +27,7 @@ cqueue_X cqueue_X_clone(cqueue_X q); void cqueue_X_clear(cqueue_X* self); void cqueue_X_copy(cqueue_X* self, cqueue_X other); -void cqueue_X_del(cqueue_X* self); // destructor +void cqueue_X_drop(cqueue_X* self); // destructor size_t cqueue_X_size(cqueue_X q); bool cqueue_X_empty(cqueue_X q); @@ -76,7 +76,7 @@ int main() { c_foreach (i, cqueue_i, Q) printf(" %d", *i.ref); - cqueue_i_del(&Q); + cqueue_i_drop(&Q); } ``` Output: diff --git a/docs/crandom_api.md b/docs/crandom_api.md index 3dd829aa..b099eeaf 100644 --- a/docs/crandom_api.md +++ b/docs/crandom_api.md @@ -108,8 +108,8 @@ int main() } } // Cleanup - cstr_del(&bar); - csmap_i_del(&mhist); + cstr_drop(&bar); + csmap_i_drop(&mhist); } ``` Output: diff --git a/docs/cset_api.md b/docs/cset_api.md index 4883664d..b70f5fb9 100644 --- a/docs/cset_api.md +++ b/docs/cset_api.md @@ -11,10 +11,10 @@ A **cset** is an associative container that contains a set of unique objects of #define i_hash // hash func: REQUIRED IF i_keyraw is a non-pod type
#define i_cmp // three-way compare two i_keyraw*: REQUIRED IF i_keyraw is a non-integral type
#define i_equ // equality comparison two i_keyraw*: ALTERNATIVE to i_cmp
-#define i_del // destroy key func - defaults to empty destruct
+#define i_drop // destroy key func - defaults to empty destruct
#define i_keyraw // convertion "raw" type - defaults to i_key
-#define i_keyfrom // convertion func i_keyraw => i_key - defaults to plain copy
-#define i_keyto // convertion func i_key* => i_keyraw - defaults to plain copy
+#define i_keyfrom // convertion func i_keyraw => i_key - defaults to plain copy
+#define i_keyto // convertion func i_key* => i_keyraw - defaults to plain copy
#define i_tag // defaults to i_key
#include <stc/cset.h>
```
@@ -33,7 +33,7 @@ void cset_X_max_load_factor(cset_X* self, float max_load); bool cset_X_reserve(cset_X* self, size_t size);
void cset_X_shrink_to_fit(cset_X* self);
void cset_X_swap(cset_X* a, cset_X* b);
-void cset_X_del(cset_X* self); // destructor
+void cset_X_drop(cset_X* self); // destructor
size_t cset_X_size(cset_X set); // num. of allocated buckets
size_t cset_X_capacity(cset_X set); // buckets * max_load_factor
diff --git a/docs/csmap_api.md b/docs/csmap_api.md index 646f8c99..88d58cd4 100644 --- a/docs/csmap_api.md +++ b/docs/csmap_api.md @@ -18,11 +18,11 @@ See the c++ class [std::map](https://en.cppreference.com/w/cpp/container/map) fo #define i_key // key: REQUIRED #define i_val // value: REQUIRED #define i_cmp // three-way compare two i_keyraw* : REQUIRED IF i_keyraw is a non-integral type -#define i_keydel // destroy key func - defaults to empty destruct +#define i_keydrop // destroy key func - defaults to empty destruct #define i_keyraw // convertion "raw" type - defaults to i_key #define i_keyfrom // convertion func i_keyraw => i_key - defaults to plain copy #define i_keyto // convertion func i_key* => i_keyraw - defaults to plain copy -#define i_valdel // destroy value func - defaults to empty destruct +#define i_valdrop // destroy value func - defaults to empty destruct #define i_valraw // convertion "raw" type - defaults to i_val #define i_valfrom // convertion func i_valraw => i_val - defaults to plain copy #define i_valto // convertion func i_val* => i_valraw - defaults to plain copy @@ -40,12 +40,12 @@ csmap_X csmap_X_clone(csmap_x map); void csmap_X_clear(csmap_X* self); void csmap_X_copy(csmap_X* self, csmap_X other); void csmap_X_swap(csmap_X* a, csmap_X* b); -void csmap_X_del(csmap_X* self); // destructor +void csmap_X_drop(csmap_X* self); // destructor size_t csmap_X_size(csmap_X map); bool csmap_X_empty(csmap_X map); -const csmap_X_mapped* csmap_X_at(const csmap_X* self, i_keyraw rkey); // rkey must be in map. +csmap_X_mapped* csmap_X_at(const csmap_X* self, i_keyraw rkey); // rkey must be in map. const csmap_X_value* csmap_X_get(const csmap_X* self, i_keyraw rkey); // return NULL if not found csmap_X_value* csmap_X_get_mut(csmap_X* self, i_keyraw rkey); // mutable get bool csmap_X_contains(const csmap_X* self, i_keyraw rkey); @@ -144,7 +144,7 @@ int main() { uint32_t col = 0xcc7744ff; csmap_id idnames = csmap_id_init(); - c_autodefer (csmap_id_del(&idnames)) + c_autodefer (csmap_id_drop(&idnames)) { c_apply_pair(csmap_id, emplace, &idnames, { {100, "Red"}, @@ -174,7 +174,7 @@ Demonstrate csmap with plain-old-data key type Vec3i and int as mapped type: csm ```c typedef struct { int x, y, z; } Vec3i; -static int Vec3i_compare(const Vec3i* a, const Vec3i* b) { +static int Vec3i_cmp(const Vec3i* a, const Vec3i* b) { int c; if ((c = a->x - b->x) != 0) return c; if ((c = a->y - b->y) != 0) return c; @@ -183,7 +183,7 @@ static int Vec3i_compare(const Vec3i* a, const Vec3i* b) { #define i_key Vec3i #define i_val int -#define i_cmp Vec3i_compare // uses c_default_hash +#define i_cmp Vec3i_cmp // uses c_default_hash #define i_tag vi #include <stc/csmap.h> #include <stdio.h> @@ -224,7 +224,7 @@ typedef struct { int x, y, z; } Vec3i; int main() { // equivalent to: c_auto (csmap_iv, vecs) - c_autovar (csmap_iv vecs = csmap_iv_init(), csmap_iv_del(&vecs)) + c_autovar (csmap_iv vecs = csmap_iv_init(), csmap_iv_drop(&vecs)) { csmap_iv_insert(&vecs, 1, (Vec3i){100, 0, 0}); csmap_iv_insert(&vecs, 2, (Vec3i){0, 100, 0}); diff --git a/docs/csptr_api.md b/docs/csptr_api.md index 640f5cf1..5cbe746f 100644 --- a/docs/csptr_api.md +++ b/docs/csptr_api.md @@ -1,19 +1,19 @@ -# STC [csptr](../include/stc/csptr.h): Shared Pointers +# STC [csptr](../include/stc/csptr.h): Atomic Reference Counted **csptr** is a smart pointer that retains shared ownership of an object through a pointer. Several **csptr** objects may own the same object. The object is destroyed and its memory -deallocated when the last remaining **csptr** owning the object is destroyed with *csptr_X_del()*; +deallocated when the last remaining **csptr** owning the object is destroyed with *csptr_X_drop()*; -The object is destroyed using *csptr_X_del()*. A **csptr** may also own no objects, in which -case it is called empty. The *csptr_X_compare()*, *csptr_X_del()* methods are defined based on -the `i_cmp` and `i_valdel` macros specified. Use *csptr_X_clone(p)* when sharing ownership of +The object is destroyed using *csptr_X_drop()*. A **csptr** may also own no objects, in which +case it is called empty. The *csptr_X_cmp()*, *csptr_X_drop()* methods are defined based on +the `i_cmp` and `i_valdrop` macros specified. Use *csptr_X_clone(p)* when sharing ownership of the pointed-to object. All **csptr** functions can be called by multiple threads on different instances of **csptr** without additional synchronization even if these instances are copies and share ownership of the same object. -**csptr** uses thread-safe atomic reference counting, through the *csptr_X_clone()* and *csptr_X_del()* methods. +**csptr** uses thread-safe atomic reference counting, through the *csptr_X_clone()* and *csptr_X_drop()* methods. -When declaring a container with shared pointers, define `i_val_ref` as the csptr type, see example. +When declaring a container with shared pointers, define `i_val_bind` as the csptr type, see example. For containers, make sure to pass the result of create functions *csptr_X_new()* **only** to *insert()*, *push_back()*, and *push()* functions. Use *emplace()* method for sharing existing **csptr**s between @@ -26,7 +26,7 @@ See similar c++ class [std::shared_ptr](https://en.cppreference.com/w/cpp/memory ```c #define i_val // value: REQUIRED #define i_cmp // three-way compare two i_val* : REQUIRED IF i_val is a non-integral type -#define i_del // destroy value func - defaults to empty destruct +#define i_drop // destroy value func - defaults to empty destruct #define i_tag // defaults to i_val #define i_opt c_no_atomic // Non-atomic reference counting, like Rust Rc. #include <stc/csptr.h> @@ -35,34 +35,34 @@ 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_rawval 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_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_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_del(csptr_X* self); // destruct (decrease use count, free at 0) +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_rawval raw); // make and assign new csptr from raw value. +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. -int csptr_X_compare(const csptr_X* x, const csptr_X* y); // compares pointer addresses if 'i_opt c_no_compare' - // is defined. Otherwise uses 'i_cmp' or default compare. +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. ``` ## 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 | +| 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 @@ -73,24 +73,24 @@ int csptr_X_compare(const csptr_X* x, const csptr_X* y); // compares poi #define i_type Map #define i_key_str // strings #define i_val int -#define i_keydel(p) (printf("del name: %s\n", (p)->str), cstr_del(p)) +#define i_keydrop(p) (printf("drop name: %s\n", (p)->str), cstr_drop(p)) #include <stc/csmap.h> #define i_type Arc // (atomic) ref. counted type #define i_val Map #define i_from Map_clone -#define i_del(p) (printf("del Arc:\n"), Map_del(p)) +#define i_drop(p) (printf("drop Arc:\n"), Map_drop(p)) // no comparison of Maps needed (or available), and // no need for atomic ref. count in single thread: -#define i_opt c_no_compare|c_no_atomic +#define i_opt c_no_cmp|c_no_atomic #include <stc/csptr.h> #define i_type Stack -#define i_val_ref Arc // define i_val_ref for csptr / cbox value, not i_val +#define i_val_bind Arc // define i_val_bind for csptr / cbox value, not i_val #include <stc/cstack.h> #define i_type List -#define i_val_ref Arc // as above +#define i_val_bind Arc // as above #include <stc/clist.h> int main() @@ -155,22 +155,22 @@ LIST Joanna:1992 Joey:1990 Mary:1995 Brad:1999 Jack:1980 Rosanna:2001 SHARED:2021 Brad:1999 CLONED:2021 Jack:1980 Rosanna:2001 -del Arc: -del name: Rick -del name: Tracy -del name: Steve -del Arc: -del name: CLONED -del name: Brad -del name: Rosanna -del name: Jack -del Arc: -del name: Brad -del name: SHARED -del name: Rosanna -del name: Jack -del Arc: -del name: Joanna -del name: Mary -del name: Joey +drop Arc: +drop name: Rick +drop name: Tracy +drop name: Steve +drop Arc: +drop name: CLONED +drop name: Brad +drop name: Rosanna +drop name: Jack +drop Arc: +drop name: Brad +drop name: SHARED +drop name: Rosanna +drop name: Jack +drop Arc: +drop name: Joanna +drop name: Mary +drop name: Joey ``` diff --git a/docs/csset_api.md b/docs/csset_api.md index 501473a6..c5cd0f88 100644 --- a/docs/csset_api.md +++ b/docs/csset_api.md @@ -10,10 +10,10 @@ See the c++ class [std::set](https://en.cppreference.com/w/cpp/container/set) fo ```c
#define i_key // key: REQUIRED
#define i_cmp // three-way compare two i_keyraw* : REQUIRED IF i_keyraw is a non-integral type
-#define i_del // destroy key func - defaults to empty destruct
+#define i_drop // destroy key func - defaults to empty destruct
#define i_keyraw // convertion "raw" type - defaults to i_key
-#define i_keyfrom // convertion func i_keyraw => i_key - defaults to plain copy
-#define i_keyto // convertion func i_key* => i_keyraw - defaults to plain copy
+#define i_keyfrom // convertion func i_keyraw => i_key - defaults to plain copy
+#define i_keyto // convertion func i_key* => i_keyraw - defaults to plain copy
#define i_tag // defaults to i_key
#include <stc/csset.h>
```
@@ -28,7 +28,7 @@ csset_X csset_X_clone(csset_x set); void csset_X_clear(csset_X* self);
void csset_X_copy(csset_X* self, csset_X other);
void csset_X_swap(csset_X* a, csset_X* b);
-void csset_X_del(csset_X* self); // destructor
+void csset_X_drop(csset_X* self); // destructor
size_t csset_X_size(csset_X set);
bool csset_X_empty(csset_X set);
@@ -59,8 +59,8 @@ csset_X_value csset_X_value_clone(csset_X_value val); | Type name | Type definition | Used to represent... |
|:-------------------|:--------------------------------------------------|:----------------------------|
| `csset_X` | `struct { ... }` | The csset type |
-| `csset_X_rawkey` | `i_rawkey` | The raw key type |
-| `csset_X_rawvalue` | `i_rawkey` | The raw key type |
+| `csset_X_rawkey` | `i_keyraw` | The raw key type |
+| `csset_X_rawvalue` | `i_keyraw` | The raw key type |
| `csset_X_key` | `i_key` | The key type |
| `csset_X_value` | `i_key ` | The value: key is immutable |
| `csset_X_result` | `struct { csset_X_value* ref; bool inserted; }` | Result of insert/emplace |
diff --git a/docs/cstack_api.md b/docs/cstack_api.md index e71b213f..b6992dc0 100644 --- a/docs/cstack_api.md +++ b/docs/cstack_api.md @@ -10,7 +10,7 @@ See the c++ class [std::stack](https://en.cppreference.com/w/cpp/container/stack ```c #define i_val // value: REQUIRED #define i_cmp // three-way compare two i_valraw* : REQUIRED IF i_valraw is a non-integral type -#define i_del // destroy value func - defaults to empty destruct +#define i_drop // destroy value func - defaults to empty destruct #define i_valraw // convertion "raw" type - defaults to i_val #define i_valfrom // convertion func i_valraw => i_val - defaults to plain copy #define i_valto // convertion func i_val* => i_valraw - defaults to plain copy @@ -31,7 +31,7 @@ void cstack_X_clear(cstack_X* self); bool cstack_X_reserve(cstack_X* self, size_t n); void cstack_X_shrink_to_fit(cstack_X* self); void cstack_X_copy(cstack_X* self, cstack_X other); -void cstack_X_del(cstack_X* self); // destructor +void cstack_X_drop(cstack_X* self); // destructor size_t cstack_X_size(cstack_X st); size_t cstack_X_capacity(cstack_X st); @@ -81,7 +81,7 @@ int main() { printf("top: %d\n", *cstack_i_top(&S)); - cstack_i_del(&S); + cstack_i_drop(&S); } ``` Output: diff --git a/docs/cstr_api.md b/docs/cstr_api.md index e54b3a4c..463a723c 100644 --- a/docs/cstr_api.md +++ b/docs/cstr_api.md @@ -26,7 +26,7 @@ cstr cstr_clone(cstr s); cstr* cstr_take(cstr* self, cstr s); // take the constructed or moved string cstr cstr_move(cstr* self); // move string to caller, leave empty string -void cstr_del(cstr *self); // destructor +void cstr_drop(cstr *self); // destructor const char* cstr_str(const cstr* self); // self->str char* cstr_data(cstr* self); // self->str @@ -85,13 +85,14 @@ Note that all methods with arguments `(..., const char* str, size_t n)`, `n` mus #### Helper methods: ```c -int cstr_compare(const cstr *s1, const cstr *s2); +int cstr_cmp(const cstr *s1, const cstr *s2); bool cstr_equalto(const cstr *s1, const cstr *s2); bool cstr_hash(const cstr *s, ...); -int c_rawstr_compare(const char** x, const char** y); -bool c_rawstr_equalto(const char** x, const char** y); -uint64_t c_rawstr_hash(const char* const* x, ...); +typedef const char* c_rawstr; +int c_rawstr_cmp(const c_rawstr* x, const c_rawstr* y); +bool c_rawstr_equalto(const c_rawstr* x, const c_rawstr* y); +uint64_t c_rawstr_hash(const c_rawstr* x, size_t dummy); uint64_t c_strhash(const char* str); char* c_strnstrn(const char* str, const char* needle, size_t slen, size_t nlen); @@ -141,7 +142,7 @@ int main() { cstr full_path = cstr_from_fmt("%s/%s.%s", "directory", "filename", "ext"); printf("%s\n", full_path.str); - c_del(cstr, &s0, &s1, &full_path); + c_drop(cstr, &s0, &s1, &full_path); } ``` Output: diff --git a/docs/csview_api.md b/docs/csview_api.md index 7daa67ee..609c5d10 100644 --- a/docs/csview_api.md +++ b/docs/csview_api.md @@ -77,7 +77,7 @@ bool cstr_ends_with_v(cstr s, csview sub); ``` #### Helper methods ```c -int csview_compare(const csview* x, const csview* y); +int csview_cmp(const csview* x, const csview* y); bool csview_equalto(const csview* x, const csview* y); uint64_t csview_hash(const csview* x, size_t dummy); ``` @@ -118,7 +118,7 @@ int main () cstr s3 = cstr_from_v(cstr_substr(s1, 0, 6)); // "Apples" printf("%s %s\n", s2, s3.str); - c_del(cstr, &str1, &s1, &s2, &s3); + c_drop(cstr, &str1, &s1, &s2, &s3); } ``` Output: @@ -166,7 +166,7 @@ int main() print_split(c_sv("This has no matching separator"), c_sv("xx")); puts(""); - c_autovar (cvec_str v = string_split(c_sv("Split,this,,string,now,"), c_sv(",")), cvec_str_del(&v)) + c_autovar (cvec_str v = string_split(c_sv("Split,this,,string,now,"), c_sv(",")), cvec_str_drop(&v)) c_foreach (i, cvec_str, v) printf("\"%s\"\n", i.ref->str); } diff --git a/docs/cvec_api.md b/docs/cvec_api.md index 00c0498c..b9d4d2d5 100644 --- a/docs/cvec_api.md +++ b/docs/cvec_api.md @@ -14,7 +14,7 @@ See the c++ class [std::vector](https://en.cppreference.com/w/cpp/container/vect ```c #define i_val // value: REQUIRED #define i_cmp // three-way compare two i_valraw* : REQUIRED IF i_valraw is a non-integral type -#define i_del // destroy value func - defaults to empty destruct +#define i_drop // destroy value func - defaults to empty destruct #define i_valraw // convertion "raw" type - defaults to i_val #define i_valfrom // convertion func i_valraw => i_val - defaults to plain copy #define i_valto // convertion func i_val* => i_valraw - defaults to plain copy @@ -23,7 +23,7 @@ See the c++ class [std::vector](https://en.cppreference.com/w/cpp/container/vect ``` `X` should be replaced by the value of `i_tag` in all of the following documentation. -`i_del` may be defined instead of `i_valdel` (or `i_keydel`) for all non-map containers. +`i_drop` may be defined instead of `i_valdrop` (or `i_keydrop`) for all non-map containers. ## Methods @@ -39,7 +39,7 @@ bool cvec_X_reserve(cvec_X* self, size_t cap); bool cvec_X_resize(cvec_X* self, size_t size, i_val null); void cvec_X_shrink_to_fit(cvec_X* self); void cvec_X_swap(cvec_X* a, cvec_X* b); -void cvec_X_del(cvec_X* self); // destructor +void cvec_X_drop(cvec_X* self); // destructor bool cvec_X_empty(cvec_X vec); size_t cvec_X_size(cvec_X vec); @@ -47,7 +47,7 @@ size_t cvec_X_capacity(cvec_X vec); const cvec_X_value* cvec_X_at(const cvec_X* self, size_t idx); const cvec_X_value* cvec_X_get(const cvec_X* self, i_valraw raw); // return NULL if not found -cvec_X_value* cvec_X_get_mut(cvec_X* self, i_valraw raw); // get mutable value +cvec_X_value* cvec_X_get_mut(cvec_X* self, i_valraw raw); // get mutable value cvec_X_iter cvec_X_find(const cvec_X* self, i_valraw raw); cvec_X_iter cvec_X_find_in(cvec_X_iter i1, cvec_X_iter i2, i_valraw raw); cvec_X_iter cvec_X_bsearch(const cvec_X* self, i_valraw raw); @@ -152,13 +152,13 @@ int main() { cstr tmp = cstr_from_fmt("%d elements so far", cvec_str_size(names)); // emplace_back() will not compile if adding a new cstr type. Use push_back(): - cvec_str_push_back(&names, tmp); // tmp is moved to names, do not del() it. + cvec_str_push_back(&names, tmp); // tmp is moved to names, do not drop() it. printf("%s\n", names.data[1].str); // Access the second element c_foreach (i, cvec_str, names) printf("item: %s\n", i.ref->str); - cvec_str_del(&names); + cvec_str_drop(&names); } ``` Output: @@ -179,12 +179,12 @@ typedef struct { int id; } User; -int User_compare(const User* a, const User* b) { +int User_cmp(const User* a, const User* b) { int c = strcmp(a->name.str, b->name.str); return c != 0 ? c : a->id - b->id; } -void User_del(User* self) { - cstr_del(&self->name); +void User_drop(User* self) { + cstr_drop(&self->name); } User User_clone(User user) { user.name = cstr_clone(user.name); @@ -194,9 +194,9 @@ User User_clone(User user) { // Declare a memory managed, clonable vector of users. // Note that cvec_u_emplace_back() will clone input: #define i_val User -#define i_cmp User_compare -#define i_del User_del -#define i_valfrom User_clone +#define i_cmp User_cmp +#define i_drop User_drop +#define i_from User_clone #define i_tag u #include <stc/cvec.h> @@ -209,6 +209,6 @@ int main(void) { c_foreach (i, cvec_u, vec2) printf("%s: %d\n", i.ref->name.str, i.ref->id); - c_del(cvec_u, &vec, &vec2); // cleanup + c_drop(cvec_u, &vec, &vec2); // cleanup } ``` |
