From 8f57f3d331de4cb4aa7d06862c2de3424eb1ba5b Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Fri, 22 Apr 2022 12:20:31 +0200 Subject: Readded push()/emplace() to all containers missing them. Made _hash function required for i_key_bind, _eq is derived from _cmp. --- examples/box.c | 4 ++++ examples/city.c | 7 +++++-- examples/person_arc.c | 4 ++++ examples/rawptr_elements.c | 4 ++-- examples/vikings.c | 7 ++++--- 5 files changed, 19 insertions(+), 7 deletions(-) (limited to 'examples') 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 index 16c9b6f1..0e1cbe96 100644 --- a/examples/city.c +++ b/examples/city.c @@ -1,7 +1,6 @@ #include -typedef struct -{ +typedef struct { cstr name; cstr country; float lat, lon; @@ -13,6 +12,10 @@ static inline int City_cmp(const City* a, const City* b) { 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); 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 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 -- cgit v1.2.3