summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/new_queue.c
blob: 7fae90d2f54f6a7e991952a8edc003fcec13444f (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
38
39
40
41
42
43
44
45
46
#include <stc/crandom.h>
#include <stc/forward.h>
#include <stdio.h>
#include <time.h>

forward_cqueue(cqueue_pnt, struct Point);

struct Point { int x, y; } typedef Point;
int point_cmp(const Point* a, const Point* b) {
    int c = c_default_cmp(&a->x, &b->x);
    return c ? c : c_default_cmp(&a->y, &b->y);
}
#define i_val Point
#define i_cmp point_cmp
#define i_opt c_is_forward
#define i_tag pnt
#include <stc/cqueue.h>

#define i_type IQ
#define i_val int
#include <stc/cqueue.h>

int main() {
    int n = 50000000;
    stc64_t rng = stc64_new((uint64_t)time(NULL));
    stc64_uniform_t dist = stc64_uniform_new(0, n);

    c_auto (IQ, Q)
    {
        // Push 50'000'000 random numbers onto the queue.
        c_forrange (n)
            IQ_push(&Q, (int)stc64_uniform(&rng, &dist));

        // Push or pop on the queue 50 million times
        printf("befor: size %" c_ZI ", capacity %" c_ZI "\n", IQ_size(&Q), IQ_capacity(&Q));
        
        c_forrange (n) {
            int r = (int)stc64_uniform(&rng, &dist);
            if (r & 3)
                IQ_push(&Q, r);
            else
                IQ_pop(&Q);
        }
        printf("after: size %" c_ZI ", capacity %" c_ZI "\n", IQ_size(&Q), IQ_capacity(&Q));
    }
}