From b0d60c33a073782fe0bb08c835ca9db0b94145d0 Mon Sep 17 00:00:00 2001 From: Tyge Lovset Date: Mon, 18 Apr 2022 12:12:01 +0200 Subject: Removed i_drop and i_from template specifiers to reduce redundancies and add clarity (error given if used): use i_valdrop/i_valfrom and i_keydrop/i_keyfrom instead. Added missing at_mut() function to cdeq. --- README.md | 3 +-- docs/carc_api.md | 6 +++--- docs/carray_api.md | 4 ++-- docs/cbox_api.md | 15 ++++++++------- docs/cdeq_api.md | 2 +- docs/clist_api.md | 4 ++-- docs/cmap_api.md | 2 +- docs/cpque_api.md | 2 +- docs/cqueue_api.md | 2 +- docs/cset_api.md | 2 +- docs/csset_api.md | 2 +- docs/cstack_api.md | 2 +- docs/cvec_api.md | 8 ++++---- examples/arc_containers.c | 2 +- examples/arc_demo.c | 2 +- examples/arcvec_erase.c | 2 +- examples/box2.c | 2 +- examples/complex.c | 4 ++-- examples/mapmap.c | 4 ++-- examples/music_arc.c | 2 +- examples/new_sptr.c | 2 +- examples/rawptr_elements.c | 2 +- include/stc/cdeq.h | 10 +++++----- include/stc/template.h | 20 ++++---------------- 24 files changed, 47 insertions(+), 59 deletions(-) diff --git a/README.md b/README.md index 9b488fb6..0ba24dcb 100644 --- a/README.md +++ b/README.md @@ -110,7 +110,7 @@ In order to include two **cvec**s with different element types, include cvec.h t compare function is required to enable sorting and searching (`<` and `==` operators is default and works for integral types only). Alternatively, `#define i_opt c_no_cmp` to disable methods using comparison. -Similarly, if a destructor `i_drop` is defined, either define a `i_valfrom` construct/clone function +Similarly, if a destructor `i_valdrop` is defined, either define a `i_valfrom` construct/clone function or `#define i_opt c_no_clone` to disable cloning and emplace methods. Unless these requirements are met, compile errors are generated. ```c @@ -297,7 +297,6 @@ Special: - `i_val_str`, `i_val_bind`, `i_val_arcbox` - Similar rules as for *key*. Notes: -- For non-associative containers, `i_drop` and `i_from` may be defined instead of `i_valdrop` and `i_valfrom`. - Instead of defining `i_cmp`, you may define `i_opt c_no_cmp` to disable searching and sorting functions. - Instead of defining `i_*from`, you may define `i_opt c_no_clone` to disable emplace and clone-functions. - If a destructor `i_*drop` is defined, then either `i_*from` or `i_opt c_no_clone` must be defined. diff --git a/docs/carc_api.md b/docs/carc_api.md index c137094d..8fb752f6 100644 --- a/docs/carc_api.md +++ b/docs/carc_api.md @@ -27,7 +27,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_drop // destroy value func - defaults to empty destruct +#define i_valdrop // 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 @@ -80,8 +80,8 @@ bool carc_X_value_eq(const i_val* x, const i_val* y); // cbox_X_value_c #define i_type Arc // (atomic) ref. counted type #define i_val Map -#define i_from Map_clone -#define i_drop(p) (printf("drop Arc:\n"), Map_drop(p)) +#define i_valfrom Map_clone +#define i_valdrop(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_cmp|c_no_atomic diff --git a/docs/carray_api.md b/docs/carray_api.md index 3ac6d650..8c63f381 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_drop // destroy value func - defaults to empty destruct -#define i_from // func Raw => i_val - defaults to plain copy +#define i_valdrop // destroy value func - defaults to empty destruct +#define i_valfrom // 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 diff --git a/docs/cbox_api.md b/docs/cbox_api.md index fe23fa8a..0faea829 100644 --- a/docs/cbox_api.md +++ b/docs/cbox_api.md @@ -5,8 +5,8 @@ be empty. The *cbox_X_cmp()*, *cbox_X_drop()* methods are defined based on the ` 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_bind` to the -cbox type instead of defining `i_val`. This will auto-set `i_drop`, `i_from`, and `i_cmp` using +When declaring a container of **cbox** values, define `i_val_arcbox` with the +cbox type instead of defining `i_val`. This will auto-set `i_valdrop`, `i_valfrom`, 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,9 +20,9 @@ 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_drop // destroy value func - defaults to empty destruct +#define i_valdrop // destroy value func - defaults to empty destruct #define i_valraw // convertion type -#define i_from // create from raw/clone func - REQUIRED if i_drop is defined, +#define i_valfrom // create from raw/clone func - REQUIRED if i_valdrop is defined, // unless 'i_opt c_no_clone' is defined. #define i_valto // to-raw func. #define i_tag // type name tag, defaults to i_val @@ -72,8 +72,8 @@ void int_drop(int* x) { #define i_type IBox #define i_val int -#define i_drop int_drop // optional func, just to display elements destroyed -#define i_from c_default_from // must specify because i_drop was defined. +#define i_valdrop int_drop // optional func, just to display elements destroyed +#define i_valfrom c_default_from // must specify because i_valdrop was defined. #include #define i_type ISet @@ -90,7 +90,8 @@ int main() c_auto (ISet, set) // similar { c_apply(v, IVec_push(&vec, v), IBox, { - IBox_from(2021), IBox_from(2012), IBox_from(2022), IBox_from(2015), + IBox_from(2021), IBox_from(2012), + IBox_from(2022), IBox_from(2015), }); printf("vec:"); diff --git a/docs/cdeq_api.md b/docs/cdeq_api.md index 69a40986..3cd4fece 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_drop // 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 diff --git a/docs/clist_api.md b/docs/clist_api.md index 549cdd9f..cae3e66b 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_drop // 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_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_valfrom // convertion func i_valraw => i_val - defaults to plain copy #define i_tag // defaults to i_val #include ``` diff --git a/docs/cmap_api.md b/docs/cmap_api.md index 69d3d6db..77577bd7 100644 --- a/docs/cmap_api.md +++ b/docs/cmap_api.md @@ -301,7 +301,7 @@ static inline void Viking_drop(Viking* vk) { // #define i_eq Viking_eq // #define i_hash Viking_hash // #define i_keyfrom Viking_clone -// #define i_drop Viking_drop +// #define i_keydrop Viking_drop #include int main() diff --git a/docs/cpque_api.md b/docs/cpque_api.md index 97ac70f5..c6a0fc50 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/i_valraw is a non-integral type -#define i_drop // destroy value func - defaults to empty destruct +#define i_valdrop // destroy value func - defaults to empty destruct #define i_valraw // convertion type #define i_valfrom // convertion func i_valraw => i_val - defaults to plain copy #define i_valto // convertion func i_val* => i_valraw. diff --git a/docs/cqueue_api.md b/docs/cqueue_api.md index 0ecae014..bff0b5ce 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_drop // 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 diff --git a/docs/cset_api.md b/docs/cset_api.md index 4d97b94f..e429c5ae 100644 --- a/docs/cset_api.md +++ b/docs/cset_api.md @@ -10,7 +10,7 @@ A **cset** is an associative container that contains a set of unique objects of #define i_key // hash key: REQUIRED. #define i_hash // hash func: REQUIRED IF i_keyraw is a non-pod type. #define i_eq // equality comparison two i_keyraw*: !i_cmp will be used if not defined. -#define i_drop // destroy key func - defaults to empty destruct +#define i_valdrop // 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 diff --git a/docs/csset_api.md b/docs/csset_api.md index 76b26eee..0ce8e811 100644 --- a/docs/csset_api.md +++ b/docs/csset_api.md @@ -10,7 +10,7 @@ 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_drop // destroy key func - defaults to empty destruct +#define i_valdrop // 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 diff --git a/docs/cstack_api.md b/docs/cstack_api.md index 299ea1c6..8fc5fe8b 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_drop // 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 diff --git a/docs/cvec_api.md b/docs/cvec_api.md index 660f7a67..e02a944e 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_drop // 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 @@ -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_drop` may be defined instead of `i_valdrop` (or `i_keydrop`) for all non-map containers. +`i_valdrop` may be defined instead of `i_valdrop` (or `i_keydrop`) for all non-map containers. ## Methods @@ -202,8 +202,8 @@ User User_clone(User user) { // Note that cvec_u_emplace_back() will clone input: #define i_val User #define i_cmp User_cmp -#define i_drop User_drop -#define i_from User_clone +#define i_valdrop User_drop +#define i_valfrom User_clone #define i_tag u #include diff --git a/examples/arc_containers.c b/examples/arc_containers.c index 53297270..cd049c4d 100644 --- a/examples/arc_containers.c +++ b/examples/arc_containers.c @@ -8,7 +8,7 @@ #define i_type Arc // (atomic) ref. counted type #define i_val Map -#define i_drop(p) (printf("drop Arc:\n"), Map_drop(p)) +#define i_valdrop(p) (printf("drop Arc:\n"), Map_drop(p)) // no need for atomic ref. count in single thread: // no compare function available for csmap: #define i_opt c_no_atomic|c_no_cmp diff --git a/examples/arc_demo.c b/examples/arc_demo.c index 036b7a00..12f81b8d 100644 --- a/examples/arc_demo.c +++ b/examples/arc_demo.c @@ -10,7 +10,7 @@ void int_drop(int* x) { #define i_type iref // set type name to be defined (instead of 'carc_int') #define i_val int -#define i_drop int_drop // optional, just to display the elements destroyed +#define i_valdrop int_drop // optional, just to display the elements destroyed #include // iref #define i_key_arcbox iref // note: use i_key_bind instead of i_key for carc/cbox elements diff --git a/examples/arcvec_erase.c b/examples/arcvec_erase.c index 12547f6c..10d8d4c0 100644 --- a/examples/arcvec_erase.c +++ b/examples/arcvec_erase.c @@ -4,7 +4,7 @@ void show_drop(int* x) { printf("drop: %d\n", *x); } #define i_type Arc #define i_val int -#define i_drop show_drop +#define i_valdrop show_drop // carc/cbox will use pointer address comparison of i_val // if 'i_opt c_no_cmp' is defined, otherwise i_cmp is used // to compare object values. See the two differences by diff --git a/examples/box2.c b/examples/box2.c index 68a9ccba..6d118e2c 100644 --- a/examples/box2.c +++ b/examples/box2.c @@ -27,7 +27,7 @@ struct { // Box in box: #define i_val_arcbox cbox_Point // NB: use i_val_arcbox when value is a cbox or carc! - // it will auto-set i_drop, i_from, i_cmp for you. + // it will auto-set i_valdrop, i_valfrom, i_cmp for you. #define i_opt c_no_cmp #define i_tag BoxPoint #include // cbox_BoxPoint diff --git a/examples/complex.c b/examples/complex.c index 9ccd2dae..ef7f711f 100644 --- a/examples/complex.c +++ b/examples/complex.c @@ -4,8 +4,8 @@ void check_drop(float* v) {printf("destroy %g\n", *v);} #define i_type FloatStack #define i_val float -#define i_drop check_drop -#define i_from c_default_from +#define i_valdrop check_drop +#define i_valfrom c_default_from #include #define i_type StackList diff --git a/examples/mapmap.c b/examples/mapmap.c index 7faaffd1..94b92a81 100644 --- a/examples/mapmap.c +++ b/examples/mapmap.c @@ -16,8 +16,8 @@ // Shorthand for: //#define i_val People_value //#define i_cmp People_value_cmp -//#define i_from People_value_clone -//#define i_drop People_value_drop +//#define i_valfrom People_value_clone +//#define i_valdrop People_value_drop #include void add(Departments* deps, const char* name, const char* email, const char* dep) diff --git a/examples/music_arc.c b/examples/music_arc.c index b8525022..e36477f2 100644 --- a/examples/music_arc.c +++ b/examples/music_arc.c @@ -19,7 +19,7 @@ void Song_drop(Song* s) { #define i_type SongPtr #define i_val Song -#define i_drop Song_drop +#define i_valdrop Song_drop #define i_opt c_no_cmp #include diff --git a/examples/new_sptr.c b/examples/new_sptr.c index 2fb058df..2c2c5d8a 100644 --- a/examples/new_sptr.c +++ b/examples/new_sptr.c @@ -22,7 +22,7 @@ void Person_drop(Person* p) { // ... #define i_type SPtr #define i_val int -#define i_drop(x) printf("drop: %d\n", *(x)) +#define i_valdrop(x) printf("drop: %d\n", *(x)) #include #define i_val_arcbox SPtr diff --git a/examples/rawptr_elements.c b/examples/rawptr_elements.c index 8f7dd4de..b0878941 100644 --- a/examples/rawptr_elements.c +++ b/examples/rawptr_elements.c @@ -16,8 +16,8 @@ struct { double x, y; } typedef Point; // Map of int64 pointers: For fun, define valraw as int64_t for easy emplace call! typedef int64_t inttype; #define i_key_str -#define i_valraw inttype #define i_val inttype* +#define i_valraw inttype #define i_valdrop(x) c_free(*(x)) #define i_valfrom(raw) c_new(inttype, raw) #define i_valto(x) **(x) diff --git a/include/stc/cdeq.h b/include/stc/cdeq.h index baa7cb04..0dfe79dd 100644 --- a/include/stc/cdeq.h +++ b/include/stc/cdeq.h @@ -103,16 +103,16 @@ STC_INLINE _cx_iter _cx_memb(_advance)(_cx_iter it, intptr_t offs) #if !defined _i_queue +STC_INLINE size_t _cx_memb(_index)(_cx_self cx, _cx_iter it) + { return it.ref - cx.data; } STC_INLINE void _cx_memb(_pop_back)(_cx_self* self) { _cx_value* p = &self->data[--cdeq_rep_(self)->size]; i_valdrop(p); } STC_INLINE const _cx_value* _cx_memb(_at)(const _cx_self* self, const size_t idx) { - assert(idx < cdeq_rep_(self)->size); - return self->data + idx; + assert(idx < cdeq_rep_(self)->size); return self->data + idx; } - -STC_INLINE size_t _cx_memb(_index)(_cx_self cx, _cx_iter it) { - return it.ref - cx.data; +STC_INLINE _cx_value* _cx_memb(_at_mut)(_cx_self* self, const size_t idx) { + assert(idx < cdeq_rep_(self)->size); return self->data + idx; } STC_INLINE _cx_iter diff --git a/include/stc/template.h b/include/stc/template.h index 243eec6f..934a5d4a 100644 --- a/include/stc/template.h +++ b/include/stc/template.h @@ -97,21 +97,11 @@ #error "if i_keyraw defined, i_keyfrom (and often i_keyto) must be defined" #endif -/* Resolve i_drop and i_from here */ -#if defined i_drop && !defined i_keydrop && defined _i_isset - #define i_keydrop i_drop -#elif defined i_drop && !defined i_key - #define i_valdrop i_drop -#elif defined i_drop - #error "i_drop not supported when i_key defined. Define i_keydrop/i_valdrop instead." +#if defined i_drop + #error "i_drop not supported Define i_keydrop/i_valdrop instead." #endif - -#if defined i_from && !defined i_keyfrom && defined _i_isset - #define i_keyfrom i_from -#elif defined i_from && !defined i_key - #define i_valfrom i_from -#elif defined i_from - #error "i_from not supported when i_key defined. Define i_keyfrom/i_valfrom instead." +#if defined i_from + #error "i_from not supported. Define i_keyfrom/i_valfrom instead." #endif #ifdef i_val_str @@ -237,8 +227,6 @@ #undef i_cmp #undef i_eq #undef i_hash -#undef i_from -#undef i_drop #undef i_size #undef i_val -- cgit v1.2.3