summaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2022-10-20 21:28:00 +0200
committerTyge Løvset <[email protected]>2022-10-20 21:28:00 +0200
commit4694e23046f3692f7e4b857f8ea8a8ffd0374dbf (patch)
tree21c9c861ee701c6a5f9f5e6d165cf55d98845c2f /examples
parentb40ca18d43d31f997f64de6168ada58c7dfad9b9 (diff)
downloadSTC-modified-4694e23046f3692f7e4b857f8ea8a8ffd0374dbf.tar.gz
STC-modified-4694e23046f3692f7e4b857f8ea8a8ffd0374dbf.zip
Fixed previous commit, and city.c example.
Diffstat (limited to 'examples')
-rw-r--r--examples/city.c61
1 files changed, 36 insertions, 25 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 <stc/cbox.h>
+//#include <stc/carc.h> // try instead of cbox.h
+
+#define i_type Cities
+#define i_key_arcbox CityArc
+#include <stc/cvec.h>
+
+#define i_type CityMap
+#define i_key int
+#define i_val_arcbox CityArc
+#include <stc/csmap.h>
+
+
+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 <stc/cbox.h>
-#include <stc/carc.h> // try instead of cbox.h
-
-#define i_type Cities
-#define i_key_arcbox CityArc
-#include <stc/cvec.h>
-
-#define i_type CityMap
-#define i_key int
-#define i_val_arcbox CityArc
-#include <stc/csmap.h>
-
-
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("");
}
}