From 42a30211e5bd1f680060e1bce4e60ef08528f427 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Fri, 31 Jul 2020 10:32:37 +0200 Subject: Improved advanced.c and namings in cdefs.h --- examples/README.md | 18 +++++++++--------- examples/advanced.c | 8 ++++---- stc/cdefs.h | 10 +++++----- stc/clist.h | 2 +- stc/cmap.h | 8 ++++---- stc/cvec.h | 2 +- 6 files changed, 24 insertions(+), 24 deletions(-) diff --git a/examples/README.md b/examples/README.md index 1bf38f58..589caddf 100644 --- a/examples/README.md +++ b/examples/README.md @@ -22,17 +22,17 @@ typedef struct VikingVw { const char* country; } VikingVw; + uint32_t vikingvw_hash(const VikingVw* vw, size_t ignore) { - uint32_t hash = c_defaultHash(vw->name, strlen(vw->name)) - ^ c_defaultHash(vw->country, strlen(vw->country)); + uint32_t hash = c_default_hash(vw->name, strlen(vw->name)) + ^ c_default_hash(vw->country, strlen(vw->country)); return hash; } -int vikingvw_equals(const VikingVw* x, const VikingVw* y) { - if (strcmp(x->name, y->name) != 0) return false; - return strcmp(x->country, y->country) == 0; +static inline int vikingvw_equals(const VikingVw* x, const VikingVw* y) { + return strcmp(x->name, y->name) == 0 && strcmp(x->country, y->country) == 0; } ``` -And the Viking data struct: +And the Viking data struct with destroy and convertion functions between VikingVw <-> Viking structs. ``` typedef struct Viking { cstr_t name; @@ -45,17 +45,17 @@ void viking_destroy(Viking* vk) { cstr_destroy(&vk->country); } -VikingVw viking_getVw(Viking* vk) { +static inline VikingVw viking_toVw(Viking* vk) { VikingVw vw = {vk->name.str, vk->country.str}; return vw; } -Viking viking_fromVw(VikingVw vw) { +static inline Viking viking_fromVw(VikingVw vw) { // note: parameter is by value Viking vk = {cstr_make(vw.name), cstr_make(vw.country)}; return vk; } ``` With this in place, we use the full declare_cmap() macro to define {Viking -> int} hash map type: ``` declare_cmap(vk, Viking, int, c_default_destroy, vikingvw_equals, vikingvw_hash, - viking_destroy, VikingVw, viking_getVw, viking_fromVw); + viking_destroy, VikingVw, viking_toVw, viking_fromVw); ``` cmap_vk uses vikingvw_hash() for hash value calculations, and vikingvw_equals() for equality test. cmap_vk_destroy() will free all memory allocated for Viking keys and the hash table values. Finally, main which also demos the generic c_push() of multiple elements: diff --git a/examples/advanced.c b/examples/advanced.c index 90ec7cc0..7162bdc9 100644 --- a/examples/advanced.c +++ b/examples/advanced.c @@ -22,8 +22,8 @@ typedef struct VikingVw { } VikingVw; uint32_t vikingvw_hash(const VikingVw* vw, size_t ignore) { - uint32_t hash = c_defaultHash(vw->name, strlen(vw->name)) - ^ c_defaultHash(vw->country, strlen(vw->country)); + uint32_t hash = c_default_hash(vw->name, strlen(vw->name)) + ^ c_default_hash(vw->country, strlen(vw->country)); return hash; } int vikingvw_equals(const VikingVw* x, const VikingVw* y) { @@ -44,7 +44,7 @@ void viking_destroy(Viking* vk) { cstr_destroy(&vk->country); } -VikingVw viking_getVw(Viking* vk) { +VikingVw viking_toVw(Viking* vk) { VikingVw vw = {vk->name.str, vk->country.str}; return vw; } Viking viking_fromVw(VikingVw vw) { @@ -54,7 +54,7 @@ Viking viking_fromVw(VikingVw vw) { // Using the full declare_cmap() macro to define [Viking -> int] hash map type: declare_cmap(vk, Viking, int, c_default_destroy, vikingvw_equals, vikingvw_hash, - viking_destroy, VikingVw, viking_getVw, viking_fromVw); + viking_destroy, VikingVw, viking_toVw, viking_fromVw); // cmap_vk uses vikingvw_hash() for hash value calculations, and vikingvw_equals() for equality test. // cmap_vk_destroy() will free all memory allocated for Viking keys and the hash table values. diff --git a/stc/cdefs.h b/stc/cdefs.h index 18cc9f51..9328d723 100644 --- a/stc/cdefs.h +++ b/stc/cdefs.h @@ -62,14 +62,14 @@ #define c_max_alloca (1000) #define c_swap(T, x, y) { T __t = x; x = y; y = __t; } -#define c_defaultInitRaw(x) (x) -#define c_defaultGetRaw(ptr) (*(ptr)) +#define c_default_from_raw(x) (x) +#define c_default_to_raw(ptr) (*(ptr)) #define c_no_compare(x, y) (0) #define c_mem_compare(x, y) memcmp(x, y, sizeof(*(y))) -#define c_mem_equals(x, y) (memCompare(x, y) == 0) +#define c_mem_equals(x, y) (c_mem_compare(x, y) == 0) #define c_default_equals(x, y) (*(x) == *(y)) #define c_default_less(x, y) (*(x) < *(y)) -#define c_compare(less, x, y) (less(x, y) ? -1 : less(y, x)) +#define c_compare(less, x, y) (less(x, y) ? -1 : less(y, x)) #define c_default_compare(x, y) c_compare(c_default_less, x, y) #define c_default_destroy(p) ((void)0) @@ -82,7 +82,7 @@ } while (0) /* One-byte-at-a-time hash based on Murmur's mix */ -static inline uint32_t c_defaultHash(const void *data, size_t len) { +static inline uint32_t c_default_hash(const void *data, size_t len) { const volatile uint8_t *key = (const uint8_t *) data; uint32_t x = UINT32_C(0xc613fc15); while (len--) { diff --git a/stc/clist.h b/stc/clist.h index e55880ba..c27f254c 100644 --- a/stc/clist.h +++ b/stc/clist.h @@ -63,7 +63,7 @@ #define declare_clist_3(tag, Value, valueDestroy) \ declare_clist_4(tag, Value, valueDestroy, c_default_compare) #define declare_clist_4(tag, Value, valueDestroy, valueCompare) \ - declare_clist_6(tag, Value, valueDestroy, Value, valueCompare, c_defaultGetRaw) + declare_clist_6(tag, Value, valueDestroy, Value, valueCompare, c_default_to_raw) #define declare_clist_str() \ declare_clist_6(str, cstr_t, cstr_destroy, const char*, cstr_compareRaw, cstr_getRaw) diff --git a/stc/cmap.h b/stc/cmap.h index 7398b838..3f416145 100644 --- a/stc/cmap.h +++ b/stc/cmap.h @@ -71,11 +71,11 @@ enum {chash_HASH = 0x7f, chash_USED = 0x80}; declare_cmap_4(tag, Key, Value, c_default_destroy) #define declare_cmap_4(tag, Key, Value, valueDestroy) \ - declare_cmap_6(tag, Key, Value, valueDestroy, c_default_equals, c_defaultHash) + declare_cmap_6(tag, Key, Value, valueDestroy, c_default_equals, c_default_hash) #define declare_cmap_6(tag, Key, Value, valueDestroy, keyEquals, keyHash) \ declare_cmap_10(tag, Key, Value, valueDestroy, keyEquals, keyHash, \ - c_default_destroy, Key, c_defaultGetRaw, c_defaultInitRaw) + c_default_destroy, Key, c_default_to_raw, c_default_from_raw) #define declare_cmap_10(tag, Key, Value, valueDestroy, keyEqualsRaw, keyHashRaw, \ keyDestroy, RawKey, keyGetRaw, keyInitRaw) \ @@ -87,14 +87,14 @@ enum {chash_HASH = 0x7f, chash_USED = 0x80}; c_MACRO_OVERLOAD(declare_cset, __VA_ARGS__) #define declare_cset_2(tag, Key) \ - declare_cset_4(tag, Key, c_default_equals, c_defaultHash) + declare_cset_4(tag, Key, c_default_equals, c_default_hash) #define declare_cset_4(tag, Key, keyEquals, keyHash) \ declare_cset_5(tag, Key, keyEquals, keyHash, c_default_destroy) #define declare_cset_5(tag, Key, keyEquals, keyHash, keyDestroy) \ declare_cset_8(tag, Key, keyEquals, keyHash, keyDestroy, \ - Key, c_defaultGetRaw, c_defaultInitRaw) + Key, c_default_to_raw, c_default_from_raw) #define declare_cset_8(tag, Key, keyEqualsRaw, keyHashRaw, keyDestroy, \ RawKey, keyGetRaw, keyInitRaw) \ diff --git a/stc/cvec.h b/stc/cvec.h index d76c001b..c4824353 100644 --- a/stc/cvec.h +++ b/stc/cvec.h @@ -40,7 +40,7 @@ #define declare_cvec_3(tag, Value, valueDestroy) \ declare_cvec_4(tag, Value, valueDestroy, c_default_compare) #define declare_cvec_4(tag, Value, valueDestroy, valueCompare) \ - declare_cvec_6(tag, Value, valueDestroy, valueCompare, Value, c_defaultGetRaw) + declare_cvec_6(tag, Value, valueDestroy, valueCompare, Value, c_default_to_raw) #define declare_cvec_str() \ declare_cvec_6(str, cstr_t, cstr_destroy, cstr_compareRaw, const char*, cstr_getRaw) -- cgit v1.2.3