summaryrefslogtreecommitdiffhomepage
path: root/examples/queue.c
blob: 7be2536780db00070ba402c294ae70e84d195aa4 (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
#include <stc/crandom.h>
#include <stdio.h>

#define i_val int
#define i_tag i
#include <stc/cqueue.h>

int main() {
    int n = 100000000;
    stc64_uniform_t dist;
    stc64_t rng = stc64_new(1234);
    dist = stc64_uniform_new(0, n);

    c_auto (cqueue_i, queue)
    {
        // Push ten million random numbers onto the queue.
        c_forloop (n)
            cqueue_i_push(&queue, stc64_uniform(&rng, &dist));

        // Push or pop on the queue ten million times
        printf("%d\n", n);
        c_forloop (n) { // forrange uses initial n only.
            int r = stc64_uniform(&rng, &dist);
            if (r & 1)
                ++n, cqueue_i_push(&queue, r);
            else
                --n, cqueue_i_pop(&queue);
        }
        printf("%d, %" PRIuMAX "\n", n, cqueue_i_size(&queue));
    }
}