From ef9697b470d6b2a6d210a1f7439c3d4d6da9d7ee Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Wed, 8 Sep 2021 15:22:44 +0200 Subject: Fixed linkage stuff. --- examples/advanced.c | 18 +++++++++++------ include/stc/ccommon.h | 6 ++++++ include/stc/clist.h | 26 ++++++++++++------------- include/stc/cmap.h | 53 +++++++++++++++++++++++---------------------------- 4 files changed, 55 insertions(+), 48 deletions(-) diff --git a/examples/advanced.c b/examples/advanced.c index 69060242..5d54275f 100644 --- a/examples/advanced.c +++ b/examples/advanced.c @@ -1,5 +1,4 @@ #include -#include #include typedef struct Viking { @@ -20,7 +19,7 @@ typedef struct VikingRaw { } VikingRaw; uint64_t vikingraw_hash(const VikingRaw* raw, size_t ignore) { - uint64_t hash = c_string_hash(raw->name) ^ (c_string_hash(raw->country) >> 15); + uint64_t hash = c_rawstr_hash(&raw->name) ^ (c_rawstr_hash(&raw->country) >> 15); return hash; } static inline int vikingraw_equals(const VikingRaw* rx, const VikingRaw* ry) { @@ -34,10 +33,17 @@ static inline VikingRaw viking_toRaw(const Viking* vk) { return c_make(VikingRaw){vk->name.str, vk->country.str}; } -// With this in place, we use the using_cmap_keydef() macro to define {Viking -> int} hash map type: - -using_cmap_keydef(vk, Viking, int, vikingraw_equals, vikingraw_hash, - viking_del, viking_fromRaw, viking_toRaw, VikingRaw, c_true); +// With this in place, we define the Viking => int hash map type: +#define i_tag vk +#define i_key Viking +#define i_val int +#define i_equ vikingraw_equals +#define i_hash vikingraw_hash +#define i_keydel viking_del +#define i_keyraw VikingRaw +#define i_keyfrom viking_fromRaw +#define i_keyto viking_toRaw +#include int main() { diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index bad9dc8c..8250c4b6 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -109,6 +109,12 @@ #define c_default_del(ptr) ((void) (ptr)) +STC_API uint64_t c_default_hash(const void *key, size_t len); +#define c_default_hash32(data, len_is_4) \ + ((*(const uint32_t*)data * 0xc6a4a7935bd1e99d) >> 15) +#define c_default_hash64(data, len_is_8) \ + (*(const uint64_t *)data * 0xc6a4a7935bd1e99d) + /* Generic algorithms */ #define c_foreach(...) c_MACRO_OVERLOAD(c_foreach, __VA_ARGS__) diff --git a/include/stc/clist.h b/include/stc/clist.h index 12be1146..9d920ead 100644 --- a/include/stc/clist.h +++ b/include/stc/clist.h @@ -55,6 +55,7 @@ */ #ifndef CLIST_H_INCLUDED +#define CLIST_H_INCLUDED #include "ccommon.h" #include "forward.h" #include @@ -70,29 +71,28 @@ _c_clist_types(clist_VOID, int); _c_clist_complete_types(clist_VOID, dummy); + +#define _c_clist_insert_after(self, Self, node, val) \ + cx_node_t *entry = c_new (cx_node_t); \ + if (node) entry->next = node->next, node->next = entry; \ + else entry->next = entry; \ + entry->value = val + // +: set self->last based on node + #endif // CLIST_H_INCLUDED #define i_module clist #include "template.h" #if !defined i_fwd -cx_deftypes(_c_clist_types, Self, i_val); + cx_deftypes(_c_clist_types, Self, i_val); #endif cx_deftypes(_c_clist_complete_types, Self, dummy); typedef i_valraw cx_rawvalue_t; - -#ifndef CLIST_H_INCLUDED -#define CLIST_H_INCLUDED -#define _c_clist_insert_after(self, Self, node, val) \ - cx_node_t *entry = c_new (cx_node_t); \ - if (node) entry->next = node->next, node->next = entry; \ - else entry->next = entry; \ - entry->value = val - // +: set self->last based on node - -#if !defined(STC_HEADER) || defined(i_imp) && (i_imp == 2) -// NON TEMPLATED CODE +#if !defined(STC_HEADER) || defined(i_imp) && i_imp == 2 +#ifndef CLIST_NON_TEMPLATED +#define CLIST_NON_TEMPLATED STC_DEF size_t _clist_count(const clist_VOID* self) { diff --git a/include/stc/cmap.h b/include/stc/cmap.h index f820d620..a03d5c83 100644 --- a/include/stc/cmap.h +++ b/include/stc/cmap.h @@ -57,34 +57,6 @@ int main(void) { #define _cmap_inits {NULL, NULL, 0, 0, 0.85f} typedef struct { size_t idx; uint_fast8_t hx; } chash_bucket_t; - -#if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION) || defined(i_imp) - -STC_INLINE uint64_t c_default_hash(const void *key, size_t len) { - const uint64_t m = 0xb5ad4eceda1ce2a9; - uint64_t k, h = m + len; - const uint8_t *p = (const uint8_t *)key, *end = p + (len & ~7ull); - for (; p != end; p += 8) {memcpy(&k, p, 8); h ^= m*k; } - switch (len & 7) { - case 7: h ^= (uint64_t) p[6] << 48; - case 6: h ^= (uint64_t) p[5] << 40; - case 5: h ^= (uint64_t) p[4] << 32; - case 4: h ^= (uint64_t) p[3] << 24; - case 3: h ^= (uint64_t) p[2] << 16; - case 2: h ^= (uint64_t) p[1] << 8; - case 1: h ^= (uint64_t) p[0]; h *= m; - } - return h ^ (h >> 15); -} - -STC_INLINE uint64_t c_string_hash(const char *s) - { return c_default_hash(s, strlen(s)); } -STC_INLINE uint64_t c_default_hash32(const void* data, size_t ignored) - { return *(const uint32_t *)data * 0xc6a4a7935bd1e99d; } -STC_INLINE uint64_t c_default_hash64(const void* data, size_t ignored) - { return *(const uint64_t *)data * 0xc6a4a7935bd1e99d; } - -#endif #endif // CMAP_H_INCLUDED #ifndef i_module @@ -240,6 +212,29 @@ cx_memb(_erase_at)(Self* self, cx_iter_t it) { /* -------------------------- IMPLEMENTATION ------------------------- */ +#if !defined(STC_HEADER) || defined(i_imp) && i_imp == 2 +#ifndef CMAP_NON_TEMPLATED +#define CMAP_NON_TEMPLATED + +STC_DEF uint64_t c_default_hash(const void *key, size_t len) { + const uint64_t m = 0xb5ad4eceda1ce2a9; + uint64_t k, h = m + len; + const uint8_t *p = (const uint8_t *)key, *end = p + (len & ~7ull); + for (; p != end; p += 8) { memcpy(&k, p, 8); h ^= m*k; } + switch (len & 7) { + case 7: h ^= (uint64_t) p[6] << 48; + case 6: h ^= (uint64_t) p[5] << 40; + case 5: h ^= (uint64_t) p[4] << 32; + case 4: h ^= (uint64_t) p[3] << 24; + case 3: h ^= (uint64_t) p[2] << 16; + case 2: h ^= (uint64_t) p[1] << 8; + case 1: h ^= (uint64_t) p[0]; h *= m; + } + return h ^ (h >> 15); +} +#endif +#endif + #if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION) || defined(i_imp) //STC_INLINE size_t fastrange_uint64_t(uint64_t x, uint64_t n) @@ -379,7 +374,7 @@ cx_memb(_erase_entry)(Self* self, cx_value_t* _val) { --self->size; } -#endif // IMPLEMENTATION +#endif // TEMPLATED IMPLEMENTATION #undef cx_keyref #undef cx_MAP_ONLY #undef cx_SET_ONLY -- cgit v1.2.3