diff options
| author | Tyge Løvset <[email protected]> | 2023-07-24 08:48:41 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-07-24 08:48:41 +0200 |
| commit | 374b3c27831cd4e09461867ed231669777b96951 (patch) | |
| tree | 88011006f6d536cdb1ad1eca8073392ca80687cc /misc/examples/queues/queue.c | |
| parent | 177418232a2d8a8b0df1667d3e4bd15dc37db59f (diff) | |
| parent | 650b053f443f9132dadb6d1ca924c0b36849739f (diff) | |
| download | STC-modified-374b3c27831cd4e09461867ed231669777b96951.tar.gz STC-modified-374b3c27831cd4e09461867ed231669777b96951.zip | |
Merge pull request #65 from stclib/dev43
Dev43
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); +} |
