diff options
| author | Tyge Løvset <[email protected]> | 2022-04-22 12:20:31 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2022-04-22 12:20:31 +0200 |
| commit | 8f57f3d331de4cb4aa7d06862c2de3424eb1ba5b (patch) | |
| tree | 1a730578475e4d6e260948578cd0c2a5fd4463f4 /examples | |
| parent | 182099800f230f876fb46dac9f1f49a4fe3c3981 (diff) | |
| download | STC-modified-8f57f3d331de4cb4aa7d06862c2de3424eb1ba5b.tar.gz STC-modified-8f57f3d331de4cb4aa7d06862c2de3424eb1ba5b.zip | |
Readded push()/emplace() to all containers missing them. Made _hash function required for i_key_bind, _eq is derived from _cmp.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/box.c | 4 | ||||
| -rw-r--r-- | examples/city.c | 7 | ||||
| -rw-r--r-- | examples/person_arc.c | 4 | ||||
| -rw-r--r-- | examples/rawptr_elements.c | 4 | ||||
| -rw-r--r-- | examples/vikings.c | 7 |
5 files changed, 19 insertions, 7 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 index 16c9b6f1..0e1cbe96 100644 --- a/examples/city.c +++ b/examples/city.c @@ -1,7 +1,6 @@ #include <stc/cstr.h>
-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 <stc/cset.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 |
