summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/priorityqueues/priority.c
blob: 18684e73b06d6966e2685e397fbcfac090795901 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

#include <stdio.h>
#include <time.h>
#include <stc/crand.h>

#define i_key int64_t
#define i_cmp -c_default_cmp  // min-heap (increasing values)
#define i_tag i
#include <stc/cpque.h>

int main(void) {
    intptr_t N = 10000000;
    crand_t rng = crand_init((uint64_t)time(NULL));
    crand_uniform_t dist = crand_uniform_init(0, N * 10);

    cpque_i heap = {0};

    // Push ten million random numbers to priority queue
    printf("Push %" c_ZI " numbers\n", N);
    c_forrange (N)
        cpque_i_push(&heap, crand_uniform(&rng, &dist));

    // push some negative numbers too.
    c_forlist (i, int, {-231, -32, -873, -4, -343})
        cpque_i_push(&heap, *i.ref);

    c_forrange (N)
        cpque_i_push(&heap, crand_uniform(&rng, &dist));

    puts("Extract the hundred smallest.");
    c_forrange (100) {
        printf("%" PRId64 " ", *cpque_i_top(&heap));
        cpque_i_pop(&heap);
    }

    cpque_i_drop(&heap);
}