From 4694e23046f3692f7e4b857f8ea8a8ffd0374dbf Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Thu, 20 Oct 2022 21:28:00 +0200 Subject: Fixed previous commit, and city.c example. --- examples/city.c | 61 ++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 25 deletions(-) (limited to 'examples') diff --git a/examples/city.c b/examples/city.c index 79b8b17e..ba575f1e 100644 --- a/examples/city.c +++ b/examples/city.c @@ -7,45 +7,49 @@ typedef struct { int population; } City; -static inline int City_cmp(const City* a, const City* b) { +int City_cmp(const City* a, const City* b); +uint64_t City_hash(const City* a); +City City_clone(City c); +void City_drop(City* c); + +#define i_type CityArc +#define i_val_class City +#define i_opt c_no_hash + +#include +//#include // try instead of cbox.h + +#define i_type Cities +#define i_key_arcbox CityArc +#include + +#define i_type CityMap +#define i_key int +#define i_val_arcbox CityArc +#include + + +int City_cmp(const City* a, const City* b) { int c = cstr_cmp(&a->name, &b->name); return c ? c : cstr_cmp(&a->country, &b->country); } -static inline uint64_t City_hash(const City* a) { - printf("hash %s\n", cstr_str(&a->name)); +uint64_t City_hash(const City* a) { return cstr_hash(&a->name) ^ cstr_hash(&a->country); } -static inline City City_clone(City c) { - printf("clone %s\n", cstr_str(&c.name)); +City City_clone(City c) { c.name = cstr_clone(c.name); c.country = cstr_clone(c.country); return c; } -static inline void City_drop(City* c) { +void City_drop(City* c) { printf("drop %s\n", cstr_str(&c->name)); c_drop(cstr, &c->name, &c->country); } -#define i_type CityArc -#define i_key_class City -#define i_opt c_no_atomic -//#include -#include // try instead of cbox.h - -#define i_type Cities -#define i_key_arcbox CityArc -#include - -#define i_type CityMap -#define i_key int -#define i_val_arcbox CityArc -#include - - int main(void) { c_auto (Cities, cities, copy) @@ -56,8 +60,16 @@ int main(void) {cstr_new("Paris"), cstr_new("France"), 4.3f, 23.2f, 9000000}, {cstr_new("Berlin"), cstr_new("Germany"), 4.3f, 23.2f, 9000000}, {cstr_new("London"), cstr_new("UK"), 4.3f, 23.2f, 9000000}, - }) Cities_emplace(&cities, *i.ref); + }) Cities_emplace(&cities, *i.ref); // NB. creates smart pointers! + Cities_sort(&cities); + + printf("Vec:\n"); + c_foreach (c, Cities, cities) + printf("city:%s, %d, use:%ld\n", cstr_str(&c.ref->get->name), + c.ref->get->population, + CityArc_use_count(c.ref)); + puts(""); copy = Cities_clone(cities); // share each element! int k = 0, id[] = {8, 4, 3, 9, 2, 5}; @@ -72,11 +84,10 @@ int main(void) printf("city:%s, %d, use:%ld\n", cstr_str(&c.ref->get->name), c.ref->get->population, CityArc_use_count(c.ref)); - printf("\nMap:\n"); c_forpair (id, city, CityMap, map) printf("id:%d, city:%s, %d, use:%ld\n", *_.id, cstr_str(&_.city->get->name), - _.city->get->population, CityArc_use_count(_.city)); + _.city->get->population, CityArc_use_count(_.city)); puts(""); } } -- cgit v1.2.3