From 84c1716de710d2d9067c9f8a50487e3ab2d6fa42 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Sun, 20 Sep 2020 13:37:27 +0200 Subject: Possible to redefine STC memory allocator globally. --- examples/inits.c | 15 +++++++-------- examples/queue.c | 4 ++-- 2 files changed, 9 insertions(+), 10 deletions(-) (limited to 'examples') diff --git a/examples/inits.c b/examples/inits.c index 14fe8a64..27ee3c9b 100644 --- a/examples/inits.c +++ b/examples/inits.c @@ -10,18 +10,17 @@ using_cmap_strkey(cnt, int); typedef struct {int x, y;} ipair_t; inline static int ipair_compare(const ipair_t* a, const ipair_t* b) { - int c = c_default_compare(&a->x, &b->x); - return c != 0 ? c : c_default_compare(&a->y, &b->y); + int cx = c_default_compare(&a->x, &b->x); + return cx == 0 ? c_default_compare(&a->y, &b->y) : cx; } + using_cvec(ip, ipair_t, c_default_del, ipair_compare); using_clist(ip, ipair_t, c_default_del, ipair_compare); - using_cvec(f, float); using_cpqueue(f, cvec_f, >); - -int main(void) { - +int main(void) +{ // CVEC FLOAT / PRIORITY QUEUE cvec_f floats = cvec_INIT; @@ -32,11 +31,11 @@ int main(void) { // CVEC PRIORITY QUEUE - cpqueue_f_build(&floats); // reorganise vec + cpqueue_f_build(&floats); // make heap c_push_items(&floats, cpqueue_f, {40.0f, 20.0f, 50.0f, 30.0f, 10.0f}); // sorted: - while (cvec_size(floats) > 0) { + while (! cpqueue_f_empty(floats)) { printf("%.1f ", *cpqueue_f_top(&floats)); cpqueue_f_pop(&floats); } diff --git a/examples/queue.c b/examples/queue.c index e4426884..c56214a7 100644 --- a/examples/queue.c +++ b/examples/queue.c @@ -14,12 +14,12 @@ int main() { cqueue_i queue = cqueue_i_init(); // Push ten million random numbers onto the queue. - c_forrange (i, int, 0, n) + c_forrange (n) cqueue_i_push(&queue, crand_uniform_i32(&rng, &dist)); // Push or pop on the queue ten million times printf("%d\n", n); - c_forrange (i, int, n, 0, -1) { + c_forrange (n) { // range uses initial n only. int r = crand_uniform_i32(&rng, &dist); if (r & 1) ++n, cqueue_i_push(&queue, r); -- cgit v1.2.3