summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2020-10-02 07:04:35 +0200
committerTyge Løvset <[email protected]>2020-10-02 07:04:35 +0200
commit5a6bb1f5061a6ac75d70edf90f75dd976cfaba7d (patch)
tree0231a274aa64b9acc880a264f6f7e39ea82f9538
parentb4561502823e9eb0c0f1160ea4cdb69118a86589 (diff)
downloadSTC-modified-5a6bb1f5061a6ac75d70edf90f75dd976cfaba7d.tar.gz
STC-modified-5a6bb1f5061a6ac75d70edf90f75dd976cfaba7d.zip
Fixed generic less function - only worked for int
-rw-r--r--examples/heap.c8
-rw-r--r--stc/cdefs.h6
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))