diff options
| author | _Tradam <[email protected]> | 2023-09-08 01:29:47 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-09-08 01:29:47 +0000 |
| commit | 3c76c7f3d5db3f9586a90d03f8fbb02d79de9acd (patch) | |
| tree | afbe4b540967223911f7c5de36559b82154f02f3 /misc/examples/queues/queue.c | |
| parent | 0841165881871ee01b782129be681209aeed2423 (diff) | |
| parent | 1a72205fe05c2375cfd380dd8381a8460d9ed8d1 (diff) | |
| download | STC-modified-modified.tar.gz STC-modified-modified.zip | |
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); +} |
