From 2cefae32252f3051f75f44f4718a62a1210bd3a7 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Fri, 21 Oct 2022 09:26:13 +0200 Subject: Fixed wrong formatting of 64bit values various places. Finialized cbox/carc issues. --- examples/birthday.c | 7 +++++-- examples/city.c | 1 - examples/demos.c | 4 ++-- examples/music_arc.c | 1 + examples/random.c | 11 +++++++---- include/stc/carc.h | 20 ++++++++++---------- include/stc/cbox.h | 20 ++++++++++---------- include/stc/ccommon.h | 1 + include/stc/template.h | 6 +++++- 9 files changed, 41 insertions(+), 30 deletions(-) diff --git a/examples/birthday.c b/examples/birthday.c index 443394b2..48b8ceaa 100644 --- a/examples/birthday.c +++ b/examples/birthday.c @@ -24,7 +24,8 @@ static void test_repeats(void) c_forrange (i, N) { uint64_t k = stc64_rand(&rng) & mask; int v = cmap_ic_insert(&m, k, 0).ref->second += 1; - if (v > 1) printf("repeated value %" c_zu " (%d) at 2^%d\n", k, v, (int) log2((double) i)); + if (v > 1) printf("repeated value %" PRIu64 " (%d) at 2^%d\n", + k, v, (int) log2((double) i)); } } } @@ -52,7 +53,9 @@ void test_distribution(void) sum /= map.size; c_foreach (i, cmap_x, map) { - printf("%4u: %" c_zu " - %" c_zu ": %11.8f\n", i.ref->first, i.ref->second, sum, (1 - (double) i.ref->second / sum)); + printf("%4" PRIu32 ": %" PRIu64 " - %" PRIu64 ": %11.8f\n", + i.ref->first, i.ref->second, sum, + (1 - (double)i.ref->second / sum)); } } } diff --git a/examples/city.c b/examples/city.c index 0bae631a..891e08f8 100644 --- a/examples/city.c +++ b/examples/city.c @@ -14,7 +14,6 @@ void City_drop(City* c); #define i_type CityArc #define i_val_class City - #include //#include // try instead of cbox.h diff --git a/examples/demos.c b/examples/demos.c index d15e5c6e..457e5f4d 100644 --- a/examples/demos.c +++ b/examples/demos.c @@ -41,14 +41,14 @@ void vectordemo1() for (size_t i = 10; i <= 100; i += 10) cvec_ix_push(&bignums, i * i); - printf("erase - %d: %" c_zu "\n", 3, bignums.data[3]); + printf("erase - %d: %" PRIu64 "\n", 3, bignums.data[3]); cvec_ix_erase_n(&bignums, 3, 1); // erase index 3 cvec_ix_pop(&bignums); // erase the last cvec_ix_erase_n(&bignums, 0, 1); // erase the first for (size_t i = 0; i < cvec_ix_size(&bignums); ++i) { - printf("%" c_zu ": %" c_zu "\n", i, bignums.data[i]); + printf("%" c_zu ": %" PRIu64 "\n", i, bignums.data[i]); } } } diff --git a/examples/music_arc.c b/examples/music_arc.c index d4b9ab14..0fb0dd70 100644 --- a/examples/music_arc.c +++ b/examples/music_arc.c @@ -22,6 +22,7 @@ void Song_drop(Song* s) { // Define the reference counted type #define i_type SongArc #define i_val_class Song +#define i_opt c_no_hash #include // ... and a vector of it diff --git a/examples/random.c b/examples/random.c index 38ef2e39..6ebda57e 100644 --- a/examples/random.c +++ b/examples/random.c @@ -15,10 +15,11 @@ int main() sum = 0; before = clock(); c_forrange (N) { - sum += (uint32_t) stc64_rand(&rng); + sum += (uint32_t)stc64_rand(&rng); } diff = clock() - before; - printf("full range\t\t: %f secs, %" c_zu ", avg: %f\n", (float) diff / CLOCKS_PER_SEC, N, (double) sum / N); + printf("full range\t\t: %f secs, %" c_zu ", avg: %f\n", + (float)diff / CLOCKS_PER_SEC, N, (double)sum / N); stc64_uniform_t dist1 = stc64_uniform_new(0, range); rng = stc64_new(seed); @@ -28,7 +29,8 @@ int main() sum += stc64_uniform(&rng, &dist1); // unbiased } diff = clock() - before; - printf("unbiased 0-%" c_zu "\t: %f secs, %" c_zu ", avg: %f\n", range, (float) diff / CLOCKS_PER_SEC, N, (double) sum / N); + printf("unbiased 0-%" PRIu64 "\t: %f secs, %" c_zu ", avg: %f\n", + range, (float)diff/CLOCKS_PER_SEC, N, (double)sum / N); sum = 0; rng = stc64_new(seed); @@ -37,6 +39,7 @@ int main() sum += stc64_rand(&rng) % (range + 1); // biased } diff = clock() - before; - printf("biased 0-%" c_zu " \t: %f secs, %" c_zu ", avg: %f\n", range, (float) diff / CLOCKS_PER_SEC, N, (double) sum / N); + printf("biased 0-%" PRIu64 " \t: %f secs, %" c_zu ", avg: %f\n", + range, (float)diff / CLOCKS_PER_SEC, N, (double)sum / N); } diff --git a/include/stc/carc.h b/include/stc/carc.h index cbe2e9e7..7209eb28 100644 --- a/include/stc/carc.h +++ b/include/stc/carc.h @@ -81,7 +81,7 @@ int main() { #if !(defined i_cmp || defined i_less || defined i_key_class || defined i_val_class) #define _i_no_cmp #endif -#if !(defined i_eq || defined i_hash) +#if !(defined i_eq || defined i_hash || defined i_key_class || defined i_val_class) #define _i_no_hash #endif #include "template.h" @@ -174,15 +174,6 @@ STC_INLINE void _cx_memb(_take)(_cx_self* self, _cx_self ptr) { *self = ptr; } -STC_INLINE uint64_t _cx_memb(_value_hash)(const _cx_value* x) { - #if defined _i_no_hash - return (uint64_t)x; - #else - _cx_raw rx = i_keyto(x); - return i_hash((&rx)); - #endif -} - STC_INLINE int _cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y) { #if defined _i_no_cmp return c_default_cmp(&x, &y); @@ -192,6 +183,15 @@ STC_INLINE int _cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y) { #endif } +STC_INLINE uint64_t _cx_memb(_value_hash)(const _cx_value* x) { + #if defined _i_no_hash + return (uintptr_t)x; + #else + _cx_raw rx = i_keyto(x); + return i_hash((&rx)); + #endif +} + STC_INLINE bool _cx_memb(_value_eq)(const _cx_value* x, const _cx_value* y) { #if defined _i_no_hash return x == y; diff --git a/include/stc/cbox.h b/include/stc/cbox.h index fbd19c50..17ec026f 100644 --- a/include/stc/cbox.h +++ b/include/stc/cbox.h @@ -73,7 +73,7 @@ int main() { #if !(defined i_cmp || defined i_less || defined i_key_class || defined i_val_class) #define _i_no_cmp #endif -#if !(defined i_eq || defined i_hash) +#if !(defined i_eq || defined i_hash || defined i_key_class || defined i_val_class) #define _i_no_hash #endif #include "template.h" @@ -148,15 +148,6 @@ STC_INLINE void _cx_memb(_take)(_cx_self* self, _cx_self other) { *self = other; } -STC_INLINE uint64_t _cx_memb(_value_hash)(const _cx_value* x) { - #if defined _i_no_hash - return (uint64_t)x; - #else - _cx_raw rx = i_keyto(x); - return i_hash((&rx)); - #endif -} - STC_INLINE int _cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y) { #if defined _i_no_cmp return c_default_cmp(&x, &y); @@ -166,6 +157,15 @@ STC_INLINE int _cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y) { #endif } +STC_INLINE uint64_t _cx_memb(_value_hash)(const _cx_value* x) { + #if defined _i_no_hash + return (uintptr_t)x; + #else + _cx_raw rx = i_keyto(x); + return i_hash((&rx)); + #endif +} + STC_INLINE bool _cx_memb(_value_eq)(const _cx_value* x, const _cx_value* y) { #if defined _i_no_hash return x == y; diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index 52c05693..5ecb42f8 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -111,6 +111,7 @@ #define c_no_atomic (1<<1) #define c_no_clone (1<<2) #define c_no_cmp (1<<3) +#define c_no_hash (1<<4) /* Generic algorithms */ diff --git a/include/stc/template.h b/include/stc/template.h index 769c07c9..55201526 100644 --- a/include/stc/template.h +++ b/include/stc/template.h @@ -163,6 +163,9 @@ #if c_option(c_no_cmp) #define _i_no_cmp #endif +#if c_option(c_no_hash) + #define _i_no_hash +#endif #if c_option(c_no_clone) || (!defined i_keyclone && (defined i_keydrop || defined i_keyraw)) #define _i_no_clone #endif @@ -291,6 +294,7 @@ #undef i_val_ssv #undef i_val_arcbox #undef i_val_class +#undef i_val_bind // [deprecated] #undef i_valraw #undef i_valclone #undef i_valfrom @@ -302,6 +306,7 @@ #undef i_key_ssv #undef i_key_arcbox #undef i_key_class +#undef i_key_bind // [deprecated] #undef i_keyraw #undef i_keyclone #undef i_keyfrom @@ -320,6 +325,5 @@ #undef _i_no_cmp #undef _i_no_clone #undef _i_no_emplace -#undef _i_no_hash #undef _i_template #endif -- cgit v1.2.3