diff options
| author | Tyge Løvset <[email protected]> | 2022-04-24 20:50:53 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-04-24 20:50:53 +0200 |
| commit | 81b541b85f85b48660ceb461b851f1fb09d68344 (patch) | |
| tree | 909d8c7cdeb5fc6daad007d4cd46e65d4af1c6e4 /examples | |
| parent | 91385d449d03145cfe8cc91f2704d4b24c63d37e (diff) | |
| parent | 8aeec88bd7f15069a388f7fc8fe0008af4d1ab44 (diff) | |
| download | STC-modified-81b541b85f85b48660ceb461b851f1fb09d68344.tar.gz STC-modified-81b541b85f85b48660ceb461b851f1fb09d68344.zip | |
Merge pull request #21 from tylov/keyval
Version 3.5 RC
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/box.c | 4 | ||||
| -rw-r--r-- | examples/city.c | 78 | ||||
| -rw-r--r-- | examples/make.sh | 2 | ||||
| -rw-r--r-- | examples/person_arc.c | 4 | ||||
| -rw-r--r-- | examples/rawptr_elements.c | 4 | ||||
| -rw-r--r-- | examples/read.c | 2 | ||||
| -rw-r--r-- | examples/sso_map.c | 1 | ||||
| -rw-r--r-- | examples/sso_substr.c | 1 | ||||
| -rw-r--r-- | examples/vikings.c | 7 |
9 files changed, 94 insertions, 9 deletions
diff --git a/examples/box.c b/examples/box.c index d2d98218..4a43b149 100644 --- a/examples/box.c +++ b/examples/box.c @@ -7,6 +7,10 @@ Person Person_new(const char* name, const char* last) { return (Person){.name = cstr_from(name), .last = cstr_from(last)};
}
+uint64_t Person_hash(const Person* a, size_t n) {
+ return cstr_hash(&a->name, 0) ^ cstr_hash(&a->last, 0);
+}
+
int Person_cmp(const Person* a, const Person* b) {
int c = cstr_cmp(&a->name, &b->name);
return c ? c : cstr_cmp(&a->last, &b->last);
diff --git a/examples/city.c b/examples/city.c new file mode 100644 index 00000000..0e1cbe96 --- /dev/null +++ b/examples/city.c @@ -0,0 +1,78 @@ +#include <stc/cstr.h>
+
+typedef struct {
+ cstr name;
+ cstr country;
+ float lat, lon;
+ int population;
+} City;
+
+static inline 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, size_t n) {
+ return cstr_hash(&a->name, 0) ^ cstr_hash(&a->country, 0);
+}
+
+static inline 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) {
+ printf("drop %s\n", cstr_str(&c->name));
+ c_drop(cstr, &c->name, &c->country);
+}
+
+#define i_type CityArc
+#define i_val_bind City
+//#include <stc/cbox.h>
+#include <stc/carc.h>
+
+#define i_type Cities
+#define i_val_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)
+ c_auto (CityMap, map)
+ {
+ struct City_s { const char *name, *country; float lat, lon; int pop; };
+
+ c_apply(c, Cities_push(&cities, CityArc_from((City){cstr_from(c.name), cstr_from(c.country),
+ c.lat, c.lon, c.pop})), struct City_s, {
+ {"New York", "US", 4.3, 23.2, 9000000},
+ {"Paris", "France", 4.3, 23.2, 9000000},
+ {"Berlin", "Germany", 4.3, 23.2, 9000000},
+ {"London", "UK", 4.3, 23.2, 9000000},
+ });
+
+ copy = Cities_clone(cities); // share each element!
+
+ int k = 0, id[] = {8, 4, 3, 9, 2, 5};
+ c_foreach (i, Cities, cities)
+ CityMap_insert(&map, id[k++], CityArc_clone(*i.ref));
+
+ Cities_pop(&cities);
+ Cities_pop(&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));
+
+ 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));
+ puts("");
+ }
+}
diff --git a/examples/make.sh b/examples/make.sh index ef0468c7..4687ed97 100644 --- a/examples/make.sh +++ b/examples/make.sh @@ -1,7 +1,7 @@ #!/bin/bash cc='gcc -s -O2 -Wall -std=c99 -pedantic' #cc='gcc -x c++ -s -O2 -Wall -std=c++20' -#cc='clang -s -O2 -Wall -std=c99 -pedantic -DSTC_USE_SSO' +#cc='clang -s -O2 -Wall -std=c99 -pedantic -DSTC_OLD_CSTR' #cc='clang' #cc='clang -c -DSTC_HEADER' #cc='cl -O2 -nologo -W2 -MD' diff --git a/examples/person_arc.c b/examples/person_arc.c index 2fb51be5..9d245340 100644 --- a/examples/person_arc.c +++ b/examples/person_arc.c @@ -12,6 +12,10 @@ int Person_cmp(const Person* a, const Person* b) { return c ? c : cstr_cmp(&a->last, &b->last);
}
+uint64_t Person_hash(const Person* a, size_t n) {
+ return cstr_hash(&a->name, 0) ^ cstr_hash(&a->last, 0);
+}
+
Person Person_clone(Person p) {
p.name = cstr_clone(p.name);
p.last = cstr_clone(p.last);
diff --git a/examples/rawptr_elements.c b/examples/rawptr_elements.c index b0878941..20231528 100644 --- a/examples/rawptr_elements.c +++ b/examples/rawptr_elements.c @@ -8,8 +8,8 @@ struct { double x, y; } typedef Point; #define i_key Point*
#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_eq(x, y) c_memcmp_eq(*(x), *(y))
+#define i_hash(x, n) c_default_hash(*(x), sizeof **(x))
+#define i_cmp(x, y) memcmp(*(x), *(y), sizeof **(x)) // not good!
#define i_tag pnt
#include <stc/cset.h>
diff --git a/examples/read.c b/examples/read.c index 127373b1..5f31e357 100644 --- a/examples/read.c +++ b/examples/read.c @@ -1,4 +1,4 @@ -#include <stc/alt/cstr.h>
+#include <stc/cstr.h>
#define i_val_str
#include <stc/cvec.h>
#include <errno.h>
diff --git a/examples/sso_map.c b/examples/sso_map.c index a70722f7..d6174da8 100644 --- a/examples/sso_map.c +++ b/examples/sso_map.c @@ -1,4 +1,3 @@ -#define STC_USE_SSO 1
#include <stc/cstr.h>
#define i_key_str
#define i_val_str
diff --git a/examples/sso_substr.c b/examples/sso_substr.c index 60fb9997..fdb53d0b 100644 --- a/examples/sso_substr.c +++ b/examples/sso_substr.c @@ -1,4 +1,3 @@ -#define STC_USE_SSO 1 #include <stc/cstr.h> #include <stc/csview.h> diff --git a/examples/vikings.c b/examples/vikings.c index b093ff9b..ff2fe8ab 100644 --- a/examples/vikings.c +++ b/examples/vikings.c @@ -22,8 +22,9 @@ 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_eq(const RViking* rx, const RViking* ry) { - return strcmp(rx->name, ry->name) == 0 && strcmp(rx->country, ry->country) == 0; +static inline int RViking_cmp(const RViking* rx, const RViking* ry) { + int c = strcmp(rx->name, ry->name); + return c ? c : strcmp(rx->country, ry->country); } static inline Viking Viking_from(RViking raw) { // note: parameter is by value @@ -40,7 +41,7 @@ static inline RViking Viking_toraw(const Viking* vk) { #define i_val int // i_key_bind auto-binds these functions: // i_hash => Viking_hash -// i_eq => Viking_eq +// i_cmp => Viking_cmp // i_keyfrom => Viking_from // not _clone because i_keyraw is defined // i_keyto => Viking_toraw // i_keydrop => Viking_drop |
