diff options
Diffstat (limited to 'misc/examples/queues')
| -rw-r--r-- | misc/examples/queues/new_queue.c | 47 | ||||
| -rw-r--r-- | misc/examples/queues/queue.c | 32 |
2 files changed, 79 insertions, 0 deletions
diff --git a/misc/examples/queues/new_queue.c b/misc/examples/queues/new_queue.c new file mode 100644 index 00000000..3904c50c --- /dev/null +++ b/misc/examples/queues/new_queue.c @@ -0,0 +1,47 @@ +#include <stc/crand.h> +#include <stc/forward.h> +#include <stdio.h> +#include <time.h> + +forward_cqueue(cqueue_pnt, struct Point); + +typedef struct Point { int x, y; } 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_key Point +#define i_cmp point_cmp +#define i_is_forward +#define i_tag pnt +#include <stc/cqueue.h> + +#define i_type IQ +#define i_key int +#include <stc/cqueue.h> + +int main(void) { + int n = 50000000; + crand_t rng = crand_init((uint64_t)time(NULL)); + crand_uniform_t dist = crand_uniform_init(0, n); + + IQ Q = {0}; + + // Push 50'000'000 random numbers onto the queue. + c_forrange (n) + IQ_push(&Q, (int)crand_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)crand_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)); + IQ_drop(&Q); +} 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); +} |
