diff options
Diffstat (limited to 'misc/examples/queues/queue.c')
| -rw-r--r-- | misc/examples/queues/queue.c | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/misc/examples/queues/queue.c b/misc/examples/queues/queue.c new file mode 100644 index 00000000..5b1f7606 --- /dev/null +++ b/misc/examples/queues/queue.c @@ -0,0 +1,32 @@ +#include <stc/crand.h> +#include <stdio.h> + +#define i_key int +#define i_tag i +#include <stc/cqueue.h> + +int main(void) { + int n = 1000000; + crand_uniform_t dist; + crand_t rng = crand_init(1234); + dist = crand_uniform_init(0, n); + + cqueue_i queue = {0}; + + // Push ten million random numbers onto the queue. + c_forrange (n) + cqueue_i_push(&queue, (int)crand_uniform(&rng, &dist)); + + // Push or pop on the queue ten million times + printf("%d\n", n); + c_forrange (n) { // forrange uses initial n only. + int r = (int)crand_uniform(&rng, &dist); + if (r & 1) + ++n, cqueue_i_push(&queue, r); + else + --n, cqueue_i_pop(&queue); + } + printf("%d, %" c_ZI "\n", n, cqueue_i_size(&queue)); + + cqueue_i_drop(&queue); +} |
