diff options
| author | Tyge Løvset <[email protected]> | 2022-10-20 16:17:33 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2022-10-20 16:17:33 +0200 |
| commit | 14f67d1936fa76be436eaaee739861268ca534f7 (patch) | |
| tree | c365789721d90cb09c311f0a65527ba31832da8b | |
| parent | 79d43229e64c53cd8b358a02a58fdbe124aa5e0f (diff) | |
| download | STC-modified-14f67d1936fa76be436eaaee739861268ca534f7.tar.gz STC-modified-14f67d1936fa76be436eaaee739861268ca534f7.zip | |
Switch from #define i_val_bind to i_val_class and i_key_class.
i_val_bind/i_key_bind is deprecated but available for now.
| -rw-r--r-- | README.md | 8 | ||||
| -rw-r--r-- | docs/carc_api.md | 2 | ||||
| -rw-r--r-- | docs/cmap_api.md | 8 | ||||
| -rw-r--r-- | docs/cvec_api.md | 36 | ||||
| -rw-r--r-- | examples/arc_containers.c | 2 | ||||
| -rw-r--r-- | examples/box.c | 4 | ||||
| -rw-r--r-- | examples/city.c | 2 | ||||
| -rw-r--r-- | examples/complex.c | 49 | ||||
| -rw-r--r-- | examples/forfilter.c | 2 | ||||
| -rw-r--r-- | examples/mapmap.c | 2 | ||||
| -rw-r--r-- | examples/mmap.c | 2 | ||||
| -rw-r--r-- | examples/multimap.c | 4 | ||||
| -rw-r--r-- | examples/new_sptr.c | 2 | ||||
| -rw-r--r-- | examples/person_arc.c | 2 | ||||
| -rw-r--r-- | examples/vikings.c | 4 | ||||
| -rw-r--r-- | include/stc/cbox.h | 2 | ||||
| -rw-r--r-- | include/stc/template.h | 37 |
17 files changed, 85 insertions, 83 deletions
@@ -330,13 +330,13 @@ Specials: - `i_key_str` - Define key type *cstr* and container i_tag = *str*. It binds type convertion from/to *const char*\*, and the ***cmp***, ***eq***, ***hash***, and ***keydrop*** functions. - `i_key_ssv` - Define key type *cstr* and container i_tag = *ssv*. It binds type convertion from/to *csview*, and its ***cmp***, ***eq***, ***hash***, and ***keydrop*** functions. - `i_key_arcbox TYPE` - Define container key type where TYPE is a smart pointer **carc** or **cbox**. NB: not to be used when defining carc/cbox types themselves. -- `i_key_bind TYPE` - General version of the three above - will auto-bind to standard named functions: *TYPE_clone*, *TYPE_drop*, *TYPE_cmp*, *TYPE_eq*, *TYPE_hash*. If `i_keyraw` is defined, *TYPE_toraw* function is bound to `i_keyto`. Only functions required by the particular container need to be defined. E.g., only **cmap** and **cset** and smart pointers uses *TYPE_hash* and *TYPE_eq*. **cstack** does not use *TYPE_cmp*. *TYPE_clone* is not used if *#define i_opt c_no_clone* is specified. Likewise, *TYPE_cmp* is not used if *#define i_opt c_no_cmp* is specified. -- `i_val_str`, `i_val_ssv`, `i_val_arcbox`, `i_val_bind` - Similar rules as for ***key***. +- `i_key_class TYPE` - General version of the three above - will auto-bind to standard named functions: *TYPE_clone*, *TYPE_drop*, *TYPE_cmp*, *TYPE_eq*, *TYPE_hash*. If `i_keyraw` is defined, *TYPE_toraw* function is bound to `i_keyto`. Only functions required by the particular container need to be defined. E.g., only **cmap** and **cset** and smart pointers uses *TYPE_hash* and *TYPE_eq*. **cstack** does not use *TYPE_cmp*. *TYPE_clone* is not used if *#define i_opt c_no_clone* is specified. Likewise, *TYPE_cmp* is not used if *#define i_opt c_no_cmp* is specified. +- `i_val_str`, `i_val_ssv`, `i_val_arcbox`, `i_val_class` - Similar rules as for ***key***. **Notes**: - Instead of defining `i_cmp`, you may define *i_opt c_no_cmp* to disable *searching and sorting* functions. - Instead of defining `i_*clone`, you may define *i_opt c_no_clone* to disable *clone* functionality. -- For `i_key_bind`, if *i_keyraw RAWTYPE* is defined along with it, *i_keyfrom* may also be defined to enable the *emplace*-functions. Note: the signature for ***cmp***, ***eq***, and ***hash*** uses *RAWTYPE* as input. +- For `i_key_class`, if *i_keyraw RAWTYPE* is defined along with it, *i_keyfrom* may also be defined to enable the *emplace*-functions. Note: the signature for ***cmp***, ***eq***, and ***hash*** uses *RAWTYPE* as input. The *emplace* versus non-emplace container methods -------------------------------------------------- @@ -526,7 +526,7 @@ Memory efficiency - Changed: the [**c_apply**](docs/ccommon_api.md) macros API. - Replaced: *csview_first_token()* and *csview_next_token()* with one function: `csview_token()`. - Added: **checkauto** tool for checking that c-source files uses `c_auto*` macros correctly. -- Added: general `i_key_bind` / `i_val_bind` template parameters which auto-binds template functions. +- Added: general `i_key_class` / `i_val_class` template parameters which auto-binds template functions. - Added: `i_opt` template parameter: compile-time options: `c_no_cmp`, `c_no_clone`, `c_no_atomic`, `c_is_fwd`; may be combined with `|` - Added: [**cbox**](docs/cbox_api.md) type: smart pointer, similar to [Rust Box](https://doc.rust-lang.org/rust-by-example/std/box.html) and [std::unique_ptr](https://en.cppreference.com/w/cpp/memory/unique_ptr). - Added: [**c_forpair**](docs/ccommon_api.md) macro: for-loop with "structured binding" diff --git a/docs/carc_api.md b/docs/carc_api.md index 08070889..c8248b67 100644 --- a/docs/carc_api.md +++ b/docs/carc_api.md @@ -84,7 +84,7 @@ bool carc_X_value_eq(const i_val* x, const i_val* y); #include <stc/csmap.h> #define i_type Arc // (atomic) ref. counted pointer -#define i_val_bind Map // Note i_val_bind: Map is a "class", i.e. has clone, drop functions. +#define i_val_class Map // Note i_val_class: Map is a "class", i.e. has clone, drop functions. #define i_valdrop(p) (printf("drop Arc:\n"), Map_drop(p)) // override Map_drop(p): #include <stc/carc.h> diff --git a/docs/cmap_api.md b/docs/cmap_api.md index 10bca836..119b72f2 100644 --- a/docs/cmap_api.md +++ b/docs/cmap_api.md @@ -298,10 +298,10 @@ static inline void Viking_drop(Viking* vk) { } #define i_type Vikings -#define i_key_bind Viking +#define i_key_class Viking #define i_val int /* - i_key_bind implies these defines, unless they are already defined: + i_key_class implies these defines, unless they are already defined: #define i_cmp Viking_cmp #define i_hash Viking_hash #define i_keyclone Viking_clone @@ -377,7 +377,7 @@ static inline RViking Viking_toraw(const Viking* vp) { // With this in place, we define the Viking => int hash map type: #define i_type Vikings -#define i_key_bind Viking +#define i_key_class Viking #define i_keyraw RViking #define i_keyfrom Viking_from #define i_opt c_no_clone // disable map cloning @@ -385,7 +385,7 @@ static inline RViking Viking_toraw(const Viking* vp) { #define i_val int #include <stc/cmap.h> /* - i_key_bind implies these defines, unless they are already defined: + i_key_class implies these defines, unless they are already defined: #define i_cmp RViking_cmp //#define i_hash RViking_hash // already defined above. //#define i_keyclone Viking_clone // not used because c_no_clone diff --git a/docs/cvec_api.md b/docs/cvec_api.md index c9442bb1..50fb4448 100644 --- a/docs/cvec_api.md +++ b/docs/cvec_api.md @@ -156,19 +156,21 @@ sorted: 5 7 8 13 16 25 int main() { cvec_str names = cvec_str_init(); + cvec_str_emplace(&names, "Mary"); cvec_str_emplace(&names, "Joe"); cstr_assign(&names.data[1], "Jake"); // replace "Joe". cstr tmp = cstr_from_fmt("%d elements so far", cvec_str_size(names)); - // emplace() will not compile if adding a new cstr type. Use push_back(): - cvec_str_push(&names, tmp); // tmp is moved to names, do not drop() it. + // cvec_str_emplace() only accept const char*, so use push(): + cvec_str_push(&names, tmp); // tmp is "moved" to names (must not be dropped). - printf("%s\n", cstr_str(&names.data[1])); // Access the second element + printf("%s\n", cstr_str(&names.data[1])); // Access second element c_foreach (i, cvec_str, names) printf("item: %s\n", cstr_str(i.ref)); + cvec_str_drop(&names); } ``` @@ -192,7 +194,7 @@ typedef struct { int User_cmp(const User* a, const User* b) { int c = strcmp(cstr_str(&a->name), cstr_str(&b->name)); - return c != 0 ? c : a->id - b->id; + return c ? c : a->id - b->id; } void User_drop(User* self) { cstr_drop(&self->name); @@ -202,24 +204,22 @@ User User_clone(User user) { return 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_cmp -#define i_valdrop User_drop -#define i_valclone User_clone -#define i_tag u +// Declare a managed, clonable vector of users. +#define i_type UVec +#define i_val_class User // User is a "class" as it has _cmp, _clone and _drop functions. #include <stc/cvec.h> int main(void) { - cvec_u vec = cvec_u_init(); - cvec_u_push(&vec, (User) {cstr_new("admin"), 0}); - cvec_u_push(&vec, (User) {cstr_new("joe"), 1}); + UVec vec = UVec_init(); + UVec_push(&vec, (User){cstr_new("mary"), 0}); + UVec_push(&vec, (User){cstr_new("joe"), 1}); + UVec_push(&vec, (User){cstr_new("admin"), 2}); - cvec_u vec2 = cvec_u_clone(vec); - c_foreach (i, cvec_u, vec2) + UVec vec2 = UVec_clone(vec); + + c_foreach (i, UVec, vec2) printf("%s: %d\n", cstr_str(&i.ref->name), i.ref->id); - c_drop(cvec_u, &vec, &vec2); // cleanup + c_drop(UVec, &vec, &vec2); // cleanup } -``` +```
\ No newline at end of file diff --git a/examples/arc_containers.c b/examples/arc_containers.c index 356afaf5..c4c68bcf 100644 --- a/examples/arc_containers.c +++ b/examples/arc_containers.c @@ -9,7 +9,7 @@ #include <stc/csmap.h> #define i_type Arc // (atomic) ref. counted type -#define i_val_bind Map +#define i_val_class Map #define i_valdrop(p) (printf("drop Arc:\n"), Map_drop(p)) // no need for atomic ref. count in single thread: #define i_opt c_no_atomic diff --git a/examples/box.c b/examples/box.c index b0f05530..c95b2cb7 100644 --- a/examples/box.c +++ b/examples/box.c @@ -28,11 +28,11 @@ void Person_drop(Person* p) { } #define i_type PBox -#define i_val_bind Person // binds Person_cmp, ... +#define i_val_class Person // "class" binds _cmp, _clone, _drop functions. #include <stc/cbox.h> #define i_type Persons -#define i_val_arcbox PBox // informs that PBox is a smart pointer. +#define i_val_arcbox PBox // "arcbox" informs that PBox is a smart pointer. #include <stc/csset.h> int main() diff --git a/examples/city.c b/examples/city.c index 80bfbb12..79b8b17e 100644 --- a/examples/city.c +++ b/examples/city.c @@ -31,7 +31,7 @@ static inline void City_drop(City* c) { #define i_type CityArc -#define i_key_bind City +#define i_key_class City #define i_opt c_no_atomic //#include <stc/cbox.h> #include <stc/carc.h> // try instead of cbox.h diff --git a/examples/complex.c b/examples/complex.c index 5e3f53a9..71ee6b0b 100644 --- a/examples/complex.c +++ b/examples/complex.c @@ -1,60 +1,55 @@ + +// Define similar c++ data types: +// +// using FloatStack = std::stack<float>; +// using StackList = std::stack<FloatStack>; +// using ListMap = std::unordered_map<int, std::forward_list<StackList>>; +// using MapMap = std::unordered_map<std::string, ListMap>; + #include <stc/cstr.h> -void check_drop(float* v) {printf("destroy %g\n", *v);} #define i_type FloatStack #define i_val float -#define i_valdrop check_drop -#define i_valclone(x) x // required to allow cloning when i_valdrop is defined - // (not for carc as it does not use i_valclone to clone). #include <stc/cstack.h> #define i_type StackList -#define i_val_bind FloatStack -#define i_opt c_no_cmp +#define i_val_class FloatStack // "class" picks up _clone, _drop +#define i_opt c_no_cmp // no FloatStack_cmp() #include <stc/clist.h> #define i_type ListMap #define i_key int -#define i_val_bind StackList +#define i_val_class StackList // "class" picks up _clone, _drop #include <stc/cmap.h> #define i_type MapMap #define i_key_str -#define i_val_bind ListMap +#define i_val_class ListMap #include <stc/cmap.h> -// c++: -// using FloatStack = std::stack<float>; -// using map_lst = std::unordered_map<int, std::forward_list<array2f>>; -// using map_map = std::unordered_map<std::string, map_lst>; - -int main() { - int xdim = 4, ydim = 6; - int x = 1, tableKey = 42; - const char* strKey = "first"; +int main() +{ c_auto (MapMap, mmap) { - FloatStack stack = FloatStack_with_size(xdim * ydim, 0); + FloatStack stack = FloatStack_with_size(10, 0); - // Put in some data in stack array - stack.data[x] = 3.1415927f; + // Put in some data in the structures + stack.data[3] = 3.1415927f; printf("stack size: %" c_zu "\n", FloatStack_size(&stack)); StackList list = StackList_init(); StackList_push_back(&list, stack); ListMap lmap = ListMap_init(); - ListMap_insert(&lmap, tableKey, list); - MapMap_insert(&mmap, cstr_from(strKey), lmap); + ListMap_insert(&lmap, 42, list); + MapMap_insert(&mmap, cstr_from("first"), lmap); // Access the data entry - const ListMap* lmap_p = MapMap_at(&mmap, strKey); - const StackList* list_p = ListMap_at(lmap_p, tableKey); + const ListMap* lmap_p = MapMap_at(&mmap, "first"); + const StackList* list_p = ListMap_at(lmap_p, 42); const FloatStack* stack_p = StackList_back(list_p); - printf("value (%d) is: %f\n", x, *FloatStack_at(stack_p, x)); - - stack.data[x] = 1.41421356f; // change the value in array + printf("value is: %f\n", *FloatStack_at(stack_p, 3)); // pi } } diff --git a/examples/forfilter.c b/examples/forfilter.c index 336407de..e1a3f1e5 100644 --- a/examples/forfilter.c +++ b/examples/forfilter.c @@ -9,7 +9,7 @@ #include <stc/cstack.h> #define i_type SVec -#define i_val_bind csview +#define i_val_class csview #include <stc/cstack.h> // filters and transforms: diff --git a/examples/mapmap.c b/examples/mapmap.c index 7cdc2ba0..99f5e58d 100644 --- a/examples/mapmap.c +++ b/examples/mapmap.c @@ -12,7 +12,7 @@ // Departments: std::map<std::string, People> #define i_type Departments #define i_key_str -#define i_val_bind People +#define i_val_class People // Shorthand for: // #define i_val People // #define i_cmp People_cmp diff --git a/examples/mmap.c b/examples/mmap.c index b7fff59a..6fe041e4 100644 --- a/examples/mmap.c +++ b/examples/mmap.c @@ -11,7 +11,7 @@ // Map of int => clist_str. #define i_type Multimap #define i_key int -#define i_val_bind clist_str // uses clist_str as i_val and binds clist_str_clone, clist_str_drop +#define i_val_class clist_str // uses clist_str as i_val and binds clist_str_clone, clist_str_drop #define i_cmp -c_default_cmp // like std::greater<int> #include <stc/csmap.h> diff --git a/examples/multimap.c b/examples/multimap.c index f580cf7e..f8c5b6c2 100644 --- a/examples/multimap.c +++ b/examples/multimap.c @@ -39,14 +39,14 @@ OlympicLocation OlympicLocation_clone(OlympicLocation loc); void OlympicLocation_drop(OlympicLocation* self); // Create a clist<OlympicLocation>, can be sorted by year. -#define i_val_bind OlympicLocation // binds _cmp, _clone and _drop. +#define i_val_class OlympicLocation // binds _cmp, _clone and _drop. #define i_tag OL #define i_extern // define _clist_mergesort() #include <stc/clist.h> // Create a csmap<cstr, clist_OL> where key is country name #define i_key_str // binds cstr_equ, cstr_hash, cstr_clone, ++ -#define i_val_bind clist_OL // binds clist_OL_clone, clist_OL_drop +#define i_val_class clist_OL // binds clist_OL_clone, clist_OL_drop #define i_tag OL #include <stc/csmap.h> diff --git a/examples/new_sptr.c b/examples/new_sptr.c index 1c0ea89b..c4c9f7f5 100644 --- a/examples/new_sptr.c +++ b/examples/new_sptr.c @@ -8,7 +8,7 @@ int Person_cmp(const Person* a, const Person* b); uint64_t Person_hash(const Person* p); #define i_type PersonArc -#define i_val_bind Person // "class" ensure Person_drop will be called +#define i_val_class Person // "class" ensure Person_drop will be called #define i_cmp Person_cmp // enable carc object comparisons (not ptr to obj) #define i_hash Person_hash // enable carc object hash (not ptr to obj) #include <stc/carc.h> diff --git a/examples/person_arc.c b/examples/person_arc.c index 203d362a..26bdd44d 100644 --- a/examples/person_arc.c +++ b/examples/person_arc.c @@ -28,7 +28,7 @@ void Person_drop(Person* p) { } #define i_type PSPtr -#define i_val_bind Person // ensure Person_drop +#define i_val_class Person // ensure Person_drop #define i_cmp Person_cmp // specify object cmp, instead of ptr cmp for arc. #include <stc/carc.h> diff --git a/examples/vikings.c b/examples/vikings.c index 528c30bf..1cfcf72e 100644 --- a/examples/vikings.c +++ b/examples/vikings.c @@ -32,7 +32,7 @@ static inline RViking Viking_toraw(const Viking* vp) { // With this in place, we define the Viking => int hash map type: #define i_type Vikings -#define i_key_bind Viking // key type +#define i_key_class Viking // key type #define i_keyraw RViking // lookup type #define i_keyfrom Viking_from #define i_opt c_no_clone @@ -40,7 +40,7 @@ static inline RViking Viking_toraw(const Viking* vp) { #define i_val int // mapped type #include <stc/cmap.h> /* - i_key_bind implies these defines, unless they are already defined: + i_key_class implies these defines, unless they are already defined: i_cmp => RViking_cmp //i_hash => RViking_hash // already defined. //i_keyclone => Viking_clone // not used, because of c_no_clone diff --git a/include/stc/cbox.h b/include/stc/cbox.h index 410656c2..62bb506a 100644 --- a/include/stc/cbox.h +++ b/include/stc/cbox.h @@ -40,7 +40,7 @@ void Person_drop(Person* p) { c_drop(cstr, &p->name, &p->email); } -#define i_key_bind Person // bind Person clone+drop fn's +#define i_key_class Person // bind Person clone+drop fn's #define i_type PBox #include <stc/cbox.h> diff --git a/include/stc/template.h b/include/stc/template.h index 8ac14583..f8d9e7b7 100644 --- a/include/stc/template.h +++ b/include/stc/template.h @@ -58,8 +58,15 @@ #error "i_valfrom defined without i_valraw" #endif +#ifdef i_key_bind // [deprecated] + #define i_key_class i_key_bind +#endif +#ifdef i_val_bind // [deprecated] + #define i_val_class i_val_bind +#endif + #if !(defined i_key || defined i_key_str || defined i_key_ssv || \ - defined i_key_bind || defined i_key_arcbox) + defined i_key_class || defined i_key_arcbox) #define _i_key_from_val #if defined _i_ismap #error "i_key* must be defined for maps." @@ -74,8 +81,8 @@ #if defined i_val_arcbox #define i_key_arcbox i_val_arcbox #endif - #if defined i_val_bind - #define i_key_bind i_val_bind + #if defined i_val_class + #define i_key_class i_val_class #endif #if defined i_val #define i_key i_val @@ -98,14 +105,14 @@ #endif #if defined i_key_str - #define i_key_bind cstr + #define i_key_class cstr #define i_keyraw crawstr #define i_keyfrom cstr_from #ifndef i_tag #define i_tag str #endif #elif defined i_key_ssv - #define i_key_bind cstr + #define i_key_class cstr #define i_keyraw csview #define i_keyfrom cstr_from_sv #define i_keyto cstr_sv @@ -114,15 +121,15 @@ #define i_tag ssv #endif #elif defined i_key_arcbox - #define i_key_bind i_key_arcbox + #define i_key_class i_key_arcbox #define i_keyraw c_paste(i_key_arcbox, _value) #define i_keyfrom c_paste(i_key_arcbox, _from) #define i_keyto(x) *(x)->get #define i_eq c_paste(i_key_arcbox, _value_eq) #endif -#ifdef i_key_bind - #define i_key i_key_bind +#ifdef i_key_class + #define i_key i_key_class #ifndef i_keyclone #define i_keyclone c_paste(i_key, _clone) #endif @@ -201,23 +208,23 @@ #if defined _i_ismap // ---- process cmap/csmap value i_val, ... ---- #ifdef i_val_str - #define i_val_bind cstr + #define i_val_class cstr #define i_valraw crawstr #define i_valfrom cstr_from #elif defined i_val_ssv - #define i_val_bind cstr + #define i_val_class cstr #define i_valraw csview #define i_valfrom cstr_from_sv #define i_valto cstr_sv #elif defined i_val_arcbox - #define i_val_bind i_val_arcbox + #define i_val_class i_val_arcbox #define i_valraw c_paste(i_val_arcbox, _value) #define i_valfrom c_paste(i_val_arcbox, _from) #define i_valto(x) *(x)->get #endif -#ifdef i_val_bind - #define i_val i_val_bind +#ifdef i_val_class + #define i_val i_val_class #ifndef i_valclone #define i_valclone c_paste(i_val, _clone) #endif @@ -283,7 +290,7 @@ #undef i_val_str #undef i_val_ssv #undef i_val_arcbox -#undef i_val_bind +#undef i_val_class #undef i_valraw #undef i_valclone #undef i_valfrom @@ -294,7 +301,7 @@ #undef i_key_str #undef i_key_ssv #undef i_key_arcbox -#undef i_key_bind +#undef i_key_class #undef i_keyraw #undef i_keyclone #undef i_keyfrom |
