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 /examples | |
| 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.
Diffstat (limited to 'examples')
| -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 |
11 files changed, 35 insertions, 40 deletions
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 |
