From 5a6bb1f5061a6ac75d70edf90f75dd976cfaba7d Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Fri, 2 Oct 2020 07:04:35 +0200 Subject: Fixed generic less function - only worked for int --- examples/heap.c | 8 ++++---- stc/cdefs.h | 6 ++++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/examples/heap.c b/examples/heap.c index bca5f2e7..cc8d1df1 100644 --- a/examples/heap.c +++ b/examples/heap.c @@ -18,13 +18,13 @@ int main() pcg = crand_rng32_init(seed); clock_t start = clock(); c_forrange (i, int, N) - cvec_f_push_back(&pq, (float) crand_i32(&pcg)); + cvec_f_push_back(&pq, (float) crand_f32(&pcg)*100000); cpqueue_f_make_heap(&pq); printf("Built priority queue: %f secs\n", (clock() - start) / (float) CLOCKS_PER_SEC); c_forrange (i, int, M) - printf("%.0f ", *cpqueue_f_top(&pq)), cpqueue_f_pop(&pq); + printf("%g ", *cpqueue_f_top(&pq)), cpqueue_f_pop(&pq); start = clock(); c_forrange (i, int, M, N) @@ -34,11 +34,11 @@ int main() pcg = crand_rng32_init(seed); start = clock(); c_forrange (i, int, N) - cpqueue_f_push(&pq, (float) crand_i32(&pcg)); + cpqueue_f_push(&pq, (float) crand_f32(&pcg)*100000); printf("pushed PQ: %f secs\n", (clock() - start) / (float) CLOCKS_PER_SEC); c_forrange (i, int, M) - printf("%.0f ", *cpqueue_f_top(&pq)), cpqueue_f_pop(&pq); + printf("%g ", *cpqueue_f_top(&pq)), cpqueue_f_pop(&pq); puts(""); cpqueue_f_del(&pq); diff --git a/stc/cdefs.h b/stc/cdefs.h index 909ba69e..7bfccd3e 100644 --- a/stc/cdefs.h +++ b/stc/cdefs.h @@ -72,12 +72,14 @@ enum {_c_max_buffer = 512}; #define c_mem_equals(x, y) (memcmp(x, y, sizeof(*(y))) == 0) #define c_default_equals(x, y) (*(x) == *(y)) #define c_default_less(x, y) (*(x) < *(y)) -#define c_less_compare(less, x, y) (less(x, y) ? -1 : less(y, x)) -#define c_default_compare(x, y) (*(x) - *(y)) +#define c_less_compare(less, x, y) (less(y, x) - less(x, y)) +#define c_default_compare(x, y) c_less_compare(c_default_less, x, y) #define c_default_from_raw(x) (x) #define c_default_to_raw(ptr) (*(ptr)) #define c_default_del(ptr) ((void) (ptr)) +/* Generic algorithms */ + #define c_foreach(...) c_MACRO_OVERLOAD(c_foreach, __VA_ARGS__) #define c_foreach_3(it, ctype, cnt) \ for (ctype##_iter_t it = ctype##_begin(&cnt), it##_end_ = ctype##_end(&cnt); it.val != it##_end_.val; ctype##_next(&it)) -- cgit v1.2.3