From 92b950333c6c7002bdbf1b60af44a249dc0cef9c Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Sun, 19 Dec 2021 12:21:44 +0100 Subject: 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. --- examples/advanced.c | 68 ---------------------------------------- examples/astar.c | 18 +++++------ examples/bits.c | 6 ++-- examples/box.c | 39 +++++++++++------------ examples/box2.c | 7 +++-- examples/complex.c | 61 +++++++++++++++++------------------- examples/cpque.c | 6 ++-- examples/csmap_find.c | 2 +- examples/csmap_insert.c | 2 +- examples/cstr_match.c | 2 +- examples/demos.c | 12 +++---- examples/ex_gauss1.c | 2 +- examples/inits.c | 10 +++--- examples/mapmap.c | 78 +++++++++++++++++++++++++++++----------------- examples/mmap.c | 24 +++++++------- examples/multimap.c | 12 +++---- examples/new_arr.c | 6 ++-- examples/new_deq.c | 8 ++--- examples/new_list.c | 8 ++--- examples/new_map.c | 8 ++--- examples/new_pque.c | 4 +-- examples/new_queue.c | 8 ++--- examples/new_smap.c | 8 ++--- examples/new_sptr.c | 20 ++++++------ examples/new_vec.c | 16 +++++----- examples/prime.c | 2 +- examples/priority.c | 2 +- examples/queue.c | 2 +- examples/rawptr_elements.c | 4 +-- examples/read.c | 8 ++--- examples/replace.c | 2 +- examples/splitstr.c | 2 +- examples/sptr_demo.c | 16 +++++----- examples/sptr_erase.c | 41 ++++++++++++------------ examples/sptr_music.c | 23 +++++++------- examples/sptr_pthread.c | 7 ++--- examples/sptr_to_maps.c | 11 ++++--- examples/vikings.c | 69 ++++++++++++++++++++++++++++++++++++++++ 38 files changed, 321 insertions(+), 303 deletions(-) delete mode 100644 examples/advanced.c create mode 100644 examples/vikings.c (limited to 'examples') diff --git a/examples/advanced.c b/examples/advanced.c deleted file mode 100644 index 60276b09..00000000 --- a/examples/advanced.c +++ /dev/null @@ -1,68 +0,0 @@ -#include -#include - -typedef struct Viking { - cstr name; - cstr country; -} Viking; - -void viking_del(Viking* vk) { - cstr_del(&vk->name); - cstr_del(&vk->country); -} - -// Define Viking raw struct with hash, equals, and convertion functions between Viking and VikingRaw structs: - -typedef struct VikingRaw { - const char* name; - const char* country; -} VikingRaw; - -uint64_t vikingraw_hash(const VikingRaw* raw, size_t ignore) { - uint64_t hash = c_strhash(raw->name) ^ (c_strhash(raw->country) >> 15); - return hash; -} -static inline bool vikingraw_equalto(const VikingRaw* rx, const VikingRaw* ry) { - return strcmp(rx->name, ry->name) == 0 && strcmp(rx->country, ry->country) == 0; -} - -static inline Viking viking_fromRaw(VikingRaw raw) { // note: parameter is by value - return c_make(Viking){cstr_from(raw.name), cstr_from(raw.country)}; -} -static inline VikingRaw viking_toRaw(const Viking* vk) { - return c_make(VikingRaw){vk->name.str, vk->country.str}; -} - -// With this in place, we define the Viking => int hash map type: -#define i_tag vk -#define i_key Viking -#define i_val int -#define i_equ vikingraw_equalto -#define i_hash vikingraw_hash -#define i_keyraw VikingRaw -#define i_keyfrom viking_fromRaw -#define i_keyto viking_toRaw -#define i_keydel viking_del -#include - -int main() -{ - c_auto (cmap_vk, vikings) { - c_apply_pair(cmap_vk, emplace, &vikings, { - {{"Einar", "Norway"}, 20}, - {{"Olaf", "Denmark"}, 24}, - {{"Harald", "Iceland"}, 12}, - }); - VikingRaw bjorn = {"Bjorn", "Sweden"}; - cmap_vk_emplace_or_assign(&vikings, bjorn, 10); - - VikingRaw einar = {"Einar", "Norway"}; - cmap_vk_value *e = cmap_vk_find(&vikings, einar).ref; - e->second += 3; // add 3 hp points to Einar - cmap_vk_emplace(&vikings, einar, 0).ref->second += 5; // add 5 more to Einar - - c_foreach (k, cmap_vk, vikings) { - printf("%s of %s has %d hp\n", k.ref->first.name.str, k.ref->first.country.str, k.ref->second); - } - } -} \ No newline at end of file diff --git a/examples/astar.c b/examples/astar.c index 88c602ef..ee002b79 100644 --- a/examples/astar.c +++ b/examples/astar.c @@ -24,9 +24,9 @@ point_init(int x, int y, int width) } int -point_compare_priority(const point* a, const point* b) +point_cmp_priority(const point* a, const point* b) { - return c_default_compare(&a->priorty, &b->priorty); + return c_default_cmp(&a->priorty, &b->priorty); } int @@ -49,7 +49,7 @@ point_index(const point* p) } int -point_key_compare(const point* a, const point* b) +point_key_cmp(const point* a, const point* b) { int i = point_index(a); int j = point_index(b); @@ -57,22 +57,22 @@ point_key_compare(const point* a, const point* b) } #define i_val point -#define i_cmp point_compare_priority +#define i_cmp point_cmp_priority #include #define i_val point -#define i_opt c_no_compare +#define i_opt c_no_cmp #include #define i_key point #define i_val int -#define i_cmp point_key_compare +#define i_cmp point_key_cmp #define i_tag pcost #include #define i_key point #define i_val point -#define i_cmp point_key_compare +#define i_cmp point_key_cmp #define i_tag pstep #include @@ -155,10 +155,10 @@ main(void) "# # # # # # # # # #\n" "# @ # ##### ##### ##### ######### ##### # ######### # #\n" "# # # # # # #\n" - "#########################################################################\n"), cstr_del(&maze)) + "#########################################################################\n"), cstr_drop(&maze)) { int width = cstr_find(maze, "\n") + 1; - c_autovar (cdeq_point path = astar(&maze, width), cdeq_point_del(&path)) + c_autovar (cdeq_point path = astar(&maze, width), cdeq_point_drop(&path)) { c_foreach (it, cdeq_point, path) maze.str[point_index(it.ref)] = 'x'; printf("%s", maze.str); diff --git a/examples/bits.c b/examples/bits.c index fbdee3e8..ac59bd32 100644 --- a/examples/bits.c +++ b/examples/bits.c @@ -3,13 +3,13 @@ int main() { - c_autovar (cbits set = cbits_with_size(23, true), cbits_del(&set)) { + c_autovar (cbits set = cbits_with_size(23, true), cbits_drop(&set)) { printf("count %zu, %zu\n", cbits_count(set), set.size); cbits s1 = cbits_new("1110100110111"); char buf[256]; cbits_to_str(s1, buf, 0, -1); printf("buf: %s: %zu\n", buf, cbits_count(s1)); - cbits_del(&s1); + cbits_drop(&s1); cbits_reset(&set, 9); cbits_resize(&set, 43, false); @@ -36,7 +36,7 @@ int main() printf("%d", cbits_test(set, i)); puts(""); - c_autovar (cbits s2 = cbits_clone(set), cbits_del(&s2)) { + c_autovar (cbits s2 = cbits_clone(set), cbits_drop(&s2)) { cbits_flip_all(&s2); cbits_set(&s2, 16); cbits_set(&s2, 17); diff --git a/examples/box.c b/examples/box.c index c7763bb8..d400799b 100644 --- a/examples/box.c +++ b/examples/box.c @@ -3,7 +3,7 @@ typedef struct { cstr name, last; } Person; -Person Person_from(const char* name, const char* last) { +Person Person_new(const char* name, const char* last) { return (Person){.name = cstr_from(name), .last = cstr_from(last)}; } @@ -17,44 +17,43 @@ Person Person_clone(Person p) { p.last = cstr_clone(p.last); return p; } -void Person_del(Person* p) { - printf("del: %s %s\n", p->name.str, p->last.str); - c_del(cstr, &p->name, &p->last); + +void Person_drop(Person* p) { + printf("drop: %s %s\n", p->name.str, p->last.str); + c_drop(cstr, &p->name, &p->last); } -#define i_val Person -#define i_cmp Person_cmp -#define i_from Person_clone -#define i_del Person_del -#define i_tag prs +#define i_type PPtr +#define i_val_bind Person // binds Person_cmp, ... #include -#define i_val_ref cbox_prs -#define i_tag prs +#define i_type Persons +#define i_val_bind PPtr // binds PPtr_cmp, ... #include int main() { - c_auto (cvec_prs, vec) - c_auto (cbox_prs, p, q) + c_auto (Persons, vec) + c_auto (PPtr, p, q) { - p = cbox_prs_new(Person_from("Dave", "Cooper")); + p = PPtr_new(Person_new("Dave", "Cooper")); - q = cbox_prs_clone(p); + q = PPtr_clone(p); cstr_assign(&q.get->name, "Dale"); printf("%s %s.\n", p.get->name.str, p.get->last.str); printf("%s %s.\n", q.get->name.str, q.get->last.str); - cvec_prs_push_back(&vec, cbox_prs_new(Person_from("Laura", "Palmer"))); - cvec_prs_push_back(&vec, cbox_prs_new(Person_from("Shelly", "Johnson"))); + Persons_push_back(&vec, PPtr_new(Person_new("Laura", "Palmer"))); + Persons_push_back(&vec, PPtr_new(Person_new("Shelly", "Johnson"))); - c_foreach (i, cvec_prs, vec) + c_foreach (i, Persons, vec) printf("%s: %s\n", i.ref->get->name.str, i.ref->get->last.str); - c_autovar (Person per = Person_from("Laura", "Palmer"), Person_del(&per)) { - const cbox_prs *v = cvec_prs_get(&vec, (cbox_prs){&per}); + // Look-up Shelly! + c_autovar (Person s = Person_new("Shelly", "Johnson"), Person_drop(&s)) { + const PPtr *v = Persons_get(&vec, (PPtr){&s}); if (v) printf("found: %s: %s\n", v->get->name.str, v->get->last.str); } } diff --git a/examples/box2.c b/examples/box2.c index 48266963..5549dade 100644 --- a/examples/box2.c +++ b/examples/box2.c @@ -18,14 +18,17 @@ struct { } typedef Rectangle; #define i_val Point +#define i_opt c_no_cmp #include // cbox_Point #define i_val Rectangle +#define i_opt c_no_cmp #include // cbox_Rectangle // Box in box: -#define i_val_ref cbox_Point // NB: adviced to use i_val_ref when value is a cbox or csptr! - // it will auto-set i_del, i_from, i_cmp for you. +#define i_val_bind cbox_Point // NB: adviced to use i_val_arc when value is a cbox or csptr! + // it will auto-set i_drop, i_from, 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 b6aef5a8..17a10fad 100644 --- a/examples/complex.c +++ b/examples/complex.c @@ -1,35 +1,30 @@ #include -void check_del(float* v) {printf("destroy %g\n", *v);} +void check_drop(float* v) {printf("destroy %g\n", *v);} +#define i_type FloatStack #define i_val float -#define i_valdel check_del -#define i_opt c_no_clone -#define i_tag f +#define i_drop check_drop +#define i_from c_default_clone #include -#define i_val cstack_f -#define i_opt c_no_clone|c_no_compare -#define i_valdel cstack_f_del -#define i_tag arr +#define i_type StackList +#define i_val_bind FloatStack +#define i_opt c_no_cmp #include +#define i_type ListMap #define i_key int -#define i_val clist_arr -#define i_valdel clist_arr_del -#define i_opt c_no_clone -#define i_tag lst +#define i_val_bind StackList #include +#define i_type MapMap #define i_key_str -#define i_val cmap_lst -#define i_valdel cmap_lst_del -#define i_opt c_no_clone -#define i_tag map +#define i_val_bind ListMap #include // c++: -// using cstack_f = std::stack; +// using FloatStack = std::stack; // using map_lst = std::unordered_map>; // using map_map = std::unordered_map; @@ -38,29 +33,29 @@ int main() { int x = 1, y = 3, tableKey = 42; const char* strKey = "first"; - c_auto (cmap_map, myMap) + c_auto (MapMap, mmap) { - cstack_f stk = cstack_f_with_capacity(xdim * ydim); - memset(stk.data, 0, xdim*ydim*sizeof *stk.data); - stk.size = stk.capacity; + FloatStack stack = FloatStack_with_capacity(xdim * ydim); + memset(stack.data, 0, xdim*ydim*sizeof *stack.data); + stack.size = stack.capacity; // Put in some data in stack array - stk.data[x] = 3.1415927f; - printf("stk size: %zu\n", cstack_f_size(stk)); + stack.data[x] = 3.1415927f; + printf("stack size: %zu\n", FloatStack_size(stack)); - clist_arr tableList = clist_arr_init(); - clist_arr_push_back(&tableList, stk); + StackList list = StackList_init(); + StackList_push_back(&list, stack); - cmap_lst listMap = cmap_lst_init(); - cmap_lst_insert(&listMap, tableKey, tableList); - cmap_map_insert(&myMap, cstr_from(strKey), listMap); + ListMap lmap = ListMap_init(); + ListMap_insert(&lmap, tableKey, list); + MapMap_insert(&mmap, cstr_from(strKey), lmap); // Access the data entry - const cmap_lst* mapL = cmap_map_at(&myMap, strKey); - const clist_arr* lstA = cmap_lst_at(mapL, tableKey); - const cstack_f* arr = clist_arr_back(lstA); - printf("value (%d) is: %f\n", x, *cstack_f_at(arr, x)); + const ListMap* lmap_p = MapMap_at(&mmap, strKey); + const StackList* list_p = ListMap_at(lmap_p, tableKey); + const FloatStack* stack_p = StackList_back(list_p); + printf("value (%d) is: %f\n", x, *FloatStack_at(stack_p, x)); - stk.data[x] = 1.41421356f; // change the value in array + stack.data[x] = 1.41421356f; // change the value in array } } diff --git a/examples/cpque.c b/examples/cpque.c index b3ee4adf..0b8a610c 100644 --- a/examples/cpque.c +++ b/examples/cpque.c @@ -13,7 +13,7 @@ static int (*icmp_fn)(const int* x, const int* y); #define imix_less(left, right) ((*(left) ^ 1) < (*(right) ^ 1)) static int imax_cmp(const int* x, const int* y) { return *x - *y; } static int imin_cmp(const int* x, const int* y) { return *y - *x; } -static int imix_cmp(const int* x, const int* y) { return c_less_compare(imix_less, x, y); } +static int imix_cmp(const int* x, const int* y) { return c_less_cmp(imix_less, x, y); } void print_queue(ipque q) { ipque copy = ipque_clone(q); @@ -22,13 +22,13 @@ void print_queue(ipque q) { ipque_pop(©); } puts(""); - ipque_del(©); + ipque_drop(©); } int main() { const int data[] = {1,8,5,6,3,4,0,9,7,2}, n = c_arraylen(data); - c_auto (ipque, q, q2, q3) // init() and defered del() + c_auto (ipque, q, q2, q3) // init() and defered drop() { icmp_fn = imax_cmp; c_forrange (i, n) diff --git a/examples/csmap_find.c b/examples/csmap_find.c index 78b6bbf5..3ca7ff87 100644 --- a/examples/csmap_find.c +++ b/examples/csmap_find.c @@ -8,7 +8,7 @@ #include #define i_val csmap_istr_rawvalue -#define i_opt c_no_compare +#define i_opt c_no_cmp #define i_tag istr #include diff --git a/examples/csmap_insert.c b/examples/csmap_insert.c index 920cf02d..c3690c83 100644 --- a/examples/csmap_insert.c +++ b/examples/csmap_insert.c @@ -15,7 +15,7 @@ #include #define i_val csmap_ii_rawvalue -#define i_opt c_no_compare +#define i_opt c_no_cmp #define i_tag ii #include diff --git a/examples/cstr_match.c b/examples/cstr_match.c index 041a2d84..dc1423d7 100644 --- a/examples/cstr_match.c +++ b/examples/cstr_match.c @@ -3,7 +3,7 @@ int main() { - c_autovar (cstr ss = cstr_new("The quick brown fox jumps over the lazy dog.JPG"), cstr_del(&ss)) { + c_autovar (cstr ss = cstr_new("The quick brown fox jumps over the lazy dog.JPG"), cstr_drop(&ss)) { size_t pos = cstr_find_n(ss, "brown", 0, 5); printf("%zu [%s]\n", pos, pos == cstr_npos ? "" : &ss.str[pos]); printf("equals: %d\n", cstr_equals(ss, "The quick brown fox jumps over the lazy dog.JPG")); diff --git a/examples/demos.c b/examples/demos.c index 70aa722d..32e5dde0 100644 --- a/examples/demos.c +++ b/examples/demos.c @@ -3,7 +3,7 @@ void stringdemo1() { printf("\nSTRINGDEMO1\n"); - c_autovar (cstr cs = cstr_new("one-nine-three-seven-five"), cstr_del(&cs)) + c_autovar (cstr cs = cstr_new("one-nine-three-seven-five"), cstr_drop(&cs)) { printf("%s.\n", cs.str); @@ -35,7 +35,7 @@ void stringdemo1() void vectordemo1() { printf("\nVECTORDEMO1\n"); - c_autovar (cvec_ix bignums = cvec_ix_with_capacity(100), cvec_ix_del(&bignums)) + c_autovar (cvec_ix bignums = cvec_ix_with_capacity(100), cvec_ix_drop(&bignums)) { cvec_ix_reserve(&bignums, 100); for (size_t i = 10; i <= 100; i += 10) @@ -118,7 +118,7 @@ void setdemo1() c_foreach (i, cset_i, nums) printf("set: %d\n", *i.ref); - cset_i_del(&nums); + cset_i_drop(&nums); } #define i_key int @@ -133,7 +133,7 @@ void mapdemo1() cmap_ii_emplace(&nums, 8, 64); cmap_ii_emplace(&nums, 11, 121); printf("val 8: %d\n", *cmap_ii_at(&nums, 8)); - cmap_ii_del(&nums); + cmap_ii_drop(&nums); } #define i_key_str @@ -181,7 +181,7 @@ void mapdemo3() printf("size %zu\n", cmap_str_size(table)); c_foreach (i, cmap_str, table) printf("entry: %s: %s\n", i.ref->first.str, i.ref->second.str); - cmap_str_del(&table); // frees key and value cstrs, and hash table. + cmap_str_drop(&table); // frees key and value cstrs, and hash table. } #define i_val float @@ -192,7 +192,7 @@ void arraydemo1() { printf("\nARRAYDEMO1\n"); c_autovar (carr3_f arr3 = carr3_f_with_values(30, 20, 10, 0.0f), - carr3_f_del(&arr3)) + carr3_f_drop(&arr3)) { arr3.data[5][4][3] = 10.2f; float **arr2 = arr3.data[5]; diff --git a/examples/ex_gauss1.c b/examples/ex_gauss1.c index c96562b6..6725393f 100644 --- a/examples/ex_gauss1.c +++ b/examples/ex_gauss1.c @@ -13,7 +13,7 @@ // Declare int vector with map entries that can be sorted by map keys. struct {int first; size_t second;} typedef mapval; static int compare(mapval *a, mapval *b) { - return c_default_compare(&a->first, &b->first); + return c_default_cmp(&a->first, &b->first); } #define i_val mapval diff --git a/examples/inits.c b/examples/inits.c index 386be86e..a1733caf 100644 --- a/examples/inits.c +++ b/examples/inits.c @@ -12,19 +12,19 @@ #include typedef struct {int x, y;} ipair_t; -inline static int ipair_compare(const ipair_t* a, const ipair_t* b) { - int c = c_default_compare(&a->x, &b->x); - return c ? c : c_default_compare(&a->y, &b->y); +inline static int ipair_cmp(const ipair_t* a, const ipair_t* b) { + int c = c_default_cmp(&a->x, &b->x); + return c ? c : c_default_cmp(&a->y, &b->y); } #define i_val ipair_t -#define i_cmp ipair_compare +#define i_cmp ipair_cmp #define i_tag ip #include #define i_val ipair_t -#define i_cmp ipair_compare +#define i_cmp ipair_cmp #define i_tag ip #include diff --git a/examples/mapmap.c b/examples/mapmap.c index e22a4f7b..61b55493 100644 --- a/examples/mapmap.c +++ b/examples/mapmap.c @@ -1,47 +1,67 @@ // unordered_map>: +#define i_type People #define i_key_str #define i_val_str -#define i_tag sect -#include // csmap_sect +#define i_keydrop(p) (printf("kdrop: %s\n", (p)->str), cstr_drop(p)) +#include +#define i_type Departments #define i_key_str -#define i_val csmap_sect -#define i_valdel csmap_sect_del -#define i_valfrom csmap_sect_clone -#define i_tag conf -#include // csmap_conf +#define i_val_bind People +#include -void add(csmap_conf* map, const char* section, const char* entry, const char* value) +#define i_type Stack +#define i_val_bind People_value +#define i_opt c_no_cmp +//#define i_from People_value_clone +//#define i_drop People_value_drop +#include + +void add(Departments* deps, const char* name, const char* email, const char* dep) { - csmap_sect *smap = &csmap_conf_insert(map, cstr_from(section), csmap_sect_init()).ref->second; - csmap_sect_emplace_or_assign(smap, entry, value); + People *people = &Departments_insert(deps, cstr_from(dep), People_init()).ref->second; + People_emplace_or_assign(people, name, email); } -bool contains(csmap_conf* map, const char* section, const char* entry) +Stack contains(Departments* map, const char* name) { - const csmap_conf_value *val = csmap_conf_get(map, section); - return val && csmap_sect_get(&val->second, entry); + Stack stk = Stack_init(); + const People_value* v; + c_foreach (i, Departments, *map) + if ((v = People_get(&i.ref->second, name))) { + Stack_push(&stk, People_value_clone(*v)); + } + return stk; } int main(void) { - c_auto (csmap_conf, map) + c_auto (Departments, map) { - add(&map, "user", "name", "Joe"); - add(&map, "user", "groups", "proj1,proj3"); - add(&map, "group", "proj1", "Energy"); - add(&map, "group", "proj2", "Windy"); - add(&map, "group", "proj3", "Oil"); - add(&map, "admin", "employees", "2302"); - add(&map, "group", "proj2", "Wind"); // Update - - printf("contains: %d\n", contains(&map, "group", "employees")); - printf("contains: %d\n", contains(&map, "admin", "name")); - printf("contains: %d\n", contains(&map, "admin", "employees")); - - c_foreach (i, csmap_conf, map) - c_foreach (j, csmap_sect, i.ref->second) - printf("%s: %s - %s\n", i.ref->first.str, j.ref->first.str, j.ref->second.str); + add(&map, "Anna Kendro", "Anna@myplace.com", "Support"); + add(&map, "Terry Dane", "Terry@myplace.com", "Development"); + add(&map, "Kik Winston", "Kik@myplace.com", "Finance"); + add(&map, "Nancy Drew", "Nancy@live.com", "Development"); + add(&map, "Nick Denton", "Nick@myplace.com", "Finance"); + add(&map, "Stan Whiteword", "Stan@myplace.com", "Marketing"); + add(&map, "Serena Bath", "Serena@myplace.com", "Support"); + add(&map, "Patrick Dust", "Patrick@myplace.com", "Finance"); + add(&map, "Red Winger", "Red@gmail.com", "Marketing"); + add(&map, "Nick Denton", "Nick@yahoo.com", "Support"); + add(&map, "Colin Turth", "Colin@myplace.com", "Support"); + add(&map, "Dennis Kay", "Dennis@mail.com", "Marketing"); + add(&map, "Anne Dickens", "Anne@myplace.com", "Development"); + + c_foreach (i, Departments, map) + c_forpair (name, email, People, i.ref->second) + printf("%s: %s - %s\n", i.ref->first.str, _.name.str, _.email.str); + puts(""); + + c_auto (Stack, s) printf("found: %zu\n", Stack_size(s = contains(&map, "Nick Denton"))); + c_auto (Stack, s) printf("found: %zu\n", Stack_size(s = contains(&map, "Patrick Dust"))); + c_auto (Stack, s) printf("found: %zu\n", Stack_size(s = contains(&map, "Dennis Kay"))); + c_auto (Stack, s) printf("found: %zu\n", Stack_size(s = contains(&map, "Serena Bath"))); + puts("Done"); } } \ No newline at end of file diff --git a/examples/mmap.c b/examples/mmap.c index 26b271d1..ea08017b 100644 --- a/examples/mmap.c +++ b/examples/mmap.c @@ -1,36 +1,34 @@ // This implements the multimap c++ example found at: // https://en.cppreference.com/w/cpp/container/multimap/insert -// Map of int => clist_str. Note the negation of c_default_compare +// Map of int => clist_str. Note the negation of c_default_cmp #define i_val_str #include +#define i_type Multimap #define i_key int -#define i_val clist_str -#define i_cmp -c_default_compare -#define i_valdel clist_str_del -#define i_valfrom clist_str_clone -#define i_tag mult +#define i_val_bind clist_str +#define i_cmp -c_default_cmp #include -void print(const csmap_mult mmap) +void print(const Multimap mmap) { - c_foreach (e, csmap_mult, mmap) { + c_foreach (e, Multimap, mmap) { c_foreach (s, clist_str, e.ref->second) printf("{%d,%s} ", e.ref->first, s.ref->str); } puts(""); } -void insert(csmap_mult* mmap, int key, const char* str) +void insert(Multimap* mmap, int key, const char* str) { - clist_str *list = &csmap_mult_insert(mmap, key, clist_str_init()).ref->second; + clist_str *list = &Multimap_insert(mmap, key, clist_str_init()).ref->second; clist_str_emplace_back(list, str); } int main() { - c_auto (csmap_mult, mmap) + c_auto (Multimap, mmap) { // list-initialize struct {int i; const char* s;} vals[] = {{2, "foo"}, {2, "bar"}, {3, "baz"}, {1, "abc"}, {5, "def"}}; @@ -53,12 +51,12 @@ int main() print(mmap); // erase all entries with key 5 - csmap_mult_erase(&mmap, 5); + Multimap_erase(&mmap, 5); print(mmap); // find and erase a specific entry clist_str_iter pos; - c_foreach (e, csmap_mult, mmap) + c_foreach (e, Multimap, mmap) if ((pos = clist_str_find(&e.ref->second, "bar")).ref != clist_str_end(&e.ref->second).ref) { clist_str_erase_at(&e.ref->second, pos); break; diff --git a/examples/multimap.c b/examples/multimap.c index 543b8b22..424ae606 100644 --- a/examples/multimap.c +++ b/examples/multimap.c @@ -35,7 +35,7 @@ struct OlympicsData { int year; const char *city, *country, *date; } ol_data[] = typedef struct { int year; cstr city, date; } OlympicLocation; -int OlympicLocation_compare(OlympicLocation* a, OlympicLocation* b) { +int OlympicLocation_cmp(OlympicLocation* a, OlympicLocation* b) { return a->year - b->year; } @@ -44,14 +44,14 @@ OlympicLocation OlympicLocation_clone(OlympicLocation loc) { loc.date = cstr_clone(loc.date); return loc; } -void OlympicLocation_del(OlympicLocation* self) { - c_del(cstr, &self->city, &self->date); +void OlympicLocation_drop(OlympicLocation* self) { + c_drop(cstr, &self->city, &self->date); } // Create a clist, can be sorted by year. #define i_val OlympicLocation -#define i_cmp OlympicLocation_compare -#define i_del OlympicLocation_del +#define i_cmp OlympicLocation_cmp +#define i_drop OlympicLocation_drop #define i_from OlympicLocation_clone #define i_tag OL #include @@ -59,7 +59,7 @@ void OlympicLocation_del(OlympicLocation* self) { // Create a csmap where key is country name #define i_key_str #define i_val clist_OL -#define i_valdel clist_OL_del +#define i_valdrop clist_OL_drop #define i_valfrom clist_OL_clone #define i_tag OL #include diff --git a/examples/new_arr.c b/examples/new_arr.c index dc597e4a..a593536e 100644 --- a/examples/new_arr.c +++ b/examples/new_arr.c @@ -15,7 +15,7 @@ int main() int w = 7, h = 5, d = 3; c_autovar (carr2_int volume = carr2_int_init(w, h), - carr2_int_del(&volume)) + carr2_int_drop(&volume)) { int *dat = carr2_int_data(&volume); for (size_t i = 0; i < carr2_int_size(volume); ++i) @@ -32,7 +32,7 @@ int main() } c_autovar (carr3_int volume = carr3_int_init(w, h, d), - carr3_int_del(&volume)) + carr3_int_drop(&volume)) { int *dat = carr3_int_data(&volume); for (size_t i = 0; i < carr3_int_size(volume); ++i) @@ -50,7 +50,7 @@ int main() } c_autovar (carr2_str text2d = carr2_str_with_values(h, d, cstr_init()), - carr2_str_del(&text2d)) + carr2_str_drop(&text2d)) { cstr_assign(&text2d.data[2][1], "hello"); cstr_assign(&text2d.data[4][0], "world"); diff --git a/examples/new_deq.c b/examples/new_deq.c index cc3ff76b..57224869 100644 --- a/examples/new_deq.c +++ b/examples/new_deq.c @@ -16,13 +16,13 @@ struct MyStruct { #include struct Point { int x, y; } typedef Point; -int point_compare(const Point* a, const Point* b) { - int c = c_default_compare(&a->x, &b->x); - return c ? c : c_default_compare(&a->y, &b->y); +int point_cmp(const Point* a, const Point* b) { + int c = a->x - b->x; + return c ? c : a->y - b->y; } #define i_val Point -#define i_cmp point_compare +#define i_cmp point_cmp #define i_opt c_is_fwd #define i_tag pnt #include diff --git a/examples/new_list.c b/examples/new_list.c index e07030b5..0867c7a6 100644 --- a/examples/new_list.c +++ b/examples/new_list.c @@ -15,13 +15,13 @@ struct MyStruct { #include struct Point { int x, y; } typedef Point; -int point_compare(const Point* a, const Point* b) { - int c = c_default_compare(&a->x, &b->x); - return c ? c : c_default_compare(&a->y, &b->y); +int point_cmp(const Point* a, const Point* b) { + int c = a->x - b->x; + return c ? c : a->y - b->y; } #define i_val Point -#define i_cmp point_compare +#define i_cmp point_cmp #define i_opt c_is_fwd #define i_tag pnt #include diff --git a/examples/new_map.c b/examples/new_map.c index 8975cd77..953ca793 100644 --- a/examples/new_map.c +++ b/examples/new_map.c @@ -16,15 +16,15 @@ struct MyStruct { // Point => int map struct Point { int x, y; } typedef Point; -int point_compare(const Point* a, const Point* b) { - int c = c_default_compare(&a->x, &b->x); - return c ? c : c_default_compare(&a->y, &b->y); +int point_cmp(const Point* a, const Point* b) { + int c = a->x - b->x; + return c ? c : a->y - b->y; } // Point => int map #define i_key Point #define i_val int -#define i_cmp point_compare +#define i_cmp point_cmp #define i_opt c_is_fwd #define i_tag pnt #include diff --git a/examples/new_pque.c b/examples/new_pque.c index 5048ea77..1485e630 100644 --- a/examples/new_pque.c +++ b/examples/new_pque.c @@ -16,8 +16,8 @@ struct MyStruct { struct Point { int x, y; } typedef Point; int Point_cmp(const Point* a, const Point* b) { - int c = c_default_compare(&a->x, &b->x); - return c ? c : c_default_compare(&a->y, &b->y); + int c = a->x - b->x; + return c ? c : a->y - b->y; } #define i_val Point diff --git a/examples/new_queue.c b/examples/new_queue.c index 3ad571bb..9219a1b4 100644 --- a/examples/new_queue.c +++ b/examples/new_queue.c @@ -6,12 +6,12 @@ forward_cqueue(cqueue_pnt, struct Point); struct Point { int x, y; } typedef Point; -int point_compare(const Point* a, const Point* b) { - int c = c_default_compare(&a->x, &b->x); - return c ? c : c_default_compare(&a->y, &b->y); +int point_cmp(const Point* a, const Point* b) { + int c = c_default_cmp(&a->x, &b->x); + return c ? c : c_default_cmp(&a->y, &b->y); } #define i_val Point -#define i_cmp point_compare +#define i_cmp point_cmp #define i_opt c_is_fwd #define i_tag pnt #include diff --git a/examples/new_smap.c b/examples/new_smap.c index 7f0730db..2cb35b99 100644 --- a/examples/new_smap.c +++ b/examples/new_smap.c @@ -15,14 +15,14 @@ struct MyStruct { // Point => int map struct Point { int x, y; } typedef Point; -int point_compare(const Point* a, const Point* b) { - int c = c_default_compare(&a->x, &b->x); - return c ? c : c_default_compare(&a->y, &b->y); +int point_cmp(const Point* a, const Point* b) { + int c = a->x - b->x; + return c ? c : a->y - b->y; } #define i_key Point #define i_val int -#define i_cmp point_compare +#define i_cmp point_cmp #define i_opt c_is_fwd #define i_tag pnt #include diff --git a/examples/new_sptr.c b/examples/new_sptr.c index 4a715160..d1eed636 100644 --- a/examples/new_sptr.c +++ b/examples/new_sptr.c @@ -2,33 +2,33 @@ struct Person { cstr name, last; } typedef Person; -Person Person_from(const char* name, const char* last) { +Person Person_new(const char* name, const char* last) { return (Person){.name = cstr_from(name), .last = cstr_from(last)}; } -void Person_del(Person* p) { - printf("del: %s %s\n", p->name.str, p->last.str); - c_del(cstr, &p->name, &p->last); +void Person_drop(Person* p) { + printf("drop: %s %s\n", p->name.str, p->last.str); + c_drop(cstr, &p->name, &p->last); } #define i_val Person -#define i_del Person_del -#define i_opt c_no_compare +#define i_drop Person_drop +#define i_opt c_no_cmp #define i_tag person #include // ... #define i_val int -#define i_del(x) printf("del: %d\n", *(x)) +#define i_drop(x) printf("drop: %d\n", *(x)) #include -#define i_val_ref csptr_int +#define i_val_bind csptr_int #define i_tag iptr #include int main(void) { - c_autovar (csptr_person p = csptr_person_new(Person_from("John", "Smiths")), csptr_person_del(&p)) - c_autovar (csptr_person q = csptr_person_clone(p), csptr_person_del(&q)) // share the pointer + c_autovar (csptr_person p = csptr_person_new(Person_new("John", "Smiths")), csptr_person_drop(&p)) + c_autovar (csptr_person q = csptr_person_clone(p), csptr_person_drop(&q)) // share the pointer { printf("%s %s. uses: %lu\n", q.get->name.str, q.get->last.str, *q.use_count); } diff --git a/examples/new_vec.c b/examples/new_vec.c index 62804125..c5570942 100644 --- a/examples/new_vec.c +++ b/examples/new_vec.c @@ -15,13 +15,13 @@ struct MyStruct { #include struct Point { int x, y; } typedef Point; -int point_compare(const Point* a, const Point* b) { - int c = c_default_compare(&a->x, &b->x); - return c ? c : c_default_compare(&a->y, &b->y); +int point_cmp(const Point* a, const Point* b) { + int c = c_default_cmp(&a->x, &b->x); + return c ? c : c_default_cmp(&a->y, &b->y); } #define i_val Point -#define i_cmp point_compare +#define i_cmp point_cmp #define i_opt c_is_fwd #define i_tag pnt #include @@ -37,11 +37,11 @@ int main() { cvec_i32 vec = cvec_i32_init(); cvec_i32_push_back(&vec, 123); - cvec_i32_del(&vec); + cvec_i32_drop(&vec); cvec_float fvec = cvec_float_init(); cvec_float_push_back(&fvec, 123.3); - cvec_float_del(&fvec); + cvec_float_drop(&fvec); cvec_pnt pvec = cvec_pnt_init(); cvec_pnt_push_back(&pvec, (Point){42, 14}); @@ -51,9 +51,9 @@ int main() c_foreach (i, cvec_pnt, pvec) printf(" (%d %d)", i.ref->x, i.ref->y); puts(""); - cvec_pnt_del(&pvec); + cvec_pnt_drop(&pvec); cvec_str svec = cvec_str_init(); cvec_str_emplace_back(&svec, "Hello, friend"); - cvec_str_del(&svec); + cvec_str_drop(&svec); } \ No newline at end of file diff --git a/examples/prime.c b/examples/prime.c index f858831a..0e2ede56 100644 --- a/examples/prime.c +++ b/examples/prime.c @@ -27,7 +27,7 @@ int main(void) printf("computing prime numbers up to %zu\n", n); clock_t t1 = clock(); - c_autovar (cbits primes = sieveOfEratosthenes(n + 1), cbits_del(&primes)) { + c_autovar (cbits primes = sieveOfEratosthenes(n + 1), cbits_drop(&primes)) { puts("done"); size_t np = cbits_count(primes); clock_t t2 = clock(); diff --git a/examples/priority.c b/examples/priority.c index 1068cb91..5164d85d 100644 --- a/examples/priority.c +++ b/examples/priority.c @@ -4,7 +4,7 @@ #include #define i_val int64_t -#define i_cmp -c_default_compare // min-heap (increasing values) +#define i_cmp -c_default_cmp // min-heap (increasing values) #define i_tag i #include diff --git a/examples/queue.c b/examples/queue.c index 4acaa4d6..ffd0a1e7 100644 --- a/examples/queue.c +++ b/examples/queue.c @@ -2,7 +2,7 @@ #include #define i_val int -#define i_del(x) printf("drop %d\n", *(x)) +#define i_drop(x) printf("drop %d\n", *(x)) #define i_from c_default_clone #define i_tag i #include diff --git a/examples/rawptr_elements.c b/examples/rawptr_elements.c index c5416822..463a7f35 100644 --- a/examples/rawptr_elements.c +++ b/examples/rawptr_elements.c @@ -6,7 +6,7 @@ struct { double x, y; } typedef Point; // Set of Point pointers: define all template parameters "in-line" // Note it may be simpler to use a cbox for this. #define i_key Point* -#define i_keydel(x) c_free(*(x)) +#define i_keydrop(x) c_free(*(x)) #define i_keyfrom(x) c_new(Point, *(x)) #define i_hash(x, n) c_default_hash(*(x), sizeof *(x)) #define i_equ(x, y) c_memcmp_equalto(*(x), *(y)) @@ -18,7 +18,7 @@ typedef int64_t inttype; #define i_key_str #define i_valraw inttype #define i_val inttype* -#define i_valdel(x) c_free(*(x)) +#define i_valdrop(x) c_free(*(x)) #define i_valfrom(raw) c_new(inttype, raw) #define i_valto(x) **(x) #include diff --git a/examples/read.c b/examples/read.c index 4e670a10..5313cb65 100644 --- a/examples/read.c +++ b/examples/read.c @@ -6,16 +6,16 @@ cvec_str read_file(const char* name) { cvec_str vec = cvec_str_init(); c_autovar (FILE* f = fopen(name, "r"), fclose(f)) - c_autovar (cstr line = cstr_init(), cstr_del(&line)) - while (cstr_getline(&line, f)) - cvec_str_emplace_back(&vec, line.str); + c_autovar (cstr line = cstr_init(), cstr_drop(&line)) + while (cstr_getline(&line, f)) + cvec_str_emplace_back(&vec, line.str); return vec; } int main() { int n = 0; - c_autovar (cvec_str vec = read_file(__FILE__), cvec_str_del(&vec)) + c_autovar (cvec_str vec = read_file(__FILE__), cvec_str_drop(&vec)) c_foreach (i, cvec_str, vec) printf("%5d: %s\n", ++n, i.ref->str); diff --git a/examples/replace.c b/examples/replace.c index ebb40cc5..0349d9f4 100644 --- a/examples/replace.c +++ b/examples/replace.c @@ -13,7 +13,7 @@ int main () // Ustring positions: 0123456789*123456789*12345 cstr s = cstr_from(base); // "this is a test string." cstr m = cstr_clone(s); - c_autodefer (cstr_del(&s), cstr_del(&m)) { + c_autodefer (cstr_drop(&s), cstr_drop(&m)) { cstr_append(&m, m.str); cstr_append(&m, m.str); printf("%s\n", m.str); diff --git a/examples/splitstr.c b/examples/splitstr.c index 8bea911a..789b2c59 100644 --- a/examples/splitstr.c +++ b/examples/splitstr.c @@ -36,7 +36,7 @@ int main() cstr string = cstr_new("Split,this,,string,now,"); cvec_str vec = string_split(cstr_sv(string), c_sv(",")); - c_autodefer (cvec_str_del(&vec), cstr_del(&string)) + c_autodefer (cvec_str_drop(&vec), cstr_drop(&string)) c_foreach (i, cvec_str, vec) printf("\t\"%s\"\n", i.ref->str); } \ No newline at end of file diff --git a/examples/sptr_demo.c b/examples/sptr_demo.c index c3da5287..a6b33de6 100644 --- a/examples/sptr_demo.c +++ b/examples/sptr_demo.c @@ -1,8 +1,8 @@ #include #include -void int_del(int* x) { - printf("del: %d\n", *x); +void int_drop(int* x) { + printf("drop: %d\n", *x); } // csptr implements its own clone method using reference counting, @@ -10,19 +10,19 @@ void int_del(int* x) { #define i_type iref // set type name to be defined (instead of 'csptr_int') #define i_val int -#define i_del int_del // optional, just to display the elements destroyed +#define i_drop int_drop // optional, just to display the elements destroyed #include // iref -#define i_key_ref iref // note: use i_key_ref instead of i_key for csptr/cbox elements +#define i_key_bind iref // note: use i_key_bind instead of i_key for csptr/cbox elements #include // csset_iref (like: std::set>) -#define i_val_ref iref // note: as above. +#define i_val_bind iref // note: as above. #include // cvec_iref (like: std::vector>) int main() { - c_auto (cvec_iref, vec) // declare and init vec, call cvec_iref_del() at scope exit - c_auto (csset_iref, set) // declare and init set, call csset_iref_del() at scope exit + c_auto (cvec_iref, vec) // declare and init vec, call cvec_iref_drop() at scope exit + c_auto (csset_iref, set) // declare and init set, call csset_iref_drop() at scope exit { const int years[] = {2021, 2012, 2022, 2015}; c_forrange (i, c_arraylen(years)) @@ -47,7 +47,7 @@ int main() printf("\nset:"); c_foreach (i, csset_iref, set) printf(" %d", *i.ref->get); - c_autovar (iref p = iref_clone(vec.data[0]), iref_del(&p)) { + c_autovar (iref p = iref_clone(vec.data[0]), iref_drop(&p)) { printf("\n%d is now owned by %ld objects\n", *p.get, *p.use_count); } diff --git a/examples/sptr_erase.c b/examples/sptr_erase.c index 4707abb1..d08b9072 100644 --- a/examples/sptr_erase.c +++ b/examples/sptr_erase.c @@ -1,50 +1,51 @@ #include -void show_del(int* x) { printf("del: %d\n", *x); } +void show_drop(int* x) { printf("drop: %d\n", *x); } +#define i_type Arc #define i_val int -#define i_del show_del // this "destroy" func shows which elements are destroyed +#define i_drop show_drop // this "destroy" func shows which elements are destroyed // csptr/cbox will use pointer address comparison of i_val if no i_cmp func is specified, -// or 'i_opt c_no_compare' is defined, otherwise i_cmp is used to compare object values. +// or 'i_opt c_no_cmp' is defined, otherwise i_cmp is used to compare object values. // See the different results by commenting out the next line. -#define i_cmp(x, y) (*(x) - *(y)) -#include // csptr_int: shared pointer to int +//#define i_cmp(x, y) (*(x) - *(y)) +#include // Arc: shared pointer to int -#define i_val_ref csptr_int -#define i_tag intp -#include // cvec_intp: cvec +#define i_type Vec +#define i_val_bind Arc +#include // Vec: cvec int main() { - c_auto (cvec_intp, vec) + c_auto (Vec, vec) { const int v[] = {2012, 1990, 2012, 2019, 2015}; c_forrange (i, c_arraylen(v)) - cvec_intp_push_back(&vec, csptr_int_new(v[i])); + Vec_push_back(&vec, Arc_new(v[i])); // clone the second 2012 and push it back. // note: cloning make sure that vec.data[2] has ref count 2. - cvec_intp_emplace_back(&vec, vec.data[2]); + Vec_emplace_back(&vec, vec.data[2]); printf("vec before erase :"); - c_foreach (i, cvec_intp, vec) + c_foreach (i, Vec, vec) printf(" %d", *i.ref->get); puts(""); // erase vec.data[2]; or first matching value depending on compare. - cvec_intp_iter it; - it = cvec_intp_find(&vec, vec.data[2]); - if (it.ref != cvec_intp_end(&vec).ref) - cvec_intp_erase_at(&vec, it); + Vec_iter it; + it = Vec_find(&vec, vec.data[2]); + if (it.ref != Vec_end(&vec).ref) + Vec_erase_at(&vec, it); int year = 2015; - it = cvec_intp_find(&vec, (csptr_int){&year}); // Ok as tmp only. - if (it.ref != cvec_intp_end(&vec).ref) - cvec_intp_erase_at(&vec, it); + it = Vec_find(&vec, (Arc){&year}); // Ok as tmp only. + if (it.ref != Vec_end(&vec).ref) + Vec_erase_at(&vec, it); printf("vec after erase :"); - c_foreach (i, cvec_intp, vec) + c_foreach (i, Vec, vec) printf(" %d", *i.ref->get); puts("\nDone"); diff --git a/examples/sptr_music.c b/examples/sptr_music.c index fcb7a43f..04ec28cc 100644 --- a/examples/sptr_music.c +++ b/examples/sptr_music.c @@ -9,20 +9,21 @@ struct Song cstr title; } typedef Song; -Song Song_from(const char* artist, const char* title) +Song Song_new(const char* artist, const char* title) { return (Song){cstr_from(artist), cstr_from(title)}; } -void Song_del(Song* s) { - printf("del: %s\n", s->title.str); - c_del(cstr, &s->artist, &s->title); +void Song_drop(Song* s) { + printf("drop: %s\n", s->title.str); + c_drop(cstr, &s->artist, &s->title); } #define i_val Song -#define i_del Song_del +#define i_drop Song_drop +#define i_opt c_no_cmp #define i_tag song #include // define csptr_song -#define i_val_ref csptr_song +#define i_val_bind csptr_song #define i_tag song #include @@ -31,9 +32,9 @@ void example3() c_auto (cvec_song, v, v2) { c_apply(cvec_song, push_back, &v, { - csptr_song_new(Song_from("Bob Dylan", "The Times They Are A Changing")), - csptr_song_new(Song_from("Aretha Franklin", "Bridge Over Troubled Water")), - csptr_song_new(Song_from("Thalia", "Entre El Mar y Una Estrella")) + csptr_song_new(Song_new("Bob Dylan", "The Times They Are A Changing")), + csptr_song_new(Song_new("Aretha Franklin", "Bridge Over Troubled Water")), + csptr_song_new(Song_new("Thalia", "Entre El Mar y Una Estrella")) }); c_foreach (s, cvec_song, v) @@ -41,8 +42,8 @@ void example3() cvec_song_emplace_back(&v2, *s.ref); // note: calls csptr_song_clone() c_apply(cvec_song, push_back, &v2, { - csptr_song_new(Song_from("Michael Jackson", "Billie Jean")), - csptr_song_new(Song_from("Rihanna", "Stay")), + csptr_song_new(Song_new("Michael Jackson", "Billie Jean")), + csptr_song_new(Song_new("Rihanna", "Stay")), }); c_foreach (s, cvec_song, v2) diff --git a/examples/sptr_pthread.c b/examples/sptr_pthread.c index cfb845b6..501fdcef 100644 --- a/examples/sptr_pthread.c +++ b/examples/sptr_pthread.c @@ -11,11 +11,10 @@ struct Base int value; } typedef Base; -void Base_del(Base* b) { printf("Base::~Base()\n"); } - #define i_val Base -#define i_del Base_del +#define i_drop(x) printf("Base::~Base()\n") #define i_tag base +#define i_opt c_no_cmp #include void* thr(csptr_base* lp) @@ -28,7 +27,7 @@ void* thr(csptr_base* lp) " p.get() = %p, p.use_count() = %ld\n", (void*)lp->get, *lp->use_count); } /* atomically decrease ref. */ - csptr_base_del(lp); + csptr_base_drop(lp); return NULL; } diff --git a/examples/sptr_to_maps.c b/examples/sptr_to_maps.c index a66e5b31..69798563 100644 --- a/examples/sptr_to_maps.c +++ b/examples/sptr_to_maps.c @@ -3,22 +3,23 @@ #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 #define i_type Arc // (atomic) ref. counted type #define i_val Map -#define i_del(p) (printf("del Arc:\n"), Map_del(p)) +#define i_drop(p) (printf("drop Arc:\n"), Map_drop(p)) // no need for atomic ref. count in single thread: -#define i_opt c_no_atomic +// no compare function available for csmap: +#define i_opt c_no_atomic|c_no_cmp #include #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 #define i_type List -#define i_val_ref Arc // as above +#define i_val_bind Arc // as above #include int main() diff --git a/examples/vikings.c b/examples/vikings.c new file mode 100644 index 00000000..87977538 --- /dev/null +++ b/examples/vikings.c @@ -0,0 +1,69 @@ +#include +#include + +typedef struct Viking { + cstr name; + cstr country; +} Viking; + +void Viking_drop(Viking* vk) { + cstr_drop(&vk->name); + cstr_drop(&vk->country); +} + +// Define Viking raw struct with hash, equals, and convertion functions between Viking and RViking structs: + +typedef struct RViking { + const char* name; + const char* country; +} RViking; + +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 inline Viking Viking_from(RViking raw) { // note: parameter is by value + return c_make(Viking){cstr_from(raw.name), cstr_from(raw.country)}; +} +static inline RViking Viking_toraw(const Viking* vk) { + return c_make(RViking){vk->name.str, vk->country.str}; +} + +// 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 auto-maps these functions: +// #define i_hash Viking_hash +// #define i_equ Viking_equalto +// #define i_keyfrom Viking_from // uses _from because i_keyraw is defined +// #define i_keyto Viking_toraw +// #define i_keydrop Viking_drop +#include + +int main() +{ + c_auto (Vikings, vikings) { + c_apply_pair(Vikings, emplace, &vikings, { + {{"Einar", "Norway"}, 20}, + {{"Olaf", "Denmark"}, 24}, + {{"Harald", "Iceland"}, 12}, + }); + RViking bjorn = {"Bjorn", "Sweden"}; + Vikings_emplace_or_assign(&vikings, bjorn, 10); + + RViking einar = {"Einar", "Norway"}; + Vikings_value *e = Vikings_find(&vikings, einar).ref; + e->second += 3; // add 3 hp points to Einar + Vikings_emplace(&vikings, einar, 0).ref->second += 5; // add 5 more to Einar + + c_forpair (viking, hp, Vikings, vikings) { + printf("%s of %s has %d hp\n", _.viking.name.str, _.viking.country.str, _.hp); + } + } +} \ No newline at end of file -- cgit v1.2.3