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 +++++++++++++++++++++++++++++--------------------- include/stc/carc.h | 4 ++-- include/stc/cbox.h | 4 ++-- include/stc/template.h | 2 +- 4 files changed, 41 insertions(+), 30 deletions(-) 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(""); } } diff --git a/include/stc/carc.h b/include/stc/carc.h index 94dc9bd6..4b9cc875 100644 --- a/include/stc/carc.h +++ b/include/stc/carc.h @@ -78,10 +78,10 @@ int main() { #ifndef _i_prefix #define _i_prefix carc_ #endif -#if !(defined i_cmp || defined i_less || defined i_val_class) +#if !(defined i_cmp || defined i_less || defined i_key_class || defined i_val_class) #define _i_no_cmp #endif -#if !(defined i_eq || defined i_hash || defined i_val_class) +#if !(defined i_eq || defined i_hash || defined i_key_class || defined i_val_class) #define _i_no_hash #endif #include "template.h" diff --git a/include/stc/cbox.h b/include/stc/cbox.h index f31c5feb..651abf4b 100644 --- a/include/stc/cbox.h +++ b/include/stc/cbox.h @@ -70,10 +70,10 @@ int main() { #ifndef _i_prefix #define _i_prefix cbox_ #endif -#if !(defined i_cmp || defined i_less || defined i_val_class) +#if !(defined i_cmp || defined i_less || defined i_key_class || defined i_val_class) #define _i_no_cmp #endif -#if !(defined i_eq || defined i_hash || defined i_val_class) +#if !(defined i_eq || defined i_hash || defined i_key_class || defined i_val_class) #define _i_no_hash #endif #include "template.h" diff --git a/include/stc/template.h b/include/stc/template.h index 000b516e..4e954b46 100644 --- a/include/stc/template.h +++ b/include/stc/template.h @@ -142,7 +142,7 @@ #ifndef i_cmp #define i_cmp c_paste(i_keyraw, _cmp) #endif - #if !defined i_hash + #ifndef i_hash #define i_hash c_paste(i_keyraw, _hash) #endif #endif -- cgit v1.2.3